james james - 2 months ago 11
iOS Question

IAPs are being restoring when they have not yet been purchased yet

When the user calls restorePurchases(), the non-consumable com.premium is restored even thought they do not own it.
Here are the functions that are responsible for the restoring purchases and purchasing IAPs. This is only an issue with non-consumable IAPs.
There is no issue with purchasing. And if the user attempts to purchase an IAP that they already have, it is simply restored. Thank you for looking in to this.

func restorePurchases() {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}


Here is my payment queue.

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add paymnet")

for transaction:AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)

switch trans.transactionState {

case .restored:
print(".restoring attempt")
print(p.productIdentifier)

let prodID = p.productIdentifier as String
switch prodID {
case "com.HBM.counterFire.premium":
print(".restoring ads")
removeAds() // restoring here

case "com.HBM.counterFire.cash":
print(".restoring coins")

case "com.HBM.counterFire.level":
print(".restoring levels")

default:
print("can't buy")

}

break

case .purchased:
print("buying, ok unlock iap here")
print(p.productIdentifier)

let prodID = p.productIdentifier as String
switch prodID {
case "com.HBM.counterFire.premium":
print("buying ads")
removeAds()
case "com.HBM.counterFire.cash":
print("buying coins")
addCoins()
case "com.HBM.counterFire.level":
print("buying levels")
addLevels()
default:
print("can't buy")
}

queue.finishTransaction(trans)
break;
case .failed:
print("buy error")
queue.finishTransaction(trans)
break;
default:
print("default")
break;

}
}
}

Answer

You should not update any purchase status in paymentQueueRestoreCompletedTransactionsFinished. This function just lets you know that the restoration process has completed. You could use this to update your UI or display an alert or something.

The restoration process delivers the transactions to be restored to the updatedTransactions function where you handle the .restored state in the same way that you handle the .purchased state.

Essentially "restore" just replays the purchase transaction process for non-consumable and auto-renewing subscription purchase types.