@aglyn/plugins-commerce 1.29.3 → 1.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -13
- package/src/lib/components/cart.js +11 -5
- package/src/lib/components/cart.js.map +1 -1
- package/src/lib/components/console/pos/pos-item-dialog.component.js +12 -0
- package/src/lib/components/console/pos/pos-item-dialog.component.js.map +1 -1
- package/src/lib/components/console/pos-kiosk-page.component.js +3 -5
- package/src/lib/components/console/pos-kiosk-page.component.js.map +1 -1
- package/src/lib/components/console/product-add-ons-fields.component.d.ts +11 -0
- package/src/lib/components/console/product-add-ons-fields.component.js +229 -13
- package/src/lib/components/console/product-add-ons-fields.component.js.map +1 -1
- package/src/lib/components/console/product-editor-dialog.component.js +5 -0
- package/src/lib/components/console/product-editor-dialog.component.js.map +1 -1
- package/src/lib/components/console/product-traffic-card.component.d.ts +24 -0
- package/src/lib/components/console/product-traffic-card.component.js +172 -0
- package/src/lib/components/console/product-traffic-card.component.js.map +1 -0
- package/src/lib/components/console/products-hub-card.component.js +181 -59
- package/src/lib/components/console/products-hub-card.component.js.map +1 -1
- package/src/lib/components/console/quote-editor-dialog.component.js +38 -0
- package/src/lib/components/console/quote-editor-dialog.component.js.map +1 -1
- package/src/lib/components/console/shipping-settings-card.component.js +31 -9
- package/src/lib/components/console/shipping-settings-card.component.js.map +1 -1
- package/src/lib/components/product-add-ons.d.ts +21 -0
- package/src/lib/components/product-add-ons.js +77 -1
- package/src/lib/components/product-add-ons.js.map +1 -1
- package/src/lib/components/product-builder-mockup.d.ts +1 -1
- package/src/lib/components/product-builder-mockup.js +17 -5
- package/src/lib/components/product-builder-mockup.js.map +1 -1
- package/src/lib/components/product-choice-cards.js +14 -2
- package/src/lib/components/product-choice-cards.js.map +1 -1
- package/src/lib/model/commerce-pos-kiosk.js +8 -2
- package/src/lib/model/commerce-pos-kiosk.js.map +1 -1
- package/src/lib/model/commerce-pos-offline.d.ts +1 -1
- package/src/lib/model/commerce-pos-offline.js +8 -2
- package/src/lib/model/commerce-pos-offline.js.map +1 -1
- package/src/lib/model/commerce-quote-request.d.ts +1 -0
- package/src/lib/model/commerce-quote-request.js +6 -2
- package/src/lib/model/commerce-quote-request.js.map +1 -1
- package/src/lib/model/commerce-quotes.js +10 -5
- package/src/lib/model/commerce-quotes.js.map +1 -1
- package/src/lib/model/commerce-record-routes.js +31 -0
- package/src/lib/model/commerce-record-routes.js.map +1 -1
- package/src/lib/model/commerce-shipping.d.ts +39 -16
- package/src/lib/model/commerce-shipping.js +315 -30
- package/src/lib/model/commerce-shipping.js.map +1 -1
- package/src/lib/model/commerce.js +1 -1
- package/src/lib/model/commerce.js.map +1 -1
- package/src/lib/model/product-modifiers.d.ts +53 -2
- package/src/lib/model/product-modifiers.js +127 -27
- package/src/lib/model/product-modifiers.js.map +1 -1
- package/src/lib/model/product-write.d.ts +10 -1
- package/src/lib/model/product-write.js +13 -2
- package/src/lib/model/product-write.js.map +1 -1
- package/src/lib/server/carrier-shipping.js +3 -2
- package/src/lib/server/carrier-shipping.js.map +1 -1
- package/src/lib/server/cart-checkout.js +10 -4
- package/src/lib/server/cart-checkout.js.map +1 -1
- package/src/lib/server/cart.js +2 -2
- package/src/lib/server/cart.js.map +1 -1
- package/src/lib/server/checkout.js +6 -4
- package/src/lib/server/checkout.js.map +1 -1
- package/src/lib/server/draft-order.js +3 -3
- package/src/lib/server/draft-order.js.map +1 -1
- package/src/lib/server/pos-kiosk.js +1 -4
- package/src/lib/server/pos-kiosk.js.map +1 -1
- package/src/lib/server/product.d.ts +5 -0
- package/src/lib/server/product.js +10 -2
- package/src/lib/server/product.js.map +1 -1
- package/src/lib/server/products-write.js +10 -2
- package/src/lib/server/products-write.js.map +1 -1
- package/src/lib/transfer/product-register-cells.d.ts +7 -0
- package/src/lib/transfer/product-register-cells.js +88 -10
- package/src/lib/transfer/product-register-cells.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/commerce-shipping.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Shipping v1 (AGL-288): zones own countries; rates belong to a zone and\n * price as flat, free-over-threshold, or subtotal/weight tiers. Settings\n * live on `hosts/{hostId}/settings/store` under `shipping`. Pure.\n *\n * `resolveShippingRates` is the per-destination resolver;\n * `resolveCheckoutShippingOptions` is the Stripe Checkout adapter,\n * `planCheckoutShipping` decides which destinations one session may serve,\n * `appendCheckoutShippingParams` emits it onto a session, and\n * `summarizeShippingCoverage` answers the same question for the console, so a\n * merchant can see which destinations their zones refuse. AGL-288 shipped this\n * model with no production call site at all — the doc comment here claimed the\n * cart estimator, checkout and POS pickup used it, and none of them did, so\n * nothing ever read the settings a merchant saved and no shipping was ever\n * charged. Cart checkout calls it (AGL-1707), buy-now calls it for physical\n * one-time products (AGL-1720), and a draft order's payment link calls it for\n * physical products (AGL-1792).\n *\n * POS DOES NOT, AND SHOULD NOT (AGL-1792). A register has no destination\n * anywhere in it — no address field, no delivery choice — and all three\n * tenders settle at the counter: cash, a card QR the customer scans while\n * standing there, and a room folio collected at check-out. There is nothing to\n * price a parcel against, so the only rate POS could resolve is the free\n * `Local pickup` one this module appends, which is 0¢ and would add a step to\n * a register flow in exchange for nothing. A POS sale that must be shipped is\n * a draft order.\n */\n\nexport interface ShippingZone {\n id: string\n name: string\n /** ISO-3166 alpha-2 codes; '*' = rest of world. */\n countries: string[]\n}\n\nexport interface ShippingTier {\n /** Tier applies while subtotal (cents) or weight (grams) ≤ this. */\n upTo: number\n amountCents: number\n}\n\n/**\n * A live carrier rate (AGL-3612): the zone's price is what a carrier quotes\n * for the parcel to the shopper's address, asked through core's\n * `core.shipping-rate-quoter` seam of whichever plugin quotes carriers. With\n * no quoter, a quoter that is not available for the site, no postal code to\n * quote against, or a quote slower than {@link CARRIER_QUOTE_TIMEOUT_MS}, it\n * offers nothing and its fallback rate is offered instead.\n */\nexport interface CarrierRateSettings {\n /** Service keys (`carrier:service`) to offer; empty offers every quoted service. */\n services?: string[]\n /** Added to each quote, in percent. */\n markupPct?: number\n /** Added to each quote after the markup, in cents. */\n handlingCents?: number\n /**\n * A rate of this zone offered when no live quote is: it is HIDDEN while\n * quotes are offered, so the shopper picks between carriers, not between a\n * carrier and the merchant's guess.\n */\n fallbackRateId?: string\n}\n\n/** How long checkout waits for live carrier quotes before it falls back. */\nexport const CARRIER_QUOTE_TIMEOUT_MS = 3_000\n\nexport interface ShippingRate {\n id: string\n zoneId: string\n name: string\n kind: 'flat' | 'free_over' | 'price_tiers' | 'weight_tiers' | 'carrier'\n /** flat + free_over base amount. */\n amountCents?: number\n /** free_over: order subtotal (cents) at/above which shipping is free. */\n freeOverCents?: number\n /** price_tiers (upTo = subtotal cents) / weight_tiers (upTo = grams). */\n tiers?: ShippingTier[]\n /** carrier: the live rate's options (AGL-3612). */\n carrier?: CarrierRateSettings\n}\n\nexport interface ShippingSettings {\n zones?: ShippingZone[]\n rates?: ShippingRate[]\n /**\n * Offer free local pickup as a delivery choice. Does NOT widen where the\n * store ships: a destination no rate reaches is still refused (AGL-2325).\n */\n localPickup?: boolean\n}\n\nexport interface ResolvedShippingRate {\n rateId: string\n name: string\n amountCents: number\n}\n\n/**\n * The rate id appended for local pickup (AGL-2325).\n *\n * Exported because pickup is the one entry in a resolved list that is NOT a\n * shipping promise: it is appended AFTER and INDEPENDENT of zone matching, so\n * it resolves for every destination on earth. Anything asking \"does this store\n * ship there\" — the checkout refusal, the console coverage card — has to be\n * able to tell it apart from a rate, or it reads a merchant who offers\n * collection as a merchant who delivers worldwide.\n */\nexport const LOCAL_PICKUP_RATE_ID = 'pickup'\n\n/**\n * One stored country code, normalised (AGL-2298).\n *\n * TRIMMED as well as upper-cased. The console trims on save, so this only\n * matters for a document written any other way — an import, the REST API, a\n * hand edit — but the failure it produces is silent and expensive: `' US'`\n * upper-cases to `' US'`, matches nothing, and the store refuses every US\n * shopper with \"This store does not ship to the United States\" while its\n * settings card shows `US` right there.\n */\nconst zoneCode = (code: unknown): string => String(code ?? '').trim().toUpperCase()\n\n/** Whether a stored code is the rest-of-world wildcard. */\nconst isWildcard = (code: unknown): boolean => zoneCode(code) === '*'\n\nfunction zoneMatches(zone: ShippingZone, country: string): boolean {\n return (zone.countries ?? []).some(\n (code) => isWildcard(code) || zoneCode(code) === country,\n )\n}\n\nfunction tierAmount(\n tiers: ShippingTier[] | undefined,\n value: number,\n): number | null {\n const sorted = [...(tiers ?? [])].sort((a, b) => a.upTo - b.upTo)\n for (const tier of sorted) {\n if (value <= tier.upTo) return tier.amountCents\n }\n // Beyond the last tier the rate does not apply (merchant should add a\n // catch-all tier with a large upTo).\n return null\n}\n\n/**\n * The zones a destination falls in (AGL-1707, factored out for AGL-3612).\n * Specific country zones beat '*' zones: when any zone names the country\n * outright, rest-of-world zones are ignored.\n */\nexport function matchedZoneIds(\n settings: ShippingSettings | undefined,\n destinationCountry: string,\n): Set<string> {\n const country = destinationCountry.toUpperCase()\n const zones = settings?.zones ?? []\n // A zone is SPECIFIC when it names this country outright — not merely when\n // it is \"not wildcard-only\" (AGL-2298). The old test was `!every(code ===\n // '*')`, so a zone spelled `['*','US']` counted as specific for all six\n // destinations (it matches every country through the `*`) and suppressed\n // every genuine rest-of-world zone globally. Asking the narrower question\n // makes such a zone specific for `US` and wildcard for the rest, which is\n // what its two entries actually say.\n const specific = zones.filter((zone) =>\n (zone.countries ?? []).some((code) => zoneCode(code) === country),\n )\n const matched = specific.length\n ? specific\n : zones.filter((zone) => zoneMatches(zone, country))\n return new Set(matched.map((zone) => zone.id))\n}\n\n/** The live carrier rates a destination's zones carry (AGL-3612). */\nexport function carrierRatesFor(\n settings: ShippingSettings | undefined,\n destinationCountry: string,\n): ShippingRate[] {\n if (!settings) return []\n const zoneIds = matchedZoneIds(settings, destinationCountry)\n return (settings.rates ?? []).filter(\n (rate) => rate.kind === 'carrier' && zoneIds.has(rate.zoneId),\n )\n}\n\n/** Whether any zone carries a live carrier rate (AGL-3612). */\nexport function hasCarrierRates(settings: ShippingSettings | undefined): boolean {\n return (settings?.rates ?? []).some((rate) => rate.kind === 'carrier')\n}\n\n/** The rate id a quoted service is offered under: stable per rate and service. */\nexport function carrierOptionRateId(rateId: string, serviceKey: string): string {\n return `carrier:${rateId}:${serviceKey}`\n}\n\n/**\n * What a shopper pays for a carrier's quote under one carrier rate: the\n * quote, plus the markup, plus the handling fee, in whole cents.\n */\nexport function carrierOptionAmountCents(\n quotedCents: number,\n settings: CarrierRateSettings | undefined,\n): number {\n const quote = Math.max(0, Math.round(Number(quotedCents) || 0))\n const pct = Number(settings?.markupPct)\n const handling = Number(settings?.handlingCents)\n return (\n quote +\n (Number.isFinite(pct) && pct > 0 ? Math.round((quote * Math.min(pct, 500)) / 100) : 0) +\n (Number.isFinite(handling) && handling > 0 ? Math.round(handling) : 0)\n )\n}\n\n/**\n * Rates available for a destination + cart, cheapest first. Specific\n * country zones beat '*' zones: when any specific zone matches, '*'\n * zones are ignored (rest-of-world semantics).\n *\n * Local pickup is appended AFTER and INDEPENDENT of zone matching, so this\n * list is never empty for a merchant who offers it, at any destination. That\n * is correct for a resolver — a shopper anywhere may choose to come and\n * collect — but it means A NON-EMPTY RESULT IS NOT A PROMISE TO SHIP THERE.\n * Callers deciding whether a destination is served must filter pickup out\n * first; `planCheckoutShipping` and `summarizeShippingCoverage` do (AGL-2325).\n */\nexport function resolveShippingRates(\n settings: ShippingSettings | undefined,\n destinationCountry: string | undefined,\n cart: { subtotalCents: number; totalGrams?: number },\n): ResolvedShippingRate[] {\n if (!settings || !destinationCountry) return []\n const zoneIds = matchedZoneIds(settings, destinationCountry)\n // A zone is SPECIFIC when it names this country outright — not merely when\n // it is \"not wildcard-only\" (AGL-2298). The old test was `!every(code ===\n // '*')`, so a zone spelled `['*','US']` counted as specific for all six\n // destinations (it matches every country through the `*`) and suppressed\n // every genuine rest-of-world zone globally. Asking the narrower question\n // makes such a zone specific for `US` and wildcard for the rest, which is\n // what its two entries actually say.\n const resolved: ResolvedShippingRate[] = []\n for (const rate of settings.rates ?? []) {\n if (!zoneIds.has(rate.zoneId)) continue\n let amountCents: number | null = null\n switch (rate.kind) {\n case 'flat':\n amountCents = Math.max(0, Math.round(rate.amountCents ?? 0))\n break\n case 'free_over':\n amountCents =\n rate.freeOverCents != null &&\n cart.subtotalCents >= rate.freeOverCents\n ? 0\n : Math.max(0, Math.round(rate.amountCents ?? 0))\n break\n case 'price_tiers':\n amountCents = tierAmount(rate.tiers, cart.subtotalCents)\n break\n case 'weight_tiers':\n amountCents = tierAmount(rate.tiers, cart.totalGrams ?? 0)\n break\n // A carrier's price exists only once a carrier quoted it for an\n // address (AGL-3612), so a table read offers nothing for it and its\n // fallback rate is what resolves here. `carrier-shipping.ts` adds the\n // quotes where one can be had.\n case 'carrier':\n break\n }\n if (amountCents == null) continue\n resolved.push({ rateId: rate.id, name: rate.name, amountCents })\n }\n if (settings.localPickup) {\n resolved.push({\n rateId: LOCAL_PICKUP_RATE_ID,\n name: 'Local pickup',\n amountCents: 0,\n })\n }\n return resolved.sort((a, b) => a.amountCents - b.amountCents)\n}\n\n/**\n * The resolved options that actually carry a parcel (AGL-2325).\n *\n * Safe to apply AFTER `MAX_CHECKOUT_SHIPPING_OPTIONS` truncation: pickup is\n * 0¢ and sorts first, so it occupies at most one of the five slots and a\n * merchant with any shippable rate at all keeps at least one of them.\n */\nfunction shippableOnly(\n options: readonly ResolvedShippingRate[],\n): ResolvedShippingRate[] {\n return options.filter((option) => option.rateId !== LOCAL_PICKUP_RATE_ID)\n}\n\n/** Stripe Checkout accepts at most 5 `shipping_options` on one session. */\nexport const MAX_CHECKOUT_SHIPPING_OPTIONS = 5\n\n/**\n * Destinations a storefront Checkout Session collects an address for, and\n * therefore the destinations shipping rates are resolved against (AGL-1707).\n *\n * NOT narrowed to the zones the merchant configured: narrowing would block\n * checkouts that complete today, turning a money fix into lost sales. The\n * wider list only means a shopper outside the merchant's zones is offered some\n * rate rather than none. Shared by cart checkout and buy-now (AGL-1720) so the\n * two paths cannot drift into offering different destinations.\n */\nexport const CHECKOUT_SHIPPING_COUNTRIES = [\n 'US',\n 'CA',\n 'GB',\n 'AU',\n 'DE',\n 'FR',\n] as const\n\n/**\n * The rates to declare as `shipping_options` on a Stripe Checkout Session\n * (AGL-1707), unioned over `destinationCountries`. Stripe charges shipping\n * ONLY when the session declares them.\n *\n * A UNION IS ONLY HONEST OVER DESTINATIONS THE SESSION IS ALSO RESTRICTED TO.\n * Called with every collectable country while the session accepted an address\n * in any of them — which is what AGL-1707 shipped — it offers a shopper in one\n * zone a rate belonging to another, and Stripe applies whichever the shopper\n * picks (a `shipping_rate_data` carries no country of its own, so nothing\n * downstream re-checks it against the address). That is AGL-1721. Callers now\n * reach this through `planCheckoutShipping`, which pairs the country list it\n * unions over with the country list the session allows, so the two cannot\n * disagree. Do not call it directly from a handler.\n *\n * Returns `[]` for a merchant who has configured nothing, and the caller must\n * then declare no `shipping_options` at all rather than an empty array — a\n * merchant who never set shipping up keeps a session identical to today's.\n */\nexport function resolveCheckoutShippingOptions(\n settings: ShippingSettings | undefined,\n destinationCountries: readonly string[],\n cart: { subtotalCents: number; totalGrams?: number },\n): ResolvedShippingRate[] {\n if (!settings) return []\n const byRateId = new Map<string, ResolvedShippingRate>()\n for (const country of destinationCountries) {\n for (const rate of resolveShippingRates(settings, country, cart)) {\n // Stripe requires a display name, and a rate id is the dedupe key —\n // the console lets a merchant add a blank row, so neither is assured.\n if (!rate.rateId || !rate.name) continue\n const amountCents = Math.max(0, Math.round(Number(rate.amountCents) || 0))\n const existing = byRateId.get(rate.rateId)\n // One rate resolving differently for two countries should not be\n // possible (a zone selects a rate, it does not reprice one), but if it\n // ever is, keep the dearer. Under-charging is the defect being fixed.\n if (!existing || amountCents > existing.amountCents) {\n byRateId.set(rate.rateId, { ...rate, amountCents })\n }\n }\n }\n return (\n [...byRateId.values()]\n // Cheapest first, id as a tie-break so the emitted params are stable.\n .sort(\n (a, b) =>\n a.amountCents - b.amountCents || a.rateId.localeCompare(b.rateId),\n )\n // Past Stripe's cap the dearest are dropped, which is the harmless end\n // to lose: a shopper offered the cheap options would have taken one.\n .slice(0, MAX_CHECKOUT_SHIPPING_OPTIONS)\n )\n}\n\n/** {@link CHECKOUT_SHIPPING_COUNTRIES} as plain codes, for the apps' draft order (AGL-3841). */\nexport const CHECKOUT_SHIPPING_COUNTRY_CODES: readonly string[] = CHECKOUT_SHIPPING_COUNTRIES\n\n/** Shopper-facing names for the collectable destinations (AGL-1721). */\nexport const CHECKOUT_SHIPPING_COUNTRY_NAMES: Readonly<Record<string, string>> =\n {\n US: 'United States',\n CA: 'Canada',\n GB: 'United Kingdom',\n AU: 'Australia',\n DE: 'Germany',\n FR: 'France',\n }\n\n/**\n * A destination a shopper declared, or `undefined` for anything this session\n * could not collect an address for anyway. Never throws: an unknown code is\n * an undeclared destination, not an error, so a stale storefront that posts\n * one is answered by the plan below rather than by a 400 nobody can act on.\n */\nexport function normalizeCheckoutShippingCountry(\n value: unknown,\n): string | undefined {\n // `typeof` rather than `String(value)`: a JSON body is caller-shaped, and\n // `String(['US'])` is 'US'. Coercing would let an array declare a\n // destination, which is not a spelling any storefront sends.\n if (typeof value !== 'string') return undefined\n const code = value.trim().toUpperCase()\n return (CHECKOUT_SHIPPING_COUNTRIES as readonly string[]).includes(code)\n ? code\n : undefined\n}\n\nexport interface CheckoutShippingPlan {\n /** `shipping_address_collection[allowed_countries]` for this session. */\n countries: readonly string[]\n /** `shipping_options` for this session — valid for EVERY `countries` entry. */\n options: ResolvedShippingRate[]\n /**\n * Set when no session may be created at all:\n *\n * - `destination-required` — the merchant's rates differ by destination, so\n * there is no set of options honest for every address Stripe would accept.\n * Ask the shopper where they are shipping and plan again.\n * - `destination-unserved` — the merchant ships somewhere, but not there.\n * - `cart-unpriceable` — the merchant priced shipping, but no rate of theirs\n * resolves for THIS cart at ANY destination (AGL-2232). A tier table that\n * stops at 2 kg says nothing about a 3 kg parcel, and the honest answer is\n * to refuse rather than to ship it for nothing.\n */\n refusal?: 'destination-required' | 'destination-unserved' | 'cart-unpriceable'\n}\n\n/**\n * Whether this merchant prices shipping AT ALL (AGL-2232).\n *\n * A pure question about the SETTINGS, deliberately taking no cart, and that is\n * the whole point of it existing. `planCheckoutShipping` used to ask \"does any\n * rate resolve for any destination?\" and pass the live cart into the probe, so\n * a cart that exhausted every tier made the probe empty too — and an empty\n * probe reads as \"this merchant never set shipping up\", the one case that must\n * complete with no `shipping_options` and nothing charged. A 3 kg order at a\n * store whose weight tiers stop at 2 kg therefore shipped free, silently, on\n * every one of the three doors.\n *\n * Answered from the stored zones and rates instead: a rate bound to a zone\n * that exists, or local pickup, means the merchant made a shipping decision\n * and a cart their table cannot price is a REFUSAL. A merchant with no zones,\n * no rates and no pickup has made no such decision, and their session stays\n * byte-identical to the one AGL-1707 shipped.\n */\nexport function merchantPricesShipping(\n settings: ShippingSettings | undefined,\n): boolean {\n if (!settings) return false\n if (settings.localPickup) return true\n const zoneIds = new Set((settings.zones ?? []).map((zone) => zone.id))\n // A rate whose `zoneId` names no zone is unreachable by every resolver here,\n // so it is not evidence of a decision — counting it would refuse carts at a\n // store with nothing but orphaned rows.\n return (settings.rates ?? []).some((rate) => zoneIds.has(rate.zoneId))\n}\n\n/**\n * What a shopper is told when `cart-unpriceable` refuses (AGL-2232).\n *\n * One string, shared by all three doors, because the three used to phrase the\n * two existing refusals identically and drifting here would make the same\n * store say different things depending on which button was pressed. It names\n * the cart rather than the destination on purpose — a `Ship to` select cannot\n * fix a parcel no tier covers, so the refusal deliberately does NOT set\n * `needsShippingCountry`.\n */\nexport const CART_UNPRICEABLE_SHIPPING_MESSAGE =\n 'This store cannot price shipping for this order — it falls outside every ' +\n 'shipping rate the store has set up. Try removing an item, or contact the ' +\n 'store.'\n\n/** Identity of a resolved list, for comparing two destinations' offers. */\nfunction optionsKey(options: readonly ResolvedShippingRate[]): string {\n return options\n .map((option) => `${option.rateId}:${option.amountCents}`)\n .join('|')\n}\n\n/**\n * The whole shipping half of a Checkout Session: which destinations it may\n * accept an address for, and which rates it may charge (AGL-1721).\n *\n * THE INVARIANT IS THAT EVERY RETURNED OPTION IS VALID FOR EVERY RETURNED\n * COUNTRY. Stripe offers a session's `shipping_options` to whoever reaches the\n * page and charges whichever the shopper picks — it does not filter them by\n * the address entered, and there is no field on a `shipping_rate_data` that\n * would let it. So the ONLY place a zone can be enforced is here, by choosing\n * the two lists together: narrowing the allowed countries to the declared\n * destination is what makes the rates offered for it unpickable by anyone\n * else. Filtering the rate list alone would leave a session that still accepts\n * a French address against a US rate.\n *\n * Three shapes come out of it:\n *\n * 1. A merchant whose rates resolve the SAME for every collectable country —\n * one zone, a single '*' zone, or none at all — needs no destination and\n * keeps the session AGL-1707 shipped, byte for byte. The issue notes this\n * is the common configuration, and it is the one that must not regress.\n * 2. A declared destination resolves exactly, and the session is restricted\n * to it. A caller may not skip the restriction: the country arrives in a\n * request body a shopper can craft, so a session that took the country's\n * cheap rate while still accepting any address would hand that shopper\n * the defect on purpose.\n * 3. Otherwise the caller must refuse — see `refusal`. Refusing is not lost\n * revenue in either case: with rates that differ by zone there is no\n * correct amount to charge without knowing where the parcel is going, and\n * the union AGL-1707 shipped chose the wrong one silently.\n *\n * `cart.subtotalCents` is the PRE-discount items total, matching what the cart\n * displays, so a free-over threshold means what the shopper saw it mean.\n */\nexport function planCheckoutShipping(\n settings: ShippingSettings | undefined,\n cart: { subtotalCents: number; totalGrams?: number },\n destinationCountry?: unknown,\n): CheckoutShippingPlan {\n const countries = CHECKOUT_SHIPPING_COUNTRIES as readonly string[]\n const destination = normalizeCheckoutShippingCountry(destinationCountry)\n if (destination) {\n const options = resolveCheckoutShippingOptions(\n settings,\n [destination],\n cart,\n )\n // REFUSAL IS DECIDED ON SHIPPABLE RATES, PICKUP IS OFFERED AFTERWARDS\n // (AGL-2325). `resolveShippingRates` appends pickup independent of zone\n // matching, so a merchant who turned it on had a non-empty list for every\n // destination on earth and this branch returned before it could refuse\n // anyone: a shopper in France picked `Local pickup — Free` on a physical\n // order from a US-only store, Stripe collected their French address\n // because the session still allowed it, and the merchant was paid nothing\n // to carry the parcel there. Pickup is collection, not delivery, so it\n // cannot answer \"do you ship to France\".\n //\n // `shipsElsewhere` is the same question the old probe asked, narrowed to\n // shippable rates, and it keeps the same two-way fork: a merchant who\n // configured NO shipping is not refusing anyone, so that store keeps\n // completing exactly as it does today, while a merchant who ships\n // elsewhere but not here is refusing and saying so is the honest answer.\n // A merchant who ships NOWHERE is untouched by this: pickup-only is a\n // real configuration — they deliver to no one and refuse no one — and it\n // falls through to the offer below exactly as it did.\n const shipsElsewhere = countries.some(\n (country) =>\n shippableOnly(resolveCheckoutShippingOptions(settings, [country], cart))\n .length > 0,\n )\n if (shippableOnly(options).length === 0 && shipsElsewhere) {\n return { countries, options: [], refusal: 'destination-unserved' }\n }\n if (options.length > 0) return { countries: [destination], options }\n // NOTHING resolves anywhere for this cart, and the two reasons that can be\n // true of are opposite (AGL-2232). Asked of the SETTINGS rather than of\n // this cart, because the probe above cannot tell them apart: a merchant\n // who priced shipping and whose table simply does not reach this order is\n // refusing, and used to be handed the \"never set shipping up\" branch.\n return merchantPricesShipping(settings)\n ? { countries, options: [], refusal: 'cart-unpriceable' }\n : { countries, options: [] }\n }\n const offers = new Set(\n countries.map((country) =>\n optionsKey(resolveCheckoutShippingOptions(settings, [country], cart)),\n ),\n )\n // Compared as EMITTED — capped and sanitized — because that is the list a\n // shopper can pick from. Two destinations whose rates differ only past\n // Stripe's cap still offer the same five, and offering them is honest.\n if (offers.size > 1) {\n return { countries, options: [], refusal: 'destination-required' }\n }\n const union = resolveCheckoutShippingOptions(settings, countries, cart)\n // Every destination agrees, and what they agree on is NOTHING. The same\n // fork as the declared-destination branch above (AGL-2232): a store with no\n // shipping settings completes untouched, a store whose rates cannot reach\n // this cart refuses instead of carrying the parcel for free.\n if (union.length === 0 && merchantPricesShipping(settings)) {\n return { countries, options: [], refusal: 'cart-unpriceable' }\n }\n return { countries, options: union }\n}\n\n/**\n * A cart chosen so that every rate a merchant could have written resolves if\n * it can resolve at all (AGL-1791). The settings page has no cart, and two of\n * the four rate kinds price against one: `tierAmount` takes the first tier\n * whose `upTo` is at least the value, so a zero subtotal and zero weight clear\n * every tier a merchant would write, and a `free_over` rate resolves at any\n * subtotal.\n *\n * That makes a coverage answer a LOWER BOUND on the gaps. A destination\n * reported unserved is unserved for every cart; a destination reported served\n * may still be refused for a cart dearer or heavier than the merchant's last\n * tier. Erring in this direction is the deliberate half: the console must not\n * warn a merchant whose settings are fine.\n */\nconst COVERAGE_PROBE_CART = { subtotalCents: 0, totalGrams: 0 }\n\n/** What a merchant's saved zones and rates cost their shoppers (AGL-1791). */\nexport interface ShippingCoverage {\n /** Collectable destinations at least one rate reaches. */\n served: string[]\n /**\n * Collectable destinations CHECKOUT REFUSES — `destination-unserved`.\n *\n * EMPTY FOR A MERCHANT WHOSE RATES REACH NOWHERE, who is not misconfigured:\n * `planCheckoutShipping` refuses nobody when no rate resolves anywhere, so\n * that store charges no shipping and completes exactly as it always has.\n * The rule lives here rather than in the caller because reading `served`\n * against a bare partition is how a surface comes to warn every store that\n * does not ship.\n */\n unserved: string[]\n /** No rate reaches anywhere: no shipping is charged, and nobody refused. */\n shipsNowhere: boolean\n /**\n * Rates differ by destination, so the storefront asks the shopper where the\n * parcel is going before it can price one — a checkout step the merchant\n * added by saving these zones, and never chose.\n */\n asksDestination: boolean\n /** Some zone already claims rest of world, so `*` is not the missing piece. */\n hasRestOfWorldZone: boolean\n /**\n * The merchant offers local pickup, so checkout presents a free collection\n * choice at every destination it does not refuse (AGL-2325).\n *\n * Deliberately NOT folded into `served`: pickup resolves for every country\n * because it is appended independent of zone matching, so counting it as\n * coverage told a US-only merchant who had switched it on that checkout\n * \"can price shipping to every destination\", which is the sentence that\n * hid the defect. It is reported alongside instead, so the card can say\n * both true things: these destinations are refused, and pickup is offered\n * to the ones that are not.\n */\n offersLocalPickup: boolean\n}\n\n/**\n * Which of the collectable destinations a merchant's settings actually cover\n * (AGL-1791), for a console surface to show them.\n *\n * Measured against `CHECKOUT_SHIPPING_COUNTRIES` and nothing wider: those six\n * are the only destinations a storefront session collects an address for, so\n * they are the only ones a merchant can lose an order to. Counting coverage\n * against every country on earth would report a gap for destinations no\n * shopper can reach checkout from.\n *\n * Answered through `resolveCheckoutShippingOptions` — the same function the\n * session is built from — so a row the console allows but Stripe would reject\n * (blank id, blank name) counts as the nothing it resolves to, and a specific\n * zone with no rates still hides a `*` zone here exactly as it does at\n * checkout.\n */\nexport function summarizeShippingCoverage(\n settings: ShippingSettings | undefined,\n): ShippingCoverage {\n const served: string[] = []\n const unserved: string[] = []\n const offers = new Set<string>()\n for (const country of CHECKOUT_SHIPPING_COUNTRIES) {\n const options = resolveCheckoutShippingOptions(\n settings,\n [country],\n COVERAGE_PROBE_CART,\n )\n // `asksDestination` is asked of the FULL list — pickup is a real option a\n // shopper picks, so a merchant whose US destination offers a rate plus\n // pickup while France offers only pickup genuinely does make checkout ask.\n offers.add(optionsKey(options))\n // `served` is asked of the SHIPPABLE list, so that it answers the same\n // question `planCheckoutShipping` refuses on (AGL-2325).\n ;(shippableOnly(options).length > 0 ? served : unserved).push(country)\n }\n return {\n served,\n unserved: served.length > 0 ? unserved : [],\n shipsNowhere: served.length === 0,\n asksDestination: offers.size > 1,\n offersLocalPickup: Boolean(settings?.localPickup),\n hasRestOfWorldZone: (settings?.zones ?? []).some((zone) =>\n zone.countries.some((code) => code === '*'),\n ),\n }\n}\n\n/**\n * What the Shipping card says about a store's coverage, in words (AGL-1791): the warning's headline and body when checkout\n * turns destinations away, otherwise the one line that says what it covers, and the line that says checkout asks where the\n * parcel is going. The console card and the native apps both read these, so the same store reads the same everywhere.\n */\nexport interface ShippingCoverageWords {\n /** Set when destinations are turned away: the warning's title. */\n headline: string\n /** The warning's body; empty when nothing is turned away. */\n detail: string\n /** The caption shown when nothing is turned away; empty when something is. */\n note: string\n /** Shown when rates differ by destination; empty otherwise. */\n asks: string\n}\n\nconst coverageCountryNames = (codes: readonly string[]) =>\n codes.map((code) => CHECKOUT_SHIPPING_COUNTRY_NAMES[code] ?? code).join(', ')\n\nexport function describeShippingCoverage(\n coverage: ShippingCoverage,\n): ShippingCoverageWords {\n const asks = coverage.asksDestination\n ? 'Rates differ by destination, so checkout asks the shopper where the parcel is going before it can price one.'\n : ''\n if (coverage.unserved.length > 0) {\n return {\n headline: `Checkout turns away ${coverage.unserved.length} of the ${CHECKOUT_SHIPPING_COUNTRIES.length} destinations it collects addresses for`,\n detail:\n `No rate reaches ${coverageCountryNames(coverage.unserved)}, so an order shipping there is refused rather than posted for free. ` +\n (coverage.offersLocalPickup\n ? 'Local pickup does not cover them: it is a collection choice, not a rate, so it is offered where checkout completes and closes no gap here. '\n : '') +\n (coverage.hasRestOfWorldZone\n ? 'A rest-of-world zone is already saved, so what those destinations are missing is a rate on the zone that covers them.'\n : 'Give them a zone with a rate, or add a rest-of-world zone and price one on it.'),\n note: '',\n asks,\n }\n }\n return {\n headline: '',\n detail: '',\n note: coverage.shipsNowhere\n ? coverage.offersLocalPickup\n ? 'No rate reaches any destination, so local pickup is the only delivery choice checkout offers. Nobody is turned away and no shipping is charged.'\n : 'No rate reaches any destination, so checkout charges no shipping and turns nobody away.'\n : `Checkout can price shipping to every destination it collects an address for: ${coverageCountryNames(coverage.served)}.`,\n asks,\n }\n}\n\n/**\n * Emit `shipping_address_collection[allowed_countries][n]` onto a session's\n * form body. Stripe will not apply a `shipping_options` rate without an\n * address to ship to, so the two always travel together — and the countries\n * are the OTHER half of what makes a rate honest (AGL-1721), so pass the list\n * `planCheckoutShipping` returned rather than defaulting it beside a rate list\n * resolved for something narrower.\n */\nexport function appendShippingAddressCollectionParams(\n params: URLSearchParams,\n countries: readonly string[] = CHECKOUT_SHIPPING_COUNTRIES,\n): void {\n countries.forEach((code, index) => {\n params.set(`shipping_address_collection[allowed_countries][${index}]`, code)\n })\n}\n\n/**\n * Emit resolved rates as `shipping_options` on a Checkout Session's form body.\n *\n * THIS IS THE ONLY THING THAT MAKES STRIPE CHARGE SHIPPING. Without these keys\n * Stripe presents no shipping choice and `total_details.amount_shipping` is 0\n * however many zones and rates the merchant saved (AGL-1707).\n *\n * An empty list emits NOTHING — not an empty array — so a merchant who\n * configured no shipping keeps a session byte-identical to the one built\n * before shipping was wired at all. That guarantee lives here rather than in\n * each caller, so neither checkout path can lose it independently (AGL-1720).\n */\nexport function appendCheckoutShippingParams(\n params: URLSearchParams,\n options: readonly ResolvedShippingRate[],\n): void {\n options.forEach((option, index) => {\n const field = `shipping_options[${index}][shipping_rate_data]`\n params.set(`${field}[type]`, 'fixed_amount')\n params.set(`${field}[display_name]`, option.name.slice(0, 100))\n params.set(`${field}[fixed_amount][amount]`, String(option.amountCents))\n params.set(`${field}[fixed_amount][currency]`, 'usd')\n })\n}\n"],"names":["CARRIER_QUOTE_TIMEOUT_MS","LOCAL_PICKUP_RATE_ID","zoneCode","code","String","trim","toUpperCase","isWildcard","zoneMatches","zone","country","countries","some","tierAmount","tiers","value","sorted","sort","a","b","upTo","tier","amountCents","matchedZoneIds","settings","destinationCountry","zones","specific","filter","matched","length","Set","map","id","carrierRatesFor","zoneIds","rates","rate","kind","has","zoneId","hasCarrierRates","carrierOptionRateId","rateId","serviceKey","carrierOptionAmountCents","quotedCents","quote","Math","max","round","Number","pct","markupPct","handling","handlingCents","isFinite","min","resolveShippingRates","cart","resolved","freeOverCents","subtotalCents","totalGrams","push","name","localPickup","shippableOnly","options","option","MAX_CHECKOUT_SHIPPING_OPTIONS","CHECKOUT_SHIPPING_COUNTRIES","resolveCheckoutShippingOptions","destinationCountries","byRateId","Map","existing","get","set","values","localeCompare","slice","CHECKOUT_SHIPPING_COUNTRY_CODES","CHECKOUT_SHIPPING_COUNTRY_NAMES","US","CA","GB","AU","DE","FR","normalizeCheckoutShippingCountry","undefined","includes","merchantPricesShipping","CART_UNPRICEABLE_SHIPPING_MESSAGE","optionsKey","join","planCheckoutShipping","destination","shipsElsewhere","refusal","offers","size","union","COVERAGE_PROBE_CART","summarizeShippingCoverage","served","unserved","add","shipsNowhere","asksDestination","offersLocalPickup","Boolean","hasRestOfWorldZone","coverageCountryNames","codes","describeShippingCoverage","coverage","asks","headline","detail","note","appendShippingAddressCollectionParams","params","forEach","index","appendCheckoutShippingParams","field"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC;AAsCD,0EAA0E,GAC1E,OAAO,MAAMA,2BAA2B,KAAK;AAiC7C;;;;;;;;;CASC,GACD,OAAO,MAAMC,uBAAuB,SAAQ;AAE5C;;;;;;;;;CASC,GACD,MAAMC,WAAW,CAACC,OAA0BC,OAAOD,eAAAA,OAAQ,IAAIE,IAAI,GAAGC,WAAW;AAEjF,yDAAyD,GACzD,MAAMC,aAAa,CAACJ,OAA2BD,SAASC,UAAU;AAElE,SAASK,YAAYC,IAAkB,EAAEC,OAAe;QAC9CD;IAAR,OAAO,EAACA,kBAAAA,KAAKE,SAAS,YAAdF,kBAAkB,EAAE,EAAEG,IAAI,CAChC,CAACT,OAASI,WAAWJ,SAASD,SAASC,UAAUO;AAErD;AAEA,SAASG,WACPC,KAAiC,EACjCC,KAAa;IAEb,MAAMC,SAAS;WAAKF,gBAAAA,QAAS,EAAE;KAAE,CAACG,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAEE,IAAI,GAAGD,EAAEC,IAAI;IAChE,KAAK,MAAMC,QAAQL,OAAQ;QACzB,IAAID,SAASM,KAAKD,IAAI,EAAE,OAAOC,KAAKC,WAAW;I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,SAASR,YAAY,GACvBQ,SAASN,iBAAiB,GACxB,oJACA,4FACF,CAAC,6EAA6E,EAAEG,qBAAqBG,SAASX,MAAM,EAAE,CAAC,CAAC;QAC5HY;IACF;AACF;AAEA;;;;;;;CAOC,GACD,OAAO,SAASI,sCACdC,MAAuB,EACvBhH,YAA+B4D,2BAA2B;IAE1D5D,UAAUiH,OAAO,CAAC,CAACzH,MAAM0H;QACvBF,OAAO7C,GAAG,CAAC,CAAC,+CAA+C,EAAE+C,MAAM,CAAC,CAAC,EAAE1H;IACzE;AACF;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,SAAS2H,6BACdH,MAAuB,EACvBvD,OAAwC;IAExCA,QAAQwD,OAAO,CAAC,CAACvD,QAAQwD;QACvB,MAAME,QAAQ,CAAC,iBAAiB,EAAEF,MAAM,qBAAqB,CAAC;QAC9DF,OAAO7C,GAAG,CAAC,GAAGiD,MAAM,MAAM,CAAC,EAAE;QAC7BJ,OAAO7C,GAAG,CAAC,GAAGiD,MAAM,cAAc,CAAC,EAAE1D,OAAOJ,IAAI,CAACgB,KAAK,CAAC,GAAG;QAC1D0C,OAAO7C,GAAG,CAAC,GAAGiD,MAAM,sBAAsB,CAAC,EAAE3H,OAAOiE,OAAO/C,WAAW;QACtEqG,OAAO7C,GAAG,CAAC,GAAGiD,MAAM,wBAAwB,CAAC,EAAE;IACjD;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/commerce-shipping.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Shipping v1 (AGL-288): zones own countries; rates belong to a zone and\n * price as flat, free-over-threshold, or subtotal/weight tiers. Settings\n * live on `hosts/{hostId}/settings/store` under `shipping`. Pure.\n *\n * `resolveShippingRates` is the per-destination resolver;\n * `resolveCheckoutShippingOptions` is the Stripe Checkout adapter,\n * `planCheckoutShipping` decides which destinations one session may serve,\n * `appendCheckoutShippingParams` emits it onto a session, and\n * `summarizeShippingCoverage` answers the same question for the console, so a\n * merchant can see which destinations their zones refuse. AGL-288 shipped this\n * model with no production call site at all — the doc comment here claimed the\n * cart estimator, checkout and POS pickup used it, and none of them did, so\n * nothing ever read the settings a merchant saved and no shipping was ever\n * charged. Cart checkout calls it (AGL-1707), buy-now calls it for physical\n * one-time products (AGL-1720), and a draft order's payment link calls it for\n * physical products (AGL-1792).\n *\n * POS DOES NOT, AND SHOULD NOT (AGL-1792). A register has no destination\n * anywhere in it — no address field, no delivery choice — and all three\n * tenders settle at the counter: cash, a card QR the customer scans while\n * standing there, and a room folio collected at check-out. There is nothing to\n * price a parcel against, so the only rate POS could resolve is the free\n * `Local pickup` one this module appends, which is 0¢ and would add a step to\n * a register flow in exchange for nothing. A POS sale that must be shipped is\n * a draft order.\n */\n\nexport interface ShippingZone {\n id: string\n name: string\n /** ISO-3166 alpha-2 codes; '*' = rest of world. */\n countries: string[]\n}\n\nexport interface ShippingTier {\n /** Tier applies while subtotal (cents) or weight (grams) ≤ this. */\n upTo: number\n amountCents: number\n}\n\n/**\n * A live carrier rate (AGL-3612): the zone's price is what a carrier quotes\n * for the parcel to the shopper's address, asked through core's\n * `core.shipping-rate-quoter` seam of whichever plugin quotes carriers. With\n * no quoter, a quoter that is not available for the site, no postal code to\n * quote against, or a quote slower than {@link CARRIER_QUOTE_TIMEOUT_MS}, it\n * offers nothing and its fallback rate is offered instead.\n */\nexport interface CarrierRateSettings {\n /** Service keys (`carrier:service`) to offer; empty offers every quoted service. */\n services?: string[]\n /** Added to each quote, in percent. */\n markupPct?: number\n /** Added to each quote after the markup, in cents. */\n handlingCents?: number\n /**\n * A rate of this zone offered when no live quote is: it is HIDDEN while\n * quotes are offered, so the shopper picks between carriers, not between a\n * carrier and the merchant's guess.\n */\n fallbackRateId?: string\n}\n\n/** How long checkout waits for live carrier quotes before it falls back. */\nexport const CARRIER_QUOTE_TIMEOUT_MS = 3_000\n\nexport interface ShippingRate {\n id: string\n zoneId: string\n name: string\n kind: 'flat' | 'free_over' | 'price_tiers' | 'weight_tiers' | 'carrier'\n /** flat + free_over base amount. */\n amountCents?: number\n /** free_over: order subtotal (cents) at/above which shipping is free. */\n freeOverCents?: number\n /** price_tiers (upTo = subtotal cents) / weight_tiers (upTo = grams). */\n tiers?: ShippingTier[]\n /** carrier: the live rate's options (AGL-3612). */\n carrier?: CarrierRateSettings\n}\n\nexport interface ShippingSettings {\n zones?: ShippingZone[]\n rates?: ShippingRate[]\n /**\n * Offer free local pickup as a delivery choice. Does NOT widen where the\n * store ships: a destination no rate reaches is still refused (AGL-2325).\n */\n localPickup?: boolean\n}\n\nexport interface ResolvedShippingRate {\n rateId: string\n name: string\n amountCents: number\n}\n\n/**\n * The rate id appended for local pickup (AGL-2325).\n *\n * Exported because pickup is the one entry in a resolved list that is NOT a\n * shipping promise: it is appended AFTER and INDEPENDENT of zone matching, so\n * it resolves for every destination on earth. Anything asking \"does this store\n * ship there\" — the checkout refusal, the console coverage card — has to be\n * able to tell it apart from a rate, or it reads a merchant who offers\n * collection as a merchant who delivers worldwide.\n */\nexport const LOCAL_PICKUP_RATE_ID = 'pickup'\n\n/**\n * One stored country code, normalised (AGL-2298).\n *\n * TRIMMED as well as upper-cased. The console trims on save, so this only\n * matters for a document written any other way — an import, the REST API, a\n * hand edit — but the failure it produces is silent and expensive: `' US'`\n * upper-cases to `' US'`, matches nothing, and the store refuses every US\n * shopper with \"This store does not ship to the United States\" while its\n * settings card shows `US` right there.\n */\nconst zoneCode = (code: unknown): string => String(code ?? '').trim().toUpperCase()\n\n/** Whether a stored code is the rest-of-world wildcard. */\nconst isWildcard = (code: unknown): boolean => zoneCode(code) === '*'\n\nfunction zoneMatches(zone: ShippingZone, country: string): boolean {\n return (zone.countries ?? []).some(\n (code) => isWildcard(code) || zoneCode(code) === country,\n )\n}\n\nfunction tierAmount(\n tiers: ShippingTier[] | undefined,\n value: number,\n): number | null {\n const sorted = [...(tiers ?? [])].sort((a, b) => a.upTo - b.upTo)\n for (const tier of sorted) {\n if (value <= tier.upTo) return tier.amountCents\n }\n // Beyond the last tier the rate does not apply (merchant should add a\n // catch-all tier with a large upTo).\n return null\n}\n\n/**\n * The zones a destination falls in (AGL-1707, factored out for AGL-3612).\n * Specific country zones beat '*' zones: when any zone names the country\n * outright, rest-of-world zones are ignored.\n */\nexport function matchedZoneIds(\n settings: ShippingSettings | undefined,\n destinationCountry: string,\n): Set<string> {\n const country = destinationCountry.toUpperCase()\n const zones = settings?.zones ?? []\n // A zone is SPECIFIC when it names this country outright — not merely when\n // it is \"not wildcard-only\" (AGL-2298). The old test was `!every(code ===\n // '*')`, so a zone spelled `['*','US']` counted as specific for all six\n // destinations (it matches every country through the `*`) and suppressed\n // every genuine rest-of-world zone globally. Asking the narrower question\n // makes such a zone specific for `US` and wildcard for the rest, which is\n // what its two entries actually say.\n const specific = zones.filter((zone) =>\n (zone.countries ?? []).some((code) => zoneCode(code) === country),\n )\n const matched = specific.length\n ? specific\n : zones.filter((zone) => zoneMatches(zone, country))\n return new Set(matched.map((zone) => zone.id))\n}\n\n/** The live carrier rates a destination's zones carry (AGL-3612). */\nexport function carrierRatesFor(\n settings: ShippingSettings | undefined,\n destinationCountry: string,\n): ShippingRate[] {\n if (!settings) return []\n const zoneIds = matchedZoneIds(settings, destinationCountry)\n return (settings.rates ?? []).filter(\n (rate) => rate.kind === 'carrier' && zoneIds.has(rate.zoneId),\n )\n}\n\n/** Whether any zone carries a live carrier rate (AGL-3612). */\nexport function hasCarrierRates(settings: ShippingSettings | undefined): boolean {\n return (settings?.rates ?? []).some((rate) => rate.kind === 'carrier')\n}\n\n/** The rate id a quoted service is offered under: stable per rate and service. */\nexport function carrierOptionRateId(rateId: string, serviceKey: string): string {\n return `carrier:${rateId}:${serviceKey}`\n}\n\n/**\n * What a shopper pays for a carrier's quote under one carrier rate: the\n * quote, plus the markup, plus the handling fee, in whole cents.\n */\nexport function carrierOptionAmountCents(\n quotedCents: number,\n settings: CarrierRateSettings | undefined,\n): number {\n const quote = Math.max(0, Math.round(Number(quotedCents) || 0))\n const pct = Number(settings?.markupPct)\n const handling = Number(settings?.handlingCents)\n return (\n quote +\n (Number.isFinite(pct) && pct > 0 ? Math.round((quote * Math.min(pct, 500)) / 100) : 0) +\n (Number.isFinite(handling) && handling > 0 ? Math.round(handling) : 0)\n )\n}\n\n/**\n * Rates available for a destination + cart, cheapest first. Specific\n * country zones beat '*' zones: when any specific zone matches, '*'\n * zones are ignored (rest-of-world semantics).\n *\n * Local pickup is appended AFTER and INDEPENDENT of zone matching, so this\n * list is never empty for a merchant who offers it, at any destination. That\n * is correct for a resolver — a shopper anywhere may choose to come and\n * collect — but it means A NON-EMPTY RESULT IS NOT A PROMISE TO SHIP THERE.\n * Callers deciding whether a destination is served must filter pickup out\n * first; `planCheckoutShipping` and `summarizeShippingCoverage` do (AGL-2325).\n */\nexport function resolveShippingRates(\n settings: ShippingSettings | undefined,\n destinationCountry: string | undefined,\n cart: { subtotalCents: number; totalGrams?: number },\n): ResolvedShippingRate[] {\n if (!settings || !destinationCountry) return []\n const zoneIds = matchedZoneIds(settings, destinationCountry)\n // A zone is SPECIFIC when it names this country outright — not merely when\n // it is \"not wildcard-only\" (AGL-2298). The old test was `!every(code ===\n // '*')`, so a zone spelled `['*','US']` counted as specific for all six\n // destinations (it matches every country through the `*`) and suppressed\n // every genuine rest-of-world zone globally. Asking the narrower question\n // makes such a zone specific for `US` and wildcard for the rest, which is\n // what its two entries actually say.\n const resolved: ResolvedShippingRate[] = []\n for (const rate of settings.rates ?? []) {\n if (!zoneIds.has(rate.zoneId)) continue\n let amountCents: number | null = null\n switch (rate.kind) {\n case 'flat':\n amountCents = Math.max(0, Math.round(rate.amountCents ?? 0))\n break\n case 'free_over':\n amountCents =\n rate.freeOverCents != null &&\n cart.subtotalCents >= rate.freeOverCents\n ? 0\n : Math.max(0, Math.round(rate.amountCents ?? 0))\n break\n case 'price_tiers':\n amountCents = tierAmount(rate.tiers, cart.subtotalCents)\n break\n case 'weight_tiers':\n amountCents = tierAmount(rate.tiers, cart.totalGrams ?? 0)\n break\n // A carrier's price exists only once a carrier quoted it for an\n // address (AGL-3612), so a table read offers nothing for it and its\n // fallback rate is what resolves here. `carrier-shipping.ts` adds the\n // quotes where one can be had.\n case 'carrier':\n break\n }\n if (amountCents == null) continue\n resolved.push({ rateId: rate.id, name: rate.name, amountCents })\n }\n if (settings.localPickup) {\n resolved.push({\n rateId: LOCAL_PICKUP_RATE_ID,\n name: 'Local pickup',\n amountCents: 0,\n })\n }\n return resolved.sort((a, b) => a.amountCents - b.amountCents)\n}\n\n/**\n * The resolved options that actually carry a parcel (AGL-2325).\n *\n * Safe to apply AFTER `MAX_CHECKOUT_SHIPPING_OPTIONS` truncation: pickup is\n * 0¢ and sorts first, so it occupies at most one of the five slots and a\n * merchant with any shippable rate at all keeps at least one of them.\n */\nfunction shippableOnly(\n options: readonly ResolvedShippingRate[],\n): ResolvedShippingRate[] {\n return options.filter((option) => option.rateId !== LOCAL_PICKUP_RATE_ID)\n}\n\n/** Stripe Checkout accepts at most 5 `shipping_options` on one session. */\nexport const MAX_CHECKOUT_SHIPPING_OPTIONS = 5\n\n/**\n * The destinations a Checkout Session collects an address for when the\n * merchant prices NO shipping (AGL-1707), and so the session such a store has\n * always had. A merchant with zones gets `checkoutShippingCountries` instead:\n * the countries their zones name, or every country for rest of world.\n */\nexport const CHECKOUT_SHIPPING_COUNTRIES = [\n 'US',\n 'CA',\n 'GB',\n 'AU',\n 'DE',\n 'FR',\n] as const\n\n/**\n * The rates to declare as `shipping_options` on a Stripe Checkout Session\n * (AGL-1707), unioned over `destinationCountries`. Stripe charges shipping\n * ONLY when the session declares them.\n *\n * A UNION IS ONLY HONEST OVER DESTINATIONS THE SESSION IS ALSO RESTRICTED TO.\n * Called with every collectable country while the session accepted an address\n * in any of them — which is what AGL-1707 shipped — it offers a shopper in one\n * zone a rate belonging to another, and Stripe applies whichever the shopper\n * picks (a `shipping_rate_data` carries no country of its own, so nothing\n * downstream re-checks it against the address). That is AGL-1721. Callers now\n * reach this through `planCheckoutShipping`, which pairs the country list it\n * unions over with the country list the session allows, so the two cannot\n * disagree. Do not call it directly from a handler.\n *\n * Returns `[]` for a merchant who has configured nothing, and the caller must\n * then declare no `shipping_options` at all rather than an empty array — a\n * merchant who never set shipping up keeps a session identical to today's.\n */\nexport function resolveCheckoutShippingOptions(\n settings: ShippingSettings | undefined,\n destinationCountries: readonly string[],\n cart: { subtotalCents: number; totalGrams?: number },\n): ResolvedShippingRate[] {\n if (!settings) return []\n const byRateId = new Map<string, ResolvedShippingRate>()\n for (const country of destinationCountries) {\n for (const rate of resolveShippingRates(settings, country, cart)) {\n // Stripe requires a display name, and a rate id is the dedupe key —\n // the console lets a merchant add a blank row, so neither is assured.\n if (!rate.rateId || !rate.name) continue\n const amountCents = Math.max(0, Math.round(Number(rate.amountCents) || 0))\n const existing = byRateId.get(rate.rateId)\n // One rate resolving differently for two countries should not be\n // possible (a zone selects a rate, it does not reprice one), but if it\n // ever is, keep the dearer. Under-charging is the defect being fixed.\n if (!existing || amountCents > existing.amountCents) {\n byRateId.set(rate.rateId, { ...rate, amountCents })\n }\n }\n }\n return (\n [...byRateId.values()]\n // Cheapest first, id as a tie-break so the emitted params are stable.\n .sort(\n (a, b) =>\n a.amountCents - b.amountCents || a.rateId.localeCompare(b.rateId),\n )\n // Past Stripe's cap the dearest are dropped, which is the harmless end\n // to lose: a shopper offered the cheap options would have taken one.\n .slice(0, MAX_CHECKOUT_SHIPPING_OPTIONS)\n )\n}\n\n/**\n * {@link CHECKOUT_SHIPPING_COUNTRIES} as plain codes: the apps' draft order\n * falls back to it only when the server names no list (AGL-3841).\n */\nexport const CHECKOUT_SHIPPING_COUNTRY_CODES: readonly string[] = CHECKOUT_SHIPPING_COUNTRIES\n\n/**\n * Every country Stripe Checkout can collect a shipping address for\n * (`shipping_address_collection[allowed_countries]`), with the name a shopper\n * reads. A rest-of-world `*` zone opens checkout to all of them.\n */\nexport const CHECKOUT_SHIPPING_COUNTRY_NAMES: Readonly<Record<string, string>> =\n {\n AC: \"Ascension Island\",\n AD: \"Andorra\",\n AE: \"United Arab Emirates\",\n AF: \"Afghanistan\",\n AG: \"Antigua & Barbuda\",\n AI: \"Anguilla\",\n AL: \"Albania\",\n AM: \"Armenia\",\n AO: \"Angola\",\n AQ: \"Antarctica\",\n AR: \"Argentina\",\n AT: \"Austria\",\n AU: \"Australia\",\n AW: \"Aruba\",\n AX: \"Åland Islands\",\n AZ: \"Azerbaijan\",\n BA: \"Bosnia & Herzegovina\",\n BB: \"Barbados\",\n BD: \"Bangladesh\",\n BE: \"Belgium\",\n BF: \"Burkina Faso\",\n BG: \"Bulgaria\",\n BH: \"Bahrain\",\n BI: \"Burundi\",\n BJ: \"Benin\",\n BL: \"St. Barthélemy\",\n BM: \"Bermuda\",\n BN: \"Brunei\",\n BO: \"Bolivia\",\n BQ: \"Caribbean Netherlands\",\n BR: \"Brazil\",\n BS: \"Bahamas\",\n BT: \"Bhutan\",\n BV: \"Bouvet Island\",\n BW: \"Botswana\",\n BY: \"Belarus\",\n BZ: \"Belize\",\n CA: \"Canada\",\n CD: \"Congo - Kinshasa\",\n CF: \"Central African Republic\",\n CG: \"Congo - Brazzaville\",\n CH: \"Switzerland\",\n CI: \"Côte d’Ivoire\",\n CK: \"Cook Islands\",\n CL: \"Chile\",\n CM: \"Cameroon\",\n CN: \"China\",\n CO: \"Colombia\",\n CR: \"Costa Rica\",\n CV: \"Cape Verde\",\n CW: \"Curaçao\",\n CY: \"Cyprus\",\n CZ: \"Czechia\",\n DE: \"Germany\",\n DJ: \"Djibouti\",\n DK: \"Denmark\",\n DM: \"Dominica\",\n DO: \"Dominican Republic\",\n DZ: \"Algeria\",\n EC: \"Ecuador\",\n EE: \"Estonia\",\n EG: \"Egypt\",\n EH: \"Western Sahara\",\n ER: \"Eritrea\",\n ES: \"Spain\",\n ET: \"Ethiopia\",\n FI: \"Finland\",\n FJ: \"Fiji\",\n FK: \"Falkland Islands\",\n FO: \"Faroe Islands\",\n FR: \"France\",\n GA: \"Gabon\",\n GB: \"United Kingdom\",\n GD: \"Grenada\",\n GE: \"Georgia\",\n GF: \"French Guiana\",\n GG: \"Guernsey\",\n GH: \"Ghana\",\n GI: \"Gibraltar\",\n GL: \"Greenland\",\n GM: \"Gambia\",\n GN: \"Guinea\",\n GP: \"Guadeloupe\",\n GQ: \"Equatorial Guinea\",\n GR: \"Greece\",\n GS: \"South Georgia & South Sandwich Islands\",\n GT: \"Guatemala\",\n GU: \"Guam\",\n GW: \"Guinea-Bissau\",\n GY: \"Guyana\",\n HK: \"Hong Kong SAR China\",\n HN: \"Honduras\",\n HR: \"Croatia\",\n HT: \"Haiti\",\n HU: \"Hungary\",\n ID: \"Indonesia\",\n IE: \"Ireland\",\n IL: \"Israel\",\n IM: \"Isle of Man\",\n IN: \"India\",\n IO: \"British Indian Ocean Territory\",\n IQ: \"Iraq\",\n IS: \"Iceland\",\n IT: \"Italy\",\n JE: \"Jersey\",\n JM: \"Jamaica\",\n JO: \"Jordan\",\n JP: \"Japan\",\n KE: \"Kenya\",\n KG: \"Kyrgyzstan\",\n KH: \"Cambodia\",\n KI: \"Kiribati\",\n KM: \"Comoros\",\n KN: \"St. Kitts & Nevis\",\n KR: \"South Korea\",\n KW: \"Kuwait\",\n KY: \"Cayman Islands\",\n KZ: \"Kazakhstan\",\n LA: \"Laos\",\n LB: \"Lebanon\",\n LC: \"St. Lucia\",\n LI: \"Liechtenstein\",\n LK: \"Sri Lanka\",\n LR: \"Liberia\",\n LS: \"Lesotho\",\n LT: \"Lithuania\",\n LU: \"Luxembourg\",\n LV: \"Latvia\",\n LY: \"Libya\",\n MA: \"Morocco\",\n MC: \"Monaco\",\n MD: \"Moldova\",\n ME: \"Montenegro\",\n MF: \"St. Martin\",\n MG: \"Madagascar\",\n MK: \"North Macedonia\",\n ML: \"Mali\",\n MM: \"Myanmar (Burma)\",\n MN: \"Mongolia\",\n MO: \"Macao SAR China\",\n MQ: \"Martinique\",\n MR: \"Mauritania\",\n MS: \"Montserrat\",\n MT: \"Malta\",\n MU: \"Mauritius\",\n MV: \"Maldives\",\n MW: \"Malawi\",\n MX: \"Mexico\",\n MY: \"Malaysia\",\n MZ: \"Mozambique\",\n NA: \"Namibia\",\n NC: \"New Caledonia\",\n NE: \"Niger\",\n NG: \"Nigeria\",\n NI: \"Nicaragua\",\n NL: \"Netherlands\",\n NO: \"Norway\",\n NP: \"Nepal\",\n NR: \"Nauru\",\n NU: \"Niue\",\n NZ: \"New Zealand\",\n OM: \"Oman\",\n PA: \"Panama\",\n PE: \"Peru\",\n PF: \"French Polynesia\",\n PG: \"Papua New Guinea\",\n PH: \"Philippines\",\n PK: \"Pakistan\",\n PL: \"Poland\",\n PM: \"St. Pierre & Miquelon\",\n PN: \"Pitcairn Islands\",\n PR: \"Puerto Rico\",\n PS: \"Palestinian Territories\",\n PT: \"Portugal\",\n PY: \"Paraguay\",\n QA: \"Qatar\",\n RE: \"Réunion\",\n RO: \"Romania\",\n RS: \"Serbia\",\n RU: \"Russia\",\n RW: \"Rwanda\",\n SA: \"Saudi Arabia\",\n SB: \"Solomon Islands\",\n SC: \"Seychelles\",\n SE: \"Sweden\",\n SG: \"Singapore\",\n SH: \"St. Helena\",\n SI: \"Slovenia\",\n SJ: \"Svalbard & Jan Mayen\",\n SK: \"Slovakia\",\n SL: \"Sierra Leone\",\n SM: \"San Marino\",\n SN: \"Senegal\",\n SO: \"Somalia\",\n SR: \"Suriname\",\n SS: \"South Sudan\",\n ST: \"São Tomé & Príncipe\",\n SV: \"El Salvador\",\n SX: \"Sint Maarten\",\n SZ: \"Eswatini\",\n TA: \"Tristan da Cunha\",\n TC: \"Turks & Caicos Islands\",\n TD: \"Chad\",\n TF: \"French Southern Territories\",\n TG: \"Togo\",\n TH: \"Thailand\",\n TJ: \"Tajikistan\",\n TK: \"Tokelau\",\n TL: \"Timor-Leste\",\n TM: \"Turkmenistan\",\n TN: \"Tunisia\",\n TO: \"Tonga\",\n TR: \"Türkiye\",\n TT: \"Trinidad & Tobago\",\n TV: \"Tuvalu\",\n TW: \"Taiwan\",\n TZ: \"Tanzania\",\n UA: \"Ukraine\",\n UG: \"Uganda\",\n US: \"United States\",\n UY: \"Uruguay\",\n UZ: \"Uzbekistan\",\n VA: \"Vatican City\",\n VC: \"St. Vincent & Grenadines\",\n VE: \"Venezuela\",\n VG: \"British Virgin Islands\",\n VN: \"Vietnam\",\n VU: \"Vanuatu\",\n WF: \"Wallis & Futuna\",\n WS: \"Samoa\",\n XK: \"Kosovo\",\n YE: \"Yemen\",\n YT: \"Mayotte\",\n ZA: \"South Africa\",\n ZM: \"Zambia\",\n ZW: \"Zimbabwe\",\n }\n\n/** {@link CHECKOUT_SHIPPING_COUNTRY_NAMES}' codes, in order. */\nexport const STRIPE_SHIPPING_COUNTRY_CODES: readonly string[] = Object.keys(\n CHECKOUT_SHIPPING_COUNTRY_NAMES,\n)\n\n/**\n * The countries ONE merchant's checkout collects an address for: the\n * destinations their own zones name (international selling).\n *\n * `CHECKOUT_SHIPPING_COUNTRIES` used to be the only list, so a shopper in New\n * Zealand, Ireland or any EU country but Germany and France could not enter an\n * address at all, whatever zones the merchant saved. Now:\n *\n * - every country a zone names, in the order the merchant wrote them;\n * - every Stripe-collectable country when a zone claims rest of world (`*`);\n * - today's six for a merchant who prices no shipping (no zones and rates, or\n * pickup only), so a store that never set shipping up keeps the session it\n * has today, byte for byte.\n *\n * The AGL-1721 rule is unchanged, because `planCheckoutShipping` still pairs\n * this list with the options it unions over it.\n */\nexport function checkoutShippingCountries(\n settings: ShippingSettings | undefined,\n): readonly string[] {\n if (!merchantPricesShipping(settings) || !settings?.zones?.length) {\n return CHECKOUT_SHIPPING_COUNTRIES\n }\n const named: string[] = []\n for (const zone of settings.zones) {\n for (const raw of zone.countries ?? []) {\n const code = String(raw).trim().toUpperCase()\n if (code === '*') return STRIPE_SHIPPING_COUNTRY_CODES\n if (code in CHECKOUT_SHIPPING_COUNTRY_NAMES && !named.includes(code)) {\n named.push(code)\n }\n }\n }\n return named.length ? named : CHECKOUT_SHIPPING_COUNTRIES\n}\n\n/**\n * A destination a shopper declared, or `undefined` for anything this session\n * could not collect an address for anyway. Never throws: an unknown code is\n * an undeclared destination, not an error, so a stale storefront that posts\n * one is answered by the plan below rather than by a 400 nobody can act on.\n */\nexport function normalizeCheckoutShippingCountry(\n value: unknown,\n countries: readonly string[] = STRIPE_SHIPPING_COUNTRY_CODES,\n): string | undefined {\n // `typeof` rather than `String(value)`: a JSON body is caller-shaped, and\n // `String(['US'])` is 'US'. Coercing would let an array declare a\n // destination, which is not a spelling any storefront sends.\n if (typeof value !== 'string') return undefined\n const code = value.trim().toUpperCase()\n return countries.includes(code) ? code : undefined\n}\n\nexport interface CheckoutShippingPlan {\n /** `shipping_address_collection[allowed_countries]` for this session. */\n countries: readonly string[]\n /** `shipping_options` for this session — valid for EVERY `countries` entry. */\n options: ResolvedShippingRate[]\n /**\n * Set when no session may be created at all:\n *\n * - `destination-required` — the merchant's rates differ by destination, so\n * there is no set of options honest for every address Stripe would accept.\n * Ask the shopper where they are shipping and plan again.\n * - `destination-unserved` — the merchant ships somewhere, but not there.\n * - `cart-unpriceable` — the merchant priced shipping, but no rate of theirs\n * resolves for THIS cart at ANY destination (AGL-2232). A tier table that\n * stops at 2 kg says nothing about a 3 kg parcel, and the honest answer is\n * to refuse rather than to ship it for nothing.\n */\n refusal?: 'destination-required' | 'destination-unserved' | 'cart-unpriceable'\n}\n\n/**\n * Whether this merchant prices shipping AT ALL (AGL-2232).\n *\n * A pure question about the SETTINGS, deliberately taking no cart, and that is\n * the whole point of it existing. `planCheckoutShipping` used to ask \"does any\n * rate resolve for any destination?\" and pass the live cart into the probe, so\n * a cart that exhausted every tier made the probe empty too — and an empty\n * probe reads as \"this merchant never set shipping up\", the one case that must\n * complete with no `shipping_options` and nothing charged. A 3 kg order at a\n * store whose weight tiers stop at 2 kg therefore shipped free, silently, on\n * every one of the three doors.\n *\n * Answered from the stored zones and rates instead: a rate bound to a zone\n * that exists, or local pickup, means the merchant made a shipping decision\n * and a cart their table cannot price is a REFUSAL. A merchant with no zones,\n * no rates and no pickup has made no such decision, and their session stays\n * byte-identical to the one AGL-1707 shipped.\n */\nexport function merchantPricesShipping(\n settings: ShippingSettings | undefined,\n): boolean {\n if (!settings) return false\n if (settings.localPickup) return true\n const zoneIds = new Set((settings.zones ?? []).map((zone) => zone.id))\n // A rate whose `zoneId` names no zone is unreachable by every resolver here,\n // so it is not evidence of a decision — counting it would refuse carts at a\n // store with nothing but orphaned rows.\n return (settings.rates ?? []).some((rate) => zoneIds.has(rate.zoneId))\n}\n\n/**\n * What a shopper is told when `cart-unpriceable` refuses (AGL-2232).\n *\n * One string, shared by all three doors, because the three used to phrase the\n * two existing refusals identically and drifting here would make the same\n * store say different things depending on which button was pressed. It names\n * the cart rather than the destination on purpose — a `Ship to` select cannot\n * fix a parcel no tier covers, so the refusal deliberately does NOT set\n * `needsShippingCountry`.\n */\nexport const CART_UNPRICEABLE_SHIPPING_MESSAGE =\n 'This store cannot price shipping for this order — it falls outside every ' +\n 'shipping rate the store has set up. Try removing an item, or contact the ' +\n 'store.'\n\n/** Identity of a resolved list, for comparing two destinations' offers. */\nfunction optionsKey(options: readonly ResolvedShippingRate[]): string {\n return options\n .map((option) => `${option.rateId}:${option.amountCents}`)\n .join('|')\n}\n\n/**\n * The whole shipping half of a Checkout Session: which destinations it may\n * accept an address for, and which rates it may charge (AGL-1721).\n *\n * THE INVARIANT IS THAT EVERY RETURNED OPTION IS VALID FOR EVERY RETURNED\n * COUNTRY. Stripe offers a session's `shipping_options` to whoever reaches the\n * page and charges whichever the shopper picks — it does not filter them by\n * the address entered, and there is no field on a `shipping_rate_data` that\n * would let it. So the ONLY place a zone can be enforced is here, by choosing\n * the two lists together: narrowing the allowed countries to the declared\n * destination is what makes the rates offered for it unpickable by anyone\n * else. Filtering the rate list alone would leave a session that still accepts\n * a French address against a US rate.\n *\n * Three shapes come out of it:\n *\n * 1. A merchant whose rates resolve the SAME for every collectable country —\n * one zone, a single '*' zone, or none at all — needs no destination and\n * keeps the session AGL-1707 shipped, byte for byte. The issue notes this\n * is the common configuration, and it is the one that must not regress.\n * 2. A declared destination resolves exactly, and the session is restricted\n * to it. A caller may not skip the restriction: the country arrives in a\n * request body a shopper can craft, so a session that took the country's\n * cheap rate while still accepting any address would hand that shopper\n * the defect on purpose.\n * 3. Otherwise the caller must refuse — see `refusal`. Refusing is not lost\n * revenue in either case: with rates that differ by zone there is no\n * correct amount to charge without knowing where the parcel is going, and\n * the union AGL-1707 shipped chose the wrong one silently.\n *\n * `cart.subtotalCents` is the PRE-discount items total, matching what the cart\n * displays, so a free-over threshold means what the shopper saw it mean.\n */\nexport function planCheckoutShipping(\n settings: ShippingSettings | undefined,\n cart: { subtotalCents: number; totalGrams?: number },\n destinationCountry?: unknown,\n): CheckoutShippingPlan {\n const countries = checkoutShippingCountries(settings)\n // Any Stripe-collectable code counts as DECLARED, so a shopper naming a\n // country this store's zones do not reach is told it is not served rather\n // than asked again. Only a store whose list comes from its zones refuses\n // that way: one on the fallback list ships nowhere in particular, and an\n // outside code stays undeclared there exactly as it always was.\n const declared = normalizeCheckoutShippingCountry(destinationCountry)\n const fromZones = countries !== CHECKOUT_SHIPPING_COUNTRIES\n if (declared && !countries.includes(declared) && fromZones) {\n return { countries, options: [], refusal: 'destination-unserved' }\n }\n const destination =\n declared && countries.includes(declared) ? declared : undefined\n if (destination) {\n const options = resolveCheckoutShippingOptions(\n settings,\n [destination],\n cart,\n )\n // REFUSAL IS DECIDED ON SHIPPABLE RATES, PICKUP IS OFFERED AFTERWARDS\n // (AGL-2325). `resolveShippingRates` appends pickup independent of zone\n // matching, so a merchant who turned it on had a non-empty list for every\n // destination on earth and this branch returned before it could refuse\n // anyone: a shopper in France picked `Local pickup — Free` on a physical\n // order from a US-only store, Stripe collected their French address\n // because the session still allowed it, and the merchant was paid nothing\n // to carry the parcel there. Pickup is collection, not delivery, so it\n // cannot answer \"do you ship to France\".\n //\n // `shipsElsewhere` is the same question the old probe asked, narrowed to\n // shippable rates, and it keeps the same two-way fork: a merchant who\n // configured NO shipping is not refusing anyone, so that store keeps\n // completing exactly as it does today, while a merchant who ships\n // elsewhere but not here is refusing and saying so is the honest answer.\n // A merchant who ships NOWHERE is untouched by this: pickup-only is a\n // real configuration — they deliver to no one and refuse no one — and it\n // falls through to the offer below exactly as it did.\n const shipsElsewhere = countries.some(\n (country) =>\n shippableOnly(resolveCheckoutShippingOptions(settings, [country], cart))\n .length > 0,\n )\n if (shippableOnly(options).length === 0 && shipsElsewhere) {\n return { countries, options: [], refusal: 'destination-unserved' }\n }\n if (options.length > 0) return { countries: [destination], options }\n // NOTHING resolves anywhere for this cart, and the two reasons that can be\n // true of are opposite (AGL-2232). Asked of the SETTINGS rather than of\n // this cart, because the probe above cannot tell them apart: a merchant\n // who priced shipping and whose table simply does not reach this order is\n // refusing, and used to be handed the \"never set shipping up\" branch.\n return merchantPricesShipping(settings)\n ? { countries, options: [], refusal: 'cart-unpriceable' }\n : { countries, options: [] }\n }\n const offers = new Set(\n countries.map((country) =>\n optionsKey(resolveCheckoutShippingOptions(settings, [country], cart)),\n ),\n )\n // Compared as EMITTED — capped and sanitized — because that is the list a\n // shopper can pick from. Two destinations whose rates differ only past\n // Stripe's cap still offer the same five, and offering them is honest.\n if (offers.size > 1) {\n return { countries, options: [], refusal: 'destination-required' }\n }\n const union = resolveCheckoutShippingOptions(settings, countries, cart)\n // Every destination agrees, and what they agree on is NOTHING. The same\n // fork as the declared-destination branch above (AGL-2232): a store with no\n // shipping settings completes untouched, a store whose rates cannot reach\n // this cart refuses instead of carrying the parcel for free.\n if (union.length === 0 && merchantPricesShipping(settings)) {\n return { countries, options: [], refusal: 'cart-unpriceable' }\n }\n return { countries, options: union }\n}\n\n/**\n * A cart chosen so that every rate a merchant could have written resolves if\n * it can resolve at all (AGL-1791). The settings page has no cart, and two of\n * the four rate kinds price against one: `tierAmount` takes the first tier\n * whose `upTo` is at least the value, so a zero subtotal and zero weight clear\n * every tier a merchant would write, and a `free_over` rate resolves at any\n * subtotal.\n *\n * That makes a coverage answer a LOWER BOUND on the gaps. A destination\n * reported unserved is unserved for every cart; a destination reported served\n * may still be refused for a cart dearer or heavier than the merchant's last\n * tier. Erring in this direction is the deliberate half: the console must not\n * warn a merchant whose settings are fine.\n */\nconst COVERAGE_PROBE_CART = { subtotalCents: 0, totalGrams: 0 }\n\n/** What a merchant's saved zones and rates cost their shoppers (AGL-1791). */\nexport interface ShippingCoverage {\n /** How many destinations checkout collects an address for. */\n collectable: number\n /** Collectable destinations at least one rate reaches. */\n served: string[]\n /**\n * Collectable destinations CHECKOUT REFUSES — `destination-unserved`.\n *\n * EMPTY FOR A MERCHANT WHOSE RATES REACH NOWHERE, who is not misconfigured:\n * `planCheckoutShipping` refuses nobody when no rate resolves anywhere, so\n * that store charges no shipping and completes exactly as it always has.\n * The rule lives here rather than in the caller because reading `served`\n * against a bare partition is how a surface comes to warn every store that\n * does not ship.\n */\n unserved: string[]\n /** No rate reaches anywhere: no shipping is charged, and nobody refused. */\n shipsNowhere: boolean\n /**\n * Rates differ by destination, so the storefront asks the shopper where the\n * parcel is going before it can price one — a checkout step the merchant\n * added by saving these zones, and never chose.\n */\n asksDestination: boolean\n /** Some zone already claims rest of world, so `*` is not the missing piece. */\n hasRestOfWorldZone: boolean\n /**\n * The merchant offers local pickup, so checkout presents a free collection\n * choice at every destination it does not refuse (AGL-2325).\n *\n * Deliberately NOT folded into `served`: pickup resolves for every country\n * because it is appended independent of zone matching, so counting it as\n * coverage told a US-only merchant who had switched it on that checkout\n * \"can price shipping to every destination\", which is the sentence that\n * hid the defect. It is reported alongside instead, so the card can say\n * both true things: these destinations are refused, and pickup is offered\n * to the ones that are not.\n */\n offersLocalPickup: boolean\n}\n\n/**\n * Which of the collectable destinations a merchant's settings actually cover\n * (AGL-1791), for a console surface to show them.\n *\n * Measured against `checkoutShippingCountries(settings)` and nothing wider:\n * those are the only destinations this store's session collects an address\n * for, so they are the only ones a merchant can lose an order to.\n *\n * Answered through `resolveCheckoutShippingOptions` — the same function the\n * session is built from — so a row the console allows but Stripe would reject\n * (blank id, blank name) counts as the nothing it resolves to, and a specific\n * zone with no rates still hides a `*` zone here exactly as it does at\n * checkout.\n */\nexport function summarizeShippingCoverage(\n settings: ShippingSettings | undefined,\n): ShippingCoverage {\n const served: string[] = []\n const unserved: string[] = []\n const offers = new Set<string>()\n const collectable = checkoutShippingCountries(settings)\n for (const country of collectable) {\n const options = resolveCheckoutShippingOptions(\n settings,\n [country],\n COVERAGE_PROBE_CART,\n )\n // `asksDestination` is asked of the FULL list — pickup is a real option a\n // shopper picks, so a merchant whose US destination offers a rate plus\n // pickup while France offers only pickup genuinely does make checkout ask.\n offers.add(optionsKey(options))\n // `served` is asked of the SHIPPABLE list, so that it answers the same\n // question `planCheckoutShipping` refuses on (AGL-2325).\n ;(shippableOnly(options).length > 0 ? served : unserved).push(country)\n }\n return {\n collectable: collectable.length,\n served,\n unserved: served.length > 0 ? unserved : [],\n shipsNowhere: served.length === 0,\n asksDestination: offers.size > 1,\n offersLocalPickup: Boolean(settings?.localPickup),\n hasRestOfWorldZone: (settings?.zones ?? []).some((zone) =>\n zone.countries.some((code) => code === '*'),\n ),\n }\n}\n\n/**\n * What the Shipping card says about a store's coverage, in words (AGL-1791): the warning's headline and body when checkout\n * turns destinations away, otherwise the one line that says what it covers, and the line that says checkout asks where the\n * parcel is going. The console card and the native apps both read these, so the same store reads the same everywhere.\n */\nexport interface ShippingCoverageWords {\n /** Set when destinations are turned away: the warning's title. */\n headline: string\n /** The warning's body; empty when nothing is turned away. */\n detail: string\n /** The caption shown when nothing is turned away; empty when something is. */\n note: string\n /** Shown when rates differ by destination; empty otherwise. */\n asks: string\n}\n\n/** Names, the first eight in full: a rest-of-world gap can be 200 countries. */\nconst coverageCountryNames = (codes: readonly string[]) => {\n const names = codes.map((code) => CHECKOUT_SHIPPING_COUNTRY_NAMES[code] ?? code)\n return names.length > 8\n ? `${names.slice(0, 8).join(', ')} and ${names.length - 8} more`\n : names.join(', ')\n}\n\nexport function describeShippingCoverage(\n coverage: ShippingCoverage,\n): ShippingCoverageWords {\n const asks = coverage.asksDestination\n ? 'Rates differ by destination, so checkout asks the shopper where the parcel is going before it can price one.'\n : ''\n if (coverage.unserved.length > 0) {\n return {\n headline: `Checkout turns away ${coverage.unserved.length} of the ${coverage.collectable} destinations it collects addresses for`,\n detail:\n `No rate reaches ${coverageCountryNames(coverage.unserved)}, so an order shipping there is refused rather than posted for free. ` +\n (coverage.offersLocalPickup\n ? 'Local pickup does not cover them: it is a collection choice, not a rate, so it is offered where checkout completes and closes no gap here. '\n : '') +\n (coverage.hasRestOfWorldZone\n ? 'A rest-of-world zone is already saved, so what those destinations are missing is a rate on the zone that covers them.'\n : 'Give them a zone with a rate, or add a rest-of-world zone and price one on it.'),\n note: '',\n asks,\n }\n }\n return {\n headline: '',\n detail: '',\n note: coverage.shipsNowhere\n ? coverage.offersLocalPickup\n ? 'No rate reaches any destination, so local pickup is the only delivery choice checkout offers. Nobody is turned away and no shipping is charged.'\n : 'No rate reaches any destination, so checkout charges no shipping and turns nobody away.'\n : coverage.hasRestOfWorldZone &&\n coverage.served.length === STRIPE_SHIPPING_COUNTRY_CODES.length\n ? 'Checkout can price shipping to every country it can collect an address for.'\n : `Checkout can price shipping to every destination it collects an address for: ${coverageCountryNames(coverage.served)}.`,\n asks,\n }\n}\n\n/**\n * Emit `shipping_address_collection[allowed_countries][n]` onto a session's\n * form body. Stripe will not apply a `shipping_options` rate without an\n * address to ship to, so the two always travel together — and the countries\n * are the OTHER half of what makes a rate honest (AGL-1721), so pass the list\n * `planCheckoutShipping` returned rather than defaulting it beside a rate list\n * resolved for something narrower.\n */\nexport function appendShippingAddressCollectionParams(\n params: URLSearchParams,\n countries: readonly string[] = CHECKOUT_SHIPPING_COUNTRIES,\n): void {\n countries.forEach((code, index) => {\n params.set(`shipping_address_collection[allowed_countries][${index}]`, code)\n })\n}\n\n/**\n * Emit resolved rates as `shipping_options` on a Checkout Session's form body.\n *\n * THIS IS THE ONLY THING THAT MAKES STRIPE CHARGE SHIPPING. Without these keys\n * Stripe presents no shipping choice and `total_details.amount_shipping` is 0\n * however many zones and rates the merchant saved (AGL-1707).\n *\n * An empty list emits NOTHING — not an empty array — so a merchant who\n * configured no shipping keeps a session byte-identical to the one built\n * before shipping was wired at all. That guarantee lives here rather than in\n * each caller, so neither checkout path can lose it independently (AGL-1720).\n */\nexport function appendCheckoutShippingParams(\n params: URLSearchParams,\n options: readonly ResolvedShippingRate[],\n): void {\n options.forEach((option, index) => {\n const field = `shipping_options[${index}][shipping_rate_data]`\n params.set(`${field}[type]`, 'fixed_amount')\n params.set(`${field}[display_name]`, option.name.slice(0, 100))\n params.set(`${field}[fixed_amount][amount]`, String(option.amountCents))\n params.set(`${field}[fixed_amount][currency]`, 'usd')\n })\n}\n"],"names":["CARRIER_QUOTE_TIMEOUT_MS","LOCAL_PICKUP_RATE_ID","zoneCode","code","String","trim","toUpperCase","isWildcard","zoneMatches","zone","country","countries","some","tierAmount","tiers","value","sorted","sort","a","b","upTo","tier","amountCents","matchedZoneIds","settings","destinationCountry","zones","specific","filter","matched","length","Set","map","id","carrierRatesFor","zoneIds","rates","rate","kind","has","zoneId","hasCarrierRates","carrierOptionRateId","rateId","serviceKey","carrierOptionAmountCents","quotedCents","quote","Math","max","round","Number","pct","markupPct","handling","handlingCents","isFinite","min","resolveShippingRates","cart","resolved","freeOverCents","subtotalCents","totalGrams","push","name","localPickup","shippableOnly","options","option","MAX_CHECKOUT_SHIPPING_OPTIONS","CHECKOUT_SHIPPING_COUNTRIES","resolveCheckoutShippingOptions","destinationCountries","byRateId","Map","existing","get","set","values","localeCompare","slice","CHECKOUT_SHIPPING_COUNTRY_CODES","CHECKOUT_SHIPPING_COUNTRY_NAMES","AC","AD","AE","AF","AG","AI","AL","AM","AO","AQ","AR","AT","AU","AW","AX","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ","BL","BM","BN","BO","BQ","BR","BS","BT","BV","BW","BY","BZ","CA","CD","CF","CG","CH","CI","CK","CL","CM","CN","CO","CR","CV","CW","CY","CZ","DE","DJ","DK","DM","DO","DZ","EC","EE","EG","EH","ER","ES","ET","FI","FJ","FK","FO","FR","GA","GB","GD","GE","GF","GG","GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HN","HR","HT","HU","ID","IE","IL","IM","IN","IO","IQ","IS","IT","JE","JM","JO","JP","KE","KG","KH","KI","KM","KN","KR","KW","KY","KZ","LA","LB","LC","LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","ME","MF","MG","MK","ML","MM","MN","MO","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NC","NE","NG","NI","NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PY","QA","RE","RO","RS","RU","RW","SA","SB","SC","SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO","SR","SS","ST","SV","SX","SZ","TA","TC","TD","TF","TG","TH","TJ","TK","TL","TM","TN","TO","TR","TT","TV","TW","TZ","UA","UG","US","UY","UZ","VA","VC","VE","VG","VN","VU","WF","WS","XK","YE","YT","ZA","ZM","ZW","STRIPE_SHIPPING_COUNTRY_CODES","Object","keys","checkoutShippingCountries","merchantPricesShipping","named","raw","includes","normalizeCheckoutShippingCountry","undefined","CART_UNPRICEABLE_SHIPPING_MESSAGE","optionsKey","join","planCheckoutShipping","declared","fromZones","refusal","destination","shipsElsewhere","offers","size","union","COVERAGE_PROBE_CART","summarizeShippingCoverage","served","unserved","collectable","add","shipsNowhere","asksDestination","offersLocalPickup","Boolean","hasRestOfWorldZone","coverageCountryNames","codes","names","describeShippingCoverage","coverage","asks","headline","detail","note","appendShippingAddressCollectionParams","params","forEach","index","appendCheckoutShippingParams","field"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC;AAsCD,0EAA0E,GAC1E,OAAO,MAAMA,2BAA2B,KAAK;AAiC7C;;;;;;;;;CASC,GACD,OAAO,MAAMC,uBAAuB,SAAQ;AAE5C;;;;;;;;;CASC,GACD,MAAMC,WAAW,CAACC,OAA0BC,OAAOD,eAAAA,OAAQ,IAAIE,IAAI,GAAGC,WAAW;AAEjF,yDAAyD,GACzD,MAAMC,aAAa,CAACJ,OAA2BD,SAASC,UAAU;AAElE,SAASK,YAAYC,IAAkB,EAAEC,OAAe;QAC9CD;IAAR,OAAO,EAACA,kBAAAA,KAAKE,SAAS,YAAdF,kBAAkB,EAAE,EAAEG,IAAI,CAChC,CAACT,OAASI,WAAWJ,SAASD,SAASC,UAAUO;AAErD;AAEA,SAASG,WACPC,KAAiC,EACjCC,KAAa;IAEb,MAAMC,SAAS;WAAKF,gBAAAA,QAAS,EAAE;KAAE,CAACG,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAEE,IAAI,GAAGD,EAAEC,IAAI;IAChE,KAAK,MAAMC,QAAQL,OAAQ;QACzB,IAAID,SAASM,KAAKD,IAAI,EAAE,OAAOC,KAAKC,WAAW;IACjD;IACA,sEAAsE;IACtE,qCAAqC;IACrC,OAAO;AACT;AAEA;;;;CAIC,GACD,OAAO,SAASC,eACdC,QAAsC,EACtCC,kBAA0B;;IAE1B,MAAMf,UAAUe,mBAAmBnB,WAAW;IAC9C,MAAMoB,gBAAQF,4BAAAA,SAAUE,KAAK,mBAAI,EAAE;IACnC,2EAA2E;IAC3E,0EAA0E;IAC1E,wEAAwE;IACxE,yEAAyE;IACz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;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJC,IAAI;IACJ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A,gFAA+E;YACrFS,MAAM;YACNH;QACF;IACF;IACA,OAAO;QACLC,UAAU;QACVC,QAAQ;QACRC,MAAMJ,SAAST,YAAY,GACvBS,SAASP,iBAAiB,GACxB,oJACA,4FACFO,SAASL,kBAAkB,IACzBK,SAASb,MAAM,CAAC1T,MAAM,KAAKkS,8BAA8BlS,MAAM,GAC/D,gFACA,CAAC,6EAA6E,EAAEmU,qBAAqBI,SAASb,MAAM,EAAE,CAAC,CAAC;QAC9Hc;IACF;AACF;AAEA;;;;;;;CAOC,GACD,OAAO,SAASI,sCACdC,MAAuB,EACvBhW,YAA+B4D,2BAA2B;IAE1D5D,UAAUiW,OAAO,CAAC,CAACzW,MAAM0W;QACvBF,OAAO7R,GAAG,CAAC,CAAC,+CAA+C,EAAE+R,MAAM,CAAC,CAAC,EAAE1W;IACzE;AACF;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,SAAS2W,6BACdH,MAAuB,EACvBvS,OAAwC;IAExCA,QAAQwS,OAAO,CAAC,CAACvS,QAAQwS;QACvB,MAAME,QAAQ,CAAC,iBAAiB,EAAEF,MAAM,qBAAqB,CAAC;QAC9DF,OAAO7R,GAAG,CAAC,GAAGiS,MAAM,MAAM,CAAC,EAAE;QAC7BJ,OAAO7R,GAAG,CAAC,GAAGiS,MAAM,cAAc,CAAC,EAAE1S,OAAOJ,IAAI,CAACgB,KAAK,CAAC,GAAG;QAC1D0R,OAAO7R,GAAG,CAAC,GAAGiS,MAAM,sBAAsB,CAAC,EAAE3W,OAAOiE,OAAO/C,WAAW;QACtEqV,OAAO7R,GAAG,CAAC,GAAGiS,MAAM,wBAAwB,CAAC,EAAE;IACjD;AACF"}
|
|
@@ -269,7 +269,7 @@ export const COMMERCE_SLUG_MAX_LENGTH = 80;
|
|
|
269
269
|
*/ export function variantMatrixProblem(options) {
|
|
270
270
|
const count = variantCombinationCount(options);
|
|
271
271
|
if (count <= COMMERCE_MAX_VARIANTS) return null;
|
|
272
|
-
return `These options make ${count.toLocaleString('en-US')} combinations, and a product holds at most ` + `${COMMERCE_MAX_VARIANTS}.
|
|
272
|
+
return `These options make ${count.toLocaleString('en-US')} combinations, and a product holds at most ` + `${COMMERCE_MAX_VARIANTS}. Keep as options only what you stock separately, and move the rest to ` + 'Add-ons: they combine without a limit, can be required, and can cost more on some options. A ' + 'choice the customer types, such as a name to print, is a write-in.';
|
|
273
273
|
}
|
|
274
274
|
/**
|
|
275
275
|
* Cartesian product of option values → the variant option combinations
|