androidgoogle-playin-app-purchaseandroid-inapp-purchasesku

Query InApp purchases within my Android app


I have an Android app where users can buy 1 inapp product to unlock some features.

I've read extensively the guide at: https://developer.android.com/google/play/billing/billing_library_overview#java

I understand that in order to let users buy the inapp product I have to:

That's where things get confusing. From the Google documentation perspective the job is done, they explained to you how to retrieve/purchase/and verify a user payment. However, nowhere it is explained how to remember the payment and unlock the paid feature during the app startup.

The documentation states:

To retrieve information about purchases that a user makes from your app, call the queryPurchases()

So it seems like the app doesn't need to remember anything, just call the queryPurchases() at startup and check if the SKU is present (user already paid for it) or not (user still hasn't bought the paid version of the app).

So my app is doing just that, calling queryPurchases() at startup and check if the SKU is present or not. This method works very well, even when the app starts offline. However some users are lamenting the fact that sometimes the app doesn't start in paid mode, because (I debugged the code) the function queryPurchases() fails (sometimes) when the device is offline. Could it be that the queryPurchases() is calling the local Google Play cache which can lose track of previous purchases for some reason? (cache purging, etc...)

What's the appropriate method to remember user purchases and enable paid features at app startup?


Solution

  • You could keep your own "cache" (SharedPreferences or a DB) with the results of onPurchasesUpdated and use queryPurchaseHistoryAsync. When the app first starts you can show paid content if your cache is telling that the user purchased the product and call queryPurchaseHistoryAsync at the same time to get the most recent purchase made by the user for each SKU, when onPurchaseHistoryResponse you can update your cache and hide the paid content if the purchase expired.

    Take also into account that it's recommended for security purposes to go through purchases verification on your backend.

    If you don't want to manage your own server, it may be worth using a tool like RevenueCat, that offers a purchase/subscription backend-as-a-service.