Why is my checkout API modification not updating the shipping address?

pankaj_kamble
Shopify Partner
2 0 0

Hi there,

I'm currently attempting to modify customers' shipping addresses on the checkout page utilizing the Shopify API and webhooks via a PHP script. Despite diligently following the instructions outlined in the official documentation, I've encountered an obstacle. Upon running the cURL command, we're receiving an HTTP response of 406. Could someone kindly assist me in resolving this issue? Below, I've provided a snippet of my code for your reference.

 

date_default_timezone_set("Asia/Calcutta");
// Retrieve the incoming webhook payload
$rawPayload = file_get_contents('php://input');
$payload = json_decode($rawPayload, true);
$fp = file_put_contents('request.log', "\n\n" . date('d-M-Y h:i:s') . " Request parameter are =" . $rawPayload, FILE_APPEND);


// Shopify Store Credentials
$shopifyStore = 'mystore.myshopify.com';
$accessToken = 'access_token';
$api_key = "api_key_value";
$shared_secret = "shared_secret_value";
// Checkout ID
$checkoutId = $payload['id'];

// New Shipping Address Data
$newShippingAddress = [
"address1"=> "Chestnut Street 92",
"address2"=> "Apt 2",
"city"=> "Louisville",
"company"=> null,
"country"=> "United States",
"first_name"=> "Bob",
"last_name"=> "Norman",
"phone"=> "555-625-1199",
"province"=> "Kentucky",
"zip"=> "40202",
"country_code"=> "US",
"province_code"=> "KY"
];


// Update Shipping Address
$updateShippingAddressUrl = "https://{$api_key}:{$shared_secret}$shopifyStore/admin/api/2024-01/checkouts/$checkoutId/shipping_address.json";


$ch = curl_init($updateShippingAddressUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"X-Shopify-Access-Token: $accessToken"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["shipping_address"=>$newShippingAddress]));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

 

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

 

if ($httpCode == 200) {

file_put_contents('webhook_log.txt', "\n\n" . date('d-M-Y h:i:s') ." Shipping address updated successfully".$response, FILE_APPEND);
} else {

file_put_contents('webhook_log.txt', "\n\n" . date('d-M-Y h:i:s') ." Error updating shipping address:".$updateShippingAddressUrl, FILE_APPEND);


}

Replies 0 (0)