Show verified store trials in the native cloud purchase flow [skip ci]

This commit is contained in:
Maurice
2026-12-02 10:20:26 +01:00
parent dbcdecb6a7
commit 00fcb69423
36 changed files with 440 additions and 61 deletions
@@ -102,21 +102,61 @@ object TrekBilling {
return offers.firstOrNull { it.offerId == null } ?: offers.firstOrNull()
}
/** Play returns eligible offers. The named offer must be configured with Play's new-customer eligibility. */
private fun trialOffer(product: ProductDetails): ProductDetails.SubscriptionOfferDetails? {
val base = baseOffer(product)?.takeIf { it.offerId == null } ?: return null
val recurring = base.pricingPhases.pricingPhaseList.singleOrNull() ?: return null
return product.subscriptionOfferDetails?.firstOrNull { offer ->
offer.offerId == TRIAL_OFFER_ID && offer.basePlanId == base.basePlanId &&
isSevenDayTrial(offer.pricingPhases.pricingPhaseList, recurring)
}
}
private fun isSevenDayTrial(
phases: List<ProductDetails.PricingPhase>,
recurring: ProductDetails.PricingPhase,
): Boolean {
if (phases.size != 2) return false
val (trial, renewal) = phases
val once = trial.recurrenceMode == ProductDetails.RecurrenceMode.NON_RECURRING ||
(trial.recurrenceMode == ProductDetails.RecurrenceMode.FINITE_RECURRING && trial.billingCycleCount == 1)
return once && trial.priceAmountMicros == 0L && trial.billingPeriod in listOf("P1W", "P7D") &&
renewal.recurrenceMode == ProductDetails.RecurrenceMode.INFINITE_RECURRING &&
renewal.billingPeriod == recurring.billingPeriod && renewal.priceAmountMicros == recurring.priceAmountMicros &&
renewal.priceCurrencyCode == recurring.priceCurrencyCode
}
fun products(context: Context, call: PluginCall) {
val ids = call.getArray("ids")?.toList<String>().orEmpty()
ready(context) { ok ->
val result = JSObject().put("sandbox", BuildConfig.DEBUG)
if (!ok) return@ready call.resolve(result.put("products", JSArray()))
if (!ok || ids.isEmpty()) return@ready call.resolve(result.put("products", JSArray()))
details(context, ids) { found ->
val list = JSArray()
for (product in found) displayPrice(product)?.let { list.put(JSObject().put("id", product.productId).put("displayPrice", it)) }
call.resolve(result.put("products", list))
val candidates = found.filter { trialOffer(it) != null }.map { it.productId }.toSet()
if (candidates.isEmpty()) {
call.resolve(result.put("products", productList(found, emptySet())))
return@details
}
val params = QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build()
client(context).queryPurchasesAsync(params) { status, purchases ->
val eligible = status.responseCode == BillingClient.BillingResponseCode.OK && purchases.none { purchase ->
purchase.products.any { it.startsWith(PREFIX) }
}
call.resolve(result.put("products", productList(found, if (eligible) candidates else emptySet())))
}
}
}
}
private fun productList(products: List<ProductDetails>, freeTrials: Set<String>): JSArray = JSArray().apply {
for (product in products) displayPrice(product)?.let { price ->
put(JSObject().put("id", product.productId).put("displayPrice", price).put("freeTrial", product.productId in freeTrials))
}
}
fun purchase(activity: Activity, call: PluginCall) {
val id = call.getString("productId") ?: return call.reject("productId is required")
val trialRenewalPrice = call.getString("trialRenewalPrice")
ready(activity) { ok ->
if (!ok) return@ready call.resolve(JSObject().put("kind", "failed"))
// One subscription per Google account: a second plan would run beside the first.
@@ -126,19 +166,28 @@ object TrekBilling {
client(activity).queryPurchasesAsync(params) { result, purchases ->
val held = if (result.responseCode == BillingClient.BillingResponseCode.OK) newest(purchases, PREFIX) else null
when {
held == null -> launchPurchase(activity, call, id)
held.isAcknowledged -> call.resolve(JSObject().put("kind", "subscribed"))
else -> call.resolve(receipt(held).put("kind", "bought"))
held?.isAcknowledged == true -> call.resolve(JSObject().put("kind", "subscribed"))
held != null -> call.resolve(receipt(held).put("kind", "bought"))
trialRenewalPrice != null && result.responseCode != BillingClient.BillingResponseCode.OK ->
call.resolve(JSObject().put("kind", "offer-changed"))
purchases.any { p -> p.purchaseState == Purchase.PurchaseState.PENDING && p.products.any { it.startsWith(PREFIX) } } ->
call.resolve(JSObject().put("kind", "pending"))
else -> launchPurchase(activity, call, id, trialRenewalPrice)
}
}
}
}
private fun launchPurchase(activity: Activity, call: PluginCall, id: String) {
private fun launchPurchase(activity: Activity, call: PluginCall, id: String, trialRenewalPrice: String?) {
details(activity, listOf(id)) { found ->
val product = found.firstOrNull()
val offer = product?.let(::baseOffer)
if (product == null || offer == null) return@details call.resolve(JSObject().put("kind", "failed"))
val offer = product?.let { listed ->
if (trialRenewalPrice == null) baseOffer(listed)
else trialOffer(listed)?.takeIf { displayPrice(listed) == trialRenewalPrice }
}
if (product == null || offer == null) {
return@details call.resolve(JSObject().put("kind", if (trialRenewalPrice != null) "offer-changed" else "failed"))
}
pendingPurchase?.resolve(JSObject().put("kind", "cancelled"))
pendingPurchase = call
pendingProduct = id
@@ -275,6 +324,7 @@ object TrekBilling {
newest(purchases.filter { !it.isAcknowledged }, prefix)
private const val PREFIX = "cloud."
private const val TRIAL_OFFER_ID = "trek-trial-7-days"
internal fun newest(purchases: List<Purchase>, prefix: String): Purchase? = purchases
.filter { p -> p.purchaseState == Purchase.PurchaseState.PURCHASED && p.products.any { it.startsWith(prefix) } }