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:
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.
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.
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.