
<center><h2><strong>Ubuntu</strong></h2>
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/*
 * https://airmob.net/commande_v2/zoho.crm.php
 * */
error_reporting(E_ALL);
ini_set("display_errors", 0);

//  pour API V2
define("APIZOHO", [ "IdOrg"  => "650182655",                                  // Id organization
                    "IdCli"  => "1000.KL48ACSNU2NS34520ESYY3FWZUBS0E",        // Id client
                    "Secret" => "3b4a8173e9dbd396aaeb2603db7560f5ff9a323bb6", // Secret
    ]
);


include_once ("./lib/gestion.lib.php");
$ClassCdeV2 = new ClassCdeV2('prod');


//$idu        = "609be9533a51e";

if(!$_REQUEST['idu']) die('Veuillez indiquer ?idu=');

$idu = $_REQUEST['idu'];

$Cmd        = $ClassCdeV2->Cmd($idu);
$InfoFact   = $ClassCdeV2->ZohoTraitement($idu);
$ID_BOOKS   = $InfoFact['zoho_idclient'];


$Cherche_IdCRM = true;
if($Cherche_IdCRM) {
// ZOHO CRM, trouver l ID du Client
    $refresh_token = "1000.22c19beee7288032a8fef7398c164f5d.337b11b1cc5478129acacb05623673b9"; // Si l acces token change il faut changer ce code
    $Url           = "https://accounts.zoho.com/oauth/v2/token?refresh_token=$refresh_token&client_id=" . APIZOHO['IdCli'] . "&client_secret=" . APIZOHO['Secret'] . "&grant_type=refresh_token";
    $Param         = ['url' => $Url, 'method' => 'POST'];
    $Result        = $ClassCdeV2->MyCurl_APIZOHO_V2($Param);
    $Result        = json_decode($Result, TRUE);
    $Token         = "Zoho-oauthtoken " . $Result['access_token'];
//
    $Url    = 'https://books.zoho.com/api/v3/contacts/'.$ID_BOOKS.'/?organization_id=' . APIZOHO['IdOrg'];
    $Param  = ['url' => $Url, 'method' => 'GET', 'header' => ["Authorization: $Token", "contentType: application/x-www-form-urlencoded;charset=UTF-8"]];
    $Result = $ClassCdeV2->MyCurl_APIZOHO_V2($Param);
    $Client = json_decode($Result, TRUE);
}


// Le clietn existe
if ($Client['code'] == "0") {
    echo "<h1>Traitement</h1>";
    $zcrm_account_id = $Client['contact']['zcrm_account_id'];
    $Email           = $Client['contact']['email'];

    /*
    $zcrm_account_id = "1944598000092989010";
    $Email ="mynkler.telecom@gmail.com";
    */
// TOKEN CRM

    $refresh_token  = "1000.7f7179f96c3cefcf84941f3d721173a1.1fe542becec52b4e2f78cc8cac7ea69c"; // Si l acces token change il faut changer ce code
    $Url           = "https://accounts.zoho.com/oauth/v2/token?refresh_token=$refresh_token&client_id=" . APIZOHO['IdCli'] . "&client_secret=" . APIZOHO['Secret'] . "&grant_type=refresh_token";
    $Param          = ['url'=>$Url,'method'=>'POST'];
    $Result         = $ClassCdeV2->MyCurl_APIZOHO_V2($Param);
    $Result         = json_decode($Result,TRUE );
    $Token          = "Zoho-oauthtoken ".$Result['access_token'];


//  Mise à jour de ACCOUNT
//  ----------------------------------------------------
    $Url  = "https://www.zohoapis.com/crm/v2/Accounts";
    $Json ='{
        "data": [
            {
                "id": "'.$zcrm_account_id.'",
                "Mail_Facturation": "'.$Email.'",
                "Origine": "Commande sur le site",
            }
        ]
    }';

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $Url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'PUT',
        CURLOPT_POSTFIELDS =>$Json,
        CURLOPT_HTTPHEADER => array(
            'Authorization: '.$Token,
            'Content-Type: application/json',
        ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response,TRUE);

    if($response['data'][0]['code']=="SUCCESS") {
        echo "[OK] Modifcation Compte : ".$zcrm_account_id;
    }


    echo "<pre>";
    print_r($response);
    echo "</pre>";

// Phone ou Mobile
    $msisdn = $Cmd['telephone'];
    $Phone ="";
    $Mobile = "";
    $num    = chunk_split($msisdn, 2, ' ');
    $msisdn = preg_replace("/^0/", "+33 ", $num);
    if(substr($num,0,2)=="06" || substr($num,0,2)=="07")    $Mobile   = $msisdn;
    else                                                                             $Phone   = $msisdn;

// Last_name
    $N = explode (' ',$Cmd['nomprenom']);

// Mise à jour de ACCOUNT
//  ----------------------------------------------------
    $Url  = "https://www.zohoapis.com/crm/v2/Contacts";
    $Json ='{
        "data": [    
            {
                "Account_Name"  : "'.$zcrm_account_id.'",
                "Last_Name"     : "'.strtoupper($Cmd['nomprenom']).'",
                "Phone"         : "'.$Phone.'",
                "Mobile"         : "'.$Mobile.'",
                "Email"         : "'.($Cmd['email']).'"
            }
        ]
    }';
// Array
    echo "<pre>"; print_r($Json); echo "</pre>";
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $Url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>$Json,
        CURLOPT_HTTPHEADER => array(
            'Authorization: '.$Token,
            'Content-Type: application/json',
        ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response,TRUE);

    if($response['data'][0]['code']=="SUCCESS") {
        echo "[OK] Création contact : ".strtoupper($Cmd['nomprenom']);
    } else {
        echo "<pre>";
        print_r($response);
        echo "</pre>";
    }



} else {
    echo "ne trouve pas de correspondance client !";
}

?>