Search code examples
iosswiftin-app-purchasestorekit

How do I detect if SKProduct already purchased as consumable in app purchases before?


I imported StoreKit and set my code for consumable purchase and set a sandbox tester. I have three buttons. Each one will buy a separate product. Here is my problem:

When my sandbox tester tries to purchase the same product again and presses the same button, Firebase analytics says:

"Purchase is a duplicate and will not be reported".

Ok, fine. But how I can get this as a response from AppStore? Where does Firebase get this from?

When I look at SKPaymentTransaction.transactionState enum, there is no case about it. I tried to check cases and print something to console, like this:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        switch transaction.transactionState {
        case .purchased:
            print("purchased")
            break
        case .restored:
            print("restored")
            break
        case .failed :
            print("failed")
            break
        case .deferred:
            print("deferred")
            break
        case .purchasing:
            print("purchased")
            break
        @unknown default:
            fatalError()
        }
    }
}

I also checked transaction.error, but no error either.

This code prints nothing when the purchase fails from duplicate purchase. How can I get at least an error here?

What should I do?

My goal is show an alert and tell the client "You already purchased this".


Solution

  • As Paulw11 said in the comments, consumable purchases are meant to be purchased many times. I was wrong. Trying to stop multiple purchases from a consumable purchase is like "You purchased a cup of coffee yesterday, why do you want to purchase another one today?". Of course, someone can want another cup of coffee today.

    I changed my approach to using consumable purchases in the usual way.