Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to check if user have active subscription or not

I'm using Google Billing and trying to understand if subscription is expired or not with two solutions:

  1. billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) { billingResult, purchasesList -> }

  2. val purchasesList = billingClient.queryPurchases(BillingClient.SkuType.SUBS).purchasesList

And they returns something like that:

{
  "productId": "weekly_with_trial",
  "purchaseToken": "someToken",
  "purchaseTime": 1563305764597,
  "developerPayload": null
}
{
  "orderId": "GPA.3380-7704-5235-01361",
  "packageName": "com.myapp",
  "productId": "weekly_trial_trial",
  "purchaseTime": 1563305764597,
  "purchaseState": 0,
  "purchaseToken": "someToken",
  "autoRenewing": false,
  "acknowledged": false
}

Both of them return me a list of purchases without any info about if it expired or not. How to check it ?

like image 788
VLeonovs Avatar asked Dec 03 '22 18:12

VLeonovs


1 Answers

As the documentation of Google play billing clearly suggest's that there are two function to get Purchases List

val purchasesResult: PurchasesResult =
        billingClient.queryPurchases(SkuType.INAPP)

When you do a query purchase only active subscriptions appear on this list. As long as the in-app product is on this list, the user should have access to it.

This means that if the purchase is expired then you will not get that purchase item in the response for queryPurchase. However you have to keep in mind that it returns the local cached result of google play store.

So in order to get the purchase request from network call you need to use an asynchronous method, which is queryPurchaseHistoryAsync()

billingClient.queryPurchaseHistoryAsync(SkuType.INAPP, { billingResult, purchasesList ->
   if (billingResult.responseCode == BillingResponse.OK && purchasesList != null) {
       for (purchase in purchasesList) {
           // Process the result.
       }
   }
})

However there is a catch in object that contains info about the most recent purchase made by the user for each product ID, even if that purchase is expired, cancelled, or consumed.

So you can use queryPurchase() to get all active purchases.

like image 74
Rajat Beck Avatar answered Dec 06 '22 12:12

Rajat Beck