bizz84 / SwiftyStoreKit

Lightweight In App Purchases Swift framework for iOS 8.0+, tvOS 9.0+ and macOS 10.10+ ⛺
MIT License
6.51k stars 792 forks source link

When/How can I trigger the completeTransactions callback within SwiftyStoreKit #432

Open tunds opened 5 years ago

tunds commented 5 years ago

Platform

In app purchase type

Environment

Version

0.14.1

Issue summary

I'm unable to get the SwiftyStoreKit.completeTransactions block to trigger when I make an atomic purchase. So within my view model, I have a wrapper function to purchase an auto-renewable subscription as you can see below in my function.

static func purchase(_ product: SKProduct) -> Promise<Void> {

    let loftyLoadingViewContentModel =  LoftyLoadingViewContentModel(title: "Purchasing".uppercased(), message: "We’re currently processing your\nrequest, for your subscription.")
    UIApplication.topViewController()?.showLoadingView(.popOverScreen, loftyLoadingViewContentModel)

    return Promise { seal in

        SwiftyStoreKit.purchaseProduct(product) { purchaseResult in

            switch purchaseResult {
            case .success(let product):
                if product.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(product.transaction)
                }
                seal.fulfill()
                log.info("Purchase Success: \(product.productId)")
            case .error(let error):
                UIApplication.topViewController()?.removeLoadingView()
                seal.reject(error)
            }
        }
    }
}

But this doesn't trigger the block within my appdelegate.

What did you expect to happen

I expected the complete transactions block to execute.

    SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
        for purchase in purchases {
            switch purchase.transaction.transactionState {
            case .purchased, .restored:
                if purchase.needsFinishTransaction {
                    // TODO: Set flags here that the user has now purchased the product
                    SwiftyStoreKit.finishTransaction(purchase.transaction)
                }
            // Unlock content
            case .failed, .purchasing, .deferred:
                break // do nothing
            }
        }
    }

What happened instead

Nothing happened.

ricsantos commented 4 years ago

You should call completeTransactions as soon as the app launches.

https://github.com/bizz84/SwiftyStoreKit#complete-transactions