Modern Apps git

← Modern-Apps.git

shop: compare checkout lines as variant quantity totals

Commit
f1ac3b43f9cdb0922210978234166e86a4eaec00
Author
Vayun Mathur <[email protected]>
When
2026-12-01 19:09 UTC (4 days ago)
Parents
8a9d497
NativeCheckoutCoordinator.cartMatches checked only that the merchant's
submittedLines had the same size as the cart and that every submitted
line found some matching cart line. Duplicate merchant lines for one
variant therefore satisfied the check while another cart variant was
missing entirely: cart [variant-1 x1, variant-2 x1] with the merchant
echoing [variant-1 x1, variant-1 x1] was accepted as matching, so the
session stayed Reviewing and could open for payment instead of being
forced to BrowserRequired, contradicting the guard's purpose of never
paying against a session that does not represent the cart.

Compare both sides as variant-to-total-quantity maps instead. This
rejects duplicated/missing variants, and also accepts merchant line
items that split one variant across several lines when the summed
quantity matches the cart. publishResult and reconcile both use this
helper, so create and refresh paths now agree; ShopViewModel already
projects carts the same way for its mid-flight change check.

Regression test duplicateMerchantLineItemsDoNotMaskMissingVariant in
NativeCheckoutStateTest fails before the fix (session stays Reviewing)
and passes after (BrowserRequired, cart lines preserved in the pending
record).

Verified: ./gradlew :shop:testDevUnitTest --tests
com.vayunmathur.shop.domain.NativeCheckoutStateTest and the full
:shop:testDevUnitTest suite both pass. :shop:ktlintCheck still fails
on pre-existing backlog in untouched files; no findings on the lines
changed here.

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 69df4611c6..2a6ec34434 100644--- a/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt+++ b/shop/src/main/java/com/vayunmathur/shop/domain/NativeCheckoutCoordinator.kt@@ -135,14 +135,22 @@ internal class NativeCheckoutCoordinator(     } ?: (now() - (createdAt[key] ?: now()) !in 0 until 6 * 60 * 60 * 1000L)  -    /** True when [submitted] lines match [lines] by variant and quantity. */+    /**+     * True when [submitted] lines match [lines] by variant and quantity.+     * Compared as variant→total-quantity maps: a size+any-match check would+     * let duplicate merchant lines for one variant mask a missing variant,+     * and split merchant lines for one variant must sum to the cart quantity.+     */     private fun cartMatches(         submitted: List<SubmittedLine>,         lines: List<CartLine>,-    ): Boolean = submitted.size == lines.size &&-        submitted.all { sub ->-            lines.any { it.variantId == sub.variantId && it.quantity == sub.quantity }-        }+    ): Boolean {+        val submittedByVariant = mutableMapOf<String, Int>()+        submitted.forEach { submittedByVariant[it.variantId] = (submittedByVariant[it.variantId] ?: 0) + it.quantity }+        val cartByVariant = mutableMapOf<String, Int>()+        lines.forEach { cartByVariant[it.variantId] = (cartByVariant[it.variantId] ?: 0) + it.quantity }+        return submittedByVariant == cartByVariant+    }      /** Guards a submit; publishes the refusal reason and returns true when blocked. */     private fun submitBlocked(
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 fb90ea4f42..efca735792 100644--- a/shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/domain/NativeCheckoutStateTest.kt@@ -203,6 +203,24 @@ class NativeCheckoutStateTest {         assertEquals(1, gateway.intents.size)     } +    @Test fun duplicateMerchantLineItemsDoNotMaskMissingVariant() = runBlocking {+        val twoLines = listOf(+            CartLine(variantId = "variant-1", productTitle = "One", platformId = "generic", shopDomain = "shop.example", quantity = 1),+            CartLine(variantId = "variant-2", productTitle = "Two", platformId = "generic", shopDomain = "shop.example", quantity = 1),+        )+        val gateway = FakeGateway().apply {+            response = Result.success(+                NativeCheckoutFixtures.checkout(+                    submitted = listOf(SubmittedLine("variant-1", 1), SubmittedLine("variant-1", 1)),+                ),+            )+        }+        val coordinator = coordinator(gateway)+        coordinator.submit("generic", "shop.example", twoLines, sink)+        assertEquals(CheckoutPhase.BrowserRequired, coordinator.snapshot.single().phase)+        assertEquals(twoLines, stored.single().lines)+    }+     @Test fun invalidInputsNeverReachGateway() = runBlocking {         val gateway = FakeGateway()         val coordinator = coordinator(gateway)