IAP Receipt is invalid when loaded from Sandbox user - ios

This question is specific to StoreKit receipt validation & restore purchase testing. I made a restore purchase by calling
SKPaymentQueue.default().restoreCompletedTransactions()
After which I got a callback with updated transactions list and internally refreshes the receipt.
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { }
Meanwhile when I tested the receipt inside paymentQueueRestoreCompletedTransactionsFinished,
fileprivate func loadReceipt() -> String? {
// Get the receipt if it's available
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
print(receiptData)
let receiptString = receiptData.base64EncodedString(options: [])
print("Receipt :", receiptString)
return receiptString
} catch {
print("Couldn't read receipt data with error: " + error.localizedDescription)
}
}
return nil
}
Now, If I validate the above receipt using https://sandbox.itunes.apple.com/verifyReceipt API it gave error code 220002 which means Receipt is malformed. Also neither expiry time of receipt is set.
Is the stored receipt loading correctly? How do we make sure a valid receipt is stored ?

Related

How to check purchase status of In-App Purchase (Auto Renewing Subscription)?

I created an In-App Purchase feature for the app, using an Auto-Renewing Subscription. The problem is that I'm not sure how to check the purchase status, whether the subscription has run out or is still active.
Here's where I try saving the purchased status:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add payment")
for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState {
case .purchased:
print("buy ok, unlock IAP HERE")
print(p.productIdentifier)
let prodID = p.productIdentifier
switch prodID {
case "com.test.UnlockTools.Subscription1":
print("tool set 1 unlocked")
uTool1()
print("tool set 2 unlocked")
uTool2()
print("tool set 3 unlocked")
uTool3()
UserDefaults.standard.set(true, forKey: "isSubbed")
default:
print("IAP not found")
}
queue.finishTransaction(trans)
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
}
}
This is where I call the UserDefaults and allow or deny button interaction:
override func viewDidLoad() {
super.viewDidLoad()
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID: NSSet = NSSet(object: "com.test.UnlockTools.Subscription1")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
status = UserDefaults.standard.bool(forKey: "isSubbed") ?? false
if status == true {
unlockTool1.isUserInteractionEnabled = true
unlockTool2.isUserInteractionEnabled = true
unlockTool3.isUserInteractionEnabled = true
} else {
unlockTool1.isUserInteractionEnabled = false
unlockTool2.isUserInteractionEnabled = false
unlockTool3.isUserInteractionEnabled = false
}
}
If I find out the status then I will be able to save the state as true/false. I tried using UserDefaults with some success. Not sure if I placed the code in the best location though.
If the subscription is still active, I'd like to allow the user to click a button, and deny the button click if the subscription has run out.
I'll add more code if needed. Any help would be greatly appreciated.
The only way to check the subscription status is to verify the receipt with Apple to see if it's still valid using their /verifyReceipt endpoint - docs.
What you could do is cache some expiration date after the purchase and use that to check if the subscription is valid. If it's passed the expiration date you can re-check the receipt with Apple to see if it's renewed. There are also edge cases where a user is refunded and their subscription is cancelled before the expiration date - you should update your receipts periodically with Apple to check this case. Ideally, this should all be happening server side as well to avoid piracy.
Here's a great post that summarizes the nauces of Apple subscriptions very well: iOS Subscriptions are Hard

Swift In App Purchase Non Consumable

I am developing IAP functionality of non consumable (removing Ads).
I've created an helper for all the operation and everything works fine.
When a user buy/restore the purchase i set this:
let save = UserDefaults.standard
save.set(true, forKey: "Purchase")
save.synchronize()
This works fine as long as the user never delete the app.
I was wondering...
Is there a way to know if the user (after deleting and reinstalling the app) already had made a purchase? so to change the title of the button from "purchase" to "restore"?
For every app, Apple requires that you include a "Restore Purchases" button. This is exactly for the problem you are facing. It will recreate the SKPaymentTransaction for every previously purchased IAP with a "restored" state for the iCloud account that is currently signed in at the App Store, and put on the SKPaymentQueue. Read more...
Maybe it would make sense to have a receipt validation in your code, when the user is interacting in your app. (Apple docs: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Introduction.html)
Just have a look at this small example:
Init your product requests:
if SKPaymentQueue.canMakePayments() {
print("starting IAPS")
let productIdentifiers = Set(["YOUR_IAPP_IDENTIFIER#1", "YOUR_IAPP_IDENTIFIER#1"])
let request = SKProductsRequest(productIdentifiers: productIdentifiers as Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
Set your products in your app and do the receipt validation
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("product request")
let myProduct = response.products
for product in myProduct {
if product.productIdentifier == "YOUR_IAPP_IDENTIFIER#1" {
self.productYear = product
} else if product.productIdentifier == "YOUR_IAPP_IDENTIFIER#2" {
self.productMonth = product
}
print("product added")
print(product.productIdentifier)
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)
self.receiptValidation()
}
Validation example
func receiptValidation() {
self.logger.log.debug("In receipt validation")
if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
do {
let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
let receiptString = receiptData.base64EncodedString(options: [])
let dict = ["receipt-data" : receiptString, "password" : "YOUR_PASSWORD"] as [String : Any]
// Getting the receipt data from the JSON file and other logic
// ...
// ...
}
}
}
Please let me know, if you need more clarification on this. Furthermore you can simply restore it with the PaymentQueue (Apple Doc: https://developer.apple.com/documentation/storekit/skpaymentqueue)
If you provide a login system you will know easily , otherwise
no other way you should put buy and restore buttons for payment Or you store this key-value in
device key-chain and read them in first setup ,but you should know from ios 10.3 If you deleted the app the associated key-chain items will be deleted
Note: Apple states that all apps that have IAP should put a restore payment functionality so, If there is a workaround app will be rejected by Apple

Swift - Xcode not finding in app purchase

I am trying to implement a removing ads in app purchase into my application. However, whenever I test it out, it keeps telling me it has not found the product ID. I have triple checked the product ID, and it is correct. I can't seem to figure out the problem. This is what prints to the console:
Product not found: com.myCoolAwesomeApp.mjay.noAds
var product: SKProduct?
var productID = "com.myCoolAwesomeApp.mjay.noAds"
func getPurchaseInfo() {
if SKPaymentQueue.canMakePayments() {
let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID) as! Set<String>)
request.delegate = self
request.start()
} else {
productDescription.text = "Please enable In App Purchases in your settings."
}
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
var products = response.products
if (products.count) == 0 {
productDescription.text = "Product not found."
} else {
product = products[0]
productDescription.text = product!.localizedDescription
buyButton.isEnabled = true
}
let invalids = response.invalidProductIdentifiers
for product in invalids {
print("Product not found: \(product)")
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.purchased:
SKPaymentQueue.default().finishTransaction(transaction)
productDescription.text = "Purchase Successful"
buyButton.isEnabled = false
let save = UserDefaults.standard
save.set(true, forKey: "Purchase")
save.synchronize()
case SKPaymentTransactionState.failed:
SKPaymentQueue.default().finishTransaction(transaction)
productDescription.text = "Purchase Failed. Try again later."
default:
break
}
}
}
}
I run into the exact similar problem. Here are some steps that needs to be adressed/checked after reading this technical note from Apple:
Create a unique App ID
Enable in-app purchasing for this App ID
Generate and install a new provisioning profile on your device
Update the bundle ID and code signing profile in Xcode
Check that your project’s .plist Bundle ID match your App ID
Complete your contract, tax, and banking Information. You need a VALID contract
As mentioned by the official doc there is NO need to submit binaries neither submit screenshot of in-app purchase. I have seen a lot of missleading info about this on various blogs.
After checking you have addressed each point in this list, delete your app and reinstall it.
I was basically missing two things on my side: setting up correctly my contract for ios Paid app and i didn't install the new provisioning profile on my device. May be that was your issue?

Check if an Auto-Renewable Subscription is still valid

I would like to check the Auto Renewable Subscription status whenever I open the app.
This is to make sure that the user is still subscribed to the service. How do I achieve this?
Any thoughts? Thank you
P.S.: I am using SwiftyStoreKit
Here is several ways to do receipt validation to check is user granted to subscription. Here is two ways of doing it correctly:
Do receipt validation locally as it is written here.
Do receipt validation remotely as it is written here. It is mentioned that receipt should not be sent to App Store within an app. Short summary:
Your app sends receipt to your backend.
Your backend sends receipt to Apple backend for validation.
Your backend gets response from the apple.
Your backend sends result back to your app is receipt valid or invalid.
In both ways you will get list of in-app purchases. It will contain expired subscriptions as well. You would need to go through all subscriptions and check expiration dates. If it is still valid you must grant user with subscription.
As I understand you are using SwiftyStoreKit and here is open task for local receipt validation.
You can check with this function. its works with swift4
func receiptValidation() {
let SUBSCRIPTION_SECRET = "yourpasswordift"
let receiptPath = Bundle.main.appStoreReceiptURL?.path
if FileManager.default.fileExists(atPath: receiptPath!){
var receiptData:NSData?
do{
receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
}
catch{
print("ERROR: " + error.localizedDescription)
}
//let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)
print(base64encodedReceipt!)
let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]
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 {
let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
// if you are using your server this will be a json representation of whatever your server provided
} 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)")
}
}
}
I wanted to provide an alternative solution that uses the RevenueCat SDK for those who still stumble upon this question.
AppDelegate.swift
Configure the RevenueCat Purchases SDK with your api key an optional user identifier.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Purchases.configure(withAPIKey: "<...>", appUserID: "<...>")
...
return true
}
Subscription status function
The function below checks the PurchaserInfo to see if the user still has an active "entitlement" (or you can check for an active product ID directly).
func subscriptionStatus(completion: #escaping (Bool)-> Void) {
Purchases.shared.purchaserInfo { (info, error) in
// Check if the purchaserInfo contains the pro feature ID you configured
completion(info?.activeEntitlements.contains("pro_feature_ID") ?? false)
// Alternatively, you can directly check if there is a specific product ID
// that is active.
// completion(info?.activeSubscriptions.contains("product_ID") ?? false)
}
}
Getting subscription status
You can call the above function as often as needed, since the result is cached by the Purchases SDK it will return synchronously in most cases and not require a network request.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
subscriptionStatus { (subscribed) in
if subscribed {
// Show that great pro content
}
}
}
If you're using SwiftyStoreKit, the RevenueCat syntax is fairly similar and there is a migration guide available to help switch over.
Yet another solution to handle auto-renewable iOS subscription using Qonversion SDK.
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Qonversion.launch(withKey: "yourProjectKey")
return true
}
Get subscription status
Link App Store subscription to Qonversion Product, and link the Product to Permission.
Then you just need to trigger checkPermissions method at the start of your app to check if a user's subscription is still valid. This method will check the user receipt and will return the current permissions. And then for the still-active subscription, you can get the details if the subscriber has turned-off auto-renewal, if he is in grace period (billing retry state), etc.
Qonversion.checkPermissions { (permissions, error) in
if let error = error {
// handle error
return
}
if let premium = permissions["premium"], premium.isActive {
switch premium.renewState {
case .willRenew, .nonRenewable:
// .willRenew is the state of an auto-renewable subscription
// .nonRenewable is the state of consumable/non-consumable IAPs that could unlock lifetime access
break
case .billingIssue:
// Grace period: permission is active, but there was some billing issue.
// Prompt the user to update the payment method.
break
case .cancelled:
// The user has turned off auto-renewal for the subscription, but the subscription has not expired yet.
// Prompt the user to resubscribe with a special offer.
break
default: break
}
}
}
You can check our sample app that demonstrates auto-renewable subscription implementation here.

Restore Purchase : Non-Consumable

I have completed a small app where I have a non-consumable purchase option. It is on the App Store.
The purchase of the product runs OK. It's my Restore Purchase function that seems to do nothing.
I have added this code for the Restore Purchase #IBAction:
#IBAction func restorePurchases(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
But nothing happens when I hit the restore purchase button.
I think I have to add a function that checks if the restore was successful or not. Am planning to amend code to the following:
#IBAction func restorePurchases(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Restored:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
var alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
break;
case .Failed:
SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
var alert = UIAlertView(title: "Sorry", message: "Your purchase(s) could not be restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
break;
default:
break;
}
}
}
Will this do the trick?
I have been through every thread in relation to effecting Restore Purchase transactions, and my research has led me to the above. So I don't think this is a duplicate of a question, but perhaps may clarify how to successfully restore purchases for others facing my similar situation.
Your codes looks pretty fine for the most part, although some parts seem to be from older tutorials . There is some changes you should make, one of them is that you need to call your unlockProduct function again.
This is the code I use (Swift 3).
/// Updated transactions
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
// Transaction is being added to the server queue.
case .purchased:
// Transaction is in queue, user has been charged. Client should complete the transaction.
defer {
queue.finishTransaction(transaction)
}
let productIdentifier = transaction.payment.productIdentifier
unlockProduct(withIdentifier: productIdentifier)
case .failed:
// Transaction was cancelled or failed before being added to the server queue.
defer {
queue.finishTransaction(transaction)
}
let errorCode = (transaction.error as? SKError)?.code
if errorCode == .paymentCancelled {
print("Transaction failed - user cancelled payment")
} else if errorCode == .paymentNotAllowed { // Will show alert automatically
print("Transaction failed - payments are not allowed")
} else {
print("Transaction failed - other error")
// Show alert with localised error description
}
case .restored:
// Transaction was restored from user's purchase history. Client should complete the transaction.
defer {
queue.finishTransaction(transaction)
}
if let productIdentifier = transaction.original?.payment.productIdentifier {
unlockProduct(withIdentifier: productIdentifier)
}
case .deferred:
// The transaction is in the queue, but its final status is pending external action
// e.g family member approval (FamilySharing).
// DO NOT freeze up app. Treate as if transaction has not started yet.
}
}
}
Than use the delegate methods to show the restore alert
/// Restore finished
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
guard queue.transactions.count != 0 else {
// showAlert that nothing restored
return
}
// show restore successful alert
}
/// Restore failed
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError) {
/// handle the restore error if you need to.
}
Unlock product is just a method I am sure you already have too.
func unlockProduct(withIdentifier productIdentifier: String) {
switch productIdentifier {
/// unlock product for correct ID
}
}
As a side note, you should move this line
SKPaymentQueue.default().add(self)
out of your restore and buy function and put it in viewDidLoad.
Apple recommends you add the transaction observer as soon as your app launches and only remove it when your app is closed. A lot of tutorials unfortunately dont teach you this correctly. This way you unsure that any incomplete transactions e.g due to network error, will always resume correctly.
https://developer.apple.com/library/content/technotes/tn2387/_index.html
In my real projects my code for IAPs is in a Singleton class so I would actually using delegation to forward the unlockProduct method to my class that handles gameData. I can than also make sure the observer is added at app launch.
Hope this helps
Took me a while to suss out, but the reason my StoreKit was not updatingTransactions and restoring the purchase was because of a broken Configuration setting in my app's Scheme. When I set that to None, it worked!
In Xcode I went into Edit>Scheme (image1) clicked on the Run>Options tab and selected None for StoreKit Configuration (image2). I also went on my physical device, and logged out of my personal Apple purchase account (Settings >Your Name/Pic at the Top > Media & Purchases > Sign Out) (image3). And finally, this step might not be critical, but I logged into my test sandbox account on my device at the bottom of the Settings>App Store menu(image4 and image5). That is an account that I setup in developer.apple.com under test users.

Resources