I'm using the Play Billing Library in order to trigger and manage purchases which in turn unlocked extra functionality within an app. This part is working.
However, what is the best way to 'restore' purchases. Say for example someone who has bought the app buys a new phone. Logs in to the Play Store, downloads my app and then finds that the payment screen to 'upgrade' is being displayed. iOS has a specific method for this but I'm not aware of one for Android.
My thoughts are to query the Play Store and confirm whether the account has previously SUCCESSFULLY purchased the item, if so then I will call the local upgrade function within the app.
It appears there are two similar methods. But which one should I used in this scenario? Where a user has either wiped their phone or bought a new one?
queryPurchases()
? Or queryPurchaseHistoryAsync()
?
So far as I can see, the only valid use for queryPurchaseHistoryAsync is to provide a user with a list of their purchase history. It's a bit of an oddball. Note also: queryPurchases is synchronous so in most cases it needs to be run in some kind of background worker thread. I run mine in an AsyncTask.
Nowhere in the description of queryPurchases does it say it will return currently active purchases. It is described as "Get purchases details for all the items bought within your app", which equally sounds a lot like what queryPurchaseHistory would return.
If there are multiple accounts added to the Play Store, queryPurchases returns the active purchases of all accounts logged in the device. Which means that if two accounts are configured, and both h... Skip to content Sign up Why GitHub? Features → Mobile → Actions → Codespaces → Packages → Security → Code review → Issues →
If there are multiple accounts added to the Play Store, queryPurchases returns the active purchases of all accounts logged in the device. Which means that if two accounts are configured, and both have the same subscription, queryPurchase...
You should use queryPurchases
. That gives you all the current active (non-consumed, non-cancelled, non-expired) purchases for each SKU.
queryPurchaseHistoryAsync
won't do what you need because it will only give you a list of the most recent purchases for each SKU. They may have expired, been cancelled or been consumed, and there's no way to tell. Therefore this response can't be used to tell what purchases to apply in your app.
So far as I can see, the only valid use for queryPurchaseHistoryAsync
is to provide a user with a list of their purchase history. It's a bit of an oddball.
Note also: queryPurchases
is synchronous so in most cases it needs to be run in some kind of background worker thread. I run mine in an AsyncTask
.
Per documentation queryPurchases
uses the Play Store app cache to get the results while queryPurchaseHistoryAsync
actually checks the Purchase AP for the most recent purchases. So, in your case you should check the Async
method.
queryPurchases
Get purchases details for all the items bought within your app. This method uses a cache of Google Play Store app without initiating a network request.
queryPurchaseHistoryAsync
Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.
Also, make sure to check the documentation. It recommends to Cache purchase details on your servers
. https://developer.android.com/google/play/developer-api.html#practices
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With