package com.jeremybarbet.reactnativepurchasekit import com.android.billingclient.api.ProductDetails data class PurchaseKitPeriod( val value: Int, val unit: String, val localized: String, ) data class PurchaseKitSubscription( val period: PurchaseKitPeriod?, ) private fun parsedLocalized(value: Int, unit: String): String { return when (unit) { "P1W" -> if (value > 1) "/week for $value weeks" else "/week" "P1M" -> if (value > 1) "/month for $value months" else "/month" "P3M" -> if (value > 1) "/3 months, $value times" else "/3 months" "P6M" -> if (value > 1) "/6 months, $value times" else "/6 months" "P1Y" -> if (value > 1) "/year for $value years" else "/year" else -> "—" } } fun parseSubscription(product: ProductDetails): PurchaseKitSubscription { val details = product.subscriptionOfferDetails!![0].pricingPhases.pricingPhaseList[0] val value = details.billingCycleCount val unit = details.billingPeriod val period = PurchaseKitPeriod( value, unit, parsedLocalized(value, unit) ) return PurchaseKitSubscription( period, ) }