jinSasaki / InAppPurchase

A Simple and Lightweight framework for In App Purchase (Store Kit 1)
MIT License
289 stars 30 forks source link

Restore Product Option #36

Open Jerland2 opened 5 years ago

Jerland2 commented 5 years ago

Describe the bug Whenever I use the current restore Product option it succeeds even when a user has not purchased the in app purchase

To Reproduce Steps to reproduce the behavior:

  1. Implement IAPS in App Store Connect
  2. Call In App Purchase.Restore
  3. Call succeeds even if user has not purchased the product

Expected behavior A user should be prompted to sign in to their iCloud account and it should fail if they have not purchased the product before

Jerland2 commented 5 years ago

The solution is to modify this block of code

public func restore(handler: ((_ result: InAppPurchase.Result<Void>) -> Void)?) {
        paymentProvider.restoreCompletedTransactions { (_, error) in
            if let error = error {
                handler?(.failure(error))
                return
            }
            handler?(.success(()))
        }
    }

and change it to:

    public func restore(handler: ((_ result: InAppPurchase.Result<Void>) -> Void)?) {
        paymentProvider.restoreCompletedTransactions { (queue, error) in
            if let error = error {
                handler?(.failure(error))
                return
            }

            if queue.transactions.isEmpty {
                let emptyProductsError = Error(error: Error.emptyProducts)
                handler?(.failure(emptyProductsError))
                return
            } else {
                handler?(.success(()))
            }
        }
    }
Jerland2 commented 5 years ago

@jinSasaki please let me know if you see any issues with this fix as you are no doubt more familiar with the library. If not this should be updated in the repo

jinSasaki commented 5 years ago

It is not a bug.

I think that the restore feature should be always succeeded except StoreKit returned error, so this library returns success even if queue.transactions is empty.

However, I think that the developers should be able to handle the restored result. InAppPurchase plans to add the product ids to result after the restore method called.

Please see also #26.