Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android billing:4.0.0 - queryPurchases(INAPP) and purchase.getSku()

I refresh to android billing version 4 and 2 things are not working anymore.

First I have this:

else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {             Purchase.PurchasesResult queryAlreadyPurchasesResult = billingClient.queryPurchases(INAPP); // deprecated             List<Purchase> alreadyPurchases = queryAlreadyPurchasesResult.getPurchasesList();             if(alreadyPurchases!=null){                 handlePurchases(alreadyPurchases);             }         } 

queryPurchases is deprecated.

Second I have this:

void handlePurchases(List<Purchase>  purchases) {     for(Purchase purchase:purchases) {         //if item is purchased         if (PRODUCT_ID.equals(purchase.getSku()) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED)         {             if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {                 // Invalid purchase                 // show error to user                 Toast.makeText(getApplicationContext(), R.string.plus_error, Toast.LENGTH_SHORT).show();                 return;             } 

getSku() was working, but now it is mark as Cannot resolve method getSku() in Purchase

Any ideas how to solve this issues?


From docs:

Summary of changes Added BillingClient.queryPurchasesAsync() to replace BillingClient.queryPurchases() which will be removed in a future release.  Added Purchase#getSkus() and PurchaseHistoryRecord#getSkus(). These replace Purchase#getSku and PurchaseHistoryRecord#getSku which have been removed. 

But I don't know how to apply this new commands in my code above.

If I change getSku to getSkus my if if (PRODUCT_ID.equals(purchase.getSkus()) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) will say that it is always false. And I have no idea how to use queryPurchasesAsync(), need 2 params now.

Thanks.

like image 310
RGS Avatar asked May 19 '21 16:05

RGS


People also ask

How does Google Play billing library support multi-quantity purchases?

Supported in versions 4.0 and higher of the Google Play Billing Library, Google Play allows customers to purchase more than one of the same in-app product in one transaction by specifying a quantity from the purchase cart. Your app is expected to handle multi-quantity purchases and grant entitlement based on the specified purchase quantity.

How to update subscription in Google Play billing library?

With Google Play Billing Library 4.0.0 to update subscription you have to call BillingFlowParams.Builder.setSubscriptionUpdateParams () and pass an instance of SubscriptionUpdateParams as a parameter. Note that the setOldSku () method has been removed. 4. Multi-quantity purchases (not released yet)

Is acknowledgement required when using Google Play billing library?

Note: Acknowledgement is not required when using a version of the Google Play Billing Library prior to 2.0. The process to grant entitlement and acknowledge the purchase depends on whether the purchase is a non-consumable, a consumable, or a subscription.

What happens when a user doesn't pay for an in-app product?

Note: If the user doesn't owe money when they purchase an in-app product, such as during a free trial of a subscription, the Order ID is issued for $0. For example, when a user cancels a subscription, the subscription remains valid until the end of the billing period. If the user decides to re-signup, some credit remains in their account.


1 Answers

As I mentioned earlier in a comment you are comparing a String to a List object, but as chitgoks said it is ArrayList<String> and not List<String> as i assumed. I'm not sure if you would ever get more than one sku-string (since you probably don't order multiple things at the same time?) but either look trough them all to be sure or take a chance and compare PRODUCT_ID with only purchase.getSkus().get(0).

The new async call for purchases seems to require only small changes.

Example of old way to do it:

Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.SUBS); doSomethingWithPurchaseList(result.getPurchasesList()); 

And this would be the new way to do the same:

billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, new PurchasesResponseListener() {         @Override         public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {             doSomethingWithPurchaseList(list);         }     }); 
like image 78
Vanheden Avatar answered Sep 18 '22 06:09

Vanheden