Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the In app subscription trail period programmatically

I am working in an android application where I want to implement inapp billing subscription. I have created a subscription ID in the Google Play Developer Console with a trial period of seven days.

My requirement is to notify the user each time with the remaining days left for subscription when the app launches. So how can I get the trial period left for subscription programmatically. Is this the correct way to implement inapp billing subscription with trial period, if no please suggest me the correct way to implement this.

like image 720
Arun Avatar asked May 22 '13 10:05

Arun


1 Answers

There is querySkuDetailsAsync() method in BillingClient that retrieves a list of SkuDetails that contains the getFreeTrialPeriod() method. The method returns the trial period in ISO 8601 format. For example, P7D equates to seven days.

val skus = listOf(
    "my-first-trial-subscription-sku",
    "my-second-trial-subscription-sku"
)

val params = SkuDetailsParams
    .newBuilder()
    .setType(BillingClient.SkuType.SUBS)
    .setSkusList(skus)
    .build()

billingClient.querySkuDetailsAsync(params) { billingResult, skuDetailsList ->
    skuDetailsList.forEach {
        Log.i("test", "${it.sku} trial is ${it.freeTrialPeriod}")
    }
}

The ISO 8601 period can be parsed using java.time.Period.parse() method which is available since API 26. On older devices you can use ThreeTenABP library which contains the same Period.parse() method.

A purchase time is also available via Purchase.getPurchaseTime() method which returns unix timestamp in milliseconds. You can calculate when the trial ends by adding the trial duration to the purchase time.

And the last thing you should consider - whether the trial is available or not. The trial won't be available if the user has already spent it. There's paymentState property in SubscriptionPurchase resource in Google Play Developer API. But it seems there is no way to check it via billingclient library. See this question fore more details.

like image 61
Valeriy Katkov Avatar answered Sep 22 '22 19:09

Valeriy Katkov