Salve,
ho da poco approcciato l'uso dei webhook ed ho creato una classe in php che viene lanciata dal webhook appena arriva un ordine.
Per gli ordini in COD con app COD non ho problemi, recuperando tutti i dati, invece in checkout non mi da nessu dato del cliente (indirizzo, nome e cognome) cosa sbaglio? Anche se chiamo con postman il webhook order il json è incompleto.
posto il codice:
try {
$conn = new PDO($sql, $username, $password);
error_log(' DB Connect = Success.');
} catch (PDOException $error) {
echo 'Connection error: ' . $error->getMessage();
}
$store_id=$_GET["store_id"];
$sql = "SELECT * FROM store where id=$store_id";
error_log('Store_id : '.$sql);
foreach ($conn->query($sql) as $row) {
print $row['secret_key'] . "\t";
$shared_secret=$row['secret_key'] ;
}
error_log('Inizio verifica');
define('SHOPIFY_APP_SECRET',$shared_secret);
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, $shared_secret, true));
$verified=0;
if ($hmac_header == $calculated_hmac) {
http_response_code(200);
$verified=1;
error_log('Secret_in : '.SHOPIFY_APP_SECRET);
error_log('Calculated verifica: '.var_export($calculated_hmac,true));
error_log('HMAC verified: '.var_export($hmac_header,true));
} else {
// HMAC is invalid, return a 401 response
http_response_code(401);
error_log('errore');
}
/* error_log('Secret : '.$secret);
define('SHOPIFY_APP_SECRET',$secret);*/
/*$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
error_log('Warning verified: '.var_export($verified,true));*/
if($verified==1) {
$response = $data;
// Decode Shopify POST
$webhook_content = json_decode($response, TRUE);
$log = fopen('order'.$webhook_content['id'].'.json','w') or die ('Cant open the file');
error_log('Cant open the: '.var_export($log,true));
fwrite($log, $response);
fclose($log);
error_log('apro ordine: ');
//Assign to variable
$order_id = $webhook_content['id'];
$order_number = $webhook_content['name'];
$customer_id= $webhook_content['customer']['id'];
$payment_gateway = $webhook_content['payment_gateway_names'][0];
$financial_status = $webhook_content['financial_status'];
if($financial_status=='paid'){
$order_status = 3;
}
else{
$order_status = 0;
}
$order_value = $webhook_content['total_price'];
$contact_email = $webhook_content['contact_email'];
$shipping_province = $webhook_content['shipping_address']['province'];
// I wanted to insert the variant_id's and quantity as a string in one column. With this i can unserialise and use when needed
/*$items = [];
foreach($webhook_content["note_attributes"] as $item) {
error_log( $items["name"]);
error_log( $items["value"]);
}
*/
//dati del cliente
$pv=$webhook_content['billing_address']['province_code'];
if($webhook_content['billing_address']['first_name']!=''){
error_log('entro: ');
$f_name = addslashes($webhook_content['billing_address']['first_name']);
error_log('Nome: '. $f_name);
$full_name = addslashes($webhook_content['billing_address']['name']);
$l_name = addslashes($webhook_content['billing_address']['last_name']);
$address = addslashes($webhook_content['billing_address']['address1']);
$city = addslashes($webhook_content['billing_address']['city']);
$zip=$webhook_content['billing_address']['zip'];
$country=$webhook_content['billing_address']['country_name'];
$phone=$webhook_content['billing_address']['phone'];
if(!isset($phone))$phone=0;
}else{
error_log('fuori: ');
$i=0;
foreach($webhook_content['note_attributes'] as $item) {
print($item['value']);
if($item['name']=='Country code') $country=addslashes($item['value']);
if(str_contains($item['name'],'Nome')) $f_name=addslashes($item['value']);
if(str_contains($item['name'],'Cognome')) {
if($f_name!=addslashes($item['value'])){
$f_name.=' '.addslashes($item['value']);
}
}
if(str_contains($item['name'],'Telefono')) $phone=addslashes($item['value']);
if(str_contains($item['name'],'Indirizzo')) $address=addslashes($item['value']);
if(str_contains($item['name'],'Numero civico')) $num=$item['value'];
if(str_contains($item['name'],'Città')) $city=addslashes($item['value']);
if(str_contains($item['name'],'Cap')) $zip=addslashes($item['value']);
if(str_contains($item['name'],'Email')) $contact_email=$item['value']; $i++;
error_log($i);
}
$address=$address.",".$num;
$full_name=$f_name;
}
$created_at= date('Y-m-d H:i:s', strtotime($webhook_content['created_at'])+ 120*60);
$updated_at= date('Y-m-d H:i:s', strtotime($webhook_content['updated_at'])+ 120*60);
$shipping_method = $webhook_content['shipping_lines'][0]['title'];
$total_price=$webhook_content['total_price'];
$weight=0;
$prod_id=0;
$products=$webhook_content['line_items'];
$num=count($products);
$j=0;
foreach($products as $product){
$j++;
$product_id=$product['variant_id'];
$variant_id= $product['variant_id'];
if( $product_id==null)
{
$product_id=$product['id'];
$variant_id=$product_id;
}
$product_quantity= $product['quantity'];
$product_name=addslashes( $product['name']);
$vendor= $product['vendor'];
$price= $product['price'];
$sql = "SELECT count(*) FROM product where product_id=$product_id";
$res = $conn->query($sql);
$count = $res->fetchColumn();
error_log('query : '.$count);
if($count==0){
$stmt = $conn->query("INSERT INTO product(
product_id,
variant_id,
title,
store_id,
vendor,
weight,
price)
VALUES (
'$product_id',
'$variant_id',
'$product_name',
'$store_id',
'$vendor',
'1',
'$price')");
}
if($j==1){
$productfirst_id=$product['variant_id'];
$productfirst_quantity= $product['quantity'];
$productfirst_variant_id= $product['variant_id'];
$productfirst_name= $product['name'];
$productfirst_vendor= $product['vendor'];
$productfirst_price= $product['price'];
}
if(($num>1)&&($j>1)){
$stmt = $conn->query("INSERT INTO upsell(product_id,order_id,quantity)
VALUES (
'$variant_id',
'$order_id',
'$product_quantity'
)");
}
}
$stmt = $conn->query("INSERT INTO customer(
cust_id,
name,
surname,
phone,
address,
city,
pv,
email,
cap)
VALUES (
'$customer_id',
'$f_name',
'$l_name',
'$phone',
'$address',
'$city',
'$pv',
'$contact_email',
'$zip')");
$sql = "SELECT * FROM customer where cust_id=$customer_id ORDER BY id DESC LIMIT 1";
error_log('query : '.$sql);
foreach ($conn->query($sql) as $row) {
error_log('id_pad : '.$row['id']);
$cust_idpad=$row['id'] ;
}
error_log('id_pad fuori : '.$cust_idpad);
$sql = "SELECT * FROM customer INNER JOIN orders on orders.cust_idpad=customer.id where phone=$phone and product_id=$productfirst_id LIMIT 1";
error_log('query : '.$sql);
foreach ($conn->query($sql) as $row) {
error_log('cont : '.$row['id']);
$cont=$row['id'] ;
}
error_log('già presente : '.$cont);
if(isset($cont)){
$order_status=11;
}
$stmt = $conn->query("INSERT INTO orders(order_id,
order_number,
cust_id,
cust_fname,
cust_idpad,
product_id,
quantity,
price,
total_price,
payment_gateway,
financial_status,
order_value,
order_status,
ship_to,
created_at,
updated_at,
shipping_method,
note_courier,
id_store)
VALUES ('$order_id',
'$order_number',
'$customer_id',
'$full_name',
'$cust_idpad',
'$productfirst_id',
'$productfirst_quantity',
'$productfirst_price',
'$total_price',
'$payment_gateway',
'$financial_status',
'$order_value',
'$order_status',
'$shipping_province',
'$created_at',
'$updated_at',
'$shipping_method',
'$phone',
'$store_id')");
}else{
error_log('webhook null');
}