androidandroid-billing

How can i get information about in-app-purchases in Google Play Billing for android


I want to get information about how to get detail of already Purchases product with or without internet connection in android studio.

As i know, in google play billing library has 2 method for retrieving the information.

1. PurchasesResult purchasesResult = billingClient.queryPurchases(SkuType.INAPP);

2. billingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
                                         new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(BillingResult billingResult,
                                          List<Purchase> purchasesList) {
        if (billingResult.getResponseCode() == BillingResponse.OK
                && purchasesList != null) {
            for (Purchase purchase : purchasesList) {
                // Process the result.
            }
         }
    }
});

But i don't know how to retrieve the information.


Solution

  • This is the way I have implemented In-App Purchase. It is working smoothly. Hope it helps.
    
    mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
                @Override
                public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    
                    //  Log.d("anupam", responseCode + "");
                    if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
                        for (Purchase purchase : purchases) {
                        //List of purchases
                        }
                    } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
                        Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
                    }
                }
            }).build();
    
            mBillingClient.startConnection(new BillingClientStateListener() {
                @Override
                public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
    
                    if (billingResponseCode == BillingClient.BillingResponse.OK) {
                        // The billing client is ready. You can query purchases here.
    
                        final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
                        for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
                            if (sourcePurchase != null) {
    
                                ConsumeResponseListener listener = new ConsumeResponseListener() {
                                    @Override
                                    public void onConsumeResponse(final int responseCode, final String purchaseToken) {
                                        // Log.d("anupam2", responseCode + "  <------->  "+ purchasesResult.getPurchasesList() + "  <------->  " + purchaseToken);
                                    }
                                };
                                mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
                            } else {
    
                            }
                        }
    
                        if (purchasesResult.getPurchasesList().size() > 0) {
                            //  Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");
    
                        } else {
                            //  Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
                            BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
                            int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
                            if (responseCode == 7) {
    
                                //Item already purchased
                            }
                        }
                    }
                }
    
                @Override
                public void onBillingServiceDisconnected() {
                    // Try to restart the connection on the next request to
                    // Google Play by calling the startConnection() method.
                    Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
                }
            });