Receipt validation on iOS In-App-Purchase returns

2020-05-29 05:40发布

In-app purchase on sandbox mode returns multiple transactions on same product id.

Language Used: Swift 4.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)")
    }
}

Response:

Response:

Questions:

  • Why I am getting multiple transaction on same ID

  • Whether this response is correct or not

  • If it is correct, which ID to validate

I tried some links from apple and stack overflow but still have doubts in understanding this. Can someone please describe me about all these.

2条回答
家丑人穷心不美
2楼-- · 2020-05-29 06:24

I think you have implemented auto renewable in app purchase. Your response is proper.

Why I am getting multiple transaction on same ID

As in your response transaction performed for auto renew product at every 5 minutes(Sandbox environment renew product in 5 minutes instead of 1 month in App Store).

Whether this response is correct or not

Yes

If it is correct, which ID to validate

You have to get all the transaction for your product id tfc.premium.subscription and then grab last object and use it as your latest/last transaction.

Read this Apple Document for proper understanding.

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.

查看更多
时光不老,我们不散
3楼-- · 2020-05-29 06:42

You are using auto renewable subscriptions. There will be one receipt for every auto-renew or renew of the subscription.

So generally, if subscription has renewed 5 times then there will be 5 receipts in the sandbox environment and 6 in real environment (production environment, downloaded from AppStore). Because an receipt is generated for the app downloaded or purchased from the AppStore as well.

查看更多
登录 后发表回答