The Google Play Billing Library is returning an empty list of subscriptions when querying through the queryPurchasesAsync
method while in fact there is an active subscription. From the documentation of queryPurchasesAsync:
Only active subscriptions and non-consumed one-time purchases are returned. This method uses a cache of Google Play Store app without initiating a network request.
I tried emptying the Play Store cache, but still an empty list is returned.
I'm using the Play Billing Library 6.0.1. Here's my relevant code:
private BillingClient billingClient;
private final Set<Purchase> purchases;
boolean billingClientConnected;
public static BillingManager getInstance(Activity activity) {
if (billingManager == null) {
billingManager = new BillingManager(activity);
}
return billingManager;
}
public BillingManager(Activity activity) {
productList = new ArrayList<>();
purchases = new HashSet<>();
init(activity);
}
private void init(Activity activity) {
// Create the billing client.
billingClient = BillingClient.newBuilder(activity)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
startBillingClientConnection();
}
public void startBillingClientConnection() {
if (billingClient != null) {
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
billingClientConnected = true;
// Do some other stuff..
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
billingClientConnected = false;
}
});
}
}
public void queryPurchases() {
if (!billingClientConnected) {
startBillingClientConnection();
} else {
QueryPurchasesParams subsPurchasesParams = QueryPurchasesParams.newBuilder()
.setProductType(BillingClient.ProductType.SUBS)
.build();
billingClient.queryPurchasesAsync(subsPurchasesParams,
new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult,
@NonNull List<Purchase> purchaseList) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// Here I get an empty list.
// Handle all purchases that were successfully queried.
for (Purchase purchase : purchaseList) {
handlePurchase(purchase);
}
}
}
});
}
}