Search code examples
iosswiftxcodein-app-purchaseapp-store-connect

Testing In-App Purchases (IAP) - Need Help Clearing Transaction Queue


I've made an app with an IAP and I'm in my first attempts at testing it, I got the login prompt, but since then, I've failed to get any login prompts (I think it's because there's a failed transaction stuck in the queue).

I'm in desperate need of help in clearing the transaction queue since all this IAP language is very foreign to me. I've read in several places the the below snippet works, but I'm honestly not sure how to use it properly:

for transaction in SKPaymentQueue.default().transactions {
                guard
                    transaction.transactionState != .purchasing,
                    transaction.transactionState != .deferred
                else {
                    //Optionally provide user feedback for pending or processing transactions
                    return
                }
                //Transaction can now be safely finished
                SKPaymentQueue.default().finishTransaction(transaction)
            }

The snippet finally had the login prompt show up again, but then I'm stuck in a never-ending cycle of logging in.

Below is where I chose to place it but it's currently just having me login again right after already logging in.... (The new code snippet is between the two comment lines stating "NEW")

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { //--------- In App Purchase
        for transaction in transactions{
            //------------------------------------------------------------ New
            for transaction in SKPaymentQueue.default().transactions {
                guard
                    transaction.transactionState != .purchasing,
                    transaction.transactionState != .deferred
                else {
                    //Optionally provide user feedback for pending or processing transactions
                    return
                }
                //Transaction can now be safely finished
                SKPaymentQueue.default().finishTransaction(transaction)
            }
            //------------------------------------------------------------ New
            if transaction.transactionState == .purchased {
                print("Transaction Successful")
                packsUnlocked = true
            } else if transaction.transactionState == .failed {
                print("Transaction Failed with error: \(transaction.error)")
            }
        }

    }

Any help on this really is appreciated since Apple Support could only provide me non-technical support and I've sunk a lot of resources into this app to only be held up by the last step.

Thanks


Solution

  • If do not take into account receipt validation after purchased transaction, then here a callback which is expected

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { //--------- In App Purchase
            for transaction in transactions {
                guard
                    transaction.transactionState != .purchasing,
                    transaction.transactionState != .deferred
                else {
                    //Optionally provide user feedback for pending or processing transactions
                    continue
                }
    
                if transaction.transactionState == .purchased || transaction.transactionState == .restored {
                    print("Transaction Successful")
                    packsUnlocked = true
                } else if transaction.transactionState == .failed {
                    print("Transaction Failed with error: \(transaction.error)")
                }
    
                //Transaction can now be safely finished
                queue.finishTransaction(transaction)
            }
        }