Apple Store 支付
参考文章
* [App 内购买项目](https://developer.apple.com/cn/in-app-purchase/)
* [App 内购买项目类型](https://help.apple.com/app-store-connect/#/dev3cd978dbd)
* [App 内购买项目信息](https://help.apple.com/app-store-connect/#/dev84b80958f)
* [自动续期订阅信息](https://help.apple.com/app-store-connect/#/dev7f2d6b652)
* [App 内购买项目状态](https://help.apple.com/app-store-connect/#/dev840c56fb6)
* [测试 App 内购买项目](https://help.apple.com/app-store-connect/#/dev7e89e149d)
-
App Store Server Notifications The JSON data sent in the server notification from the App Store.
-
App Store Receipts Validate app and in-app purchase receipts with the App Store.
[图片上传失败...(image-126740-1587958325949)]
订阅通知状态
CANCEL
Indicates that either Apple customer support canceled the subscription or the user upgraded their subscription. The cancellation_date
key contains the date and time of the change.
DID_CHANGE_RENEWAL_PREF
Indicates the customer made a change in their subscription plan that takes effect at the next renewal. The currently active plan is not affected.
DID_CHANGE_RENEWAL_STATUS
Indicates a change in the subscription renewal status. Check auto_renew_status_change_date_ms
and auto_renew_status
in the JSON response to know the date and time of the last status update and the current renewal status.
DID_FAIL_TO_RENEW
Indicates a subscription that failed to renew due to a billing issue. Check is_in_billing_retry_period
to know the current retry status of the subscription, and grace_period_expires_date
to know the new service expiration date if the subscription is in a billing grace period.
DID_RECOVER
Indicates successful automatic renewal of an expired subscription that failed to renew in the past. Check expires_date
to determine the next renewal date and time.
INITIAL_BUY
Occurs at the initial purchase of the subscription. Store latest_receipt
on your server as a token to verify the user’s subscription status at any time, by validating it with the App Store.
INTERACTIVE_RENEWAL
Indicates the customer renewed a subscription interactively, either by using your app’s interface, or on the App Store in the account's Subscriptions settings. Make service available immediately.
RENEWAL
Indicates successful automatic renewal of an expired subscription that failed to renew in the past. Check expires_date
to determine the next renewal date and time.
[图片上传失败...(image-ac420d-1587958325949)]
内购流程
1、流程图
image2、流程说明
1.客户端向Appstore请求购买产品(假设产品信息已经取得),Appstore验证产品成功后,从用户的Apple账户余额中扣费。
2.Appstore向客户端返回一段receipt-data,里面记录了本次交易的证书和签名信息。
3.客户端向我们可以信任的服务器提供receipt-data。
4.服务器对receipt-data进行一次base64编码。
5.把编码后的receipt-data发往itunes.appstore进行验证。
6.itunes.appstore返回验证结果给服务器。
7.服务器对商品购买状态以及商品类型,向客户端发放相应的道具与推送数据更新通知。
8.客户端收到服务器的处理状态,进行相应的结单处理。
3、服务器处理订阅通知
- 首先用户第一次购买订阅,server需要把票据存储(最好把过期时间也记录一下,字段record_expires_date),苹果会通知我们的server的,其中notification_type 对应值为 INITIAL_BUY。
- 服务器需要做个定期(每天)检测,检测目前已有的所有订阅订单是否过期,如果发现过期了,就去苹果服务器验证receipt,其中苹果返回的latest_receipt_info 字段,会告诉最新的订阅订单情况,你可以校验expires-date与当前时间比较,判断该订阅有没有续订成功,并同时更新上述让记录的record_expires_date字段.
- 我们为什么做上述的处理?大家都知道苹果服务器会在订阅过期的前一天,对用户进行自动扣费,如果扣费成功了,苹果服务器并不会通知我们的服务器,这是重点。不过有个特例,如果苹果订阅过期前一天扣费失败了,苹果服务器后面几天还会尝试对用户自动扣费,如果后面扣费成功了(这时候用户实际状态是没有续订成功),苹果会通知我们的server的,其中notification_type 对应值为 RENEWAL,对于RENEWAL我们还是需要给用户更新为正在订阅的状态。
- 正式环境下,用户主动取消订阅,苹果会通知我们的server的,其中notification_type 对应值为 CANCEL,我们需要更新用户订阅的状态为取消。
- 总结,对于自动续订订阅,我们自己的服务器完全可以与apple server的交互应对用户的订阅状态,只需要确定客户端传来的用户第一次购买, user id 对应 original-transaction-id的关系。后面的续订,取消,变更套餐,完全不依赖于客户端传来的信息。
4、续定无通知
同一个订单凭据是可以一直使用的,不管你后面续订了多少次,随便这些中的一个凭据发给苹果验证,就能得到所有的订单信息和订阅状态,这样每个周期结束的时候(试用期最后一天或者月底),就可以根据票据信息去得到用户是否仍然续订的信息,这样就可以决定是否继续给下个月的 VIP 了。
5、容易发生的问题
1. 漏单
2. 网络异常
3. 苹果服务器异常
4. 重单
5. 游客购买