Android - queryPurchaseHistoryAsync Purchase List is empty on other devices
Asked Answered
P

2

11

I'm trying to implement the new Google Play Billing to my app, and try to retrieve my already purchased in-app product, by using queryPurchaseHistoryAsync() method, and the Purchase list always empty with 0 elemnts.

The code is working fine on my device which I purchased the item, but with my other device which has the same Google account, it returns nothing.

Also as in the Documentation, queryPurchaseHistoryAsync() should sync with Google to get the purchase history, but for some reason it does not seem to sync.

My code is:

BillingClient billingClient = BillingClient.newBuilder(getApplicationContext()).setListener(new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
        if(responseCode == BillingClient.BillingResponse.OK) {
            //Do something
        }
    }
}).build();
billingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(int responseCode) {
        if(responseCode == BillingClient.BillingResponse.OK) {
            //Response is OK and working fine
        } 
    }

    @Override
    public void onBillingServiceDisconnected() {
        //Do something
    }
});

billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(int responseCode, List<Purchase> purchasesList) {
        if(responseCode == BillingClient.BillingResponse.OK) {
            //Always returning 0 size() of purchasesList
            Toast.makeText(getApplicationContext(), "There are " + purchasesList.size() + " items you've purchased.", Toast.LENGTH_LONG).show();
        }
    }
});

Where exactly have I gone wrong with this code?

Thank you so much.

Penetrance answered 20/6, 2018 at 6:8 Comment(0)
B
13

Did you call queryPurchaseHistoryAsync() after onBillingSetupFinished()?

It does seem you call queryPurchaseHistoryAsync() before onBillingSetupFinished() is called on your source code.

You can check the following code in this doc

private BillingClient mBillingClient;
...
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
        if (billingResponseCode == BillingResponse.OK) {
            // The billing client is ready. You can query purchases here.

        }
    }
    @Override
    public void onBillingServiceDisconnected() { }
});
Bandung answered 2/7, 2018 at 14:23 Comment(2)
This might be the case in the original question, but it's not the real issue. I'm absolutely positive I'm doing everything correctly yet I also have the same issue. Even on the very same device, the async method always returns an empty list.Hemistich
Could you share the MVCE can we check?Bandung
P
1

To consume all the unconsumed items you can run this snippet in onBillingSetupFinished

val purchases = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
for (purchase in purchases.purchasesList ?: emptyList()){
    val consumeParams =
        ConsumeParams.newBuilder()
            .setPurchaseToken(purchase.purchaseToken)
            .build()
    billingClient.consumeAsync(consumeParams){ billingResultAfterConsumed , string ->
        //Talk to your server / local database to deliver the consumable to the user
    }
}
Parodic answered 3/2, 2020 at 10:55 Comment(1)
After consume I am not talking to the server. But why the billingClient.queryPurchase not shows the result for me? Initially it shows the result but after some time If i navigate to the app, it again shows the purchase item.Dev

© 2022 - 2024 — McMap. All rights reserved.