shop: hand off to the merchant's UCP continue_url when it rejects an in-app checkout
- Commit
5c12d704ea3d3d0780f0507b9fe918d5adbeb87e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 19:03 UTC (1 month ago)
- Parents
dd5088c
Diff
shop/src/main/java/com/vayunmathur/shop/data/CheckoutReview.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/data/CheckoutReview.kt b/shop/src/main/java/com/vayunmathur/shop/data/CheckoutReview.ktindex de0b89e804..0741b35188 100644--- a/shop/src/main/java/com/vayunmathur/shop/data/CheckoutReview.kt+++ b/shop/src/main/java/com/vayunmathur/shop/data/CheckoutReview.kt@@ -45,6 +45,8 @@ data class NativeCheckoutState( val phase: CheckoutPhase = CheckoutPhase.Reviewing, val checkout: UcpCheckout? = null, val message: String? = null,+ /** Merchant handoff URL from a rejected write, when no session exists to carry one. */+ val handoffUrl: String? = null, val inputs: CheckoutInputs = CheckoutInputs(), val submittedInputs: CheckoutInputs? = null, val submittedLines: List<CartLine> = emptyList(),
shop/src/main/java/com/vayunmathur/shop/data/UcpModels.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/data/UcpModels.kt b/shop/src/main/java/com/vayunmathur/shop/data/UcpModels.ktindex dd0e353741..efd31edd17 100644--- a/shop/src/main/java/com/vayunmathur/shop/data/UcpModels.kt+++ b/shop/src/main/java/com/vayunmathur/shop/data/UcpModels.kt@@ -122,7 +122,7 @@ sealed interface UcpError { data class Http(val status: Int, val snippet: String) : UcpError data class Malformed(val reason: String) : UcpError data class Rpc(val message: String) : UcpError- data class Unsupported(val reason: String) : UcpError+ data class Unsupported(val reason: String, val continueUrl: String? = null) : UcpError } fun UcpError.message(): String = when (this) {
shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt b/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.ktindex 91b191663e..fd22ed79bf 100644--- a/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt+++ b/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt@@ -102,6 +102,7 @@ internal class NativeCheckoutCoordinator( val unsupported = (it as? ShopBackendException)?.error as? UcpError.Unsupported publish(key, state.copy( phase = if (unsupported != null) CheckoutPhase.BrowserRequired else CheckoutPhase.Uncertain,+ handoffUrl = unsupported?.continueUrl?.let(MerchantUrls::httpsUrl), message = unsupported?.reason ?: ("The request may have reached the merchant. " + "Do not retry; check the session or use its website."),
shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt b/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.ktindex 8f9c90e30b..ca8296fdff 100644--- a/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt+++ b/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt@@ -178,7 +178,10 @@ internal object ShopHttpBackend : ShopBackend { "malformed" -> UcpError.Malformed(envelope.message.ifBlank { "The store sent an unexpected response. Please try again." }) "rpc" -> UcpError.Rpc(envelope.message.ifBlank { "The merchant couldn't process the request." }) "uncertain" -> UcpError.Transport("The request may have reached the merchant. Do not retry; check the session or use its website.")- "unsupported" -> UcpError.Unsupported(envelope.message.ifBlank { "These requirements need the merchant website." })+ "unsupported" -> UcpError.Unsupported(+ envelope.message.ifBlank { "These requirements need the merchant website." },+ envelope.continueUrl,+ ) // Unknown or empty envelopes (e.g. a proxy error page) say nothing about the merchant. else -> UcpError.Http(status, envelope.message) },
shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt b/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.ktindex a4e6d44e86..e4c28bf67a 100644--- a/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt+++ b/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt@@ -216,6 +216,7 @@ data class ShopErrorBody( val code: String = "", val message: String = "", val retryAfterMs: Long? = null,+ val continueUrl: String? = null, ) @Serializable
shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.kt b/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.ktindex 3a824d8e9d..9e42f9df12 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.kt@@ -79,7 +79,7 @@ fun CheckoutScreen(viewModel: ShopViewModel, onBack: () -> Unit) { else Text(if (checkout == null) "Create review session" else "Update review session") } val merchantSiteUrl = MerchantUrls.domain(key.second)?.let { "https://$it" }- val checkoutUrl = checkout?.continueUrl?.let(MerchantUrls::httpsUrl)+ val checkoutUrl = (checkout?.continueUrl ?: state.handoffUrl)?.let(MerchantUrls::httpsUrl) val cartUrl = MerchantUrls.shopifyCartUrl(key.second, lines.map { it.variantId to it.quantity }) (checkoutUrl ?: cartUrl ?: merchantSiteUrl)?.let { url -> OutlinedButton(modifier = Modifier.fillMaxWidth(), onClick = { openContinueUrl(context, url) }) {
shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.kt b/shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.ktindex 50e1035976..5108baddb4 100644--- a/shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.kt@@ -33,6 +33,19 @@ class NativeCheckoutStateTest { return NativeCheckoutCoordinator(gateway, now = clock, newIntent = { "intent-${++intent}" }) } + @Test fun rejectedCreateKeepsMerchantHandoffUrl() = runBlocking {+ val url = "https://allbirds.com/checkouts/cn/abc"+ val gateway = FakeGateway().apply {+ response = Result.failure(com.vayunmathur.shop.network.ShopBackendException(UcpError.Unsupported("Website only", url)))+ }+ val coordinator = coordinator(gateway)+ coordinator.submit("generic", "shop.example", lines, sink)+ val state = coordinator.snapshot.single()+ assertEquals(CheckoutPhase.BrowserRequired, state.phase)+ assertEquals(url, state.handoffUrl)+ assertEquals("Website only", state.message)+ }+ @Test fun duplicateTapsAndUnchangedSessionReuseNeverDuplicateMutation() = runBlocking { val gateway = FakeGateway().apply { gate = CompletableDeferred() } val coordinator = coordinator(gateway)