在PHP中检测iTunes订阅状态

时间:2018-06-05 15:45:37

标签: php ios laravel itunesconnect

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

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

为了澄清,该应用程序允许用户通过网络和移动应用程序订阅服务。通过网络很容易获得用户的订阅状态,因为我们正在使用Stripe。但是,在应用程序上,用户通过Apple Store订阅该服务。再说一次,您如何使用PHP / Laravel作为后端与Apple Store进行通信以获取用户的订阅状态?

1 个答案:

答案 0 :(得分:1)

您可以从受信任的服务器询问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