PromiseKit / StoreKit

Promises for Swift & ObjC
http://promisekit.org
MIT License
13 stars 12 forks source link

Transactions left unfinished when the purchases are interrupted #15

Open mkj-is opened 3 years ago

mkj-is commented 3 years ago

When using this extension I found edge cases when this extension does not work properly. These are mainly “interrupted purchases”, see the following excerpt from Apple documentation:

It is important to add the observer at launch, in application(_:didFinishLaunchingWithOptions:), to ensure that it persists during all launches of your app, receives all payment queue notifications, and continues transactions that may be processed outside the app, such as:

  • Promoted in-app purchases
  • Background subscription renewals
  • Interrupted purchases

Source: https://developer.apple.com/documentation/storekit/in-app_purchase/setting_up_the_transaction_observer_for_the_payment_queue

It is clear from this documentation that this extension does not implement observers according the Apple guidance. Due to the nature of PromiseKit it is hard to come up with a solution which would fit.

Reproducing the issue

The thing is, SKPayment extension currently leaves some transactions unfinished. Even worse, the behavior is different on major iOS versions: 12 works fine and 13+ do not work. They notify about updated transactions at different times.

Steps:

  1. During the purchase kill the app.
  2. When you try to purchase the same product again it is either bought instantly or it fails and new dialog for buying is presented.

This ends in one unfinished transaction which is persisted during the runs of the app. When using purely this extension you will end up in inconsistent state.

It is not as obvious with non-consumable in-app purchases, but when using with subscriptions the issue becomes clearer.

My solution

In the end, I ended up using PromiseKit nevertheless, since it is extensively used in the app where I found this issue.

Firstly, I implemented this class to complete any unfinished transactions:

final class SubscriptionFinisher: NSObject, SKPaymentTransactionObserver {
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .purchased, .restored, .failed:
                queue.finishTransaction(transaction)
            default:
                break
            }
        }
    }
}

Secondly, I add this observer on application launch and remove it during StoreKit promise runs. Finishing transactions twice can end in an undefined behavior.

queue.remove(finisher)
let purchase = SKPayment(product: product)
   .promise()
   .ensure { queue.add(finisher) }

Next steps

I either propose to update the documentation for this extension or provide guidance how to prevent others from running into this issue. As for now, I hope this issue will serve as some kind of documentation by itself.

TLDR Do not use this extension when working with auto-renewable subscriptions, because sooner or later one of your users will encounter an edge case when the transactions won't be completed properly.

mxcl commented 3 years ago

This sucks and makes me think we should remove this API altogether, needs more consideration.