<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
//use Http\Adapter\Guzzle6\Client;
function array_unique_multidimensional($input)
{
    $serialized = array_map('serialize', $input);
    $unique = array_unique($serialized);
    return array_intersect_key($input, $unique);
}
require dirname(__FILE__) . "/vendor/autoload.php";
use SudiptoChoudhury\Zoho\Subscriptions\Api;
if($_POST['token']){
    $addon1 = array('addon_code'=> 'disk1','addon_description'=>'test','price'=> 0);
     $addon2 = array('addon_code'=> 'addon-plug1','addon_description'=>'test','price'=> 0);
     $addon3 = array('addon_code'=> 'check1','addon_description'=>'test','price'=> 0);
     
  $domain = $_POST['domain'];
   // $addony =array_unique_multidimensional($addony);
   // print_r($addony);die();
$subscriptions= new Api([
    'oauthtoken' => $_POST['token'], // https://www.zoho.com/subscriptions/api/v1/#oauth
    'zohoOrgId' => '731205865',
]);
        if(isset($_POST['disk1'])){
            $addony[] = $addon1;
           
        }
        if(isset($_POST['addon-plug1'])){
            $addony[] = $addon2;
          
        }
        if(isset($_POST['check1'])){
            $addony[] = $addon3;
            
        }
$first_name = $_POST['f_name'];
$last_name = $_POST['l_name'];
$address = $_POST['address'];
$email = $_POST['u_email'];
$company_name = $_POST['company'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$state = $_POST['state'];
$phone = $_POST['u_phone'];
$plan_id = $_POST['plan_id'];


$resultJson = $subscriptions->getOrganizations(); 

//$resultJson = $subscriptions->addCustomer(['display_name'=> 'Tom testapi2', 'email'=> 'tom2@modernwebstudios.com']); // create a customer
//$resultJson = $subscriptions->addNewSubscriptionHostedpages(['customer_id'=> '2423884000000120002', 'plan'=>['plan_code'=> 'tom01', 'price'=> 1,'setup_fee'=> 5,'billing_cycles'=> -1],'addons'=> $addony,'auto_collect'=> false,'custom_fields'=>[['label' => 'Domain','value'=>$domain]] ]);
//$resultJson = $subscriptions->addNewSubscriptionHostedpages(['customer_id'=> '2423884000001562002', 'plan'=>['plan_code'=> 'wp-1', 'price'=> '24.95','setup_fee'=> '395','billing_cycles'=> -1],'addons'=> $addony,'auto_collect'=> false,'custom_fields'=>[['label' => 'Domain Name','value'=>$domain]] ]);
$resultJson = $subscriptions->addNewSubscriptionHostedpages([
   // 'customer_id'=> -1,
    "customer" => [
        "display_name" =>  $first_name." ".$last_name,
        //"salutation" =>  "Mr.",
        "first_name" =>  $first_name,
        "last_name" =>  $last_name,
        "email" =>  $email,
        "company_name" =>  $company_name,
        "billing_address" => [
            "attention" =>  $first_name." ".$last_name,
            "street" =>  $address,
            "city" =>  $city,
            "state" =>  $state,
            "country" =>  "U.S.A",
            "zip" =>  $zip,
            "phone" =>  $phone
        ],
        
        //"payment_terms" => 1,
        //"payment_terms_label" =>  "Due On Receipt",
        "currency_code" =>  "USD",
        //"vat_treatment" =>  "non_eu",
        //"vat_reg_no" => 51423456782,
        //"country_code" =>  "USA",
        //"is_taxable" => true
        
    ],
    'plan'=>['plan_code'=> $plan_id],
    'auto_collect'=> true,
    'custom_fields'=>[['label' => 'Domain Name','value'=>$domain]],
    
    ]);
    
    // 'plan'=>['plan_code'=> $plan_id, 'price'=> '1.00','setup_fee'=> '0','billing_cycles'=> -1],
if($resultJson['hostedpage']['url']){
    header('Location: '.$resultJson["hostedpage"]["url"]);
die();
}
}