I'm using Google Billing
and trying to understand if subscription is expired or not with two solutions:
billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) { billingResult, purchasesList -> }
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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With