代码之家  ›  专栏  ›  技术社区  ›  Kal

在PHP中检测iTunes订阅状态

  •  3
  • Kal  · 技术社区  · 8 年前

    我试图找到一种方法,从后端(PHP/Laravel)检查Apple Store上的用户订阅状态。我们已经知道用户的UUID,我们已经在Apple上设置了订阅。用户在试用期内通过Apple订阅服务后访问应用程序,我需要检查的是,用户在试用期结束后是否实际订阅了该服务。

    我的问题是,如何使用PHP/Laravel作为后端与Apple Store通信以获取用户的订阅状态?

    为了澄清这一点,该应用程序允许用户通过web和移动应用程序订阅服务。通过web很容易获得用户的订阅状态,因为我们使用Stripe。然而,在应用程序上,用户通过Apple Store订阅该服务。那么,您如何使用PHP/Laravel作为后端与Apple Store通信以获取用户的订阅状态呢?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Will Jones    8 年前

    您可以从受信任的服务器上查询App Store以获取此信息。

    沙盒环境的端点是 https://sandbox.itunes.apple.com/verifyReceipt
    至于制作,它是 https://buy.itunes.apple.com/verifyReceipt

    您需要将以下内容作为JSON有效负载发送:

    receipt-data 如果服务器上没有,可以通过调用 appStoreReceiptURL 方法 NSBundle .读取该文件的全部内容并将其发送到服务器。

    Password (仅适用于自动续费订阅,这将是你应用的共享秘密)

    exclude-old-transactions 仅用于包含自动续费或不续费订阅的iOS7样式的应用程序收据。如果值为true,则响应仅包括任何订阅的最新续订事务。

    然后它将返回一个包含收据的有效载荷 status 以及其他一些附加信息。

    Check this article at Apple

    编辑

    使用cURL调用App Store端点(如上链接)。这里是一个粗略的例子,您需要根据您的特定环境对其进行修改,并填写所需的变量。

    $service_url = [one of the two above];
    $curl = curl_init($service_url);
    $curl_post_data = array(
            'receipt-data' => $receiptData,
            'password' => $password, //Only required for certain types of subscription
            'exclude-old-transactions' => $excludeoldtransactions //Depends on your use case, check Apple link
    );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    $curl_response = curl_exec($curl);
    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }
    curl_close($curl);
    $decoded = json_decode($curl_response);
    if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
        die('error occured: ' . $decoded->response->errormessage);
    }
    echo 'response ok!';
    var_dump($decoded->response);
    

    http://php.net/manual/en/book.curl.php

    推荐文章