FROM CACHE - en_header

uninstall webhook

lalit4
New Member
2 0 0

so i'm using two php code one is webhook.php inside that i'm taking response from shopify via providing the path inside app setup in shop webhook field and delete.php to delete db data whenever the app is uninstall but somehow it didn't work can anyone check and tell me where is the issue. i,m attaching both file here

 

WEBHOOK.PHP>>>>>>

<?php
 
require_once("../inc/functions.php");
require_once("../inc/connect.php");
 
 
 
$shop_url = $_GET['shop'];
//$store_url = $_GET['shop'];
 
$array = array(
'webhook' => array(
'topic' => 'app/uninstalled', 
'address' => 'serverpath/web-hooks/delete.php?shop=' . $shop_url,
'format' => 'json'
)
);
 
 
$parsedUrl = parse_url('https://' . $shop_url );
$host = explode('.', $parsedUrl['host']);
$subdomain = $host[0];
$shop = $subdomain;
 
$getdata = "SELECT * FROM shopify_access_token WHERE store_url='$shop_url' AND flag='0' LIMIT 1";
$relults = mysqli_query($conn,$getdata);
$getRow = mysqli_fetch_row($relults);
$token = $getRow[2];
 
 
$webhook = shopify_call($token, $shop, "/admin/api/2021-01/webhooks.json", $array, 'POST');
$webhook = json_decode($webhook['response'], JSON_PRETTY_PRINT);
print_r($webhook);
?>
 
 
 
Delete.php>>>>>>>>>>>>>>>>>>>>>>>

<?php
require_once("../inc/connect.php");
require_once("../inc/functions.php");


echo "deleted";

define('SHOPIFY_APP_SECRET', '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');

function verify_webhook($data, $hmac_header)
{
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
return hash_equals($hmac_header, $calculated_hmac);
}

$res = '';
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$topic_header = $_SERVER['HTTP_X_SHOPIFY_TOPIC'];
$shop_header = $_SERVER['HTTP_X_SHOPIFY_SHOP_DOMAIN'];
$data = file_get_contents('php://input');
$decoded_data = json_decode($data, true);
$newFileName = "file.txt";
file_put_contents($newFileName, $decoded_data);
$verified = verify_webhook($data, $hmac_header);

if( $verified == true ) {
if( $topic_header == 'app/uninstalled' || $topic_header == 'shop/update') {
if( $topic_header == 'app/uninstalled' ) {

$sql = "DELETE FROM shopify_access_token WHERE store_url='$shop_header' LIMIT 1";
$result = mysqli_query($conn, $sql);
$response->shop_domain = $decoded_data['shop_domain'];
$res = $decoded_data['shop_domain'] . ' is successfully deleted from the database';
} else {
$res = $data;
}
}
} else {
$res = 'The request is not from Shopify';
}

error_log('Response: '. $res); //check error.log to see the result


?>

Reply 1 (1)
gthibault
New Member
3 0 0

Hi,

you must specify the protocol and domain when creating the webhooks:

$array = array(
'webhook' => array(
'topic' => 'app/uninstalled',
'address' => 'https://example.hostname.com/web-hooks/delete.php?shop=' . $shop_url,
'format' => 'json'
)
);

be careful not to expose your SHOPIFY_APP_SECRET you must generate a new one from the application's control panel