Android - queryPurchase() returns an empty list but I purchased an in app product

Viewed 1129

I have implemented in app products in my app. I can make successful purchase and everything works fine. But in one device, purchase doesn't work although when I try to make a purchase again, I get the response "Product already owned" which shows that the purchase is fine. But queryPurchase() returns an empty purchase list. I've only one email on that device.

public void queryPurchase() {
    Runnable queryPurchaseRequest = getQueryPurchaseRequest();
    executeRequest(queryPurchaseRequest);
}

    private Runnable getQueryPurchaseRequest() {
        return new Runnable() {
            @Override
            public void run() {
                Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
                if(purchasesResult.getResponseCode() == BillingResponseCode.OK) {
                    ArrayList<Purchase> purchaseList = new ArrayList<>();
                    purchaseList.addAll(purchasesResult.getPurchasesList());
    
                    if(isSubscriptionSupported()) {
                        Purchase.PurchasesResult subscriptionResult
                                = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
    
                        if (subscriptionResult.getResponseCode() == BillingResponseCode.OK) {
                            purchaseList.addAll(subscriptionResult.getPurchasesList());
                        } else {
                            Log.e(TAG, "Got an error response trying to query subscription purchases");
                        }
                    }
                    onQueryPurchasesFinished(purchaseList);
                } else {
                    Utilities.setPurchaseLog("onBilling manager on query purchase request: result unknown\n");
    
                }
            }
        };
    }

Note: Billing result, subscription response code everything seems successful. And everything is working perfectly on other devices.

Have anyone any solution? Thanks for your help!

1 Answers

It is a known bug that has been happening for ages now, since billing libraries version 2.x (Priority: P1 , Severity S2)

https://issuetracker.google.com/issues/160473001

I think the best thing you can do is going there and leaving your star/comment to try to speed things up.

However, there's something we can miss sometimes: It turns out that you need to wait for the billingClient.startConnection() call to finish doing it's thing and onBillingSetupFinished() callback gets called on the provided BillingClientStateListener. Only after that you can call the queryPurchases method. If you call queryPurchases beforee finishing the connection to the service, you will get an empty list.

Related