shop: send the whole profile (name, contact, address) with the first checkout request
- Commit
f83cfaae574b551cb7412458f4708fe462981806- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 19:29 UTC (1 month ago)
- Parents
47d96c5
Diff
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 fd22ed79bf..1c48142d76 100644--- a/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt+++ b/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt@@ -94,7 +94,9 @@ internal class NativeCheckoutCoordinator( publish(key, state.copy(phase = if (previous == null) CheckoutPhase.Preparing else CheckoutPhase.Updating, message = null)) // The server builds the exact dated projection and owns the idempotency // ledger: a write is attempted exactly once and never auto-retried.- val sentInputs = if (previous == null) state.inputs.copy(street = "", extendedAddress = "", city = "", region = "", country = "", postalCode = "", options = emptyMap(), shippingRequested = false) else state.inputs+ // The first write carries the whole profile (name, contact and address); only+ // shipping-option choices wait until the merchant has offered them.+ val sentInputs = if (previous == null) state.inputs.copy(options = emptyMap()) else state.inputs val intent = newIntent() mutationStarted = true val checkout = (if (previous == null) gateway.create(shop, lines.toList(), sentInputs, intent)
shop/src/main/java/com/vayunmathur/shop/domain/ProfileCheckout.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/domain/ProfileCheckout.kt b/shop/src/main/java/com/vayunmathur/shop/domain/ProfileCheckout.ktindex 0e6f6a9ceb..58635817c9 100644--- a/shop/src/main/java/com/vayunmathur/shop/domain/ProfileCheckout.kt+++ b/shop/src/main/java/com/vayunmathur/shop/domain/ProfileCheckout.kt@@ -12,11 +12,9 @@ object ProfileCheckout { checkout.messages.any { it.code == "delivery_address_required" } ) - /** The address is only sent once the merchant has issued shipping lines it can attach to. */+ /** Every write carries the full saved profile, including the delivery address when one is saved. */ fun inputs(profile: ShopProfile, state: NativeCheckoutState): CheckoutInputs {- val shipping = profile.hasAddress && needsDeliveryAddress(state.checkout) val inputs = profile.checkoutInputs()- return if (shipping) inputs.copy(shippingRequested = true, options = state.inputs.options)- else inputs.copy(street = "", extendedAddress = "", city = "", region = "", country = "", postalCode = "")+ return if (profile.hasAddress) inputs.copy(shippingRequested = true, options = state.inputs.options) else inputs } }
shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.ktindex 3191291774..f2600dd26b 100644--- a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt+++ b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt@@ -8,7 +8,6 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vayunmathur.shop.Route import com.vayunmathur.shop.data.CheckoutInputs-import com.vayunmathur.shop.data.CheckoutPhase import com.vayunmathur.shop.data.NativeCheckoutState import com.vayunmathur.shop.domain.NativeCheckoutCoordinator import com.vayunmathur.shop.domain.PendingCheckoutSink@@ -110,31 +109,23 @@ class ShopViewModel( val sink = PendingCheckoutSink { OrderStore.add(context, it) } viewModelScope.launch { AppInit.awaitReady()- val created = submitFromProfile(platformId, shop, lines, sink) == null- // A new session can't carry fulfillment, so the saved address follows as its own write- // only after the create succeeded. Nothing is replayed if either write is uncertain.- val state = nativeCheckout.snapshot.firstOrNull { it.platformId == platformId && it.shopDomain == shop }- if (created && state?.phase == CheckoutPhase.Reviewing && state.checkout != null) {- submitFromProfile(platformId, shop, lines, sink)- }+ submitFromProfile(platformId, shop, lines, sink) nativeCheckout.cartChanged(cart) orders = OrderStore.getAll(context) } } - /** Returns the session that existed before this write, if any. */ private suspend fun submitFromProfile( platformId: String, shop: String, lines: List<CartLine>, sink: PendingCheckoutSink,- ): UcpCheckout? {+ ) { val state = nativeCheckout.snapshot.firstOrNull { it.platformId == platformId && it.shopDomain == shop } ?: NativeCheckoutState(platformId, shop)- val inputs = checkoutInputs(state) ?: return state.checkout+ val inputs = checkoutInputs(state) ?: return nativeCheckout.edit(platformId, shop, inputs) nativeCheckout.submit(platformId, shop, lines, sink)- return state.checkout } fun updateQuery(value: String) { query = value }
shop/src/test/java/com/vayunmathur/shop/domain/ProfileCheckoutTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/domain/ProfileCheckoutTest.kt b/shop/src/test/java/com/vayunmathur/shop/domain/ProfileCheckoutTest.ktindex 6d7cc67acf..ccefb22ec2 100644--- a/shop/src/test/java/com/vayunmathur/shop/domain/ProfileCheckoutTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/domain/ProfileCheckoutTest.kt@@ -27,11 +27,14 @@ class ProfileCheckoutTest { assertFalse(withAddress.toString().contains("Ada")) } - @Test fun createNeverCarriesAddress() {+ @Test fun createCarriesTheWholeProfile() { val inputs = ProfileCheckout.inputs(withAddress, NativeCheckoutState("generic", "shop.example")) assertEquals("Ada", inputs.firstName)- assertFalse(inputs.hasAddress)- assertFalse(inputs.shippingRequested)+ assertEquals("Lovelace", inputs.lastName)+ assertEquals("1 Main St", inputs.street)+ assertEquals("78701", inputs.postalCode)+ assertTrue(inputs.shippingRequested)+ assertNull(inputs.validationError()) } @Test fun deliveryAddressRequiredSendsSavedAddress() {@@ -45,8 +48,7 @@ class ProfileCheckoutTest { assertNull(inputs.validationError()) } - @Test fun addressWithheldWhenMerchantDoesNotShipOrProfileHasNone() {- assertFalse(ProfileCheckout.inputs(withAddress, NativeCheckoutState("generic", "shop.example", checkout = noShipping)).hasAddress)+ @Test fun noAddressSentWhenProfileHasNone() { val shipping = NativeCheckoutState("generic", "shop.example", checkout = NativeCheckoutFixtures.checkout()) assertTrue(ProfileCheckout.needsDeliveryAddress(shipping.checkout)) val inputs = ProfileCheckout.inputs(contact, shipping)