<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once('config.php');
require('vendor/autoload.php');
use Shopify\PuplicApp;
use rdx\graphqlquery\Query;


$client = new Shopify\PublicApp($SHOPIFY_SHOP_DOMAIN, $SHOPIFY_API_KEY, $SHOPIFY_API_SECRET, $api_params);
  $client->setAccessToken($SHOPIFY_TOKEN);
echo '<pre>';
  $data = array();


  global $ci_connection;
  
   $webhook_content = '';
    $webhook = fopen('php://input' , 'rb');
    while(!feof($webhook)){ //loop through the input stream while the end of file is not reached
        $webhook_content .= fread($webhook, 4096); //append the content on the current iteration
    }
    fclose($webhook); //close the resource
  $fp = fopen('error_order.txt', 'w');


    $order = json_decode($webhook_content, true); 
 

$payment_gateway_names = end($order['payment_gateway_names']);

if($payment_gateway_names=='shopify_payments') {
    $payment_gateway_names = 'SHOPIFY';
}

$orderupdateshopify = 'mutation {';

        $orderupdateshopify .=  ' orderUpdate(input:{';
        $orderupdateshopify .= 'id: "gid://shopify/Order/' . $order['id'] . '",';


        $orderupdateshopify .= ' tags :  "' . $payment_gateway_names . '",
           ';


        $orderupdateshopify .= ' },';
        $orderupdateshopify .= ' ) { order {
          id
          tags
          }
          userErrors {
          field
           message
          }
          }';
          
             $orderupdateshopify .='}';
             
                fwrite($fp,json_encode($orderupdateshopify));

fclose($fp);
             
             $client->callGraphql($orderupdateshopify);