Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if user bought a product offered by IAP

In my application I offer the user to make a donation using Google Play IAP, in return I remove ads and unlock premium features.

When my application loads I want to check if user made a donation, how to do that via code knowing that after user makes a donation I'll call the following code to allow the user to make future donations if desired.

So, I want to allow the user to make further donations if desired, but I want to know if (s)he already made a donation to disable ads and unlock premium features.

BillingProcessor bp;
bp.consumePurchase(productId);

Note, my questions is about IAP online process not about saving a value offline and check it later.

like image 872
Jack Avatar asked Dec 14 '22 14:12

Jack


1 Answers

I think this guide should help show you how to do this:

https://developer.android.com/google/play/billing/billing_library_overview

Query cached purchases

To retrieve information about purchases that a user makes from your app, call the queryPurchases() method with the purchase type (SkuType.INAPP or SkuType.SUBS) on the Play Billing Library client. For example:

PurchasesResult purchasesResult = mBillingClient.queryPurchases(SkuType.INAPP);

Google Play returns the purchases made by the user account logged in to the device. If the request is successful, the Play Billing Library stores the query results in a List of Purchase objects.

Note: Only active subscriptions appear on this list. As long as the in-app product is on this list, the user should have access to it. For further information, refer to Handle SUBSCRPTION_ON_HOLD section of the Add subscription-specific features document. To retrieve the list, call the getPurchasesList() method on the PurchasesResult object. You can then call a variety of methods on the Purchase object to view relevant information about the item, such as its purchase state or time. To view the types of product detail information that are available, see the list of methods in the Purchase class.

Call queryPurchases() at least twice in your code:

Every time your app launches so that you can restore any purchases that a user has made since the app last stopped. In your onResume() method because a user can make a purchase when your app is in the background (for example, redeeming a promo code in Play Store app). Calling queryPurchases() on startup and resume guarantees that your app finds out about all purchases and redemptions the user may have made while the app wasn't running. Furthermore, if a user makes a purchase while the app is running and your app misses it for any reason, your app still finds out about the purchase the next time the activity resumes and calls queryPurchases().

Query most recent purchases

The queryPurchases() method uses a cache of the Google Play Store app without initiating a network request. If you need to check the most recent purchase made by the user for each product ID, you can use the queryPurchaseHistoryAsync() method and pass the purchase type and a PurchaseHistoryResponseListener to handle the query result.

queryPurchaseHistoryAsync() returns the most recent purchase made by the user for each product ID, even if that purchase is expired, cancelled, or consumed. Use the queryPurchases() method whenever possible, as it uses the local cache, instead of the queryPurchaseHistoryAsync() method. You could combine queryPurchaseHistoryAsync() with a Refresh button allowing users to update their list of purchases.

The following code demonstrates how you can override the onPurchaseHistoryResponse() method:

 mBillingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
                                          new PurchaseHistoryResponseListener() {
     @Override
     public void onPurchaseHistoryResponse(@BillingResponse int responseCode,
                                           List purchasesList) {
         if (responseCode == BillingResponse.OK
                 && purchasesList != null) {
             for (Purchase purchase : purchasesList) {
                 // Process the result.
             }
          }
     } });
like image 79
nasch Avatar answered Dec 31 '22 06:12

nasch