I want the delete the store details from PHP Application after uninstalling the app from Shopify store which are stored in database when Application installed,
Now I got some solution for this like following:
$create_webhook = array('webhook'=>array(
'topic'=> 'app/uninstalled',
'address'=>'{{ mydomainname}}/uninstalled.php',
'format'=> 'json'
));
$request_update = $shopify('POST /admin/webhooks.json ', array(), $create_webhook);
But can we create this webhook at installing time or any other time?
Your example code is registering a callback that will be accessed by the shopify uninstall webhook which will run when your app is uninstalled via the shopify admin UI. Make sure that "address" property is a public url to your applications uninstall callback.
Yes, you can register a callback for webhook at anytime.
When the app is uninstalled, shopify will send a POST request to the address you specified in the 'address' property with a json payload that has the customers details.
Take care to collect the X-Shopify-Hmac-SHA256 header and verify this is a genuine request before your callback deletes customer data.
Make sure you have all the data you need before the uninstall hook is run. Uninstall revokes account tokens, removes registered webhooks and app data from shopify.
Without knowing the schema it's hard to tell what data you are trying to remove. If you're able to provide an example of what data you're inserting and what sort of columns are available we may be able to provide a bit more assistance.
Having said all that, it's fairly straight forward to delete a row from a database. I assume you are looking for a script to delete something from a database, the below will do that. You need to update the SQL query to reflect your schema.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
I have implemented this code to subscribe a webhook to an uninstall event of a Shopify App. I have created a custom shopify App in PHP. So here is the explanation step by step:
/* subscribe to app uninstall webhook */
$webhook_array = array(
'webhook' => array(
'topic' => 'app/uninstalled',
'address' => 'https://yourwebsite.com/webhooks/delete.php?shop=' . $shop_url,
'format' => 'json'
)
);
$webhook = shopify_call($access_token, $shop_url, "/admin/api/2021-10/webhooks.json", $webhook_array, 'POST');
$webhook = json_decode($webhook['response'], JSON_PRETTY_PRINT);
/** subscribe to app uninstall webhook **/
$webhook = shopify_call($token, $shop, "/admin/api/2019-10/webhooks.json", array(), 'GET');
$webhook = json_decode($webhook['response'], JSON_PRETTY_PRINT);
echo print_r( $webhook );
<?php
require_once("../inc/mysql_connect.php");
require_once("../inc/functions.php");
define('SHOPIFY_APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxx'); // Replace with your SECRET KEY
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);
$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 shops WHERE shop_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
?>