Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - queryPurchaseHistoryAsync Purchase List is empty on other devices

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.

like image 798
Ahmed Avatar asked Jun 20 '18 06:06

Ahmed


1 Answers

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() { }
});
like image 56
2yonghyun Avatar answered Oct 02 '22 21:10

2yonghyun