This repository was archived by the owner on Oct 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSubscriptionFlowScreen.kt
More file actions
874 lines (812 loc) · 47.2 KB
/
SubscriptionFlowScreen.kt
File metadata and controls
874 lines (812 loc) · 47.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
package dev.hyo.martie.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.foundation.BorderStroke
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import android.app.Activity
import android.content.Context
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import dev.hyo.martie.models.AppColors
import dev.hyo.martie.IapConstants
import dev.hyo.martie.screens.uis.*
import dev.hyo.openiap.IapContext
import dev.hyo.openiap.ProductAndroid
import dev.hyo.openiap.ProductQueryType
import dev.hyo.openiap.ProductType
import dev.hyo.openiap.ProductSubscriptionAndroid
import dev.hyo.openiap.PurchaseAndroid
import dev.hyo.openiap.PurchaseState
import dev.hyo.openiap.store.OpenIapStore
import dev.hyo.openiap.store.PurchaseResultStatus
import dev.hyo.openiap.OpenIapError
import dev.hyo.openiap.ProductRequest
import dev.hyo.openiap.RequestPurchaseProps
import dev.hyo.openiap.RequestPurchaseAndroidProps
import dev.hyo.openiap.RequestPurchasePropsByPlatforms
import dev.hyo.openiap.RequestSubscriptionAndroidProps
import dev.hyo.openiap.RequestSubscriptionPropsByPlatforms
import dev.hyo.openiap.AndroidSubscriptionOfferInput
import kotlinx.coroutines.launch
import kotlinx.coroutines.delay
import dev.hyo.martie.util.findActivity
import dev.hyo.martie.util.PREMIUM_SUBSCRIPTION_PRODUCT_ID
import dev.hyo.martie.util.SUBSCRIPTION_PREFS_NAME
import dev.hyo.martie.util.resolvePremiumOfferInfo
import dev.hyo.martie.util.savePremiumOffer
// Google Play Billing SubscriptionReplacementMode values
private object ReplacementMode {
const val WITHOUT_PRORATION = 1 // No proration
const val CHARGE_PRORATED_PRICE = 2 // Charge prorated amount immediately
const val DEFERRED = 3 // Change takes effect at next billing cycle
const val WITH_TIME_PRORATION = 4 // Time-based proration
const val CHARGE_FULL_PRICE = 5 // Charge full price immediately
}
// Helper to format remaining time like "3d 4h" / "2h 12m" / "35m"
private fun formatRemaining(deltaMillis: Long): String {
if (deltaMillis <= 0) return "0m"
val totalMinutes = deltaMillis / 60000
val days = totalMinutes / (60 * 24)
val hours = (totalMinutes % (60 * 24)) / 60
val minutes = totalMinutes % 60
return when {
days > 0 -> "${days}d ${hours}h"
hours > 0 -> "${hours}h ${minutes}m"
else -> "${minutes}m"
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SubscriptionFlowScreen(
navController: NavController,
storeParam: OpenIapStore? = null
) {
val context = LocalContext.current
val activity = remember(context) { context.findActivity() }
val uiScope = rememberCoroutineScope()
val appContext = remember(context) { context.applicationContext }
// SharedPreferences to track current offer (necessary since Google doesn't provide offer info)
val prefs = remember { context.getSharedPreferences(SUBSCRIPTION_PREFS_NAME, Context.MODE_PRIVATE) }
val iapStore = storeParam ?: remember(appContext) {
val storeKey = dev.hyo.martie.BuildConfig.OPENIAP_STORE
val appId = dev.hyo.martie.BuildConfig.HORIZON_APP_ID
runCatching { OpenIapStore(appContext, storeKey, appId) }
.getOrElse { OpenIapStore(appContext, "auto", appId) }
}
val products by iapStore.products.collectAsState()
val subscriptions by iapStore.subscriptions.collectAsState()
val purchases by iapStore.availablePurchases.collectAsState()
val androidProducts = remember(products) { products.filterIsInstance<ProductAndroid>() }
val androidSubscriptions = remember(subscriptions) { subscriptions.filterIsInstance<ProductSubscriptionAndroid>() }
val androidPurchases = remember(purchases) { purchases.filterIsInstance<PurchaseAndroid>() }
val status by iapStore.status.collectAsState()
val connectionStatus by iapStore.connectionStatus.collectAsState()
val lastPurchase by iapStore.currentPurchase.collectAsState(initial = null)
val lastPurchaseAndroid: PurchaseAndroid? = remember(lastPurchase) {
when (val purchase = lastPurchase) {
is PurchaseAndroid -> purchase
else -> null
}
}
// Real-time subscription status (expiry/renewal). This requires server validation.
data class SubscriptionUiInfo(
val renewalDate: Long? = null, // expiryTimeMillis
val autoRenewing: Boolean = true,
val gracePeriodEndDate: Long? = null,
val freeTrialEndDate: Long? = null
)
var subStatus by remember { mutableStateOf<Map<String, SubscriptionUiInfo>>(emptyMap()) }
var now by remember { mutableStateOf(System.currentTimeMillis()) }
// Load subscription data on screen entry
LaunchedEffect(Unit) {
println("SubscriptionFlow: Loading subscription products and purchases")
iapStore.setActivity(activity)
// Get fresh purchases first
iapStore.getAvailablePurchases(null)
delay(500)
// Fetch products
val request = ProductRequest(
skus = IapConstants.SUBS_SKUS,
type = ProductQueryType.Subs
)
iapStore.fetchProducts(request)
// Log current state
val currentPurchases = iapStore.availablePurchases.value
println("SubscriptionFlow: Found ${currentPurchases.size} purchases")
currentPurchases.forEach { purchase ->
if (purchase is PurchaseAndroid) {
println(" - ${purchase.productId}: state=${purchase.purchaseState}")
}
}
}
// Tick clock to update countdown once per second
LaunchedEffect(Unit) {
while (true) {
now = System.currentTimeMillis()
kotlinx.coroutines.delay(1000)
}
}
// TODO: Replace with your backend call to Play Developer API
suspend fun fetchSubStatusFromServer(productId: String, purchaseToken: String): SubscriptionUiInfo? {
// Expected mapping of your server response (ReceiptValidationResultAndroid)
// return SubscriptionUiInfo(
// renewalDate = result.renewalDate,
// autoRenewing = result.autoRenewing,
// gracePeriodEndDate = result.gracePeriodEndDate,
// freeTrialEndDate = result.freeTrialEndDate,
// )
return null
}
// Refresh server-side status when purchases change
LaunchedEffect(androidPurchases) {
val map = mutableMapOf<String, SubscriptionUiInfo>()
androidPurchases
.filter { it.productId in IapConstants.SUBS_SKUS }
.forEach { purchase ->
val token = purchase.purchaseToken ?: return@forEach
val info = fetchSubStatusFromServer(purchase.productId, token)
if (info != null) {
map[purchase.productId] = info.copy(autoRenewing = purchase.isAutoRenewing)
}
}
subStatus = map
}
val statusMessage = status.lastPurchaseResult
// Modal states
var selectedProduct by remember { mutableStateOf<ProductAndroid?>(null) }
var selectedPurchase by remember { mutableStateOf<PurchaseAndroid?>(null) }
// Initialize and connect on first composition (spec-aligned names)
val startupScope = rememberCoroutineScope()
DisposableEffect(Unit) {
startupScope.launch {
try {
val connected = iapStore.initConnection()
if (connected) {
iapStore.setActivity(activity)
println("SubscriptionFlow: Loading subscription products: ${IapConstants.SUBS_SKUS}")
val request = ProductRequest(
skus = IapConstants.SUBS_SKUS,
type = ProductQueryType.Subs
)
iapStore.fetchProducts(request)
iapStore.getAvailablePurchases(null)
}
} catch (_: Exception) { }
}
onDispose {
// End connection and clear listeners when this screen leaves (per-screen lifecycle)
startupScope.launch {
runCatching { iapStore.endConnection() }
runCatching { iapStore.clear() }
}
}
}
Scaffold(
topBar = {
TopAppBar(
title = { Text("Subscription Flow") },
navigationIcon = {
IconButton(onClick = { navController.navigateUp() }) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
}
},
actions = {
val scope = rememberCoroutineScope()
IconButton(
onClick = {
scope.launch {
try {
iapStore.setActivity(activity)
val request = ProductRequest(
skus = IapConstants.SUBS_SKUS,
type = ProductQueryType.Subs
)
iapStore.fetchProducts(request)
} catch (_: Exception) { }
}
},
enabled = !status.isLoading
) {
Icon(Icons.Default.Refresh, contentDescription = "Refresh")
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = AppColors.background
)
)
}
) { paddingValues ->
val scope = rememberCoroutineScope()
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
.background(AppColors.background),
contentPadding = PaddingValues(vertical = 20.dp),
verticalArrangement = Arrangement.spacedBy(20.dp)
) {
// Header Card
item {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
shape = RoundedCornerShape(12.dp),
colors = CardDefaults.cardColors(containerColor = AppColors.cardBackground),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
Icon(
Icons.Default.Autorenew,
contentDescription = null,
modifier = Modifier.size(48.dp),
tint = AppColors.secondary
)
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
Text(
"Subscription Flow",
style = MaterialTheme.typography.headlineSmall,
fontWeight = FontWeight.Bold
)
Text(
"Manage recurring subscriptions",
style = MaterialTheme.typography.bodySmall,
color = AppColors.textSecondary
)
}
}
Text(
"Purchase and manage auto-renewable subscriptions. View active subscriptions and their renewal status.",
style = MaterialTheme.typography.bodyMedium,
color = AppColors.textSecondary
)
}
}
}
statusMessage?.let { result ->
item("status-message") {
PurchaseResultCard(
message = result.message,
status = result.status,
onDismiss = { iapStore.clearStatusMessage() },
code = result.code
)
}
}
// Loading State
if (status.isLoading) {
item {
LoadingCard()
}
}
// Active Subscriptions Section
// Treat any purchase with matching subscription SKU as subscribed
val activeSubscriptions = androidPurchases.filter { it.productId in IapConstants.SUBS_SKUS }
if (activeSubscriptions.isNotEmpty()) {
item {
SectionHeaderView(title = "Active Subscriptions")
}
items(activeSubscriptions) { subscription ->
val info = subStatus[subscription.productId]
val fmt = java.text.SimpleDateFormat("MMM dd, yyyy HH:mm", java.util.Locale.getDefault())
val statusText = when {
info?.freeTrialEndDate != null ->
"Free trial ends: ${fmt.format(java.util.Date(info.freeTrialEndDate))} (${formatRemaining(info.freeTrialEndDate - now)})"
info?.gracePeriodEndDate != null ->
"Grace period ends: ${fmt.format(java.util.Date(info.gracePeriodEndDate))} (${formatRemaining(info.gracePeriodEndDate - now)})"
info?.renewalDate != null && (info.autoRenewing) ->
"Renews on ${fmt.format(java.util.Date(info.renewalDate))} (${formatRemaining(info.renewalDate - now)})"
info?.renewalDate != null && (!info.autoRenewing) ->
"Expires on ${fmt.format(java.util.Date(info.renewalDate))} (${formatRemaining(info.renewalDate - now)})"
else -> null
}
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
shape = RoundedCornerShape(12.dp),
colors = CardDefaults.cardColors(containerColor = AppColors.cardBackground),
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) {
Column(modifier = Modifier.padding(16.dp)) {
ActiveSubscriptionListItem(
purchase = subscription,
statusText = statusText,
onClick = { selectedPurchase = subscription }
)
// Show upgrade/downgrade option for dev.hyo.martie.premium subscription offers
if (subscription.productId == PREMIUM_SUBSCRIPTION_PRODUCT_ID) {
// Find the subscription product with offers
val premiumSub = androidSubscriptions.find { it.id == PREMIUM_SUBSCRIPTION_PRODUCT_ID }
if (premiumSub != null) {
// Get available offers
val monthlyOffer = premiumSub.subscriptionOfferDetailsAndroid.find {
it.basePlanId == IapConstants.PREMIUM_MONTHLY_BASE_PLAN
}
val yearlyOffer = premiumSub.subscriptionOfferDetailsAndroid.find {
it.basePlanId == IapConstants.PREMIUM_YEARLY_BASE_PLAN
}
// Log purchase details for debugging
println("SubscriptionFlow: Current purchase details - productId: ${subscription.productId}, token: ${subscription.purchaseToken?.take(10)}")
println("SubscriptionFlow: Purchase state: ${subscription.purchaseState}")
// Resolve the active offer for this subscription
val premiumOfferInfo = resolvePremiumOfferInfo(prefs, subscription)
val currentOfferBasePlanId = premiumOfferInfo?.basePlanId
?: IapConstants.PREMIUM_MONTHLY_BASE_PLAN
val currentOfferDisplay = premiumOfferInfo?.displayName ?: when (currentOfferBasePlanId) {
IapConstants.PREMIUM_MONTHLY_BASE_PLAN -> "Monthly Plan (premium)"
IapConstants.PREMIUM_YEARLY_BASE_PLAN -> "Yearly Plan (premium-year)"
else -> "Base Plan: $currentOfferBasePlanId"
}
println(
"SubscriptionFlow: Current offer for ${subscription.productId}: $currentOfferBasePlanId ($currentOfferDisplay)"
)
val currentOffer = when (currentOfferBasePlanId) {
IapConstants.PREMIUM_MONTHLY_BASE_PLAN -> monthlyOffer
IapConstants.PREMIUM_YEARLY_BASE_PLAN -> yearlyOffer
else -> listOfNotNull(monthlyOffer, yearlyOffer)
.firstOrNull { it.basePlanId == currentOfferBasePlanId }
}
val targetOffer = when (currentOfferBasePlanId) {
IapConstants.PREMIUM_MONTHLY_BASE_PLAN -> yearlyOffer
IapConstants.PREMIUM_YEARLY_BASE_PLAN -> monthlyOffer
else -> when (currentOffer) {
monthlyOffer -> yearlyOffer
yearlyOffer -> monthlyOffer
else -> null
}
}
val isMonthly = currentOfferBasePlanId == IapConstants.PREMIUM_MONTHLY_BASE_PLAN
// Display current offer information
Spacer(modifier = Modifier.height(12.dp))
HorizontalDivider(
modifier = Modifier.fillMaxWidth(),
thickness = 1.dp,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.12f)
)
Spacer(modifier = Modifier.height(12.dp))
// Show current active offer
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.Check,
contentDescription = null,
modifier = Modifier.size(20.dp),
tint = AppColors.success
)
Spacer(modifier = Modifier.width(8.dp))
Column {
Text(
text = "Current Plan: $currentOfferDisplay",
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.Medium
)
currentOffer?.let { offer ->
val price = offer.pricingPhases.pricingPhaseList.firstOrNull()?.formattedPrice ?: ""
Text(
text = "Base Plan ID: ${offer.basePlanId} • $price",
style = MaterialTheme.typography.bodySmall,
color = AppColors.textSecondary
)
}
}
Surface(
shape = RoundedCornerShape(6.dp),
color = AppColors.primary.copy(alpha = 0.12f),
modifier = Modifier.padding(start = 12.dp)
) {
Text(
text = currentOfferBasePlanId,
modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp),
style = MaterialTheme.typography.labelSmall,
color = AppColors.primary
)
}
}
// Only show upgrade button if both offers exist
if (monthlyOffer != null && yearlyOffer != null && currentOffer != null && targetOffer != null) {
Spacer(modifier = Modifier.height(12.dp))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Column(modifier = Modifier.weight(1f)) {
Text(
text = if (isMonthly) "Upgrade to Yearly Plan" else "Switch to Monthly Plan",
style = MaterialTheme.typography.titleSmall,
fontWeight = FontWeight.SemiBold
)
val targetPrice = targetOffer.pricingPhases.pricingPhaseList.firstOrNull()?.formattedPrice ?: ""
Text(
text = "${targetOffer.basePlanId} - $targetPrice",
style = MaterialTheme.typography.bodySmall,
color = AppColors.textSecondary
)
if (isMonthly) {
Text(
text = "Save with annual billing",
style = MaterialTheme.typography.bodySmall,
color = AppColors.success
)
}
}
Button(
onClick = {
scope.launch {
try {
iapStore.setActivity(activity)
val purchaseToken = subscription.purchaseToken
if (purchaseToken == null) {
iapStore.postStatusMessage(
message = "Cannot change plan: missing purchase token",
status = PurchaseResultStatus.Error,
productId = PREMIUM_SUBSCRIPTION_PRODUCT_ID
)
return@launch
}
println("SubscriptionFlow: Changing from ${currentOffer.basePlanId} to ${targetOffer.basePlanId} with token: ${purchaseToken.take(10)}...")
// For same subscription with different offers, use CHARGE_FULL_PRICE
// This is often the only supported mode for offer changes
val replacementMode = ReplacementMode.CHARGE_FULL_PRICE
println("SubscriptionFlow: Using replacement mode: $replacementMode")
// Request subscription offer change (same product, different offer)
val offerInputs = listOf(
AndroidSubscriptionOfferInput(
sku = PREMIUM_SUBSCRIPTION_PRODUCT_ID,
offerToken = targetOffer.offerToken
)
)
val props = RequestPurchaseProps(
request = RequestPurchaseProps.Request.Subscription(
RequestSubscriptionPropsByPlatforms(
android = RequestSubscriptionAndroidProps(
isOfferPersonalized = null,
obfuscatedAccountIdAndroid = null,
obfuscatedProfileIdAndroid = null,
purchaseTokenAndroid = purchaseToken,
replacementModeAndroid = replacementMode,
skus = listOf(PREMIUM_SUBSCRIPTION_PRODUCT_ID),
subscriptionOffers = offerInputs
)
)
),
type = ProductQueryType.Subs
)
val result = iapStore.requestPurchase(props)
val purchases = when (result) {
is dev.hyo.openiap.RequestPurchaseResultPurchases -> result.value.orEmpty()
is dev.hyo.openiap.RequestPurchaseResultPurchase -> result.value?.let { listOf(it) }.orEmpty()
else -> emptyList()
}
if (purchases.isNotEmpty()) {
// Save the new offer to SharedPreferences
val newOfferBasePlanId = targetOffer.basePlanId
prefs.savePremiumOffer(PREMIUM_SUBSCRIPTION_PRODUCT_ID, newOfferBasePlanId)
println("SubscriptionFlow: Subscription change successful, saved offer: $newOfferBasePlanId")
iapStore.postStatusMessage(
message = if (isMonthly) "Upgraded to yearly plan successfully" else "Switched to monthly plan",
status = PurchaseResultStatus.Success,
productId = PREMIUM_SUBSCRIPTION_PRODUCT_ID
)
// Immediately refresh purchases
iapStore.getAvailablePurchases(null)
// Also refresh after a delay to catch any delayed updates
delay(2000)
iapStore.getAvailablePurchases(null)
// Refresh products to update subscription status
scope.launch {
val request = ProductRequest(
skus = IapConstants.SUBS_SKUS,
type = ProductQueryType.Subs
)
iapStore.fetchProducts(request)
}
}
} catch (e: Exception) {
println("SubscriptionFlow: Error changing subscription: ${e.message}")
e.printStackTrace()
// If upgrade fails, show more helpful message
val errorMessage = when {
e.message?.contains("replacement mode") == true ->
"Subscriptions may not be in the same group. Contact support."
e.message?.contains("DEVELOPER_ERROR") == true ->
"Invalid subscription configuration. Check Play Console settings."
else ->
"Subscription change failed: ${e.message}"
}
iapStore.postStatusMessage(
message = errorMessage,
status = PurchaseResultStatus.Error,
productId = PREMIUM_SUBSCRIPTION_PRODUCT_ID
)
}
}
},
colors = ButtonDefaults.buttonColors(
containerColor = if (isMonthly) AppColors.success else AppColors.secondary
),
enabled = !status.isPurchasing(PREMIUM_SUBSCRIPTION_PRODUCT_ID)
) {
if (status.isPurchasing(PREMIUM_SUBSCRIPTION_PRODUCT_ID)) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
color = MaterialTheme.colorScheme.onPrimary,
strokeWidth = 2.dp
)
} else {
Text(if (isMonthly) "Upgrade" else "Switch")
}
}
}
}
}
}
}
}
}
}
// Available Subscription Products
if (androidProducts.isNotEmpty()) {
item {
SectionHeaderView(title = "Available Subscriptions")
}
items(androidProducts) { product ->
ProductCard(
product = product,
isPurchasing = status.isPurchasing(product.id),
isSubscribed = androidPurchases.any { it.productId == product.id && it.purchaseState == PurchaseState.Purchased },
onPurchase = {
// Prevent re-purchase if already subscribed
val alreadySubscribed = androidPurchases.any { it.productId == product.id && it.purchaseState == PurchaseState.Purchased }
if (alreadySubscribed) {
iapStore.postStatusMessage(
message = "Already subscribed to ${product.id}",
status = PurchaseResultStatus.Info,
productId = product.id
)
return@ProductCard
}
scope.launch {
iapStore.setActivity(activity)
val props = if (product.type == ProductType.Subs) {
RequestPurchaseProps(
request = RequestPurchaseProps.Request.Subscription(
RequestSubscriptionPropsByPlatforms(
android = RequestSubscriptionAndroidProps(
isOfferPersonalized = null,
obfuscatedAccountIdAndroid = null,
obfuscatedProfileIdAndroid = null,
purchaseTokenAndroid = null,
replacementModeAndroid = null,
skus = listOf(product.id),
subscriptionOffers = null
)
)
),
type = ProductQueryType.Subs
)
} else {
RequestPurchaseProps(
request = RequestPurchaseProps.Request.Purchase(
RequestPurchasePropsByPlatforms(
android = RequestPurchaseAndroidProps(
isOfferPersonalized = null,
obfuscatedAccountIdAndroid = null,
obfuscatedProfileIdAndroid = null,
skus = listOf(product.id)
)
)
),
type = ProductQueryType.InApp
)
}
val result = iapStore.requestPurchase(props)
val purchases = when (result) {
is dev.hyo.openiap.RequestPurchaseResultPurchases -> result.value.orEmpty()
is dev.hyo.openiap.RequestPurchaseResultPurchase -> result.value?.let { listOf(it) }.orEmpty()
else -> emptyList()
}
// If this is a new subscription to dev.hyo.martie.premium, save the offer
if (purchases.isNotEmpty() && product.id == PREMIUM_SUBSCRIPTION_PRODUCT_ID) {
// Default to monthly offer for new purchases
// In a production app, you'd want to let the user select the offer
val defaultOffer = IapConstants.PREMIUM_MONTHLY_BASE_PLAN
prefs.savePremiumOffer(PREMIUM_SUBSCRIPTION_PRODUCT_ID, defaultOffer)
println("SubscriptionFlow: New subscription purchase completed, saved offer: $defaultOffer")
}
}
},
onClick = {
selectedProduct = product
},
onDetails = {
selectedProduct = product
}
)
}
} else if (!status.isLoading && androidProducts.isEmpty()) {
item {
EmptyStateCard(
message = "No subscription products available",
icon = Icons.Default.Autorenew
)
}
}
// Subscription Management Info
item {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
shape = RoundedCornerShape(12.dp),
colors = CardDefaults.cardColors(
containerColor = AppColors.info.copy(alpha = 0.1f)
)
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Icon(
Icons.Default.Info,
contentDescription = null,
tint = AppColors.info
)
Text(
"Subscription Management",
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.SemiBold
)
}
Text(
"• Subscriptions auto-renew until cancelled\n" +
"• Manage subscriptions in Google Play Store\n" +
"• Cancellation takes effect at the end of the current billing period\n" +
"• Family sharing may be available for some subscriptions",
style = MaterialTheme.typography.bodySmall,
color = AppColors.textSecondary
)
}
}
}
}
}
// Auto-handle purchase: validate on server then finish (expo-iap style)
// IMPORTANT: Implement real server-side receipt validation in validateReceiptOnServer()
suspend fun validateReceiptOnServer(purchase: PurchaseAndroid): Boolean {
// TODO: Replace with your real backend API call
// e.g., POST purchase.purchaseToken to your server and verify
return true
}
LaunchedEffect(lastPurchaseAndroid?.id) {
val purchase = lastPurchaseAndroid ?: return@LaunchedEffect
try {
// 1) Server-side validation (replace with your backend call)
val valid = validateReceiptOnServer(purchase)
if (!valid) {
iapStore.postStatusMessage(
message = "Receipt validation failed",
status = PurchaseResultStatus.Error,
productId = purchase.productId
)
return@LaunchedEffect
}
// 2) Determine consumable vs non-consumable (subs -> false)
val product = products.find { it.id == purchase.productId }
val isConsumable = product?.let {
it.type == ProductType.InApp &&
(it.id.contains("consumable", true) || it.id.contains("bulb", true))
} == true
// 3) Ensure connection (quick retry)
if (!connectionStatus) {
runCatching { iapStore.initConnection() }
val started = System.currentTimeMillis()
while (!iapStore.isConnected.value && System.currentTimeMillis() - started < 1500) {
kotlinx.coroutines.delay(100)
}
}
// 4) Finish transaction
try {
iapStore.finishTransaction(purchase, isConsumable)
iapStore.getAvailablePurchases(null)
iapStore.postStatusMessage(
message = "Transaction finished successfully",
status = PurchaseResultStatus.Success,
productId = purchase.productId
)
} catch (e: Exception) {
iapStore.postStatusMessage(
message = "finishTransaction failed: ${e.message}",
status = PurchaseResultStatus.Error,
productId = purchase.productId
)
}
} catch (e: Exception) {
iapStore.postStatusMessage(
message = e.message ?: "Failed to finish purchase",
status = PurchaseResultStatus.Error,
productId = purchase.productId
)
}
}
// Product Detail Modal
selectedProduct?.let { product ->
ProductDetailModal(
product = product,
onDismiss = { selectedProduct = null },
onPurchase = {
uiScope.launch {
iapStore.setActivity(activity)
val props = if (product.type == ProductType.Subs) {
RequestPurchaseProps(
request = RequestPurchaseProps.Request.Subscription(
RequestSubscriptionPropsByPlatforms(
android = RequestSubscriptionAndroidProps(
isOfferPersonalized = null,
obfuscatedAccountIdAndroid = null,
obfuscatedProfileIdAndroid = null,
purchaseTokenAndroid = null,
replacementModeAndroid = null,
skus = listOf(product.id),
subscriptionOffers = null
)
)
),
type = ProductQueryType.Subs
)
} else {
RequestPurchaseProps(
request = RequestPurchaseProps.Request.Purchase(
RequestPurchasePropsByPlatforms(
android = RequestPurchaseAndroidProps(
isOfferPersonalized = null,
obfuscatedAccountIdAndroid = null,
obfuscatedProfileIdAndroid = null,
skus = listOf(product.id)
)
)
),
type = ProductQueryType.InApp
)
}
iapStore.requestPurchase(props)
}
},
isPurchasing = status.isPurchasing(product.id)
)
}
// Purchase Detail Modal
selectedPurchase?.let { purchase ->
PurchaseDetailModal(
purchase = purchase,
onDismiss = { selectedPurchase = null }
)
}
}
// Moved to reusable component at: screens/uis/ActiveSubscriptionListItem.kt