javaandroidrestoreplay-billing-library

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


Solution

  • 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