0

I've read a lot of SO answers about handling auto-renewable subscriptions with your own user login system, but there is an issue that I am still not sure how to handle: what to do when the transaction observer is triggered while the user is logged out.

Apple recommends to implement a transaction observer right in the App Delegate:

Adding your app's observer at launch ensures that it will persist during all launches of your app, thus allowing your app to receive all the payment queue notifications.

Say the user starts a purchase, but it doesn't complete immediately (for instance because it needs to be approved by their parents, or the app crashes, etc.). The user logs out and opens the app again, and since we are observing transactions in App Delegate, we might receive a transaction immediately when launching the app, before he logs in. We are then unaware of which user to associate the subscription to.

Two ideas:

  • Non-ideal: should I store the fact that there is a subscription pending and assume that the first user to log in will be the right one, and then associate it in some way to them once they log in? And if so, where would I keep the receipt? KeyChain, UserDefaults? This sounds pretty clunky.

  • Another way that sounds better: can I store some information about the transaction when it is initiated, and then use one of these fields to actually know exactly whose user's subscription purchase just finished?

(Not particularly relevant, but FYI I am using SwiftyStoreKit).

Kqtr
  • 5,824
  • 3
  • 25
  • 32

1 Answers1

1

This is how I would approach this:

  1. When the user tries to purchase a subscription, I first have them login or create an account.

  2. Once the user is logged in, I send their appStoreReceipt to my server and store it there. I check and make sure the user is able to purchase. (At this point they should have the subscription they are trying to purchase)

  3. Once I get a response from the server that the user should be able to purchase I go ahead and start the in-app-purchase

  4. When the in-app-purchase process is done, I send the updated appStoreReceipt to my server and upgrade their account.

The problem you are worried about is what if the user gets done with step 3 but never gets to step 4.

Well, since I have their receipt stored on my server (from step 2), I can just ask Apple to give the latest version of their receipt, and if it shows up that they did purchase, I upgrade their account. You can choose when the right time is to do this check, it can be every time the app launched, or every time the user logs.

Hope this helps.

Kqtr
  • 5,824
  • 3
  • 25
  • 32
l-l
  • 3,804
  • 6
  • 36
  • 42
  • Thanks, your answer helps. This would basically imply ignoring the completeTransaction step -in the sense that we would just need to finish the transaction if needsFinishTransaction, and not upgrade any user account. Not perfect.But reading your answer, if we actually can get an appStoreReceipt to identify them BEFORE they buy any subscription or IAP (which I didn't know), that means that we can use this to check which user should be upgraded, in completeTransaction, by looking up the receipt id in my database. That way we avoid checking at every app open or login. Thanks again. – Kqtr Jul 25 '17 at 09:31
  • Can some one answer my question https://stackoverflow.com/questions/45433136/how-subscription-status-url-works-for-apple-in-app-purchases-auto-renewable – Sukeshj Aug 01 '17 at 13:21