Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

queryPurchases() vs queryPurchaseHistoryAsync() in order to 'restore' functionality?

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()?

like image 498
laurie Avatar asked Jan 26 '18 08:01

laurie


People also ask

What is the use of 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.

Does querypurchases return current purchase history?

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.

Why does querypurchases return more than one purchase in Android?

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 →

How does querypurchases work with multiple accounts in Play Store?

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...


2 Answers

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.

like image 132
HughHughTeotl Avatar answered Sep 21 '22 22:09

HughHughTeotl


Per documentation queryPurchases uses the Play Store app cache to get the results while queryPurchaseHistoryAsyncactually checks the Purchase AP for the most recent purchases. So, in your case you should check the Asyncmethod.

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

like image 26
daniribalbert Avatar answered Sep 17 '22 22:09

daniribalbert