globalesim: an orphaned card-payment secret no longer wedges top-up
- Commit
3d759b45fa40e3bc3e54f5f2884cefdb73476344- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 22:13 UTC (5 days ago)
- Parents
9901081
Dismissing the card sheet during the /stripe-intent fetch orphaned pendingClientSecret: the secret arrived after the card state was gone, was consumed as 'presented' without ever confirming, and nothing cleared it. HomeScreen keeps both Card and Crypto buttons disabled while any secret is pending, and every clearer was unreachable from that state โ top-up stayed dead until process restart (the same orphan occurs on recreation mid-fetch: the card is remember, the presented-secret marker is rememberSaveable). A secret arriving with no card/launcher to confirm it now goes through finishCardPurchase(false) (clears secret/loading without starting a credit watch), and the sheet refuses dismissal while the secret fetch is in flight. Regression tests assert the abandon fires exactly once and the hoisted-presenter path still presents. Activation codes, quote math, card validation and credit watching were audited and cleared. Verified: :globalesim:testDevUnitTest 62 tests pass; lint 0 errors.
Diff
globalesim/src/main/java/com/vayunmathur/globalesim/Navigation.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/Navigation.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/Navigation.ktindex ffe8b6302b..89f044bd77 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/Navigation.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/Navigation.kt@@ -57,6 +57,7 @@ fun Navigation(viewModel: EsimViewModel, launchers: CardPaymentLaunchers? = null onClearCryptoInvoice = viewModel::clearPendingInvoice, onClaim = viewModel::claim, onRefresh = viewModel::refresh,+ onAbandonCardPurchase = { viewModel.finishCardPurchase(false) }, ) } entry<Route.Pricing>(metadata = SiblingPage()) {
globalesim/src/main/java/com/vayunmathur/globalesim/ui/CardPaymentHost.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/ui/CardPaymentHost.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/ui/CardPaymentHost.ktindex 118940a508..225752de1f 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/ui/CardPaymentHost.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/ui/CardPaymentHost.kt@@ -46,6 +46,15 @@ fun CardPaymentHost( onSheetAmountConsumed: () -> Unit, onStartCardPurchase: (amountCents: Long, publishableKey: String?, onKey: (String) -> Unit) -> Unit, onPresentPaymentSheet: ((secret: String) -> Unit)? = null,+ /**+ * A pending secret arrived with no card (and no launcher) left to+ * confirm it โ e.g. the sheet was dismissed or recreated while the+ * secret was being fetched. The secret is abandoned through here so+ * the ViewModel can clear it; leaving it pending would keep every+ * top-up rail disabled (`busy` includes a pending secret) with no+ * reachable action that clears it.+ */+ onAbandonCardPurchase: () -> Unit = {}, ) { val context = LocalContext.current val keyStore = remember { StripeKeyStore(context) }@@ -81,6 +90,12 @@ fun CardPaymentHost( ) confirming = true paymentLauncher.confirm(confirmParams)+ } else {+ // No card/launcher can ever confirm this secret now.+ // Abandon it instead of consuming it as "presented":+ // an orphaned pending secret disables both top-up+ // rails in TopUpCard with nothing left to clear it.+ onAbandonCardPurchase() } } }@@ -107,7 +122,11 @@ fun CardPaymentHost( } }, onDismiss = {- if (!confirming && state.pendingClientSecret == null) {+ // Never dismiss while a secret fetch is in flight: the+ // guard below would pass (secret still null, not yet+ // confirming) but the arriving secret would have no card+ // left to confirm it. Busy already disables the fields.+ if (!confirming && !state.loading && state.pendingClientSecret == null) { pendingCard = null onDismissSheet() }
globalesim/src/main/java/com/vayunmathur/globalesim/ui/HomeScreen.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/ui/HomeScreen.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/ui/HomeScreen.ktindex 9ece424829..c3a46fcd19 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/ui/HomeScreen.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/ui/HomeScreen.kt@@ -64,6 +64,8 @@ fun HomeScreen( * [launchers]; previews pass neither and render launcher-free. */ onPresentPaymentSheet: ((secret: String) -> Unit)? = null,+ /** Clear a pending card secret that nothing can confirm anymore. */+ onAbandonCardPurchase: () -> Unit = {}, ) { val messenger = rememberMessenger() val context = LocalContext.current@@ -77,6 +79,7 @@ fun HomeScreen( onSheetAmountConsumed = { sheetAmountCents = null }, onStartCardPurchase = onStartCardPurchase, onPresentPaymentSheet = onPresentPaymentSheet,+ onAbandonCardPurchase = onAbandonCardPurchase, ) AppScaffold( title = stringResource(R.string.purchase_title),
globalesim/src/test/java/com/vayunmathur/globalesim/ui/CardPaymentHostTest.kt
diff --git a/globalesim/src/test/java/com/vayunmathur/globalesim/ui/CardPaymentHostTest.kt b/globalesim/src/test/java/com/vayunmathur/globalesim/ui/CardPaymentHostTest.ktindex 30a4b53911..1d80d9cb7f 100644--- a/globalesim/src/test/java/com/vayunmathur/globalesim/ui/CardPaymentHostTest.kt+++ b/globalesim/src/test/java/com/vayunmathur/globalesim/ui/CardPaymentHostTest.kt@@ -59,6 +59,51 @@ class CardPaymentHostTest { assertEquals(listOf("secret_123"), presented) } + @Test fun orphanSecretWithoutCardOrLauncherIsAbandoned() {+ // Regression: the sheet was dismissed (or recreated) while the+ // secret fetch was in flight, so it arrives with no pending card+ // and no launcher to confirm it. The host must abandon it through+ // the ViewModel clear path instead of consuming it as presented โ+ // an orphaned pendingClientSecret keeps TopUpCard's busy flag on+ // (both Card and Crypto buttons disabled) with nothing to clear it.+ var abandoned = 0+ composeRule.setContent {+ DynamicTheme {+ CardPaymentHost(+ state = EsimUiState(pendingClientSecret = "secret_orphan"),+ launchers = null,+ sheetAmountCents = null,+ onDismissSheet = {},+ onSheetAmountConsumed = {},+ onStartCardPurchase = { _, _, _ -> },+ onAbandonCardPurchase = { abandoned++ })+ }+ }+ composeRule.waitForIdle()+ assertEquals(1, abandoned)+ }++ @Test fun hoistedSecretPresentationDoesNotAbandon() {+ var abandoned = 0+ val presented = mutableListOf<String>()+ composeRule.setContent {+ DynamicTheme {+ CardPaymentHost(+ state = EsimUiState(pendingClientSecret = "secret_hoisted"),+ launchers = null,+ sheetAmountCents = null,+ onDismissSheet = {},+ onSheetAmountConsumed = {},+ onStartCardPurchase = { _, _, _ -> },+ onPresentPaymentSheet = { presented.add(it) },+ onAbandonCardPurchase = { abandoned++ })+ }+ }+ composeRule.waitForIdle()+ assertEquals(listOf("secret_hoisted"), presented)+ assertEquals(0, abandoned)+ }+ @Test fun sheetPayFlowStartsPurchaseAndCachesKey() { var startedAmount: Long? = null composeRule.setContent {