Get the In app subscription trail period programmatically
Asked Answered
T

2

12

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.

Tomcat answered 22/5, 2013 at 10:40 Comment(1)
How you done it,could you add solution for this ,Thank you.Condorcet
E
3

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.

Etymologize answered 9/9, 2019 at 9:5 Comment(4)
SkuDetail is showing the original trial period only, so how to get the trail period left?Hellebore
@Hellebore I added more details to the answer, hope it helps you!Etymologize
Thanks. The situation of previously used trial should be handled too.Hellebore
@Hellebore Indeed, and it seems to be the most difficult part of the problem. Now I've mentioned it in the answer. Thanks for pointing it out!Etymologize
S
0

Yes, In-app billing API version 3 supports a trial period! That I couldn't find whether there is an API call to give the kind of details on the trial period you need in the documentation doesn't mean there isn't such a class/method.

However, relying only on the remote Google service for the subscription status means the user will not be notified when not connected to a network. Maybe lack of notification when not on a network is not a problem. But, knowing of this limitation, I would record and keep the start date locally, as well.

Once you have captured the results of the API call that allows the user to start the trial period, you can save that data locally. There are three 'easy' ways to keep local data: via preferences (easiest), write a local file (a little harder), or setup and populate an sqlite db (yet, even harder). I use preferences all the time and is quite easy.

If you want to make the 'local' data accessible to multiple Android devices (if your subscription scheme supports multiple devices), you will need to PUT this data to a remote server, instead. And, GET the data at activity start.

Keeping it simple - data on a local device - read the local preference to see when the trial started, calculate the number of days remaining, and present a message. Also use the Google API call to confirm the subscription status. In case of a conflict, update the local preference, accordingly.

With this strategy, the user can be notified at application/activity launch on the trial period time remaining without using a Google API to get details of the trial period; and as a bonus, regardless of whether the app user is on a network, or not.

Sacaton answered 23/5, 2013 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.