Search code examples
androidin-app-purchasein-app-billing

InApp Purchase response is OK but Dialog not appear


The problem is that during the implementation of InApp Purchase and click the button the result of response is OK but dialog to buy does not appear Here is the code

 //To query Google Play for in-app product details, call this method
    mBillingClient.querySkuDetailsAsync(params.build(),
            new SkuDetailsResponseListener() {
                @Override
                public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
                    Log.d(TAG, "response from query -> " + responseCode);
                    if (responseCode == BillingClient.BillingResponse.OK
                            && skuDetailsList != null) {
                        for (SkuDetails skuDetails : skuDetailsList) {
                            String sku = skuDetails.getSku();
                            String price = skuDetails.getPrice();
                            if ("minimum".equals(sku)) {
                                //Retrieving a product’s price is an important step before
                                // a user can purchase a product because the price is different
                                // for each user based on their country of origin.
                                mOneDollarPurchasePrice = price;
                                BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                                        .setSkuDetails(skuDetails)
                                        .build();
                                responseCode = mBillingClient.launchBillingFlow(ShopActivity.this, flowParams);
                            } else  {
                                //TODO Handle other scenarios of SKU
                            }
                        }

Solution

  • You have to consume the product after purchase.

      private void consumeFromLibrary() {
        ConsumeResponseListener listener = new ConsumeResponseListener() {
            @Override
            public void onConsumeResponse(@BillingClient.BillingResponse int responseCode, String outToken) {
                Log.d(TAG, "Consume response -> " + responseCode);
                if (responseCode == BillingClient.BillingResponse.OK) {
                    // Handle the success of the consume operation.
                    // For example, increase the number of coins inside the user's basket.
                } else if (responseCode == BillingClient.BillingResponse.SERVICE_DISCONNECTED) {
                    Log.d(TAG, "SERVICE DISCONNECTED");
    
                }
            }
        };
        Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
        List<Purchase> purchaseList = purchasesResult.getPurchasesList();
        for (Purchase purchase : purchaseList) {
            mBillingClient.consumeAsync(purchase.getPurchaseToken(), listener);
        }
    }