How to check purchase state of subscription in all new Play Billing Library?

jomin v george picture jomin v george · Dec 31, 2017 · Viewed 7.2k times · Source

Hi am using new Play Billing Library for in app subscriptions. https://developer.android.com/google/play/billing/billing_library.html

Everthing works perfect and am able to subscribe the product. But i didn't find any ways to check the purchase state of subscription. I mean whether it is active or expired.

In the documentation it says : You can then call a variety of methods on the Purchase object to view relevant information about the item, such as its purchase state or time.

But there is no purchase state method in Purchase Object, check link below https://developer.android.com/reference/com/android/billingclient/api/Purchase.html

Take a look at this video too : https://www.youtube.com/watch?v=9chvh1WYCvw&feature=youtu.be&t=301

How is it possible to check the purchase state?

Answer

cbn picture cbn · Jul 31, 2018

This is an old question and you may have solved it. I've just started using the new library and I have the same question. Reading the javadoc for the Purchase class I think you have to use:

isAutoRenewing()

This will return false for any subscription that has been cancelled. It returns true for any subscription that is active or is in the grace period.

Just an update to this. This won't work in the case of a subscription that has been cancelled and has not expired.

I found it odd that the above commentators seem to be in disagreement. Further investigation reveals that the two different queries for purchases give different results - something that the javadoc doesn't seem to make clear.

queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS, listener)

will, as its javadoc explains, return everything - including all expired subscriptions. Using this there doesn't seem to be a way to differentiate between active and inactive subscriptions. This returns the history as it currently is on Google servers.

queryPurchases(BillingClient.SkuType.SUBS)

returns only the current active subscriptions. It returns a cached result immediately.