Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 961
Prajnaranjan Das Avatar asked Oct 29 '25 16:10

Prajnaranjan Das


1 Answers

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.

like image 101
technerd Avatar answered Oct 31 '25 05:10

technerd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!