iosin-app-purchaseauto-renewingreceipt-validationin-app-subscription

What is the best way of storing auto renewing subscription receipts in ios?


I am implementing Auto Renewing subscriptions in my App. I am able to complete all the Purchases and Restorations perfectly. But i am kind of struck on what would be best ways to save the receipt details. Below are my questions.

  1. Is it a good idea to save the transaction details in NSUserDefaults and retrieve the details when needed?
  2. Is it a good idea to save the receipts in Documents Directory and the read it from there?
  3. What is the best way of checking the end date of the subscription? Do i need to constantly check the current date and then compare with the end date of the subscription every time the user tries to access the content?
  4. Can i get the end date of the subscription from the saved receipt or Do i need to contact the Apple server to get the end date of the subscription?
  5. How can i update my receipt once the subscription is auto renewed after the specified period?

I am aware of some approaches. But want to know the best approaches. Please guide me the best steps to achieve the above objectives. Thank you.


Solution

  • I'll try to answer your questions but you would probably be benefited from watching the WWDC StoreKit videos, 1 and 2.

    1. I would store purchase state in the same place you are storing all other client side state.

    2. I would not save the whole receipt. This would be overkill, just extract the information you need and save it. The receipt is always accessible from the Bundle.appStoreReceiptURL so you don't need to store it.

    3. For subscriptions, you need to verify the receipt with Apple's verifyReceipt endpoint, and find the latest expires_date for the given IAP product identifier. Store this date (in defaults or elsewhere) and use that to compute the user's status.

    4. The proper way to get the latest expiration is to send the receipt to your own server, store it, and periodically check it against the verifyReceipt endpoint and update the expiration from the latest_receipt_info field in the response. A less good solution is to make sure you always have an SKPaymentQueue observer setup in your app, because when a recurring subscriptions renews a new transaction will appear on the queue. If you process this, read the new receipt data, and finish the transaction, you don't need a whole backend, however, without verifying the receipt independently from your backend, it will be vulnerable to IAP crackers.

    5. When you send a receipt to /verifyReceipt you will receive a JSON response that contains a key latest_receipt_info that will have the most recent transactions. This will be true no matter how old the receipt is.

    IAP and especially subscriptions is not a simple system so I highly recommend taking the time to watch the WWDC videos and to read Apple's guides on StoreKit.