SwiftyStoreKit icon indicating copy to clipboard operation
SwiftyStoreKit copied to clipboard

Purchasing triggers callback in completeTransactions() instead of in purchaseProduct()

Open GraemeHarrison opened this issue 3 years ago • 1 comments

What I'm trying to accomplish is:

  1. User taps to buy IAP product
  2. If success, receipt is fetched and sent to my server for validation
  3. If success, IAP product transaction is set to finish
  4. Update UI.

After a user taps purchase, a function similar to this runs:

func purchase(productId: String) {

    SwiftyStoreKit.purchaseProduct(productId, quantity: 1, atomically: false) { result in

        switch result {

        case .success(let product):

            self.validateReceiptOnServer() { (success) in

                if product.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(product.transaction)
                }

                if success {
                    self.updateUI()
                }
                else {
                    self.showError()
                }
            }

        case .error(let error):
            self.showError()
        }
    }
}

In App Delegate I have this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    SwiftyStoreKit.completeTransactions(atomically: false) { purchases in

        for purchase in purchases {

            switch purchase.transaction.transactionState {

            case .purchased:

                self.validateReceiptOnServer() { (result) in

                    if purchase.needsFinishTransaction {
                        SwiftyStoreKit.finishTransaction(purchase.transaction)
                    }
                    switch result {
                    case .success: print("success validating receipt")
                    case .failure: print("failure validating receipt")
                    } 
                }

            case .restored:

                if purchase.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(purchase.transaction)
                }

            case .failed, .purchasing, .deferred: break
            @unknown default: break
            }
        }
    }
    return true
}

For normal, uninterrupted purchases this setup seems to work fine. My problem is that when testing interrupted purchases, after calling SwiftyStoreKit.purchaseProduct(), the callback for SwiftyStoreKit.completeTransactions() in didFinishLaunchingWithOptions is triggered instead.

Is this a bug? And if not, how am I supposed to update my UI in the view controller that called SwiftyStoreKit.purchaseProduct()?

GraemeHarrison avatar Nov 05 '20 18:11 GraemeHarrison

I have the same issue. Sometimes AppDelegate completeTransactions is called instead of purchaseProduct completion block. How to solve this ?

sabiland avatar Jun 10 '21 13:06 sabiland