I've been trying to test some consumable IAP on iOS, and I'm getting a strange error. An alert pops up with the text:
"This In-App Purchase has already been bought. It will be restored for
free. [Environment: Sandbox]"
I have checked, and I'm certain that my IAP is consumable in iTunesConnect. It seems that my validation process is somehow failing, but I wouldn't expect this error message. Does anyone have any experience with this?
I'm not sure if this is the correct action, but calling:
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
on the repeating transaction cleared them out. I suspect that I never called it in the success case due to stopping in the debugger or something.
Probably you solved your issue already, but i met the same issue and have some additional solution points:
Sorry, i use Swift, but I think it's understandable here.
First of all, there are two important moments in your code, which you must cover:
class InAppPurchaseViewController: UIViewController , SKProductsRequestDelegate
, SKPaymentTransactionObserver //NOTE THIS!
{
override func viewDidLoad() {
super.viewDidLoad()
//
...
...
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
// here you create the observer
}
//before you leave this ViewController you should delete this Observer.
//I had a bug which worked like this: when user leaved the IAPViewController without
//buying something and then came back, viewDidLoad added one more Observer
// so App crashed on the attempt to buy
// for example:
@IBAction func exitTapped(sender: AnyObject) {
dismissViewControllerAnimated(true) { () -> Void in
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
}
}
after you added the observer, don't forget to finish the transaction:
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .Restored:
print("Restored")
break
case .Failed:
print("Failed")
//FINISH TRANSACTION
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
break
case .Purchasing:
print("Purchasing")
break
case .Purchased:
// DO your stuff here
// ...
// and FINISH TRANSACTION
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
}
break
case .Deferred:
print("Deferred")
break
}
}
}
One more issue, that I solved here, is: I forgot to add the finish transaction line of code, so i had open transaction with consumable IAP.
When i tried to buy one more time with this Sandbox User, i received the message "This In-App Purchase has already been bought. It will be restored for free. [Environment: Sandbox]" This happened again and again even after I added FinishTransaction. I changed the sandbox user. App became to prompt FirstSandBoxUser iTunes Password every time i ran the App. Catastrophe... Very annoying...
Every time I tried to make this purchase with FirstSandBoxUser, the transaction.transactionState was .Purchasing, and you can't finish the transaction in this state!
I made an assumption, that if transaction is not finished, StoreKit takes it like a non-consumable, so I should start a restoration process, and then catch a moment to finish the transaction. This is the code:
func paymentQueue // see above
//....
case .Purchasing:
print("Purchasing")
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
break
//...
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
for transaction in queue.transactions {
queue.finishTransaction(transaction)
}
}
It's enough to run this code once, I think, it will catch all "Purchasing" transaction, Restore them and after that Finish.
Of course, after fixing the issue remove this additional code.
Good luck!
This happens when the transaction was not finished on the client using.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
You should register a transactionObserver in your apps startup phase, to ensure you will get notified of any unfinished transactions. You should finish those transactions and do whatever else is needed to correctly deliver the purchased product to the user.
From apples programming guide (emphasis mine):
Register a transaction queue observer when your app is launched, as shown in Listing 5-1. Make sure that the observer is ready to handle a transaction at any time, not just after you add a transaction to the queue. For example, consider the case of a user buying something in your app right before going into a tunnel. Your app isn’t able to deliver the purchased content because there’s no network connection. The next time your app is launched, StoreKit calls your transaction queue observer again and delivers the purchased content at that time. Similarly, if your app fails to mark a transaction as finished, StoreKit calls the observer every time your app is launched until the transaction is properly finished.
They recommend the following code to register the observer:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* ... */
[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
}