Skip to main content
Question

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


Forum|alt.badge.img+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

Ryan Glanz
RevenueCat Staff
Forum|alt.badge.img+8
  • RevenueCat Staff
  • 383 replies
  • November 7, 2023

Hi!

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


Forum|alt.badge.img+2
  • Author
  • New Member
  • 3 replies
  • November 7, 2023

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings