Slavic the Slavic
Slavic the Slavic

Reputation: 411

How to check purchase status of In-App Purchase (Auto Renewing Subscription)?

I created an In-App Purchase feature for the app, using an Auto-Renewing Subscription. The problem is that I'm not sure how to check the purchase status, whether the subscription has run out or is still active.

Here's where I try saving the purchased status:

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    print("add payment")

    for transaction: AnyObject in transactions {
        let trans = transaction as! SKPaymentTransaction
        print(trans.error)

        switch trans.transactionState {
        case .purchased:
            print("buy ok, unlock IAP HERE")
            print(p.productIdentifier)

            let prodID = p.productIdentifier
            switch prodID {
                case "com.test.UnlockTools.Subscription1":
                    print("tool set 1 unlocked")
                    uTool1()
                    print("tool set 2 unlocked")
                    uTool2()
                    print("tool set 3 unlocked")
                    uTool3()
                    UserDefaults.standard.set(true, forKey: "isSubbed")
                default:
                    print("IAP not found")
                }
            queue.finishTransaction(trans)
        case .failed:
            print("buy error")
            queue.finishTransaction(trans)
            break

        default:
            print("Default")
            break
        }
    }
}

This is where I call the UserDefaults and allow or deny button interaction:

override func viewDidLoad() {
    super.viewDidLoad()

    if(SKPaymentQueue.canMakePayments()) {
        print("IAP is enabled, loading")
        let productID: NSSet = NSSet(object: "com.test.UnlockTools.Subscription1")
        let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
        request.delegate = self
        request.start()
    } else {
        print("please enable IAPS")
    }

    status = UserDefaults.standard.bool(forKey: "isSubbed") ?? false

    if status == true {
        unlockTool1.isUserInteractionEnabled = true
        unlockTool2.isUserInteractionEnabled = true
        unlockTool3.isUserInteractionEnabled = true
    } else {
        unlockTool1.isUserInteractionEnabled = false
        unlockTool2.isUserInteractionEnabled = false
        unlockTool3.isUserInteractionEnabled = false
    }

}

If I find out the status then I will be able to save the state as true/false. I tried using UserDefaults with some success. Not sure if I placed the code in the best location though.

If the subscription is still active, I'd like to allow the user to click a button, and deny the button click if the subscription has run out.

I'll add more code if needed. Any help would be greatly appreciated.

Upvotes: 6

Views: 4624

Answers (1)

enc_life
enc_life

Reputation: 5169

The only way to check the subscription status is to verify the receipt with Apple to see if it's still valid using their /verifyReceipt endpoint - docs.

What you could do is cache some expiration date after the purchase and use that to check if the subscription is valid. If it's passed the expiration date you can re-check the receipt with Apple to see if it's renewed. There are also edge cases where a user is refunded and their subscription is cancelled before the expiration date - you should update your receipts periodically with Apple to check this case. Ideally, this should all be happening server side as well to avoid piracy.

Here's a great post that summarizes the nauces of Apple subscriptions very well: iOS Subscriptions are Hard

Upvotes: 2

Related Questions