3

I am new to android and trying to implement in-app billing first time.

I am using google play in-app library. https://developer.android.com/google/play/billing/billing_library_overview

I want to implement consumable in-app purchase. I am using 'android.test.purchased' reserved id for testing. I could load skuDetails and make purchase successfully and consume purchase successfully

here is my handlePurchase method with consumeAsync

void handlePurchase(Purchase purchase) {



BillingClient client = BillingClient.newBuilder(NewAdActivity.this)
        .enablePendingPurchases() 
        .setListener(this)
        .build();


if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
     System.out.println("item successfully purchased");


        if (!purchase.isAcknowledged()) {


                    ConsumeParams consumeParams = ConsumeParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .setDeveloperPayload(purchase.getDeveloperPayload())
                        .build();


                     ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
                    @Override
                    public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                          if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchaseToken != null) {
                              System.out.println("SUCCESSFULLY consumed PURCHASE");
                              providecontent();

                           } 
                        else { 
                              System.out.println("FAILED TO consume:”);
                            }

                    }

                };

                client.consumeAsync(consumeParams, consumeResponseListener); 



        }

    }



}

Does it also acknowledge purchase when I consume purchase? Do I need to set "acknowledged":true in purchase.originalJson manually?

Is my code correct to consume purchased item? or I need to include a separate acknowledgePurchase before consuming item.

Please reply. Any help is truly appreciated.

Thanks.

Gregg Tate
  • 31
  • 1
  • 3

2 Answers2

1

For consumable products, you want to use consumeAsync(). For products that aren't consumable, you want to use acknowledgePurchase(). For more about acknowledging purchases in your app, check out the official documentation: https://developer.android.com/google/play/billing/billing_library_overview#acknowledge

Caren
  • 1,358
  • 2
  • 13
  • 22
0

You are incorrectly using consumeAsync(), to acknowledge a purchase you should call acknowledgePurchase().

consumeAsync() removes the item purchased, for example if the purchase allows the user to play 10 times and he uses all of them then you would call consumeAsync() to let the user to buy the item again

An example:

  if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
            // Grant entitlement to the user.
            boolean signOk = verifyPurchaseSignature(purchase.getOriginalJson(), purchase.getSignature());
            if (!signOk) {
                // Alert the user about wrong signature
                return;
            } else if (!purchase.isAcknowledged()) {
                AcknowledgePurchaseParams acknowledgePurchaseParams =
                        AcknowledgePurchaseParams.newBuilder()
                                .setPurchaseToken(purchase.getPurchaseToken())
                                .build();
                billingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
                    @Override
                    public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                        //Give thanks for the purchase                        

                    }
                });
            }
        }
Jemshit
  • 9,501
  • 5
  • 69
  • 106
from56
  • 3,976
  • 2
  • 13
  • 23
  • this will acknowledge purchase and I can provide content and set flag in backend. But will it change acknowledged flag value in OriginalJson? It still shows "acknowledged":false," after acknowledging it and running queryPurchase. Here is my OriginalJson= {"packageName":"mypackage","acknowledged":false,"orderId":"transactionId.android.test.purchased","productId":"android.test.purchased","developerPayload":"","purchaseTime":0,"purchaseState":0,"purchaseToken":mytoken"}. Will it remain false because it is a testing id instead of a real purchase id ? – Gregg Tate Dec 18 '19 at 07:42
  • I never take into account that **acknowledged** parameter in the Json, for me what really says if a purchase has been completed successfully is that it appears as **Charged** after a few minutes in **Order Management** in the **Google Play Console** – from56 Dec 18 '19 at 17:39