IabHelper: check if subscription is auto renewing

693 views Asked by At

I have an Android app with a Subscription, and I use the IabHelper to query if the user has an active subscription. Now I also need to know whether or not this subscription is auto-renewing.

I am querying the subscription status like this:

private boolean userIsSubscribed() {
    ArrayList<String> skuList = new ArrayList<>();
    skuList.add(PREMIUM_SUBSCRIPTION);
    // Check the subscription status
    try {
        Inventory inventory = iabHelper.queryInventory(true, null, skuList);
        Purchase purchase = inventory.getPurchase(PREMIUM_SUBSCRIPTION);
        // Is the user subscribed?
        if (purchase != null && purchase.getPurchaseState() == 0) {
            // User is subscribed
            Log.d(TAG, "Subscribed.");
            // TODO check if subscription is auto-renewing
            return true;
        } else {
            // User is not subscribed
            Log.d(TAG, "Not subscribed.");
            return false;
        }
    } catch (IabException | NullPointerException e) {
        // There was an error with the subscription process
        Log.e(TAG, e.getMessage(), e);
        return false;
    } catch (IllegalStateException e) {
        // IabHelper not set up
        Log.e(TAG, e.getMessage(), e);
        return false;
    }
}

The Purchase object does not have a method to ask for the autoRenewal field, unfortunately. Is there any other way I can get this information?

1

There are 1 answers

0
petey On BEST ANSWER

Call Purchase#isAutoRenewing method. If this method is missing, you may need to update your reference IAB files:

    if (purchase != null && purchase.getPurchaseState() == 0) {
        // User is subscribed
        Log.d(TAG, "Subscribed.");
        if (purchase.isAutoRenewing()){
            return true;
        }

https://github.com/googlesamples/android-play-billing/blob/master/TrivialDrive/app/src/main/java/com/example/android/trivialdrivesample/util/Purchase.java