简体   繁体   中英

In-app billing acknowledgePurchase issue [using google play in-app billing library]

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.

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

Welcome to stackoverflow !

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                        

                    }
                });
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM