bizz84 / bizz84/SwiftyStoreKit
Purchasing triggers callback in completeTransactions() instead of in purchaseProduct()
- Dominant language
- Swift
- Stars
- 6.7k
- Forks
- 801
- PR merge metrics
- No merged PRs in 30d
Description
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](https://help.apple.com/app-store-connect/#/dev7e89e149d?sub=dev55ecec74d), 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()_?
Contributor guide
Assessment
This issue has not been assessed yet.