shop: open the Shopify cart when no checkout link; show the server's checkout reason
- Commit
7ef08e7e391e9486407de4880c409dbdedf1f568- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 06:00 UTC (1 month ago)
- Parents
9ef929e
Diff
metadata_data/photos/shop/phone/4.png
diff --git a/metadata_data/photos/shop/phone/4.png b/metadata_data/photos/shop/phone/4.pngindex a975478f2b..840d3a6cf1 100644Binary files a/metadata_data/photos/shop/phone/4.png and b/metadata_data/photos/shop/phone/4.png differ
shop/src/main/java/com/vayunmathur/shop/domain/MerchantUrls.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/domain/MerchantUrls.kt b/shop/src/main/java/com/vayunmathur/shop/domain/MerchantUrls.ktindex 3bb50cc011..8299978c36 100644--- a/shop/src/main/java/com/vayunmathur/shop/domain/MerchantUrls.kt+++ b/shop/src/main/java/com/vayunmathur/shop/domain/MerchantUrls.kt@@ -5,6 +5,19 @@ import java.util.Locale /** Public HTTPS URLs only; merchant input must not name local devices or app intents. */ object MerchantUrls {+ private val shopifyVariant = Regex("^gid://shopify/ProductVariant/([0-9]+)$")++ /** Shopify cart permalink that opens the store's cart with these lines already added. */+ fun shopifyCartUrl(domain: String, lines: List<Pair<String, Int>>): String? {+ val host = domain(domain) ?: return null+ if (lines.isEmpty()) return null+ val items = lines.map { (variantId, quantity) ->+ val id = shopifyVariant.find(variantId)?.groupValues?.get(1) ?: return null+ "$id:$quantity"+ }+ return httpsUrl("https://$host/cart/${items.joinToString(",")}")+ }+ fun httpsUrl(value: String): String? { val uri = runCatching { URI(value) }.getOrNull() ?: return null if (!uri.scheme.equals("https", ignoreCase = true) || uri.userInfo != null) return null
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 062d54a5e2..f43958bb98 100644--- a/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt+++ b/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt@@ -99,9 +99,9 @@ internal class NativeCheckoutCoordinator( mutationStarted = true val checkout = (if (previous == null) gateway.create(shop, lines.toList(), sentInputs, intent) else gateway.update(shop, previous.checkoutId, lines.toList(), sentInputs, intent)).getOrElse {- val unsupported = (it as? ShopBackendException)?.error is UcpError.Unsupported- publish(key, state.copy(phase = if (unsupported) CheckoutPhase.BrowserRequired else CheckoutPhase.Uncertain,- message = if (unsupported) "These requirements need the merchant website." else "The request may have reached the merchant. Do not retry; check the session or use its website."))+ val unsupported = (it as? ShopBackendException)?.error as? UcpError.Unsupported+ publish(key, state.copy(phase = if (unsupported != null) CheckoutPhase.BrowserRequired else CheckoutPhase.Uncertain,+ message = unsupported?.reason ?: "The request may have reached the merchant. Do not retry; check the session or use its website.")) return } if (previous != null && checkout.checkoutId != previous.checkoutId) {
shop/src/main/java/com/vayunmathur/shop/ui/CheckoutReviewContent.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutReviewContent.kt b/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutReviewContent.ktindex 04a454d42b..9c35d68ca6 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutReviewContent.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutReviewContent.kt@@ -2,18 +2,15 @@ package com.vayunmathur.shop.ui import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column-import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.FilterChip import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import com.vayunmathur.shop.data.CheckoutInputs import com.vayunmathur.shop.data.NativeCheckoutState-import com.vayunmathur.shop.domain.MerchantUrls import com.vayunmathur.shop.domain.MoneyFormat /** Pure session presentation shared by the live checkout and deterministic screenshot fixtures. */@@ -62,9 +59,6 @@ internal fun CheckoutReviewContent( } } if (checkout.status == "ready_for_complete") Text("The merchant reports payment readiness. Pay on its website to place the order.", style = MaterialTheme.typography.bodySmall)- MerchantUrls.httpsUrl(checkout.continueUrl)?.let { payUrl ->- Button(onClick = { onOpenUrl(payUrl) }, modifier = Modifier.fillMaxWidth()) { Text("Pay on merchant website") }- } Text("Pending session only. Check purchase and payment status on the merchant website.", style = MaterialTheme.typography.bodySmall) checkout.review.links.forEach { link -> TextButton(onClick = { onOpenUrl(link.url) }) { Text(link.title.ifBlank { link.type }) }
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 2e6060a996..cccf104305 100644--- a/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.kt+++ b/shop/src/main/java/com/vayunmathur/shop/ui/CheckoutScreen.kt@@ -17,8 +17,8 @@ import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.CircularProgressIndicator import com.vayunmathur.library.ui.DetailSection import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.OutlinedButton import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.ui.appBarScrollBehavior import com.vayunmathur.shop.R import com.vayunmathur.shop.data.CheckoutPhase@@ -79,8 +79,16 @@ 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" }- if (checkout?.continueUrl?.let(MerchantUrls::httpsUrl) == null && merchantSiteUrl != null) {- TextButton(onClick = { openContinueUrl(context, merchantSiteUrl) }) { Text("Open merchant website") }+ val checkoutUrl = checkout?.continueUrl?.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) }) {+ Text(when (url) {+ checkoutUrl -> "Continue checkout on merchant website"+ cartUrl -> "Open cart on merchant website"+ else -> "Open merchant website"+ })+ } } } }
shop/src/test/java/com/vayunmathur/shop/domain/MerchantUrlsTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/domain/MerchantUrlsTest.kt b/shop/src/test/java/com/vayunmathur/shop/domain/MerchantUrlsTest.ktindex 8a7ed7eef8..f0083fc274 100644--- a/shop/src/test/java/com/vayunmathur/shop/domain/MerchantUrlsTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/domain/MerchantUrlsTest.kt@@ -17,4 +17,13 @@ class MerchantUrlsTest { "https://127.0.0.1", "https://[::1]", "https://router.local", "https://store.example:8443", "https://store.example/cart").forEach { assertNull(MerchantUrls.domain(it), it) } }++ @Test+ fun shopifyCartPermalinkNeedsShopifyVariantIds() {+ assertEquals("https://store.example/cart/11:2,12:1",+ MerchantUrls.shopifyCartUrl("store.example", listOf("gid://shopify/ProductVariant/11" to 2, "gid://shopify/ProductVariant/12" to 1)))+ assertNull(MerchantUrls.shopifyCartUrl("store.example", listOf("gid://shopify/ProductVariant/11" to 1, "sku-12" to 1)))+ assertNull(MerchantUrls.shopifyCartUrl("localhost", listOf("gid://shopify/ProductVariant/11" to 1)))+ assertNull(MerchantUrls.shopifyCartUrl("store.example", emptyList()))+ } }