<?php
include 'connect.php';
$orders = json_decode(file_get_contents('php://input'));

if(isset($orders->params->defaultAddress)){
    $defaultAddress = json_decode($orders->params->defaultAddress);
    $url1 = $site_api_url_fetch.'/users/'.$orders->data->customer_id.'/addresses/'.$defaultAddress->id;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json',
    'x-api-key: '.$user_key
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $adresesult = curl_exec($ch);
    $usraddress = json_decode($adresesult);
    if (is_array($usraddress->result)) {
        foreach ($usraddress->result as &$item) {
            unset($item->user);
            unset($item->metafields);
        }
        unset($item);
    } elseif (is_object($usraddress->result)) {
        unset($usraddress->result->user);
        unset($usraddress->result->metafields);
    }
    //echo "<pre>";
    //print_r($usraddress->result);

    $defaultAddress = $usraddress->result;
    $postfieldsmr = array(
    "value" =>  json_encode($defaultAddress),
    "value_type" => 'json',
    "key" => 'defaultAddress'
    );
    $data_jsonmr = json_encode($postfieldsmr);
    $chd = curl_init();
    curl_setopt($chd, CURLOPT_URL,  $API_URL.$orders->query->version.'/'.$orders->query->lang.'/'.$orders->query->project.'/orders/'.$orders->data->id.'/metafields');
    curl_setopt($chd, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($chd, CURLOPT_POSTFIELDS, $data_jsonmr);
    curl_setopt($chd, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'X-Api-Key:'.$user_key;
    curl_setopt($chd, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($chd);
    $resultd = json_decode($result);
    if (curl_errno($chd)) {
        //echo 'Error:' . curl_error($chd);
    }
    curl_close($chd);
    $orders->data->defaultAddress = $defaultAddress;
    file_put_contents('data.txt', "resultd-".json_encode($resultd). "\n", FILE_APPEND);
}

if(isset($orders->data->summary->delivery_price_with_discount) && $orders->data->summary->delivery_price_with_discount == 0){


    $url = $site_api_url_fetch.'/users/'.$orders->data->customer_id.'/metafields';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Accept: application/json',
    'x-api-key: '.$user_key
    ));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $usrresult = curl_exec($ch);
    $usrredata = json_decode($usrresult);
    $metafileds_id = '';
    $ordercountfrdel = 1;
    if(sizeof($usrredata->result) > 0){
        foreach($usrredata->result as $mval){
            if($mval->key == 'ordercountfrdel'){
                $metafileds_id = $mval->id;
                $ordercountfrdel = $ordercountfrdel + $mval->value;
                break;
            }
        }
    }


    $site_api_url = $API_URL.$orders->query->version.'/'.$orders->query->lang.'/'.$orders->query->project.'/';
    if($metafileds_id != ''){
            $postfields = array(
            "value" => $ordercountfrdel,
            "value_type" => 'integer',
            "key" => 'ordercountfrdel'
            );
            $data_json = json_encode($postfields);
            $chp = curl_init();
            curl_setopt($chp, CURLOPT_URL, $site_api_url.'users/'.$orders->data->customer_id.'/metafields/'.$metafileds_id);
            curl_setopt($chp, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chp, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chp, CURLOPT_CUSTOMREQUEST, 'PUT');
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chp, CURLOPT_HTTPHEADER, $headers);
            $resultd = curl_exec($chp);
            $resultd = json_decode($resultd);
            curl_close($chp);
        }else{
            $chd5 = curl_init();
            $postfields = array(
            "value" => $ordercountfrdel,
            "value_type" => 'integer',
            "key" => 'ordercountfrdel'
            );
            $data_json = json_encode($postfields);

            curl_setopt($chd5, CURLOPT_URL, $site_api_url.'users/'.$orders->data->customer_id.'/metafields');
            curl_setopt($chd5, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($chd5, CURLOPT_POSTFIELDS, $data_json);
            curl_setopt($chd5, CURLOPT_POST, 1);
            $headers = array();
            $headers[] = 'Content-Type: application/json';
            $headers[] = 'X-Api-Key: '.$user_key;
            curl_setopt($chd5, CURLOPT_HTTPHEADER, $headers);
            $resultd3 = curl_exec($chd5);
            $resultd3 = json_decode($resultd3);
            curl_close($chd5);
        }
}

echo json_encode($orders->data);
?>
