从 Shopify 商店卸载应用程序后,从 PHP 应用程序中删除商店详细信息

Delete the store details from PHP Application after uninstalling app from Shopify Store

我想在从 Shopify 商店卸载应用程序后从 PHP 应用程序中删除商店详细信息,这些信息在安装应用程序时存储在数据库中,

现在我得到了一些解决方案,如下所示:

$create_webhook = array('webhook'=>array( 
    'topic'=> 'app/uninstalled', 
    'address'=>'{{ mydomainname}}/uninstalled.php',
    'format'=> 'json'
));

$request_update = $shopify('POST /admin/webhooks.json ', array(), $create_webhook);

但是我们可以在安装时或任何其他时间创建这个 webhook 吗?

在不知道架构的情况下,很难判断您要删除哪些数据。如果您能够举例说明要插入的数据以及可用的列类型,我们可能会提供更多帮助。

说了这么多,从数据库中删除一行是相当直接的。我假设您正在寻找一个脚本来从数据库中删除某些内容,下面将执行此操作。您需要更新 SQL 查询以反映您的架构。

<?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();
?>

您的示例代码正在注册一个回调,shopify 卸载 webhook 将访问该回调,当您的应用程序通过 shopify 管理员 UI 卸载时,它将 运行 访问。确保 "address" 属性 是应用程序卸载回调的 public url。

是的,您可以随时为 webhook 注册回调。

应用程序卸载后,shopify 将向您在 'address' 属性 中指定的地址发送一个 POST 请求,其中包含包含客户详细信息的 json payload .

注意收集 X-Shopify-Hmac-SHA256 header 并在您的回调删除客户数据之前验证这是一个真实的请求。

Make sure you have all the data you need before the uninstall hook is run. 卸载会撤销帐户令牌,从 shopify 中删除已注册的 webhook 和应用程序数据。

我已经实现了此代码来为 webhook 订阅 Shopify 应用程序的卸载事件。我在 PHP 中创建了一个自定义 shopify 应用程序。所以这是一步一步的解释:

  1. 在您要创建网络钩子的位置添加此代码。就我而言,我将其添加到 generate_token.php 文件中。
    /* 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. 要测试是否创建了 webhook,您可以 运行 下面的代码。在我的例子中,我将此代码添加到应用程序的 index.php 文件中以检查响应:
$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. 在“webhook”文件夹下创建 delete.php 文件(因为我们已指定文件 - https://yourwebsite.com/webhooks/delete.php 执行删除)并使用 MySQL 函数从数据库。
    <?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
?>