How to Restore In-App Purchases Correctly

How to restore in-app purchases correctly?

As you did in the button to restore, you need to add:

SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()

Then you also need to add this function with the different in-app purchases you have in iTunesConnect. Change "ProductID" for case to be a product ID from iTunesConnect and add as many cases as you have in-app purchases.

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String

switch prodID {
case "ProductID1":
// implement the given in-app purchase as if it were bought
case "ProductID2":
// implement the given in-app purchase as if it were bought
default:
print("iap not found")
}
}
}

To answer your questions:
It looks like this particular tutorial you were following was focusing on teaching you the overarching ideas of in-app purchases and used their example purchase rather than code that can be used for multiple products.

Apple checks if the item was previously purchased through the restoreCompletedTransactions() function that you call when the button. This is part of the StoreKit framework that Apple provides. It automatically triggers the paymentQueueRestoreCompletedTransactionsFinished() function once it has checked. With the code I provide above, it performs different code depending on which purchases it found or prints to the console if it did not find the product to restore.

How to properly restore purchases using IAP in Swift

Your implementation of

func paymentQueue(_ pQueue: SKPaymentQueue, updatedTransactions pTransactions: [SKPaymentTransaction]) {

is wrong. You have left out the transaction state for when a purchase is restored! You have case .purchased but you forgot case .restored. Put it in. That is where you are notified and can respond.

In-App Purchases: Restoring Purchases Server Side

See this answer here: https://stackoverflow.com/a/54153169/3166209

In summary:

If you have your own account based system, you aren't required to
restore purchases from the device receipt if they can be restored by
logging in to their (your) account. You're required you to have
restore functionality, but it doesn't have to be implemented through
StoreKit. Apple vaguely mentions this in their docs as do service
providers.

You'll notice large, cross-platform, subscription apps like Netflix don't have a restore purchases button anywhere. This is because restoring purchases with the device receipt could allow a single receipt to be shared across multiple users that log-in on the same device.

StoreKit problem while restoring In-App Purchase

It's been a few years since I used StoreKit, but I remember having an issue with restore purchases also (and yes, @ElTomato is correct, use a separate case - I'm surprised your code actually builds). I found that the following worked:

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction:AnyObject in transactions {
if let trans = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
delegate.purchaseComplete()
case .failed:
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
case .restored:
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
default:
break
}}}
}
func restorePurchases() {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}
public func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
for _ in queue.transactions {
alreadyPurchased = true
delegate.purchasesRestored()
}
showAlert("Purchases have been restored.")
}

Don't worry about showAlert, that's just an extension I have for UIViewController. But what I'm not seeing in your code is paymentQueueRestoreCompletedTransactionsFinished(_ queue:).

Is offering a Restore Purchases option mandatory when using only StoreKit2 in iOS 15?

The answer is yes for now.
Because Apple still says on their documentation to keep restore mechanism .
recently I worked on a project with StoreKit 2 and its required still to keep restore option.

from review guide documentation:

you should make sure you have a restore mechanism for any restorable
in-app purchases.

so I will recommend to keep it before sending review.

How to restore purchase within in-app purchases?

I really wouldn't worry about it. No one is ever going to make an in-app purchase and then delete and re-download the app in the span of 10 minutes. If you have gotten it to work, you should be fine for any realistic case.



Related Topics



Leave a reply



Submit