IOS - In App Purchase Retore completed transaction - ios

I had been going through the documentation many times and still not sure how to go about this. From In App Purchase Programming guide - Restore Purchased Product:
In most cases, all your app needs to do is refresh its receipt and
deliver the products in its receipt. The refreshed receipt contains a
record of the user’s purchases in this app, on this device or any
other device. However, some apps need to take an alternate approach
for one of the following reasons:
If you use Apple-hosted content, restoring completed transactions
gives your app the transaction objects it uses to download the
content.
Does that means if I am using Apple-hosted content, I don't need to refresh its receipt and call restore completed transaction straight to retrieve my previous download product?

You still need to do it.
What the text you quoted means is that the receipt contains the information about what in-app products the user purchased, but the receipt does not contain the content.
So if you use Apple-hosted content, you need a mechanism to download said content. That content is not provided when you refresh the receipt. It is downloaded however when you use the various restore functions.

Related

Apple In-App Purchase and Receipt Refresh

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.
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
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.
Try and submit the receipt as soon as possible. Typically when the App has launched.
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.
If the user tries another restore purchases, OR if the call failed, then move to SKReceiptRefreshRequest to guarantee a fresh receipt.
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.
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.
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).
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.

How can I get the cancellation_date field?

First,i want to know if the user has refunded in my App.
So i find the cancellation_date field can judge it, and it can use by this method from In-App Purchase FAQ
The appStoreReceipt is refreshed only in the following cases after the
refund has occurred - when there is a successful purchase of an in-app
purchase item, when the SKReceiptRefreshRequest call is used and
when the restoreCompletedTransactions is used to restore previously
purchased products.
but this method will call the iTunes Store login alert.
So i want to know if there is another way to get the cancellation_date field or another way to know the user has refunded?
The recommended way is to store the entire receipt on your server and refresh it with the /verifyReceipt endpoint to get the current state of the subscription. Even if you only refreshed the receipt daily it would prevent a user from getting much access to your app after a refund, and you could track them even if they didn't open the app again.
Here's a good post that covers device-side and server-side nuances of Apple receipts: iOS Subscriptions Are Hard

Any reason to use restoreCompletedTransactions given appStoreReceiptURL in iOS7/8?

I am in the process of updating my app to new iOS7/8 in-app purchase libraries. I see now that as of iOS7, we have access to appStoreReceiptURL as part of NSBundle.
It appears I can access this URL, and its concomitant data, at any time, without interacting or interfacing with the SKPaymentQueue.
Previously, when a customer installed our app and wanted to restore his in-app subscription, the app would call the restoreCompletedTransactions method of [SKPaymentQueue defaultQueue], and from that obtain receipt information, one transaction at a time, that the app would upload to our server one transaction at a time.
However, while testing in the iOS App Store Sandbox, now I appear to be able to obtain one piece of master receipt data from [[NSBundle mainBundle] appStoreReceiptURL], upload that to my server, obtain a complete history of every in-app transaction this user has made, and then record transactions on my server as appropriate and send notification back to my client.
Consequently, why or when would anyone need to call restoreCompletedTransactions? In my app, I sell a single, auto-renewing in-app subscription; are there other use cases today in iOS7/8 for which this API is still helpful?
If a user has made a purchase on a different device, it can be useful (or necessary) to "restore" that purchase on a new device. restoreCompletedTransactions could enable this restore operation. By default, on this new device, I believe the >= iOS7 style receipt will not have those prior purchases. On >= iOS7, SKReceiptRefreshRequest will work in many cases.
From Apple:
In most cases, all your app needs to do is refresh its receipt and
deliver the products in its receipt. The refreshed receipt contains a
record of the user’s purchases in this app, on this device or any
other device. However, some apps need to take an alternate approach
for one of the following reasons:
If you use Apple-hosted content, restoring completed transactions
gives your app the transaction objects it uses to download the
content.
If you need to support versions of iOS earlier than iOS 7,
where the app receipt isn’t available, restore completed transactions
instead.
If your app uses non-renewing subscriptions, your app is
responsible for the restoration process.
See https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/Restoring.html#//apple_ref/doc/uid/TP40008267-CH8-SW9

why store completed transaction for in app purchase?

I've set up a non-consumable in app purchase for may app, it all works well except i haven't yet decided on the correct RestoreTransaction procedure.
Apple suggest storing a list of completed transactions in NSUserDefaults, but why?
If the customer resets the device then surely this list is lost so its a non starter?
Can anyone tell me from their experience, if I do need to list completed items and if I need to validate the receipt with apple for non-consumable purchase.
I can't see why either of these should be done for non-consumable products.
Doesn't Apple verify the purchase from OriginalTransaction when restoreCompletedTransactions is called?

In app purchase expiration content

My application currently allows the user to purchase Non-consumable products. I have a Built-in product delivery model which do not involve a developer server.
Im thinking of adding more content which will expire after 1 year. Upon expiration user should have the option to buy the content again. I would prefer the content not being auto renewed. My questions are as follows.
Does the app store only allow magazines and news paper items to be
auto-renewable? The content Im focusing might not have an update
after renewing. Can I renew with the same content after 1 year?
Do I need a server to implement auto-renewable? Can I use the current
Built-in product delivery model to validate the receipts?
What would be the main disadvantage of Non-renewing subscriptions ?
Does it mean that the app store never provides transaction
information paymentQueueRestoreCompletedTransactionsFinished call
back?
Any help on this matter would be highly appreciated.
Thank you
1) It depends on whether you're planning to have your newspaper or magazine in the App Store, or in Newsstand. If you're wanting it to appear in Newsstand, it'll need to have an auto-renewable subscription. If you just want it listed in the regular App Store, you can use whatever IAPs you wish.
2) You don't need a server (despite what the documentation seems to imply), you can verify the receipts from within the app (I've done this in several Newsstand apps). You'll have the receipt as an NSData object, you then need to encode it as a Base64 string and embed it in an HTTP request to Apple's verification servers. You can find a method for doing the conversion from NSData to Base64 string here: How to encode NSData as base64? (iPhone/iPad)
3) Yes, the disadvantage with non-renewing subscriptions is that you have to do all the work in terms of restoring, cancelling, and making the subscription expire (you can't set a time limit in iTunes Connect for them). Check the In App Purchase guide for more details on what your app is responsible for with non-renewing subscriptions (scroll down to the Registering Products with the App Store section): http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StoreKitGuide/APIOverview/OverviewoftheStoreKitAPI.html

Resources