@shop-on/theme-server 0.13.1 → 0.14.0-next.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.
@@ -1,4 +1,5 @@
1
1
  import { analyticsToken, storefrontEvent } from "./api-contract/src/storefront/analytics.js";
2
+ import { useCartSession } from "./cart-session.js";
2
3
  import { toMnt } from "./money.js";
3
4
  import { storefront } from "./storefront.js";
4
5
  import { isIP } from "node:net";
@@ -106,12 +107,22 @@ async function proxyAnalytics({ endpoint, body, visitToken, visitorToken, client
106
107
  cookies: cookies ?? void 0,
107
108
  eventSourceUrl: eventSourceUrl ?? void 0
108
109
  };
110
+ const cartToken = events.some((event) => event.name === "checkout_started") ? await buyerCartToken() : void 0;
109
111
  await api.analytics.events.record({
110
112
  ...visit,
111
113
  events,
114
+ ...cartToken ? { cartToken } : {},
112
115
  ...Object.values(marketing).some(Boolean) ? { marketing } : {}
113
116
  });
114
117
  }
118
+ /** The cart token the buyer's `cart_session` cookie holds; none when it holds none or cannot be read. */
119
+ async function buyerCartToken() {
120
+ try {
121
+ return (await useCartSession()).data.token;
122
+ } catch {
123
+ return;
124
+ }
125
+ }
115
126
  function extractClientIp(headers) {
116
127
  const realIp = headers.get("x-real-ip");
117
128
  if (realIp && isIP(realIp.trim())) return realIp.trim();
@@ -0,0 +1 @@
1
+ import "zod";
@@ -0,0 +1,23 @@
1
+ import { APP_ATTRIBUTE_PREFIX, LINE_ATTRIBUTE_VALUE_MAX } from "./limits.js";
2
+ import { z } from "zod";
3
+ // ../api-contract/src/custom-attributes.ts
4
+ /**
5
+ * Custom attributes as a buyer or a merchant sent them: a name trimmed, a
6
+ * blank one dropped, a name sent twice kept once with its last value,
7
+ * `visible` true unless false. Hidden ones are kept for the merchant and
8
+ * never answered on a line to a buyer.
9
+ */
10
+ const lineAttributes = z.array(z.object({
11
+ name: z.string().trim().max(100),
12
+ value: z.string().max(LINE_ATTRIBUTE_VALUE_MAX),
13
+ visible: z.boolean().default(true)
14
+ })).max(20).transform((attributes) => [...new Map(attributes.filter((a) => a.name !== "").map((a) => [a.name, a])).values()]);
15
+ /** One custom attribute as stored, `visible` included. */
16
+ const customAttribute = z.object({
17
+ name: z.string(),
18
+ value: z.string(),
19
+ visible: z.boolean()
20
+ });
21
+ /** Whether `name` lies in the space reserved to apps (`app--<handle>--<name>`), whichever app's. */
22
+ const isAppAttribute = (name) => name.startsWith(APP_ATTRIBUTE_PREFIX);
23
+ export { customAttribute, isAppAttribute, lineAttributes };
@@ -12,6 +12,7 @@ const LITE_ID_PREFIXES = {
12
12
  area: "administrative area",
13
13
  customer: "customer",
14
14
  customer_address: "customer address",
15
+ segment: "customer segment",
15
16
  discount: "discount",
16
17
  discount_code: "discount code",
17
18
  shipping_profile: "shipping profile",
@@ -104,7 +105,8 @@ const LITE_ID_PREFIXES = {
104
105
  wallet_funding: "wallet funding",
105
106
  feedback: "feedback",
106
107
  app: "app",
107
- app_install: "app install"
108
+ app_install: "app install",
109
+ app_version: "app version"
108
110
  };
109
111
  /**
110
112
  * `liteId("role")` accepts `role_…` UUIDv7s only; `toLiteId` gives a row's
@@ -38,7 +38,13 @@ const CART_LINE_QUANTITY_MAX = 1e4;
38
38
  const CART_NOTE_MAX = 1e3;
39
39
  /** The longest custom attribute value. */
40
40
  const LINE_ATTRIBUTE_VALUE_MAX = 1e3;
41
+ /**
42
+ * Where a cart's custom attribute names reserved to apps start: an app's are
43
+ * `app--<handle>--<name>`, written by its token only, so a buyer may name
44
+ * none (legacy App::RESERVED_NAMESPACE_START).
45
+ */
46
+ const APP_ATTRIBUTE_PREFIX = "app--";
41
47
  const DISCOUNT_CODE_PATTERN = /^[A-Za-z0-9_-]+$/;
42
48
  /** A gift card's code: 8 to 20 letters and digits, kept upper-cased (legacy's). */
43
49
  const GIFT_CARD_CODE_PATTERN = /^[A-Z0-9]{8,20}$/;
44
- export { ANALYTICS_NUMBER_MAX, ANALYTICS_URL_SENT_MAX, CART_LINE_QUANTITY_MAX, CART_NOTE_MAX, CONTACT_BODY_MAX, CONTACT_FIELDS_TOTAL_MAX, DISCOUNT_CODE_PATTERN, GIFT_CARD_CODE_PATTERN, LINE_ATTRIBUTE_VALUE_MAX, LISTING_OFFSET_MAX, REVIEW_PHOTO_BYTES_MAX, REVIEW_PHOTO_TYPES, SITEMAP_KINDS, SITEMAP_PAGE_MAX, TURNSTILE_TOKEN_MAX };
50
+ export { ANALYTICS_NUMBER_MAX, ANALYTICS_URL_SENT_MAX, APP_ATTRIBUTE_PREFIX, CART_LINE_QUANTITY_MAX, CART_NOTE_MAX, CONTACT_BODY_MAX, CONTACT_FIELDS_TOTAL_MAX, DISCOUNT_CODE_PATTERN, GIFT_CARD_CODE_PATTERN, LINE_ATTRIBUTE_VALUE_MAX, LISTING_OFFSET_MAX, REVIEW_PHOTO_BYTES_MAX, REVIEW_PHOTO_TYPES, SITEMAP_KINDS, SITEMAP_PAGE_MAX, TURNSTILE_TOKEN_MAX };
@@ -2,6 +2,7 @@ import { mntNonNegative } from "../../../contract-kit/src/money.js";
2
2
  import { liteId } from "../ids.js";
3
3
  import { ANALYTICS_NUMBER_MAX, ANALYTICS_URL_SENT_MAX } from "../limits.js";
4
4
  import { RECOMMENDATION_INTENTS } from "../recommendations.js";
5
+ import { cartToken } from "./cart.js";
5
6
  import { storefrontProcedure } from "./procedure.js";
6
7
  import { z } from "zod";
7
8
  // ../api-contract/src/storefront/analytics.ts
@@ -127,11 +128,13 @@ storefrontProcedure.meta({
127
128
  rateLimit: "storefront-analytics"
128
129
  }).route({
129
130
  summary: "Record events",
130
- description: `Logs up to 20 funnel events of a visit, each under the id the browser minted (a UUIDv7, whose time is the event's): an event sent again is not written again. \`recorded\` is how many this call wrote: an event already written, one whose id was minted more than a day from the api's clock, and every event of a bot's user agent are not. Ids are not looked up. VALIDATION_FAILED for an event of another name or shape, or a token that is not a UUIDv7; the whole call then writes nothing.`
131
+ description: `Logs up to 20 funnel events of a visit, each under the id the browser minted (a UUIDv7, whose time is the event's): an event sent again is not written again. \`recorded\` is how many this call wrote: an event already written, one whose id was minted more than a day from the api's clock, and every event of a bot's user agent are not. Ids are not looked up. \`cartToken\` is the buyer's cart: a \`checkout_started\` is logged under the cart it names in the shop, which is how an order is traced to the visit that brought it; a token that names none logs it under no cart, never an error. VALIDATION_FAILED for an event of another name or shape, or a token that is not a UUIDv7; the whole call then writes nothing.`
131
132
  }).input(z.object({
132
133
  visitId: analyticsToken,
133
134
  visitorId: analyticsToken,
134
135
  events: z.array(storefrontEvent).min(1).max(20),
136
+ /** The buyer's cart, which a `checkout_started` takes into checkout. */
137
+ cartToken: cartToken.optional(),
135
138
  /** The buyer's attribution, and the page the beacon came from (its Referer). */
136
139
  marketing: marketingAttribution.extend({ eventSourceUrl: hint.optional() }).optional()
137
140
  })).output(z.object({ recorded: z.int().min(0) }));
@@ -4,6 +4,11 @@ export declare const storefrontCart: z.ZodObject<{
4
4
  id: z.core.$ZodBranded<z.ZodTemplateLiteral<`basket_${string}`>, "id:basket", "inout">;
5
5
  expiresAt: z.ZodISODateTime;
6
6
  note: z.ZodNullable<z.ZodString>;
7
+ customAttributes: z.ZodArray<z.ZodObject<{
8
+ name: z.ZodString;
9
+ value: z.ZodString;
10
+ visible: z.ZodBoolean;
11
+ }, z.core.$strip>>;
7
12
  totalQuantity: z.ZodInt;
8
13
  lines: z.ZodArray<z.ZodObject<{
9
14
  unitPrice: z.ZodInt;
@@ -18,6 +23,7 @@ export declare const storefrontCart: z.ZodObject<{
18
23
  }, z.core.$strip>>;
19
24
  available: z.ZodBoolean;
20
25
  quantityAvailable: z.ZodNullable<z.ZodInt>;
26
+ holdExpiresAt: z.ZodNullable<z.ZodISODateTime>;
21
27
  variant: z.ZodObject<{
22
28
  id: z.core.$ZodBranded<z.ZodTemplateLiteral<`variant_${string}`>, "id:variant", "inout">;
23
29
  title: z.ZodString;
@@ -1,7 +1,8 @@
1
1
  import { mntNonNegative } from "../../../contract-kit/src/money.js";
2
+ import { customAttribute, isAppAttribute, lineAttributes } from "../custom-attributes.js";
2
3
  import { liteId } from "../ids.js";
3
4
  import { imageSchema } from "../image.js";
4
- import { CART_LINE_QUANTITY_MAX, CART_NOTE_MAX, DISCOUNT_CODE_PATTERN, LINE_ATTRIBUTE_VALUE_MAX } from "../limits.js";
5
+ import { APP_ATTRIBUTE_PREFIX, CART_LINE_QUANTITY_MAX, CART_NOTE_MAX, DISCOUNT_CODE_PATTERN } from "../limits.js";
5
6
  import { storefrontProcedure } from "./procedure.js";
6
7
  import { z } from "zod";
7
8
  // ../api-contract/src/storefront/cart.ts
@@ -16,17 +17,6 @@ const write = {
16
17
  /** The token `cart.create` answered; the cart's only key. */
17
18
  const cartToken = z.string().min(1).max(64);
18
19
  const quantity = z.int().min(1).max(CART_LINE_QUANTITY_MAX);
19
- /**
20
- * A line's custom attributes as the buyer sent them (legacy
21
- * Shop::LineItemAttributes): a name trimmed, a blank one dropped, a name
22
- * sent twice kept once with its last value, `visible` true unless false.
23
- * Hidden ones are kept for the merchant and never answered to a buyer.
24
- */
25
- const lineAttributes = z.array(z.object({
26
- name: z.string().trim().max(100),
27
- value: z.string().max(LINE_ATTRIBUTE_VALUE_MAX),
28
- visible: z.boolean().default(true)
29
- })).max(20).transform((attributes) => [...new Map(attributes.filter((a) => a.name !== "").map((a) => [a.name, a])).values()]);
30
20
  /** A refusal that leaves the cart as it was, naming the variant the call named whose stock refused it (a bundle's, for its items). */
31
21
  const variantRefusal = z.object({ variantId: liteId("variant") });
32
22
  const pricedLine = {
@@ -52,6 +42,13 @@ const storefrontCartLine = z.object({
52
42
  available: z.boolean(),
53
43
  /** Units available across the shop's active locations; null when nothing bounds them. */
54
44
  quantityAvailable: z.int().nonnegative().nullable(),
45
+ /**
46
+ * Until when an app holds the line (a booking's slot; docs/cart-holds.md):
47
+ * its quantity is the app's, so a change is LINE_HELD, and the line leaves
48
+ * the cart when the time passes. Null when no app holds it; a bundle's
49
+ * line is never held.
50
+ */
51
+ holdExpiresAt: z.iso.datetime().nullable(),
55
52
  variant: z.object({
56
53
  id: liteId("variant"),
57
54
  /** Its option values' names, joined by " / "; empty without options. */
@@ -116,6 +113,13 @@ const storefrontCart = z.object({
116
113
  /** Set when the cart was made, never extended. */
117
114
  expiresAt: z.iso.datetime(),
118
115
  note: z.string().nullable(),
116
+ /**
117
+ * The cart's own custom attributes (`cart.customAttributes.set`): every one
118
+ * the buyer set, hidden ones too (the theme wrote them, and its next write
119
+ * replaces them all), and an app's (`app--<handle>--…`) only where the app
120
+ * made it visible. The order carries them all.
121
+ */
122
+ customAttributes: z.array(customAttribute),
119
123
  /** The lines' quantities together, a bundle's in bundles. */
120
124
  totalQuantity: z.int().nonnegative(),
121
125
  /** In the order added. A line whose variant was deleted is left out (the next write removes it). */
@@ -169,6 +173,11 @@ INSUFFICIENT_INVENTORY: {
169
173
  status: 409,
170
174
  data: variantRefusal
171
175
  } };
176
+ /** An app holds the line the write would change: its quantity is what the app booked (remove the line instead). */
177
+ const LINE_HELD = {
178
+ status: 409,
179
+ data: z.object({ lineId: liteId("basket_line") })
180
+ };
172
181
  storefrontProcedure.meta(write).route({
173
182
  summary: "Open a cart",
174
183
  description: "A new, empty cart and its token, shown in this answer only: the storefront keeps it (its cookie) and names the cart by it from then on. The cart lives 30 days from now, never extended. With a signed-in buyer (`x-buyer-session`) the cart is theirs; one who has a live cart already is CONFLICT `{ resource: \"cart\" }`: claim it (`cart.claim`)."
@@ -189,7 +198,7 @@ storefrontProcedure.meta(write).route({
189
198
  }));
190
199
  storefrontProcedure.meta(write).route({
191
200
  summary: "Make a guest cart the signed-in buyer's",
192
- description: "The cart the token opens becomes the signed-in buyer's and keeps its token. Their live cart before it, if any, is merged into it and ends (merged): a line (or a bundle) of the same variant and attributes is raised by the other's quantity, the rest added, and its note kept only when this cart has none. A cart already theirs is answered unchanged. Refused, nothing changed: UNAUTHENTICATED without a signed-in buyer; NOT_FOUND `{ resource: \"cart\" }` as `cart.get`; PRECONDITION_FAILED `sales.cart_other_customer` for another customer's cart, `sales.cart_paying` while either cart is being paid; a merged line whose variant a buyer may no longer see NOT_FOUND `{ resource: \"variant\", id }`; past 100 lines LIMIT_EXCEEDED; a variant whose merged demand it cannot sell INSUFFICIENT_INVENTORY."
201
+ description: "The cart the token opens becomes the signed-in buyer's and keeps its token. Their live cart before it, if any, is merged into it and ends (merged): a line (or a bundle) of the same variant and attributes is raised by the other's quantity, the rest added, and its note kept only when this cart has none. A line an app holds is never raised, and one held on the cart before is left behind with it, its app told (`carts/line_released`). A cart already theirs is answered unchanged. Refused, nothing changed: UNAUTHENTICATED without a signed-in buyer; NOT_FOUND `{ resource: \"cart\" }` as `cart.get`; PRECONDITION_FAILED `sales.cart_other_customer` for another customer's cart, `sales.cart_paying` while either cart is being paid; a merged line whose variant a buyer may no longer see NOT_FOUND `{ resource: \"variant\", id }`; past 100 lines LIMIT_EXCEEDED; a variant whose merged demand it cannot sell INSUFFICIENT_INVENTORY."
193
202
  }).errors({
194
203
  ...cartErrors,
195
204
  LIMIT_EXCEEDED: {
@@ -199,9 +208,10 @@ storefrontProcedure.meta(write).route({
199
208
  }).input(z.object({ token: cartToken })).output(storefrontCart);
200
209
  storefrontProcedure.meta(write).route({
201
210
  summary: "Add lines to a cart",
202
- description: `Adds each line's quantity of its variant, with its custom attributes, all or nothing. A variant already on a line with the same attributes (in any order) raises that line; other attributes make a line of their own. A bundle's variant is one line (\`bundleItems\` lists what it holds), raised the same way; its items' stock is asked for bundles × units each, with any other line of the same variant. Refused: a variant a buyer may not see, NOT_FOUND \`{ resource: "variant", id }\`; past 100 lines (a bundle is one), LIMIT_EXCEEDED; a variant whose whole demand in the cart it cannot sell, INSUFFICIENT_INVENTORY naming it (a bundle whose item cannot, or whose item's product buyers cannot see, naming the bundle's variant).`
211
+ description: `Adds each line's quantity of its variant, with its custom attributes, all or nothing. A variant already on a line with the same attributes (in any order) raises that line; other attributes make a line of their own. A bundle's variant is one line (\`bundleItems\` lists what it holds), raised the same way; its items' stock is asked for bundles × units each, with any other line of the same variant. Refused: a variant a buyer may not see, NOT_FOUND \`{ resource: "variant", id }\`; past 100 lines (a bundle is one), LIMIT_EXCEEDED; a variant whose whole demand in the cart it cannot sell, INSUFFICIENT_INVENTORY naming it (a bundle whose item cannot, or whose item's product buyers cannot see, naming the bundle's variant); a line an app holds (\`holdExpiresAt\`) that an add would raise, LINE_HELD naming it.`
203
212
  }).errors({
204
213
  ...cartErrors,
214
+ LINE_HELD,
205
215
  LIMIT_EXCEEDED: {
206
216
  status: 422,
207
217
  data: z.object({ limit: z.int() })
@@ -216,8 +226,11 @@ storefrontProcedure.meta(write).route({
216
226
  })).output(storefrontCart);
217
227
  storefrontProcedure.meta(write).route({
218
228
  summary: "Change cart lines' quantities",
219
- description: "Sets each named line's quantity, all or nothing; a line named twice is VALIDATION_FAILED, a line the cart lacks NOT_FOUND `{ resource: \"cart_line\", id }`. A bundle's line sets the bundles, its items' units with them. A quantity raised past what its variant (a bundle's items) can sell is INSUFFICIENT_INVENTORY; a lowered one is never refused. Remove a line with `cart.lines.remove`."
220
- }).errors(cartErrors).input(z.object({
229
+ description: "Sets each named line's quantity, all or nothing; a line named twice is VALIDATION_FAILED, a line the cart lacks NOT_FOUND `{ resource: \"cart_line\", id }`. A bundle's line sets the bundles, its items' units with them. A quantity raised past what its variant (a bundle's items) can sell is INSUFFICIENT_INVENTORY; a lowered one is never refused. A line an app holds (`holdExpiresAt`) keeps its quantity: another is LINE_HELD (its own sent back is no change). Remove a line with `cart.lines.remove`."
230
+ }).errors({
231
+ ...cartErrors,
232
+ LINE_HELD
233
+ }).input(z.object({
221
234
  token: cartToken,
222
235
  lines: z.array(z.object({
223
236
  lineId: liteId("basket_line"),
@@ -226,7 +239,7 @@ storefrontProcedure.meta(write).route({
226
239
  })).output(storefrontCart);
227
240
  storefrontProcedure.meta(write).route({
228
241
  summary: "Remove cart lines",
229
- description: "Removes the named lines (a bundle's with everything it holds), all or nothing: a line the cart lacks is NOT_FOUND `{ resource: \"cart_line\", id }`."
242
+ description: "Removes the named lines (a bundle's with everything it holds), all or nothing: a line the cart lacks is NOT_FOUND `{ resource: \"cart_line\", id }`. A line an app holds is removed as any other, and the app is told (`carts/line_released`)."
230
243
  }).input(z.object({
231
244
  token: cartToken,
232
245
  lineIds: z.array(liteId("basket_line")).min(1).max(100)
@@ -245,4 +258,14 @@ storefrontProcedure.meta(write).route({
245
258
  token: cartToken,
246
259
  code: z.string().trim().min(1).max(64).regex(DISCOUNT_CODE_PATTERN).transform((code) => code.toUpperCase()).nullable()
247
260
  })).output(storefrontCart);
248
- export { CART_DISCOUNT_ERRORS, CART_OWNERS, CART_WARNINGS, cartToken, lineAttributes, storefrontCart, storefrontCartLine };
261
+ storefrontProcedure.meta(write).route({
262
+ summary: "Set the buyer's custom attributes on the cart",
263
+ description: `Replaces every custom attribute the buyer set on the cart with these (names trimmed, a blank one dropped, a name sent twice kept with its last value, \`visible\` true unless false); an app's are kept as they are. A name starting \`${APP_ATTRIBUTE_PREFIX}\` is an app's: VALIDATION_FAILED, nothing written. The cart holds at most 20 across the buyer's and the apps': past it, LIMIT_EXCEEDED. The order carries them.`
264
+ }).errors({ LIMIT_EXCEEDED: {
265
+ status: 422,
266
+ data: z.object({ limit: z.int() })
267
+ } }).input(z.object({
268
+ token: cartToken,
269
+ customAttributes: lineAttributes.refine((attributes) => !attributes.some((a) => isAppAttribute(a.name)), `Names starting ${APP_ATTRIBUTE_PREFIX} belong to apps`)
270
+ })).output(storefrontCart);
271
+ export { CART_DISCOUNT_ERRORS, CART_OWNERS, CART_WARNINGS, cartToken, storefrontCart, storefrontCartLine };
@@ -171,7 +171,7 @@ const checkoutLineGroup = z.object({
171
171
  url: z.string()
172
172
  })
173
173
  });
174
- const storefrontCheckoutLine = storefrontCartLine.extend({
174
+ const storefrontCheckoutLine = storefrontCartLine.omit({ holdExpiresAt: true }).extend({
175
175
  /** The unit price the buyer was shown when checkout started or they last accepted a change (a component's: its share). */
176
176
  quotedUnitPrice: mntNonNegative,
177
177
  /** On a bundle's component: the bundle, as `lineGroups` holds it. Its `unitPrice` is its share of the bundle's price, its `quantity` real units (bundles × `bundleItemQuantity`) and its attributes the bundle's. */
@@ -447,6 +447,19 @@ const placementErrors = {
447
447
  DETAILS_REQUIRED: {
448
448
  status: 422,
449
449
  data: z.object({ detail: z.enum(PLACEMENT_DETAILS) })
450
+ },
451
+ /**
452
+ * Lines an app holds that it does not confirm, or whose hold has passed
453
+ * (docs/cart-holds.md, Checkout check): each line with the words to show
454
+ * the buyer, plain text (never HTML): the app's own, or the api's in the
455
+ * buyer's language. Remove the line, or try again shortly.
456
+ */
457
+ HOLD_REFUSED: {
458
+ status: 409,
459
+ data: z.object({ lines: z.array(z.object({
460
+ lineId: liteId("basket_line"),
461
+ message: z.string()
462
+ })) })
450
463
  }
451
464
  };
452
465
  /** The ways a checkout may be paid: through Cody Pay's hosted page, or off it (cash on delivery, or the merchant's own way). */
@@ -596,11 +609,11 @@ storefrontProcedure.meta(read).route({
596
609
  }).input(z.object(opened)).output(z.object({ methods: z.array(z.enum(CHECKOUT_PAYMENT_METHODS)) }));
597
610
  storefrontProcedure.meta(write).route({
598
611
  summary: "Place the order, paid off Cody Pay",
599
- description: "Turns the checkout into an order at once: each gift card's share taken from it (a captured `gift_card` payment each), the store credit's from the buyer's credit (a captured `store_credit` payment), the rest (`amountDue`) owed by cash on delivery (or at pickup) when the shop takes it, else by the merchant's own way; an order with nothing due is born paid. Everything is priced again under the checkout's lock and compared with what the buyer was shown (the placement errors), the stock held for each line and passed to the order. Refused with PRECONDITION_FAILED `payments.payment_required` when something is due and the shop collects through Cody Pay and takes no cash on delivery (use `pay`), `sales.gift_card_tender_not_allowed` when the checkout sells a gift card and a gift card pays part of it (one applied before the gift card was added to the cart: remove it), `sales.checkout_paying` while a payment is under way, `shops.trial_orders` on a trial. The checkout token then reads the order (`order`)."
612
+ description: "Turns the checkout into an order at once: each gift card's share taken from it (a captured `gift_card` payment each), the store credit's from the buyer's credit (a captured `store_credit` payment), the rest (`amountDue`) owed by cash on delivery (or at pickup) when the shop takes it, else by the merchant's own way; an order with nothing due is born paid. Everything is priced again under the checkout's lock and compared with what the buyer was shown (the placement errors), the stock held for each line and passed to the order. Each app holding a line is asked to confirm it first (at most 1.5 s, the apps at once), and a line whose hold has passed is refused unasked: HOLD_REFUSED names every line that does not stand, and nothing is written. Refused with PRECONDITION_FAILED `payments.payment_required` when something is due and the shop collects through Cody Pay and takes no cash on delivery (use `pay`), `sales.gift_card_tender_not_allowed` when the checkout sells a gift card and a gift card pays part of it (one applied before the gift card was added to the cart: remove it), `sales.checkout_paying` while a payment is under way, `shops.trial_orders` on a trial. The checkout token then reads the order (`order`)."
600
613
  }).errors(placementErrors).input(z.object(opened)).output(storefrontOrder);
601
614
  storefrontProcedure.meta(write).route({
602
615
  summary: "Pay the checkout through Cody Pay",
603
- description: "Checks the checkout as `place` does, holds its stock, its gift cards' shares (taken from the cards now, given back if the charge ends unpaid) and its store credit (held from the buyer's credit until the charge ends: spent when it is paid, given back when it is not), and opens a Cody Pay charge for the rest (`amountDue`): answers the hosted page to send the buyer to. The checkout is `paying` from then until Cody Pay reports the charge: paid places the order (the token then reads it, `order`), expired gives the checkout back. Refused with PRECONDITION_FAILED `payments.cody_pay_unavailable` when the shop does not collect through Cody Pay, `payments.nothing_due` when nothing is due (use `place`), `payments.amount_not_whole_tugrik` for an amount due under one tögrög (Cody Pay charges whole tögrög: an amount due with möngö is charged its whole tögrög, the möngö dropped, and the charge paid pays the order in full),`sales.gift_card_tender_not_allowed` as `place`, `sales.checkout_paying` while a payment is under way, `shops.trial_orders` on a trial; NO_PAYMENT_METHOD when the shop has every Cody Pay method switched off (nothing is held); PROVIDER_UNAVAILABLE when Cody Pay does not open the charge (the checkout is given back)."
616
+ description: "Checks the checkout as `place` does (the apps holding its lines asked first: HOLD_REFUSED, nothing held or charged), holds its stock, its gift cards' shares (taken from the cards now, given back if the charge ends unpaid) and its store credit (held from the buyer's credit until the charge ends: spent when it is paid, given back when it is not), and opens a Cody Pay charge for the rest (`amountDue`): answers the hosted page to send the buyer to. The checkout is `paying` from then until Cody Pay reports the charge: paid places the order (the token then reads it, `order`), expired gives the checkout back. Refused with PRECONDITION_FAILED `payments.cody_pay_unavailable` when the shop does not collect through Cody Pay, `payments.nothing_due` when nothing is due (use `place`), `payments.amount_not_whole_tugrik` for an amount due under one tögrög (Cody Pay charges whole tögrög: an amount due with möngö is charged its whole tögrög, the möngö dropped, and the charge paid pays the order in full),`sales.gift_card_tender_not_allowed` as `place`, `sales.checkout_paying` while a payment is under way, `shops.trial_orders` on a trial; NO_PAYMENT_METHOD when the shop has every Cody Pay method switched off (nothing is held); PROVIDER_UNAVAILABLE when Cody Pay does not open the charge (the checkout is given back)."
604
617
  }).errors({
605
618
  ...placementErrors,
606
619
  NO_PAYMENT_METHOD: {