@tokenoftrust/storefront-runner 7.9.5 → 7.9.6

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.
@@ -86,9 +86,32 @@ function escapeHtml(value: string): string {
86
86
  });
87
87
  }
88
88
 
89
- function cartDenial(code: CartDenialCode, message: string, status: number): Response {
89
+ function cartDenialGuidance(code: CartDenialCode, message: string): string | null {
90
+ if (/\b(?:reload|try again|return to)\b/i.test(message)) return null;
91
+ if (code === "shipping" || code === "signing-unavailable") {
92
+ return "Please try again shortly, or contact the store for help.";
93
+ }
94
+ return "Reload the product page and try adding the item again, or contact the store if this keeps happening.";
95
+ }
96
+
97
+ function cartDenial(
98
+ code: CartDenialCode,
99
+ message: string,
100
+ status: number,
101
+ returnUrl?: string | null,
102
+ ): Response {
103
+ const guidance = cartDenialGuidance(code, message);
104
+ const guidanceHtml = guidance ? `<p data-cart-denial-guidance>${escapeHtml(guidance)}</p>` : "";
105
+ const returnLinkHtml = returnUrl
106
+ ? `<p><a href="${escapeHtml(returnUrl)}" data-cart-denial-return>Return to the product page</a></p>`
107
+ : "";
90
108
  const body =
91
- `<!doctype html><html><body><p data-cart-denial="${code}" data-payment-authorized="false">${escapeHtml(message)}</p></body></html>`;
109
+ '<!doctype html><html lang="en"><head><meta charset="utf-8">' +
110
+ '<meta name="viewport" content="width=device-width,initial-scale=1">' +
111
+ "<title>Unable to add to cart</title></head><body><main>" +
112
+ "<h1>Unable to add to cart</h1>" +
113
+ `<p data-cart-denial="${code}" data-payment-authorized="false">${escapeHtml(message)}</p>` +
114
+ `${guidanceHtml}${returnLinkHtml}</main></body></html>`;
92
115
  return new Response(body, {
93
116
  status,
94
117
  headers: {
@@ -311,11 +334,13 @@ function buyerHandoff(providerOrigin: string, selector: string, proof: string, c
311
334
 
312
335
  async function cartFormPost(context: APIContext, shopper?: GatedHandoff): Promise<Response> {
313
336
  const { tenant, commerce } = context.locals;
337
+ const returnUrl = submittedFromUrl(context.request, tenant);
314
338
  if (!commerce) {
315
339
  return cartDenial(
316
340
  "signing-unavailable",
317
341
  "Checkout is unavailable because this store has no cart configured.",
318
342
  409,
343
+ returnUrl,
319
344
  );
320
345
  }
321
346
  const signing = await resolveCartSigning(commerce);
@@ -324,14 +349,15 @@ async function cartFormPost(context: APIContext, shopper?: GatedHandoff): Promis
324
349
  "signing-unavailable",
325
350
  shopperCartDenialMessage("signing-unavailable", signing.operatorError ?? ""),
326
351
  503,
352
+ returnUrl,
327
353
  );
328
354
  }
329
355
  let form: FormData;
330
356
  try { form = await context.request.formData(); }
331
- catch { return cartDenial("signature", "Invalid cart.", 422); }
357
+ catch { return cartDenial("signature", "Invalid cart.", 422, returnUrl); }
332
358
  const submitted: Array<[string, string]> = [];
333
359
  for (const [name, value] of form.entries()) {
334
- if (typeof value !== "string") return cartDenial("signature", "Invalid cart.", 422);
360
+ if (typeof value !== "string") return cartDenial("signature", "Invalid cart.", 422, returnUrl);
335
361
  submitted.push([name, value]);
336
362
  }
337
363
  if (shopper) {
@@ -343,14 +369,14 @@ async function cartFormPost(context: APIContext, shopper?: GatedHandoff): Promis
343
369
  typeof handle !== "string" || typeof variantId !== "string" ||
344
370
  typeof quantity !== "string" || !/^[1-9]\d{0,2}$/.test(quantity) || Number(quantity) > MAX_CART_QUANTITY ||
345
371
  handle.length < 1 || handle.length > 160 || variantId.length < 1 || variantId.length > 256) {
346
- return cartDenial("signature", "Invalid cart.", 422);
372
+ return cartDenial("signature", "Invalid cart.", 422, returnUrl);
347
373
  }
348
- if (ref && !await shopper.validateVerificationRef(ref)) return cartDenial("signature", "Verification is unavailable for this session.", 422);
374
+ if (ref && !await shopper.validateVerificationRef(ref)) return cartDenial("signature", "Verification is unavailable for this session.", 422, returnUrl);
349
375
  const product = await context.locals.tot.getProductByHandle(handle);
350
- if (!shopperCatalogProduct(tenant, product)) return cartDenial("signature", "This product is unavailable.", 422);
376
+ if (!shopperCatalogProduct(tenant, product)) return cartDenial("signature", "This product is unavailable.", 422, returnUrl);
351
377
  const variant = product.variants.find(item => item.id === variantId && item.inventory.available);
352
378
  const price = variant && shopperVariantPrice(tenant, product, variant, shopper.tierId);
353
- if (!price || !variant) return cartDenial("signature", "This product is unavailable.", 422);
379
+ if (!price || !variant) return cartDenial("signature", "This product is unavailable.", 422, returnUrl);
354
380
  const payload = await buildVariantCartPayload(product, variant, {
355
381
  mode: commerce.mode, secret: signing.secret, signedPrice: price.price, tenantId: commerceTenantId(tenant),
356
382
  currency: tenant.currency, publicOrigin: tenantPublicOrigin(tenant, {
@@ -375,24 +401,24 @@ async function cartFormPost(context: APIContext, shopper?: GatedHandoff): Promis
375
401
  if (!result.ok) {
376
402
  logCartRefusal(context, result.code, result.failedFields);
377
403
  const status = result.code === "signing-unavailable" ? 503 : 422;
378
- return cartDenial(result.code, shopperCartDenialMessage(result.code, result.message), status);
404
+ return cartDenial(result.code, shopperCartDenialMessage(result.code, result.message), status, returnUrl);
379
405
  }
380
406
  const soldOut = await refuseSoldOut(context, tenant, submittedCode(result.fields));
381
407
  if (soldOut) return soldOut;
382
408
  const imaged = await completeCartFields(context, commerce, tenant, signing.secret, result.fields, {
383
- ref, returnUrl: submittedFromUrl(context.request, tenant),
409
+ ref, returnUrl,
384
410
  });
385
- if (!imaged) return cartDenial("shipping", SHIPPING_UNAVAILABLE_CART_MESSAGE, 409);
411
+ if (!imaged) return cartDenial("shipping", SHIPPING_UNAVAILABLE_CART_MESSAGE, 409, returnUrl);
386
412
  let proof: Awaited<ReturnType<GatedHandoff["prepareProof"]>> = null;
387
413
  if (shopper) {
388
- if (commerce.mode !== "signed" || !signing.secret) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503);
414
+ if (commerce.mode !== "signed" || !signing.secret) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503, returnUrl);
389
415
  const code = submittedCode(imaged);
390
416
  proof = code ? await shopper.prepareProof() : null;
391
- if (!proof || !code) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503);
417
+ if (!proof || !code) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503, returnUrl);
392
418
  imaged.push(...await signCartLine([{ name: "shopper_checkout", value: proof.proof }], { mode: commerce.mode, secret: signing.secret, code }));
393
419
  }
394
420
  const selector = await addEnforcedLine(context, commerce, tenant, imaged, signing.secret, coupon);
395
- if (!selector || (proof && !await proof.bind(selector))) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503);
421
+ if (!selector || (proof && !await proof.bind(selector))) return cartDenial("signing-unavailable", CART_UNAVAILABLE_MESSAGE, 503, returnUrl);
396
422
  if (proof && shopper) return buyerHandoff(shopper.providerOrigin, selector, proof.proof, proof.buyerConfirmation);
397
423
  const location = withCartSession(commerce.cartUrl, selector);
398
424
  // The page reads the cart that now holds the line (cartState.ts) and asks for its URL as data.
@@ -99,13 +99,26 @@ export function shopperCheckoutUnavailableHtml(): string {
99
99
  );
100
100
  }
101
101
 
102
+ /** Shopper-friendly copy for cart enforcement refusals, with clear recovery steps and no internal detail. */
103
+ const SHOPPER_CART_DENIAL_MESSAGES: Readonly<Record<string, string>> = {
104
+ "signing-unavailable": CHECKOUT_UNAVAILABLE_SHOPPER_MESSAGE,
105
+ signature:
106
+ "This item could not be added to your cart because the page is out of date. Reload the product page and try adding it again, or contact the store if this keeps happening.",
107
+ price:
108
+ "This item could not be added to your cart because its price changed since this page loaded. Reload the product page to see the current price and try adding it again.",
109
+ quantity:
110
+ "This item could not be added to your cart because the quantity entered is not available. Return to the product page, choose a valid quantity, and try again.",
111
+ tenant:
112
+ "This item could not be added to your cart because this page no longer matches this store. Reload the product page and try adding it again, or contact the store if this keeps happening.",
113
+ };
114
+
102
115
  /**
103
116
  * Message safe to put in an HTTP body. `signing-unavailable` carries operator
104
- * setup text from the cart runtime; shoppers get the neutral message instead.
117
+ * setup text from the cart runtime, and cart-enforcement refusals carry
118
+ * diagnostic text; shoppers get clear next steps instead.
105
119
  */
106
120
  export function shopperCartDenialMessage(code: string, message: string): string {
107
- if (code === "signing-unavailable") return CHECKOUT_UNAVAILABLE_SHOPPER_MESSAGE;
108
- return message;
121
+ return SHOPPER_CART_DENIAL_MESSAGES[code] ?? message;
109
122
  }
110
123
 
111
124
  export interface CartSigningResolution {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/storefront-runner",
3
- "version": "7.9.5",
3
+ "version": "7.9.6",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
6
6
  "packageManager": "pnpm@11.9.0",