13

I have used the In-app billing library for adding subscriptions in my app. Everything is working properly but I am unable to find how do I get a Users current active subscription?

As per the docs, the method queryPurchaseHistoryAsync returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed. Due to this, I am unable to know whether a current subscription is active or not.

According to this post, if we cancel the subscription, it will still be considered active for that day. But I am getting the subscriptions in the response which were canceled before 15 days.

Any help will be appreciated. Thanks in advance.

Mehul Kanzariya
  • 888
  • 3
  • 27
  • 58

2 Answers2

9

To query users subscription i use this method:

public void querySubscriptions() {
    Runnable queryToExecute = () -> {
        Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);

        if (mBillingClient == null ||
                purchasesResult.getResponseCode() != BillingClient.BillingResponse.OK) {
            return;
        }
        mPurchases.clear();
        onPurchasesUpdated(BillingClient.BillingResponse.OK, purchasesResult.getPurchasesList());
    };

    executeServiceRequest(queryToExecute);
}

If you need more details, ask.

Obsthändler
  • 313
  • 1
  • 12
  • Does this method return only active subscriptions or does it also return previously purchased subscription? –  Nov 15 '18 at 05:48
  • It will return only active subscriptions. You can test it, here is a link on how to test subscriptions: https://developer.android.com/google/play/billing/billing_testing#testing-subscriptions – Obsthändler Nov 15 '18 at 09:05
  • how to use this to trigger something? – Namikaze Minato Jun 20 '20 at 06:35
-1

A user can have multiple subscriptions active at any moment. You can check whether the subscription is active or not using isAutoRenewing method.Here is the docs for that method

Indicates whether the subscription renews automatically. If true, the subscription is active, and will automatically renew on the next billing date. If false, indicates that the user has canceled the subscription. The user has access to subscription content until the next billing date and will lose access at that time unless they re-enable automatic renewal (or manually renew, as described in Manual Renewal). If you offer a grace period, this value remains set to true for all subscriptions, as long as the grace period has not lapsed. The next billing date is extended dynamically every day until the end of the grace period or until the user fixes their payment method.

Farmaan Elahi
  • 1,620
  • 16
  • 18
  • 2
    How do I identify the case where the auto-renewal is set to false but still 10 days are left for the subscription? How can I allow user to access premium features until the subscription is expired? – Mehul Kanzariya Nov 09 '18 at 09:10
  • Dude, this is not correct. Read the text from the doc you posted. – Obsthändler Nov 14 '18 at 13:08