'How to acknowledge in-app purchases in android?

I have gone through the Play Billing Library https://developer.android.com/google/play/billing/billing_library_overview You must acknowledge all purchases within three days. Failure to properly acknowledge purchases results in those purchases being refunded. The process is doesn't provide any clarity how to acknowledge purchases. This is what i tried Is this the correct way to do it. Thanks in Advance

@Override


 public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
        if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK&&purchases!=null){
            Toast.makeText(this, "Purchase Successful", Toast.LENGTH_SHORT).show();
            for(Purchase purchase:purchases){
                handlePurchase(purchase);
            }
        }else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.USER_CANCELED){
            Toast.makeText(this, "Purchase Cancelled", Toast.LENGTH_SHORT).show();
        }else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED){
            Toast.makeText(this, "Already Purchased", Toast.LENGTH_SHORT).show();
        } else{
            Toast.makeText(this, billingResult.getDebugMessage(), Toast.LENGTH_SHORT).show();
        }



    //in handlePurchase()
 if(!purchase.isAcknowledged())
{ 
          AcknowledgePurchaseParams acknowledgePurchaseParams
                    = AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .setDeveloperPayload(purchase.getDeveloperPayload())
                    .build();

            client.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
                @Override
                public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                    if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
                        Toast.makeText(RemoveAdsActivity.this, "Purchase Acknowledged", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }


Solution 1:[1]

If you are new and using billing library 4.0.0 then above codes will not work since now all billing process has been put in background thread so make sure you do not call any ui updating code during billing process.

Use:

purchase.getSkus.contains("sku here");

instead of

purchase.getSku.equals("sku here");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elikill58