问题描述
从应用内购买(自动续订)购买产品后,我点击服务器上的API来提供购买信息。
After buying a product from in-app purchase (Auto-renewable), I hit API on server to give buying information.
如何知道该产品是否重新获得-new用于命中API?
How to know that product is re-new for hitting API ?
当用户取消该订阅时,如何知道?
When user cancel that subscription, how to known that ?
推荐答案
Swift 3:为了让您检测用户何时取消订阅,您需要执行以下操作;
Swift 3: In order for you to detect when the user has cancelled the subscription you need to do the following;
- 下载应用收据
- 验证收据,以便您可以获取包含所有应用内购买和订阅词典的json
-
现在,在每个收据中,如果这是订阅购买,则字典中有一个名为 cancellation_date 的字段,否则不能用于其他应用内购买。如果这是零,那么没有取消,但如果它有一个包含取消日期的值,那么取消确实发生了并且根据苹果:
- Download the app receipt
- Validate the receipt so you can get the json back containing all the in-app purchases and subscriptions dictionaries
Now inside each receipt there is a field in the dictionary called cancellation_date if this is subscription purchase and otherwise not available for other in-app purchases. If this is nil then there's no cancellation occurred, but if this has a value which contains the cancellation date then a cancellation did occurred and according to apple:
取消日期
对于Apple客户支持取消的交易,取消的时间和日期。
Cancellation DateFor a transaction that was canceled by Apple customer support, the time and date of the cancellation.
将取消的收据视为与如果没有购买。
Treat a canceled receipt the same as if no purchase had ever been made.
然后下面的链接说明了您可以在收据中使用的所有字段;
then link below explains all the fields you can use inside the receipts;
代码示例:
// Create receipt request
let receiptRefreshRequest = SKReceiptRefreshRequest()
// Get the receiptUrl from the main bundle
let receiptUrl = Bundle.main().appStoreReceiptURL
//If the receipt file exist on local device
if (receiptUrl as NSURL?)?.checkResourceIsReachableAndReturnError(nil) == true{
// Get the file as data
let receipt: Data = try! Data(contentsOf: receiptUrl!)
}
现在您将收据发送到苹果服务器以使用您的服务器验证它作为苹果推荐。从验证中获得回调后,请检查取消日期。
now you send the receipt to apple server to validate it using your server as apple recommend. After you get callback from the validation you check the cancellation date.
这篇关于针对应用内购买的自动续订订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!