苹果自动续签是很大的坑,不会自动通知后端
第一次购买苹果自动续签商品时,校验通过后返回的数据结构
{
"latest_receipt": "*****",
"latest_receipt_info": {
"original_purchase_date_pst": "2020-01-17 23:57:08 America\/Los_Angeles",
"quantity": "1",
"subscription_group_identifier": "20588826",
"unique_vendor_identifier": "0B78F3A0-FECF-4FBC-9F48-59BEFE8418E0",
"original_purchase_date_ms": "1579334228000",
"expires_date_formatted": "2020-01-18 08:02:07 Etc\/GMT",
"is_in_intro_offer_period": "false",
"purchase_date_ms": "1579334227000",
"expires_date_formatted_pst": "2020-01-18 00:02:07 America\/Los_Angeles",
"is_trial_period": "false",
"item_id": "1494425169",
"unique_identifier": "6f68a698a2b0c2e3fc92618f94b561e5e8def646",
"original_transaction_id": "1000000616612083",
"expires_date": "1579334527000",
"transaction_id": "1000000616612083",
"bvrs": "1",
"web_order_line_item_id": "1000000049649460",
"version_external_identifier": "0",
"bid": "com.Anyue.Cleaner",
"product_id": "m1",
"purchase_date": "2020-01-18 07:57:07 Etc\/GMT",
"purchase_date_pst": "2020-01-17 23:57:07 America\/Los_Angeles",
"original_purchase_date": "2020-01-18 07:57:08 Etc\/GMT"
},
"environment": "Sandbox",
"auto_renew_status": "true",
"password": "086b56c6662b4d13b546fec06d14b0cb",
"auto_renew_product_id": "m1",
"notification_type": "INITIAL_BUY"
}
1,首先用户第一次购买订阅,苹果会通知服务端,其中notification_type 对应值为 INITIAL_BUY,保存起来在数据表中,苹果坑就坑在第二次第三次自动订阅是不会有回调的
2,服务器做计划任务每天定期向苹果服务器检测(下面代码),通过latest_receipt检测目前已有的所有订阅订单是否过期,如果发现过期了,就去苹果服务器验证receipt,其中苹果返回的latest_receipt_info 字段,会告诉最新的订阅订单情况,你可以校验expires-date与当前时间比较,判断该订阅有没有续订成功,并同时更新上述让记录的record_expires_date字段.
/**IOS内购验证票据
* @param string $receipt_data 付款后凭证
* @param bool $sandbox 是否为沙盒
* @param bool $transactions 自动续订是必须为true
* @return mixed
*/
public function validate_applepay($receipt_data,$sandbox=true,$transactions=true)
{
$jsonData = array('receipt-data' => $receipt_data, 'password' => config('apple_pay')['password'],'exclude-old-transactions'=>$transactions);
$post_json = json_encode($jsonData);
$url=$sandbox===true?"https://sandbox.itunes.apple.com/verifyReceipt":"https://buy.itunes.apple.com/verifyReceipt";
$client=new Client(['verify' => false]);
$response= $client->post($url,[
'body'=>$post_json,
'headers' => [
'Content-Type' => 'application/json'
]
]);
$result= (string)$response->getBody()->getContents();
$res= json_decode($result, true);
if (intval($res['status']) == 0) { //验证成功
//校验通过后的处理
}
}
苹果服务器会在订阅过期的前一天,对用户进行自动扣费,如果扣费成功了,苹果服务器并不会通知我们的服务器,这是重点。不过有个特例,如果苹果订阅过期前一天扣费失败了,苹果服务器后面几天还会尝试对用户自动扣费,如果后面扣费成功了(这时候用户实际状态是没有续订成功),苹果会通知我们的server的,其中notification_type 对应值为 RENEWAL,对于RENEWAL我们还是需要给用户更新为正在订阅的状态。
正式环境下,用户主动取消订阅,苹果会通知我们的server的,其中notification_type 对应值为 CANCEL,我们需要更新用户订阅的状态为取消。
总结,对于自动续订订阅,我们自己的服务器完全可以与apple server的交互应对用户的订阅状态,只需要确定客户端传来的用户第一次购买, user id 对应 original-transaction-id的关系。后面的续订,取消,变更套餐,完全不依赖于客户端传来的信息。

4141

被折叠的 条评论
为什么被折叠?



