在 PHP 中检测 iTunes 订阅状态
Detect iTunes Subscription Status in PHP
我正在尝试找到一种方法来从后端 (PHP/Laravel) 检查 Apple Store 上的用户订阅状态。我们已经知道用户的 UUID,并且我们在 Apple 上设置了订阅。用户通过Apple订阅服务后访问App试用期,我需要检查的是用户是否在试用期结束后实际订阅了服务。
我的问题是您如何使用 PHP/Laravel 作为后端与 Apple Store 通信以获取用户的订阅状态?
澄清一下,该应用程序允许用户通过网络和移动应用程序订阅服务。通过网络很容易获得用户的订阅状态,因为我们使用的是 Stripe。但是,在应用程序上,用户通过 Apple Store 订阅该服务。再说一次,你如何与 Apple Store 通信以使用 PHP/Laravel 作为后端来获取用户的订阅状态?
您可以从受信任的服务器查询 App Store 以获取此信息。
沙盒环境的端点是https://sandbox.itunes.apple.com/verifyReceipt
对于生产,它是 https://buy.itunes.apple.com/verifyReceipt
您需要将以下内容作为 JSON 负载发送:
receipt-data
如果您的服务器上没有它,可以通过调用 NSBundle
的 appStoreReceiptURL
方法来检索它。阅读该文件的全部内容并将其发送到您的服务器。
Password
(仅适用于自动续订订阅,它将是您应用的共享密钥)
exclude-old-transactions
仅用于包含自动续订或非续订订阅的 iOS7 样式应用收据。如果值为 true,则响应仅包括任何订阅的最新续订交易。
然后它将 return 包含收据 status
和一些其他附加信息的有效负载。
编辑
使用 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);
我正在尝试找到一种方法来从后端 (PHP/Laravel) 检查 Apple Store 上的用户订阅状态。我们已经知道用户的 UUID,并且我们在 Apple 上设置了订阅。用户通过Apple订阅服务后访问App试用期,我需要检查的是用户是否在试用期结束后实际订阅了服务。
我的问题是您如何使用 PHP/Laravel 作为后端与 Apple Store 通信以获取用户的订阅状态?
澄清一下,该应用程序允许用户通过网络和移动应用程序订阅服务。通过网络很容易获得用户的订阅状态,因为我们使用的是 Stripe。但是,在应用程序上,用户通过 Apple Store 订阅该服务。再说一次,你如何与 Apple Store 通信以使用 PHP/Laravel 作为后端来获取用户的订阅状态?
您可以从受信任的服务器查询 App Store 以获取此信息。
沙盒环境的端点是https://sandbox.itunes.apple.com/verifyReceipt
对于生产,它是 https://buy.itunes.apple.com/verifyReceipt
您需要将以下内容作为 JSON 负载发送:
receipt-data
如果您的服务器上没有它,可以通过调用 NSBundle
的 appStoreReceiptURL
方法来检索它。阅读该文件的全部内容并将其发送到您的服务器。
Password
(仅适用于自动续订订阅,它将是您应用的共享密钥)
exclude-old-transactions
仅用于包含自动续订或非续订订阅的 iOS7 样式应用收据。如果值为 true,则响应仅包括任何订阅的最新续订交易。
然后它将 return 包含收据 status
和一些其他附加信息的有效负载。
编辑
使用 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);