skip to Main Content

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?

3

Answers


  1. 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();
    ?>
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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:

    1. Add this code where you want to create a webhook. In my case, I added this to generate_token.php file.
        /* 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 **/
    
    1. To test if the webhook is created or not, you can run the below code. In my case I added this code to my index.php file of the app to check the response:
    $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 );
    
    1. Create the delete.php file under "webhook" folder (as we have specified the file – https://yourwebsite.com/webhooks/delete.php to perform deletion ) and use MySQL functions to delete the store data from the database.
        <?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
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search