Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

297
Views
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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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);
        }
    });
over 4 years ago · Santiago Trujillo Report

0

So you all know new billing library has new feature

  1. every thing will be in background thread so do not do change anything on main UI during acknowledgement of purchase and restoring purchase.

  2. if you are giving consumable purchase then user can now buy same sku in more quantity in one purchase so write logic accordingly. use getQuantity() function.

  3. to restore non consumable.

billingClient.queryPurchasesAsync(
  BillingClient.SkuType.INAPP,
  new PurchasesResponseListener() {

    @Override
    public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List < Purchase > myPurchases) {

      if (!myPurchases.isEmpty()) {

        for (Object p: myPurchases) {
          final Purchase purchase = (Purchase) p;
          if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED && purchase.getSkus.contains("sku here") {
              handlePurchase(purchase);
            }

          }
        });
    }
  }
)
over 4 years ago · Santiago Trujillo Report

0

getSkus returns an ArrayList<String>. Please use contains as below.

purchase.getSkus().contains(YOUR_PRODUCT_ID.toLowerCase())
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!