Search code examples
androidin-app-purchasein-app-billingrestoreandroid-billing

Using Android BillingClient, how to restore a user's IAP after re-installing, or changing their device?


My application has a single one-time non-consumable IAP to turn off advertisement. I'm trying to provide a "Restore Purchase" option for my application. In the event a user uninstalls the application or migrates to a new device, they have an option to recover their one-time past purchase.

My app is following the purchase flow described within Google's documentation https://developer.android.com/google/play/billing/integrate

I've looked for examples to restore a past purchase for a different device, but all the examples I've found are out dated referencing the AIDL methods.

I've looked at methods, BillingClient.queryPurchases() and BillingClient.queryPurchaseHistoryAsync(). queryPurchaseHistoryAsync() give a list of PurchaseHistoryRecords, for all past purchases including canceled purchases, but does not provide the purchase status to determine if any of those are a completed or pending purchase.

In contrast, queryPurchases() will give a list of purchases and their status, but only for the device. It returns an empty list if a user made the IAP on a different device.

I do not have a backend as I'm an independent developer with limited resources, so my app must be self-contained.

Any help is appreciated. Thanks.


Solution

  • So I learned that queryPurchases() is cached locally on your device, but is updated when you call queryPurchaseHistoryAsync()

    I discovered from this Stackoverflow answer here

    So my solution, is when wanting to restore a purchase on a new device, or a fresh install of my app. Call queryPurchaseHistoryAsync() Then in the callback of onPurchaseHistoryResponse() call queryPurchases() and look within the List<Purchase> from the PurchasesResult for the purchase status of any of the user's past purchases.

    If there's an expected purchase your app can grant the entitlements of their past purchase.