0

I have some restore transactions still stuck in my payments queue - because I never called finishTransaction with the transaction once it was restored when I was testing a flawed restore purchases action.

From some online research, I realise I have to manually force unfinished transactions in my payments queue to finish.

Someone posted this code in Objective-C:

// take current payment queue
SKPaymentQueue* currentQueue = [SKPaymentQueue defaultQueue];
// finish ALL transactions in queue
[currentQueue.transactions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[currentQueue finishTransaction:(SKPaymentTransaction *)obj];
}];

I have no idea how to convert it to Swift 2.0.

Can anyone pls help me do this? Thanks :-)

1 Answers1

5

Here is a for loop that will iterate through each pending transaction and check the state, and complete transactions that have either failed or successfully purchased.

let currentQueue : SKPaymentQueue = SKPaymentQueue.default();
        for transaction in currentQueue.transactions {
            if (transaction.transactionState == SKPaymentTransactionState.failed) {
                //possibly handle the error
                currentQueue.finishTransaction(transaction);
            } else if (transaction.transactionState == SKPaymentTransactionState.purchased) {
                //deliver the content to the user
                currentQueue.finishTransaction(transaction);
            } else {
                //handle other transaction states
            }
        }
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
Chance Hudson
  • 2,849
  • 1
  • 19
  • 22
  • Ahh. Wonderful! I'll just create a temporary button to link it up as an IBAction and run the code. Will that work? Or should I set up another sandbox tester and put the code within the purchase code? Sorry, very new :-) – Katherine Jenkins Mar 18 '16 at 18:04
  • 3
    If you're just looking to clear failed transactions that are currently stuck then a temporary button is a good solution. However, I would add code like this in your SKProductsRequestDelegate updatedTransactions method. It's important to handle in-app purchase failures due to things like poor network connectivity/insufficient funds/etc. Also, in the loop you should check the state of the transaction and not just finish every transaction. See my updated answer. – Chance Hudson Mar 18 '16 at 18:09
  • Makes perfect sense. I'll do as you suggest. Tyvm. – Katherine Jenkins Mar 18 '16 at 18:20
  • Terrific solution! – Eric Duffett Nov 19 '17 at 14:20