iOS 应用内购买 returns 多笔交易的收据验证

Receipt validation on iOS In-App-Purchase returns multiple transaction

沙盒模式下的应用内购买returns同一产品 ID 的多次交易。

使用的语言:Swift4.0

func validateAppReceipt(_ receipt: Data) {
    let base64encodedReceipt = receipt.base64EncodedString()
    print(base64encodedReceipt)
    let requestDictionary = ["receipt-data":base64encodedReceipt]
    guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
    do {
        let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
        let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"  // this works but as noted above it's best to use your own trusted server
        guard let validationURL = URL(string: validationURLString) else { print("the validation url could not be created, unlikely error"); return }
        let session = URLSession(configuration: URLSessionConfiguration.default)
        var request = URLRequest(url: validationURL)
        request.httpMethod = "POST"
        request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
        let task = session.uploadTask(with: request, from: requestData) { (data, response, error) in
            if let data = data , error == nil {
                do {
                    print(data)
                    let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                    print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
                    self.getAppReceipt()
                } catch let error as NSError {
                    print("json serialization failed with error: \(error)")
                }
            } else {
                print("the upload task returned an error: \(error)")
            }
        }
        task.resume()
    } catch let error as NSError {
        print("json serialization failed with error: \(error)")
    }
}

回复:

问题:

我尝试了一些来自 apple 和 stack overflow 的链接,但仍然对理解这一点有疑问。谁能给我描述一下所有这些。

我认为您已经在应用购买中实现了自动更新。 你的回答是正确的。

为什么我在同一 ID 上收到多笔交易

正如您每 5 分钟为自动更新产品执行的响应交易(沙盒环境在 5 分钟内更新产品,而不是 App Store 中的 1 个月)。

这个回答是否正确

如果正确,验证哪个ID

您必须获取您的产品 ID tfc.premium.subscription 的所有交易,然后获取最后一个对象并将其用作您的 latest/last 交易。

阅读此 Apple Document 以获得正确的理解。

The behavior of auto-renewable subscriptions differs between the testing environment and the production environment.

In the testing environment, subscription renewals happen at an accelerated rate, and auto-renewable subscriptions renew a maximum of six times per day. This enables you to test how your app handles a subscription renewal, a subscription lapse, and a subscription history that includes gaps. See Testing Auto-Renewable Subscriptions in the In-App Purchase Configuration Guide for iTunes Connect to learn about the subscription durations for testing.

Because of the accelerated expiration and renewal rates, a subscription can expire before the system tries to renew the subscription, leaving a small lapse in the subscription period. Such lapses are also possible in production for a variety of reasons—make sure your app handles them correctly.

您正在使用自动续订订阅。每次自动续订或续订订阅都会有一张收据。

所以一般情况下,如果订阅已经续费5次,那么沙盒环境下会有5张收据,真实环境(生产环境,从AppStore下载)会有6张收据。因为从 AppStore 下载或购买的应用程序也会生成收据。