Question

How to get the free trial days of a subscription in Kotlin?

  • 3 November 2023
  • 2 replies
  • 41 views

Badge +2

Hello,

 

In the latest SDK 7.1.0, how can I obtain the days of a free trial?  Currently I’m hardcoding it like this to “3 Days Free Trial”:

 

private fun fillTrialOrNormal(it: Package) {
if (isAdded) {
val hasFreeTrial = it.product.subscriptionOptions?.freeTrial != null
Utilities().checkTrialUsedUp(it.product.id, hasFreeTrial) { trialUsedUp ->
val title: String
val header: String
if (trialUsedUp) {
title = "${it.product.price.formatted} / Year"
header = getString(R.string.upgrade_to_premium)
binding.premiumTextId.text = header
} else {
title =
"Start Your 3 Days Free Trial,\nThen ${it.product.price.formatted} / Year"
}
binding.trialPurchaseButtonId.text = title
binding.trialPurchaseButtonId.isEnabled = true
}
}
}

Many Thanks


2 replies

Userlevel 3
Badge +8

Hi!

You should be able to get that from the free trial property on SubscriptionOptions

Badge +2

Yes, thank you. I ended up doing it like this. However I’m unsure if I handle the unit correctly. As I didn’t find a better way to check if the unit is day or month.

 

val unit = it.product.subscriptionOptions?.freeTrial?.freePhase?.billingPeriod?.unit

val numberOfValues = it.product.subscriptionOptions?.freeTrial?.freePhase?.billingPeriod?.value

val title: String
val header: String
var unitString = ""
if (unit == Period.Unit.DAY) {
unitString = "day"
} else if (unit == Period.Unit.MONTH) {
unitString = "month"
}
title = "Start Your $numberOfValues $unitString Free Trial,\nThen ${it.product.price.formatted} / Year"

binding.trialPurchaseButtonId.text = title

 

Reply