I have already shared a blog where I implemented In-App Subscription in iOS. If you have not read yet then click here.
In this blog, I am going to show how we validate the Subscription? I will start with Step 11 from the previous blog.
Step 1: Call the receiptValidation() Method from the updatedTransactions. Note that only receiptValidation() call only when Transaction State is purchased.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
extension IPAService:SKPaymentTransactionObserver{ func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { for transaction in transactions{ print(transaction.transactionState.status(), transaction.payment.productIdentifier) switch transaction.transactionState { case .purchasing: break; case .purchased: self.receiptValidation() queue.finishTransaction(transaction) case .restored: queue.finishTransaction(transaction) default: queue.finishTransaction(transaction) } } } |
Step 2: Now handle the receiptValidation() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
func receiptValidation() { let verifyReceiptURL = "https://sandbox.itunes.apple.com/verifyReceipt" let receiptFileURL = Bundle.main.appStoreReceiptURL let receiptData = try? Data(contentsOf: receiptFileURL!) let recieptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) let jsonDict: [String: AnyObject] = ["receipt-data" : recieptString! as AnyObject, "password" : "password" as AnyObject] do { let requestData = try JSONSerialization.data(withJSONObject: jsonDict, options: JSONSerialization.WritingOptions.prettyPrinted) let storeURL = URL(string: verifyReceiptURL)! var storeRequest = URLRequest(url: storeURL) storeRequest.httpMethod = "POST" storeRequest.httpBody = requestData let session = URLSession(configuration: URLSessionConfiguration.default) let task = session.dataTask(with: storeRequest, completionHandler: { [weak self] (data, response, error) in do { if let jsonResponse = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary{ print("Response :",jsonResponse) if let date = self?.getExpirationDateFromResponse(jsonResponse) { print(date) } } } catch let parseError { print(parseError) } }) task.resume() } catch let parseError { print(parseError) } } |
There is two verifyReceiptURL for gathering the information from iTunes for both sandbox and live.
1 2 3 |
SandBox: "https://sandbox.itunes.apple.com/verifyReceipt" Live : "https://buy.itunes.apple.com/verifyReceipt" |
The verifyReceiptURL API having 2 params. One is receipt-data which content local receipt stored data and the second one is the password where you have to pass Shared Secret.
Step 3: Now get the expire date from the response data.
Call the getExpirationDateFromResponse() after the data received from the API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func getExpirationDateFromResponse(_ jsonResponse: NSDictionary) -> Date? { if let receiptInfo: NSArray = jsonResponse["latest_receipt_info"] as? NSArray { let lastReceipt = receiptInfo.lastObject as! NSDictionary let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV" if let expiresDate = lastReceipt["expires_date"] as? String { return formatter.date(from: expiresDate) } return nil } else { return nil } } |
Conclusion
So please follow the above steps to validate the subscription and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.