2
votes

I have a side project and I recently worked on my receipt manager to make it stronger and to relies more on the receipt of the app rather than persistently storing a value after a transaction.

However, there are 2 main points which although I read Apple docs and other answers on the web, I'm still confused about:

1. When a user restore their purchase, does the receipt get refreshed?

I made several tests in sandbox, and I have seen that when restoring, the receipt gets refreshed, and when I verify the receipt through the iTunes server verification, it returns a JSON including the latest transactions. This is very helpful because even if I close/open the app, the app receipt is updated and I can always verify it without refreshing it. However, in production, this didn't work. The app receipt wasn't refreshed after restoring purchases and my users got asked to restore purchases continuously. Can anyone answer on this point?

2. Does the refresh receipt request triggers an alert asking for the Apple ID's password in production?

From the previous point, I thought ok, I will force receipt refresh after a user restores their purchases. However, in development / sandbox, I get asked to insert my sandbox user's pass every time I try to refresh the receipt (although I could restore purchases without a password request before asking for the refresh). I read a lot about this and someone says that might not happen in production. Does any of you have a clarification on this?

Note: I know that when restoring / purchasing I get back a transaction with the receipt, however, I need to use the App Receipt to verify transactions (and this is also what Apple says to do).

Thank you in advance.

3

3 Answers

2
votes

1. Refreshing the receipt

In theory, calling restore purchases should get the latest receipt. In the instances where you are experiencing issues, take a look at SKReceiptRefreshRequest. Typically, I use this in production when a call to restore purchases has encountered errors.

Use it wisely, triggering that API can lead to showing the Sign In prompts for the App Store.

2. When is the user asked to sign in?

Sadly, I have seen this vary so I cannot give a definitive answer. More often than not, a call to restore purchases should not trigger a sign in. Explicitly using SKReceiptRefreshRequest will.

If the user is not signed in to the store, calling any Store API like attempting a purchase or restoring purchases could trigger the sign in flow.

What Apple Says

From the docs

Refreshing a receipt doesn't create new transactions; it requests the latest copy of the receipt from the App Store. Refresh the receipt only once; refreshing multiple times in a row has the same result. Restoring completed transactions creates a new transaction for every transaction previously completed, essentially replaying history for your transaction queue observer. Your app maintains its own state to keep track of why it’s restoring completed transactions and how to handle them. Restoring multiple times creates multiple restored transactions for each completed transaction.

My Recommendation

  1. Store the hash of the last receipt you used on device. You can use this hash to check against the latest receipt so you know if anything has changed. Whenever your App resumes you can always check if the current receipt hash is different from the last cached value.
  2. Try and submit the receipt as soon as possible. Typically when the App has launched.
  3. If a user tries to manually restore purchases, I would start with a call to restoreCompletedTransactions. This can trigger an App Store sign in but is often less likely. Most of the time this is enough as the receipt on the device is often pretty up to date.
  4. If the user tries another restore purchases, OR if the call failed, then move to SKReceiptRefreshRequest to guarantee a fresh receipt.
  5. When using SKReceiptRefreshRequest, I would recommend wrapping this behind UIAlertController. I normally show something that indicates it has failed and have a "Retry" button that uses the request. This will trigger a new store sign in.

Use restoreCompletedTransactions to get a playback of all completed transactions the device is aware of.

1
votes
  1. When a user restore their purchase, does the receipt get refreshed?

Yes, it should. But it also sounds like you're doing some server-side validation? If that's the case, you can send any receipt from the user to the /verifyReceipt endpoint to get the latest status. You don't need to send the latest receipt, since /verifyReceipt will also refresh it.

  1. Does the refresh receipt request triggers an alert asking for the Apple ID's password in production?

There's no clear Apple documentation on this, but it definitely will if there's no receipt file present in the app (rare in production). But if you're doing server-side validation (see #1), then you can send up any receipt you have, no need to refresh it. So you're only refreshing the receipt if nothing is present, which will trigger the sign-in. Keep in mind a receipt file is not present on the device after installing in sandbox - only after a purchase. This differs a lot from production where a receipt file is generated after installation.

From what it sounds like you're trying to do, my recommendation would be to check if any receipt file is present at launch, send it to /verifyReceipt to get the latest status for the user and cache the result. You can do this on every app launch.

In a perfect world you're storing the receipt server-side and keeping it up-to-date there, but you mentioned side project so that sounds like overkill. However, an out-of-the box solution that correctly implements all of this and will scale with you - such as RevenueCat - is another alternative (Disclaimer: I work there).

1
votes

After many tests and after I sent my app in production, I'm now able to answer my questions properly:

1. When a user restores their purchase, does the receipt get refreshed?

YES, this is immediate as for Sandbox, BUT the problem is that the receipt DOESN'T include non-consumable purchases. This means in other words that the receipt will include the purchased subscriptions, but you won't find purchases of non-consumable products. However, when the user restores or purchases, you get the transactions in return, and you can extract the non-consumable products, and store this info somewhere like UserDefaults or Keychain, so you can use them when the user opens your app. For the rest, the best approach is to always validate and check the receipt when the app is opened.

2. Does the refresh receipt request trigger an alert asking for the Apple ID's password in production?

YES. For sure it does the first time.

Thank you to Daniel and enc for their answers that can still be useful.