@aglyn/plugins-commerce 1.0.0-beta.289 → 1.0.0-beta.291

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.
Files changed (140) hide show
  1. package/README.md +4 -2
  2. package/package.json +13 -13
  3. package/src/lib/components/commerce-console-page.js +2 -1
  4. package/src/lib/components/commerce-console-page.js.map +1 -1
  5. package/src/lib/components/console/commerce-pdf-download.d.ts +30 -0
  6. package/src/lib/components/console/commerce-pdf-download.js +53 -0
  7. package/src/lib/components/console/commerce-pdf-download.js.map +1 -0
  8. package/src/lib/components/console/host-orders-card.component.d.ts +6 -0
  9. package/src/lib/components/console/host-orders-card.component.js +3 -2
  10. package/src/lib/components/console/host-orders-card.component.js.map +1 -1
  11. package/src/lib/components/console/host-quotes-card.component.js +20 -4
  12. package/src/lib/components/console/host-quotes-card.component.js.map +1 -1
  13. package/src/lib/components/console/invoice-send.component.d.ts +44 -0
  14. package/src/lib/components/console/invoice-send.component.js +237 -0
  15. package/src/lib/components/console/invoice-send.component.js.map +1 -0
  16. package/src/lib/components/console/order-detail-dialog.component.d.ts +5 -0
  17. package/src/lib/components/console/order-detail-dialog.component.js +9 -2
  18. package/src/lib/components/console/order-detail-dialog.component.js.map +1 -1
  19. package/src/lib/components/console/order-invoice.component.d.ts +15 -4
  20. package/src/lib/components/console/order-invoice.component.js +93 -12
  21. package/src/lib/components/console/order-invoice.component.js.map +1 -1
  22. package/src/lib/components/console/quote-editor-dialog.component.d.ts +29 -0
  23. package/src/lib/components/console/quote-editor-dialog.component.js +154 -5
  24. package/src/lib/components/console/quote-editor-dialog.component.js.map +1 -1
  25. package/src/lib/components/console/quote-proofs.component.d.ts +48 -0
  26. package/src/lib/components/console/quote-proofs.component.js +694 -0
  27. package/src/lib/components/console/quote-proofs.component.js.map +1 -0
  28. package/src/lib/components/console/quote-send.component.d.ts +42 -0
  29. package/src/lib/components/console/quote-send.component.js +307 -0
  30. package/src/lib/components/console/quote-send.component.js.map +1 -0
  31. package/src/lib/components/console/quote-start.component.d.ts +45 -0
  32. package/src/lib/components/console/quote-start.component.js +182 -0
  33. package/src/lib/components/console/quote-start.component.js.map +1 -0
  34. package/src/lib/components/product-options-field-body.d.ts +44 -0
  35. package/src/lib/components/product-options-field-body.js +260 -0
  36. package/src/lib/components/product-options-field-body.js.map +1 -0
  37. package/src/lib/components/product-options-field.d.ts +32 -0
  38. package/src/lib/components/product-options-field.js +128 -0
  39. package/src/lib/components/product-options-field.js.map +1 -0
  40. package/src/lib/components/quote-page-body.js +67 -20
  41. package/src/lib/components/quote-page-body.js.map +1 -1
  42. package/src/lib/components/quote-proof-panel.d.ts +42 -0
  43. package/src/lib/components/quote-proof-panel.js +436 -0
  44. package/src/lib/components/quote-proof-panel.js.map +1 -0
  45. package/src/lib/model/commerce-invoices.d.ts +132 -0
  46. package/src/lib/model/commerce-invoices.js +332 -0
  47. package/src/lib/model/commerce-invoices.js.map +1 -0
  48. package/src/lib/model/commerce-orders.d.ts +29 -2
  49. package/src/lib/model/commerce-orders.js.map +1 -1
  50. package/src/lib/model/commerce-product-field.d.ts +23 -0
  51. package/src/lib/model/commerce-product-field.js +26 -0
  52. package/src/lib/model/commerce-product-field.js.map +1 -0
  53. package/src/lib/model/commerce-quote-email.d.ts +67 -0
  54. package/src/lib/model/commerce-quote-email.js +154 -0
  55. package/src/lib/model/commerce-quote-email.js.map +1 -0
  56. package/src/lib/model/commerce-quote-pay.d.ts +76 -6
  57. package/src/lib/model/commerce-quote-pay.js +105 -39
  58. package/src/lib/model/commerce-quote-pay.js.map +1 -1
  59. package/src/lib/model/commerce-quote-proofs.d.ts +159 -0
  60. package/src/lib/model/commerce-quote-proofs.js +348 -0
  61. package/src/lib/model/commerce-quote-proofs.js.map +1 -0
  62. package/src/lib/model/commerce-quote-request.d.ts +93 -0
  63. package/src/lib/model/commerce-quote-request.js +207 -0
  64. package/src/lib/model/commerce-quote-request.js.map +1 -0
  65. package/src/lib/model/commerce-quotes.d.ts +140 -3
  66. package/src/lib/model/commerce-quotes.js.map +1 -1
  67. package/src/lib/model/index.d.ts +4 -0
  68. package/src/lib/model/index.js +4 -0
  69. package/src/lib/model/index.js.map +1 -1
  70. package/src/lib/plugin.js +19 -0
  71. package/src/lib/plugin.js.map +1 -1
  72. package/src/lib/server/billing-webhook.js +115 -70
  73. package/src/lib/server/billing-webhook.js.map +1 -1
  74. package/src/lib/server/commerce-pdf.d.ts +104 -0
  75. package/src/lib/server/commerce-pdf.js +310 -0
  76. package/src/lib/server/commerce-pdf.js.map +1 -0
  77. package/src/lib/server/draft-order.js +23 -3
  78. package/src/lib/server/draft-order.js.map +1 -1
  79. package/src/lib/server/invoice-email.d.ts +76 -0
  80. package/src/lib/server/invoice-email.js +183 -0
  81. package/src/lib/server/invoice-email.js.map +1 -0
  82. package/src/lib/server/order-invoice-send.d.ts +43 -0
  83. package/src/lib/server/order-invoice-send.js +218 -0
  84. package/src/lib/server/order-invoice-send.js.map +1 -0
  85. package/src/lib/server/order-print.d.ts +6 -0
  86. package/src/lib/server/order-print.js +22 -5
  87. package/src/lib/server/order-print.js.map +1 -1
  88. package/src/lib/server/order-status.d.ts +11 -0
  89. package/src/lib/server/order-status.js +44 -8
  90. package/src/lib/server/order-status.js.map +1 -1
  91. package/src/lib/server/pdf-response.d.ts +31 -0
  92. package/src/lib/server/pdf-response.js +30 -0
  93. package/src/lib/server/pdf-response.js.map +1 -0
  94. package/src/lib/server/person-eraser.d.ts +3 -2
  95. package/src/lib/server/person-eraser.js +25 -6
  96. package/src/lib/server/person-eraser.js.map +1 -1
  97. package/src/lib/server/pos-reader-splash.d.ts +2 -18
  98. package/src/lib/server/pos-reader-splash.js +8 -93
  99. package/src/lib/server/pos-reader-splash.js.map +1 -1
  100. package/src/lib/server/quote-email.d.ts +85 -0
  101. package/src/lib/server/quote-email.js +211 -0
  102. package/src/lib/server/quote-email.js.map +1 -0
  103. package/src/lib/server/quote-link-token.d.ts +11 -0
  104. package/src/lib/server/quote-link-token.js +21 -0
  105. package/src/lib/server/quote-link-token.js.map +1 -1
  106. package/src/lib/server/quote-pay.d.ts +38 -6
  107. package/src/lib/server/quote-pay.js +115 -53
  108. package/src/lib/server/quote-pay.js.map +1 -1
  109. package/src/lib/server/quote-proofs.d.ts +51 -0
  110. package/src/lib/server/quote-proofs.js +500 -0
  111. package/src/lib/server/quote-proofs.js.map +1 -0
  112. package/src/lib/server/quote-uploads.d.ts +33 -0
  113. package/src/lib/server/quote-uploads.js +29 -0
  114. package/src/lib/server/quote-uploads.js.map +1 -0
  115. package/src/lib/server/quote.d.ts +25 -0
  116. package/src/lib/server/quote.js +638 -8
  117. package/src/lib/server/quote.js.map +1 -1
  118. package/src/lib/server/site-image-source.d.ts +38 -0
  119. package/src/lib/server/site-image-source.js +130 -0
  120. package/src/lib/server/site-image-source.js.map +1 -0
  121. package/src/lib/server/site-page-resolver.js +2 -1
  122. package/src/lib/server/site-page-resolver.js.map +1 -1
  123. package/src/lib/server.js +24 -1
  124. package/src/lib/server.js.map +1 -1
  125. package/src/lib/site.js +8 -0
  126. package/src/lib/site.js.map +1 -1
  127. package/src/lib/tenant-emails.js +243 -0
  128. package/src/lib/tenant-emails.js.map +1 -1
  129. package/src/lib/utils/commerce-document-pdf.d.ts +82 -0
  130. package/src/lib/utils/commerce-document-pdf.js +613 -0
  131. package/src/lib/utils/commerce-document-pdf.js.map +1 -0
  132. package/src/lib/utils/invoice-email-content.d.ts +45 -0
  133. package/src/lib/utils/invoice-email-content.js +126 -0
  134. package/src/lib/utils/invoice-email-content.js.map +1 -0
  135. package/src/lib/utils/order-invoice.d.ts +52 -3
  136. package/src/lib/utils/order-invoice.js +124 -64
  137. package/src/lib/utils/order-invoice.js.map +1 -1
  138. package/src/lib/utils/quote-email-content.d.ts +62 -0
  139. package/src/lib/utils/quote-email-content.js +154 -0
  140. package/src/lib/utils/quote-email-content.js.map +1 -0
@@ -37,3 +37,14 @@ export declare function quotePayLinkUrl(host: {
37
37
  cname?: unknown;
38
38
  subdomain?: unknown;
39
39
  } | null | undefined, hostId: string, quoteId: string, version: number): string | null;
40
+ /**
41
+ * The absolute address of one of the current revision's proofs, behind the
42
+ * same signed link as the page (`GET /api/commerce/quote-proof`, AGL-4096),
43
+ * or null when the site has no public address or no signing secret. What the
44
+ * quote email draws a proof image from (AGL-4097); a link the business
45
+ * replaces stops serving it, with the page.
46
+ */
47
+ export declare function quoteProofLinkUrl(host: {
48
+ cname?: unknown;
49
+ subdomain?: unknown;
50
+ } | null | undefined, hostId: string, quoteId: string, version: number, proofId: string): string | null;
@@ -93,5 +93,26 @@ function signQuoteLink(hostId, quoteId, version) {
93
93
  if (!origin || !token) return null;
94
94
  return `${origin}${quotePageHref(quoteId, token)}`;
95
95
  }
96
+ /**
97
+ * The absolute address of one of the current revision's proofs, behind the
98
+ * same signed link as the page (`GET /api/commerce/quote-proof`, AGL-4096),
99
+ * or null when the site has no public address or no signing secret. What the
100
+ * quote email draws a proof image from (AGL-4097); a link the business
101
+ * replaces stops serving it, with the page.
102
+ */ export function quoteProofLinkUrl(host, hostId, quoteId, version, proofId) {
103
+ const origin = hostPublicOrigin({
104
+ cname: typeof (host == null ? void 0 : host.cname) === 'string' ? host.cname : null,
105
+ subdomain: typeof (host == null ? void 0 : host.subdomain) === 'string' ? host.subdomain : null
106
+ });
107
+ const token = mintQuoteLinkToken(hostId, quoteId, version);
108
+ if (!origin || !token || !proofId) return null;
109
+ const query = new URLSearchParams({
110
+ hostId,
111
+ q: quoteId,
112
+ t: token,
113
+ file: proofId
114
+ });
115
+ return `${origin}/api/commerce/quote-proof?${query.toString()}`;
116
+ }
96
117
 
97
118
  //# sourceMappingURL=quote-link-token.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/quote-link-token.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\nimport { hostPublicOrigin } from '@aglyn/aglyn/server'\nimport { createHmac, timingSafeEqual } from 'crypto'\nimport { quotePageHref } from '../constants/quote-page'\nimport { tokenSigningSecret } from './download'\n\n/**\n * A quote's pay link (AGL-4094): `/pay-quote?q=<quoteId>&t=<token>` on the\n * business's own site, where the customer sees the quote and pays it.\n *\n * The token is `<version>.<signature>`: the quote's link version\n * (`quote.linkVersion`, absent reading as 1) and the first 32 hex characters of\n * `HMAC(quote-link:hostId:quoteId:version)` under the commerce\n * `TOKEN_SIGNING_SECRET`. A pure function of what the quote already holds, so\n * nothing is stored per link and every message that carried the link carries\n * the same working one. The `quote-link:` prefix keeps it from being replayed\n * as an order-status, download or supplier token, which sign other payloads\n * under the same secret.\n *\n * NO EXPIRY. A quote is paid when the customer is ready, and a link that died\n * first would be a quote they could not pay. The version is what retires a\n * link: replacing it bumps the version on the quote, and a link signed for an\n * older version still verifies, so its page can say the quote was replaced,\n * without showing any of it.\n */\nconst SIGNATURE_LENGTH = 32\nconst MAX_VERSION = 1_000_000\n\nfunction signQuoteLink(hostId: string, quoteId: string, version: number): string {\n return createHmac('sha256', tokenSigningSecret())\n .update(`quote-link:${hostId}:${quoteId}:${version}`)\n .digest('hex')\n .slice(0, SIGNATURE_LENGTH)\n}\n\n/** The token's two halves, or null for a token not shaped like one. */\nexport function parseQuoteLinkToken(token: string): { version: number; signature: string } | null {\n const match = /^([1-9]\\d{0,6})\\.([0-9a-f]{32})$/.exec(String(token ?? ''))\n if (!match) return null\n const version = Number(match[1])\n if (!Number.isSafeInteger(version) || version > MAX_VERSION) return null\n return { version, signature: match[2] }\n}\n\n/** The token for one version of a quote's link, or null when the signing secret is unset. */\nexport function mintQuoteLinkToken(hostId: string, quoteId: string, version: number): string | null {\n if (!hostId || !quoteId || !Number.isSafeInteger(version) || version < 1 || version > MAX_VERSION) {\n return null\n }\n try {\n return `${version}.${signQuoteLink(hostId, quoteId, version)}`\n } catch {\n return null\n }\n}\n\n/**\n * Checks a presented token in constant time and answers the link version it\n * was signed for, or null for a token that is wrong, malformed or unverifiable\n * (no secret). The caller compares the version with the quote's own.\n */\nexport function verifyQuoteLinkToken(hostId: string, quoteId: string, token: string): number | null {\n if (!hostId || !quoteId) return null\n const parsed = parseQuoteLinkToken(token)\n if (!parsed) return null\n let expected: string\n try {\n expected = signQuoteLink(hostId, quoteId, parsed.version)\n } catch {\n return null\n }\n const a = Buffer.from(parsed.signature)\n const b = Buffer.from(expected)\n if (a.length !== b.length) return null\n return timingSafeEqual(new Uint8Array(a), new Uint8Array(b)) ? parsed.version : null\n}\n\n/**\n * The absolute pay link for a quote at a link version, or null when either\n * half is missing: a site with no public address, or no signing secret. What\n * the console copies, and what a quote or invoice email puts in front of the\n * customer.\n */\nexport function quotePayLinkUrl(\n host: { cname?: unknown; subdomain?: unknown } | null | undefined,\n hostId: string,\n quoteId: string,\n version: number,\n): string | null {\n const origin = hostPublicOrigin({\n cname: typeof host?.cname === 'string' ? host.cname : null,\n subdomain: typeof host?.subdomain === 'string' ? host.subdomain : null,\n })\n const token = mintQuoteLinkToken(hostId, quoteId, version)\n if (!origin || !token) return null\n return `${origin}${quotePageHref(quoteId, token)}`\n}\n"],"names":["hostPublicOrigin","createHmac","timingSafeEqual","quotePageHref","tokenSigningSecret","SIGNATURE_LENGTH","MAX_VERSION","signQuoteLink","hostId","quoteId","version","update","digest","slice","parseQuoteLinkToken","token","match","exec","String","Number","isSafeInteger","signature","mintQuoteLinkToken","verifyQuoteLinkToken","parsed","expected","a","Buffer","from","b","length","Uint8Array","quotePayLinkUrl","host","origin","cname","subdomain"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,gBAAgB,QAAQ,sBAAqB;AACtD,SAASC,UAAU,EAAEC,eAAe,QAAQ,SAAQ;AACpD,SAASC,aAAa,QAAQ,6BAAyB;AACvD,SAASC,kBAAkB,QAAQ,gBAAY;AAE/C;;;;;;;;;;;;;;;;;;CAkBC,GACD,MAAMC,mBAAmB;AACzB,MAAMC,cAAc;AAEpB,SAASC,cAAcC,MAAc,EAAEC,OAAe,EAAEC,OAAe;IACrE,OAAOT,WAAW,UAAUG,sBACzBO,MAAM,CAAC,CAAC,WAAW,EAAEH,OAAO,CAAC,EAAEC,QAAQ,CAAC,EAAEC,SAAS,EACnDE,MAAM,CAAC,OACPC,KAAK,CAAC,GAAGR;AACd;AAEA,qEAAqE,GACrE,OAAO,SAASS,oBAAoBC,KAAa;IAC/C,MAAMC,QAAQ,mCAAmCC,IAAI,CAACC,OAAOH,gBAAAA,QAAS;IACtE,IAAI,CAACC,OAAO,OAAO;IACnB,MAAMN,UAAUS,OAAOH,KAAK,CAAC,EAAE;IAC/B,IAAI,CAACG,OAAOC,aAAa,CAACV,YAAYA,UAAUJ,aAAa,OAAO;IACpE,OAAO;QAAEI;QAASW,WAAWL,KAAK,CAAC,EAAE;IAAC;AACxC;AAEA,2FAA2F,GAC3F,OAAO,SAASM,mBAAmBd,MAAc,EAAEC,OAAe,EAAEC,OAAe;IACjF,IAAI,CAACF,UAAU,CAACC,WAAW,CAACU,OAAOC,aAAa,CAACV,YAAYA,UAAU,KAAKA,UAAUJ,aAAa;QACjG,OAAO;IACT;IACA,IAAI;QACF,OAAO,GAAGI,QAAQ,CAAC,EAAEH,cAAcC,QAAQC,SAASC,UAAU;IAChE,EAAE,eAAM;QACN,OAAO;IACT;AACF;AAEA;;;;CAIC,GACD,OAAO,SAASa,qBAAqBf,MAAc,EAAEC,OAAe,EAAEM,KAAa;IACjF,IAAI,CAACP,UAAU,CAACC,SAAS,OAAO;IAChC,MAAMe,SAASV,oBAAoBC;IACnC,IAAI,CAACS,QAAQ,OAAO;IACpB,IAAIC;IACJ,IAAI;QACFA,WAAWlB,cAAcC,QAAQC,SAASe,OAAOd,OAAO;IAC1D,EAAE,eAAM;QACN,OAAO;IACT;IACA,MAAMgB,IAAIC,OAAOC,IAAI,CAACJ,OAAOH,SAAS;IACtC,MAAMQ,IAAIF,OAAOC,IAAI,CAACH;IACtB,IAAIC,EAAEI,MAAM,KAAKD,EAAEC,MAAM,EAAE,OAAO;IAClC,OAAO5B,gBAAgB,IAAI6B,WAAWL,IAAI,IAAIK,WAAWF,MAAML,OAAOd,OAAO,GAAG;AAClF;AAEA;;;;;CAKC,GACD,OAAO,SAASsB,gBACdC,IAAiE,EACjEzB,MAAc,EACdC,OAAe,EACfC,OAAe;IAEf,MAAMwB,SAASlC,iBAAiB;QAC9BmC,OAAO,QAAOF,wBAAAA,KAAME,KAAK,MAAK,WAAWF,KAAKE,KAAK,GAAG;QACtDC,WAAW,QAAOH,wBAAAA,KAAMG,SAAS,MAAK,WAAWH,KAAKG,SAAS,GAAG;IACpE;IACA,MAAMrB,QAAQO,mBAAmBd,QAAQC,SAASC;IAClD,IAAI,CAACwB,UAAU,CAACnB,OAAO,OAAO;IAC9B,OAAO,GAAGmB,SAAS/B,cAAcM,SAASM,QAAQ;AACpD"}
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/quote-link-token.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\nimport { hostPublicOrigin } from '@aglyn/aglyn/server'\nimport { createHmac, timingSafeEqual } from 'crypto'\nimport { quotePageHref } from '../constants/quote-page'\nimport { tokenSigningSecret } from './download'\n\n/**\n * A quote's pay link (AGL-4094): `/pay-quote?q=<quoteId>&t=<token>` on the\n * business's own site, where the customer sees the quote and pays it.\n *\n * The token is `<version>.<signature>`: the quote's link version\n * (`quote.linkVersion`, absent reading as 1) and the first 32 hex characters of\n * `HMAC(quote-link:hostId:quoteId:version)` under the commerce\n * `TOKEN_SIGNING_SECRET`. A pure function of what the quote already holds, so\n * nothing is stored per link and every message that carried the link carries\n * the same working one. The `quote-link:` prefix keeps it from being replayed\n * as an order-status, download or supplier token, which sign other payloads\n * under the same secret.\n *\n * NO EXPIRY. A quote is paid when the customer is ready, and a link that died\n * first would be a quote they could not pay. The version is what retires a\n * link: replacing it bumps the version on the quote, and a link signed for an\n * older version still verifies, so its page can say the quote was replaced,\n * without showing any of it.\n */\nconst SIGNATURE_LENGTH = 32\nconst MAX_VERSION = 1_000_000\n\nfunction signQuoteLink(hostId: string, quoteId: string, version: number): string {\n return createHmac('sha256', tokenSigningSecret())\n .update(`quote-link:${hostId}:${quoteId}:${version}`)\n .digest('hex')\n .slice(0, SIGNATURE_LENGTH)\n}\n\n/** The token's two halves, or null for a token not shaped like one. */\nexport function parseQuoteLinkToken(token: string): { version: number; signature: string } | null {\n const match = /^([1-9]\\d{0,6})\\.([0-9a-f]{32})$/.exec(String(token ?? ''))\n if (!match) return null\n const version = Number(match[1])\n if (!Number.isSafeInteger(version) || version > MAX_VERSION) return null\n return { version, signature: match[2] }\n}\n\n/** The token for one version of a quote's link, or null when the signing secret is unset. */\nexport function mintQuoteLinkToken(hostId: string, quoteId: string, version: number): string | null {\n if (!hostId || !quoteId || !Number.isSafeInteger(version) || version < 1 || version > MAX_VERSION) {\n return null\n }\n try {\n return `${version}.${signQuoteLink(hostId, quoteId, version)}`\n } catch {\n return null\n }\n}\n\n/**\n * Checks a presented token in constant time and answers the link version it\n * was signed for, or null for a token that is wrong, malformed or unverifiable\n * (no secret). The caller compares the version with the quote's own.\n */\nexport function verifyQuoteLinkToken(hostId: string, quoteId: string, token: string): number | null {\n if (!hostId || !quoteId) return null\n const parsed = parseQuoteLinkToken(token)\n if (!parsed) return null\n let expected: string\n try {\n expected = signQuoteLink(hostId, quoteId, parsed.version)\n } catch {\n return null\n }\n const a = Buffer.from(parsed.signature)\n const b = Buffer.from(expected)\n if (a.length !== b.length) return null\n return timingSafeEqual(new Uint8Array(a), new Uint8Array(b)) ? parsed.version : null\n}\n\n/**\n * The absolute pay link for a quote at a link version, or null when either\n * half is missing: a site with no public address, or no signing secret. What\n * the console copies, and what a quote or invoice email puts in front of the\n * customer.\n */\nexport function quotePayLinkUrl(\n host: { cname?: unknown; subdomain?: unknown } | null | undefined,\n hostId: string,\n quoteId: string,\n version: number,\n): string | null {\n const origin = hostPublicOrigin({\n cname: typeof host?.cname === 'string' ? host.cname : null,\n subdomain: typeof host?.subdomain === 'string' ? host.subdomain : null,\n })\n const token = mintQuoteLinkToken(hostId, quoteId, version)\n if (!origin || !token) return null\n return `${origin}${quotePageHref(quoteId, token)}`\n}\n\n/**\n * The absolute address of one of the current revision's proofs, behind the\n * same signed link as the page (`GET /api/commerce/quote-proof`, AGL-4096),\n * or null when the site has no public address or no signing secret. What the\n * quote email draws a proof image from (AGL-4097); a link the business\n * replaces stops serving it, with the page.\n */\nexport function quoteProofLinkUrl(\n host: { cname?: unknown; subdomain?: unknown } | null | undefined,\n hostId: string,\n quoteId: string,\n version: number,\n proofId: string,\n): string | null {\n const origin = hostPublicOrigin({\n cname: typeof host?.cname === 'string' ? host.cname : null,\n subdomain: typeof host?.subdomain === 'string' ? host.subdomain : null,\n })\n const token = mintQuoteLinkToken(hostId, quoteId, version)\n if (!origin || !token || !proofId) return null\n const query = new URLSearchParams({ hostId, q: quoteId, t: token, file: proofId })\n return `${origin}/api/commerce/quote-proof?${query.toString()}`\n}\n"],"names":["hostPublicOrigin","createHmac","timingSafeEqual","quotePageHref","tokenSigningSecret","SIGNATURE_LENGTH","MAX_VERSION","signQuoteLink","hostId","quoteId","version","update","digest","slice","parseQuoteLinkToken","token","match","exec","String","Number","isSafeInteger","signature","mintQuoteLinkToken","verifyQuoteLinkToken","parsed","expected","a","Buffer","from","b","length","Uint8Array","quotePayLinkUrl","host","origin","cname","subdomain","quoteProofLinkUrl","proofId","query","URLSearchParams","q","t","file","toString"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,gBAAgB,QAAQ,sBAAqB;AACtD,SAASC,UAAU,EAAEC,eAAe,QAAQ,SAAQ;AACpD,SAASC,aAAa,QAAQ,6BAAyB;AACvD,SAASC,kBAAkB,QAAQ,gBAAY;AAE/C;;;;;;;;;;;;;;;;;;CAkBC,GACD,MAAMC,mBAAmB;AACzB,MAAMC,cAAc;AAEpB,SAASC,cAAcC,MAAc,EAAEC,OAAe,EAAEC,OAAe;IACrE,OAAOT,WAAW,UAAUG,sBACzBO,MAAM,CAAC,CAAC,WAAW,EAAEH,OAAO,CAAC,EAAEC,QAAQ,CAAC,EAAEC,SAAS,EACnDE,MAAM,CAAC,OACPC,KAAK,CAAC,GAAGR;AACd;AAEA,qEAAqE,GACrE,OAAO,SAASS,oBAAoBC,KAAa;IAC/C,MAAMC,QAAQ,mCAAmCC,IAAI,CAACC,OAAOH,gBAAAA,QAAS;IACtE,IAAI,CAACC,OAAO,OAAO;IACnB,MAAMN,UAAUS,OAAOH,KAAK,CAAC,EAAE;IAC/B,IAAI,CAACG,OAAOC,aAAa,CAACV,YAAYA,UAAUJ,aAAa,OAAO;IACpE,OAAO;QAAEI;QAASW,WAAWL,KAAK,CAAC,EAAE;IAAC;AACxC;AAEA,2FAA2F,GAC3F,OAAO,SAASM,mBAAmBd,MAAc,EAAEC,OAAe,EAAEC,OAAe;IACjF,IAAI,CAACF,UAAU,CAACC,WAAW,CAACU,OAAOC,aAAa,CAACV,YAAYA,UAAU,KAAKA,UAAUJ,aAAa;QACjG,OAAO;IACT;IACA,IAAI;QACF,OAAO,GAAGI,QAAQ,CAAC,EAAEH,cAAcC,QAAQC,SAASC,UAAU;IAChE,EAAE,eAAM;QACN,OAAO;IACT;AACF;AAEA;;;;CAIC,GACD,OAAO,SAASa,qBAAqBf,MAAc,EAAEC,OAAe,EAAEM,KAAa;IACjF,IAAI,CAACP,UAAU,CAACC,SAAS,OAAO;IAChC,MAAMe,SAASV,oBAAoBC;IACnC,IAAI,CAACS,QAAQ,OAAO;IACpB,IAAIC;IACJ,IAAI;QACFA,WAAWlB,cAAcC,QAAQC,SAASe,OAAOd,OAAO;IAC1D,EAAE,eAAM;QACN,OAAO;IACT;IACA,MAAMgB,IAAIC,OAAOC,IAAI,CAACJ,OAAOH,SAAS;IACtC,MAAMQ,IAAIF,OAAOC,IAAI,CAACH;IACtB,IAAIC,EAAEI,MAAM,KAAKD,EAAEC,MAAM,EAAE,OAAO;IAClC,OAAO5B,gBAAgB,IAAI6B,WAAWL,IAAI,IAAIK,WAAWF,MAAML,OAAOd,OAAO,GAAG;AAClF;AAEA;;;;;CAKC,GACD,OAAO,SAASsB,gBACdC,IAAiE,EACjEzB,MAAc,EACdC,OAAe,EACfC,OAAe;IAEf,MAAMwB,SAASlC,iBAAiB;QAC9BmC,OAAO,QAAOF,wBAAAA,KAAME,KAAK,MAAK,WAAWF,KAAKE,KAAK,GAAG;QACtDC,WAAW,QAAOH,wBAAAA,KAAMG,SAAS,MAAK,WAAWH,KAAKG,SAAS,GAAG;IACpE;IACA,MAAMrB,QAAQO,mBAAmBd,QAAQC,SAASC;IAClD,IAAI,CAACwB,UAAU,CAACnB,OAAO,OAAO;IAC9B,OAAO,GAAGmB,SAAS/B,cAAcM,SAASM,QAAQ;AACpD;AAEA;;;;;;CAMC,GACD,OAAO,SAASsB,kBACdJ,IAAiE,EACjEzB,MAAc,EACdC,OAAe,EACfC,OAAe,EACf4B,OAAe;IAEf,MAAMJ,SAASlC,iBAAiB;QAC9BmC,OAAO,QAAOF,wBAAAA,KAAME,KAAK,MAAK,WAAWF,KAAKE,KAAK,GAAG;QACtDC,WAAW,QAAOH,wBAAAA,KAAMG,SAAS,MAAK,WAAWH,KAAKG,SAAS,GAAG;IACpE;IACA,MAAMrB,QAAQO,mBAAmBd,QAAQC,SAASC;IAClD,IAAI,CAACwB,UAAU,CAACnB,SAAS,CAACuB,SAAS,OAAO;IAC1C,MAAMC,QAAQ,IAAIC,gBAAgB;QAAEhC;QAAQiC,GAAGhC;QAASiC,GAAG3B;QAAO4B,MAAML;IAAQ;IAChF,OAAO,GAAGJ,OAAO,0BAA0B,EAAEK,MAAMK,QAAQ,IAAI;AACjE"}
@@ -55,19 +55,50 @@ export interface QuotePayDeps {
55
55
  now(): number;
56
56
  }
57
57
  export declare function defaultQuotePayDeps(): QuotePayDeps;
58
- type Outcome = {
58
+ /** A link route's answer: a status, a JSON body and any extra headers. */
59
+ export type QuoteLinkOutcome = {
59
60
  status: number;
60
61
  body: Record<string, unknown>;
61
62
  headers?: Record<string, string>;
62
63
  };
64
+ /** The rate limit a link route spends per address per site, BEFORE the token is checked. */
65
+ export declare function limitedQuoteLink(deps: QuotePayDeps, req: PluginApiRequest, key: string, limit: number): Promise<QuoteLinkOutcome | null>;
66
+ export interface LoadedQuote {
67
+ hostRef: FirebaseFirestore.DocumentReference;
68
+ host: Record<string, unknown>;
69
+ order: CommerceModel.HostOrder;
70
+ storeName: string;
71
+ store: FirebaseFirestore.DocumentSnapshot;
72
+ /** A quote, or an order's invoice (only for a route that opens invoices). */
73
+ kind: CommerceModel.OrderPayLinkKind;
74
+ }
75
+ /**
76
+ * The quote a link names, after its token: the quote, the replaced answer for
77
+ * a link to an older version, or the one not-found answer for everything else.
78
+ * With `invoices`, a draft order's invoice too (AGL-4099).
79
+ */
80
+ export declare function loadLinkedQuote(deps: QuotePayDeps, hostId: string, quoteId: string, token: string, options?: {
81
+ invoices?: boolean;
82
+ }): Promise<{
83
+ loaded: LoadedQuote;
84
+ } | {
85
+ outcome: QuoteLinkOutcome;
86
+ }>;
87
+ /** The link's site, quote and token from a query or a body, and whether all three are there. */
88
+ export declare function quoteLinkParams(source: Record<string, unknown>): {
89
+ hostId: string;
90
+ quoteId: string;
91
+ token: string;
92
+ complete: boolean;
93
+ };
63
94
  /** `GET /api/commerce/quote-view`: the quote page's data. */
64
- export declare function handleQuoteView(deps: QuotePayDeps, req: PluginApiRequest): Promise<Outcome>;
65
- /** The Checkout Session a quote's Pay opens, as form params. Pure over its inputs. */
95
+ export declare function handleQuoteView(deps: QuotePayDeps, req: PluginApiRequest): Promise<QuoteLinkOutcome>;
96
+ /** The Checkout Session a quote's (or an invoice's) Pay opens, as form params. Pure over its inputs. */
66
97
  export declare function buildQuoteSessionParams(input: {
67
98
  hostId: string;
68
99
  quoteId: string;
69
100
  order: CommerceModel.HostOrder;
70
- charge: CommerceModel.QuoteCheckoutCharge;
101
+ charge: CommerceModel.QuoteCheckoutCharge & Partial<Pick<CommerceModel.InvoiceCheckoutCharge, 'shippingOptions' | 'shippingCountries'>>;
71
102
  accountId: string;
72
103
  feeCents: number;
73
104
  taxRateId: string;
@@ -75,9 +106,10 @@ export declare function buildQuoteSessionParams(input: {
75
106
  paymentMethods: CommerceModel.StorefrontPaymentMethodControls;
76
107
  }): URLSearchParams;
77
108
  /** `POST /api/commerce/quote-pay`: a fresh Checkout Session for the quote. */
78
- export declare function handleQuotePay(deps: QuotePayDeps, req: PluginApiRequest): Promise<Outcome>;
109
+ export declare function handleQuotePay(deps: QuotePayDeps, req: PluginApiRequest): Promise<QuoteLinkOutcome>;
110
+ /** Sends a link route's answer, never cached or indexed. */
111
+ export declare function sendQuoteLinkOutcome(res: Parameters<PluginApiHandler>[1], outcome: QuoteLinkOutcome): void;
79
112
  /** `GET /api/commerce/quote-view`. */
80
113
  export declare const quoteViewHandler: PluginApiHandler;
81
114
  /** `POST /api/commerce/quote-pay`. */
82
115
  export declare const quotePayHandler: PluginApiHandler;
83
- export {};
@@ -30,7 +30,8 @@ import { orderStatusUrl } from "./order-status-token.js";
30
30
  import { ensureCheckoutDomain } from "./payment-method-domains.js";
31
31
  import { verifyQuoteLinkToken } from "./quote-link-token.js";
32
32
  /*==========================================
33
- * THE CUSTOMER'S QUOTE PAGE AND ITS PAY BUTTON (AGL-4094).
33
+ * THE CUSTOMER'S QUOTE PAGE AND ITS PAY BUTTON (AGL-4094), AND AN
34
+ * INVOICE'S (AGL-4099).
34
35
  *
35
36
  * GET /api/commerce/quote-view?hostId=&q=<quoteId>&t=<token>
36
37
  * → 200 QuotePageView | 200 { state: 'replaced', storeName } | 404
@@ -53,6 +54,16 @@ import { verifyQuoteLinkToken } from "./quote-link-token.js";
53
54
  *
54
55
  * The payment completes in `billing-webhook.ts`, which turns this same
55
56
  * document into the order (`paidQuoteOrder`); nothing here marks it paid.
57
+ *
58
+ * AN INVOICE'S LINK IS THIS ONE (AGL-4099). A draft order the business sent
59
+ * an invoice for opens on the same page from the same kind of link (its
60
+ * version on `payLink.version`, `commerce-invoices.ts`), as "Invoice #N" with
61
+ * no proofs or approval, and Pay opens the same kind of fresh session from
62
+ * its stored lines, discount, tax and the shipping rates it was made with.
63
+ * Its webhook is the draft branch it always completed through, which flips
64
+ * the draft to paid without restamping its list fields. The proof and answer
65
+ * routes stay a quote's alone: `loadLinkedQuote` opens an invoice only for
66
+ * the routes that ask (`{ invoices: true }`).
56
67
  *=========================================*/ /** Lookups and presses one address may make per site, per ten minutes. */ export const QUOTE_VIEW_LOOKUPS_PER_WINDOW = 60;
57
68
  export const QUOTE_PAY_PRESSES_PER_WINDOW = 20;
58
69
  export const QUOTE_WINDOW_MS = 10 * 60000;
@@ -95,6 +106,13 @@ const NOT_FOUND = {
95
106
  error: 'We could not find that quote. Check the link you were sent.'
96
107
  }
97
108
  };
109
+ /** The not-found answer of a route an invoice's link reaches too: it names neither. */ const NOT_FOUND_ANY = {
110
+ status: 404,
111
+ body: {
112
+ error: 'We could not find that quote or invoice. Check the link you were sent.'
113
+ }
114
+ };
115
+ /** `quote` or `invoice`, as the customer reads it. */ const nounOf = (kind)=>kind === 'invoice' ? 'invoice' : 'quote';
98
116
  function bodyOf(req) {
99
117
  const raw = req.body;
100
118
  if (typeof raw === 'string') {
@@ -113,7 +131,7 @@ function headerOf(req, name) {
113
131
  const value = (_headers_name_toLowerCase = headers[name.toLowerCase()]) != null ? _headers_name_toLowerCase : headers[name];
114
132
  return String(Array.isArray(value) ? (_value_ = value[0]) != null ? _value_ : '' : value != null ? value : '').trim();
115
133
  }
116
- async function limited(deps, req, key, limit) {
134
+ /** The rate limit a link route spends per address per site, BEFORE the token is checked. */ export async function limitedQuoteLink(deps, req, key, limit) {
117
135
  var _readClientIp;
118
136
  var _req_socket;
119
137
  const ip = (_readClientIp = readClientIp(req.headers, {
@@ -137,11 +155,13 @@ async function limited(deps, req, key, limit) {
137
155
  /**
138
156
  * The quote a link names, after its token: the quote, the replaced answer for
139
157
  * a link to an older version, or the one not-found answer for everything else.
140
- */ async function loadLinkedQuote(deps, hostId, quoteId, token) {
158
+ * With `invoices`, a draft order's invoice too (AGL-4099).
159
+ */ export async function loadLinkedQuote(deps, hostId, quoteId, token, options = {}) {
141
160
  var _hostSnapshot_data, _resolveHostToken;
161
+ const notFound = options.invoices ? NOT_FOUND_ANY : NOT_FOUND;
142
162
  const version = verifyQuoteLinkToken(hostId, quoteId, token);
143
163
  if (version === null) return {
144
- outcome: NOT_FOUND
164
+ outcome: notFound
145
165
  };
146
166
  const hostRef = deps.firestore().collection('hosts').doc(hostId);
147
167
  const [hostSnapshot, store, orderSnapshot] = await Promise.all([
@@ -150,17 +170,18 @@ async function limited(deps, req, key, limit) {
150
170
  hostRef.collection('orders').doc(quoteId).get()
151
171
  ]);
152
172
  if (!hostSnapshot.exists || !orderSnapshot.exists) return {
153
- outcome: NOT_FOUND
173
+ outcome: notFound
154
174
  };
155
175
  const order = orderSnapshot.data();
156
- if (!CommerceModel.isQuote(order) || !order.quote) return {
157
- outcome: NOT_FOUND
176
+ const kind = CommerceModel.orderPayLinkKind(order);
177
+ if (kind === null || kind === 'invoice' && !options.invoices) return {
178
+ outcome: notFound
158
179
  };
159
180
  const host = (_hostSnapshot_data = hostSnapshot.data()) != null ? _hostSnapshot_data : {};
160
181
  const storeName = String((_resolveHostToken = resolveHostToken('businessName', host)) != null ? _resolveHostToken : '');
161
- const current = CommerceModel.quoteLinkVersion(order.quote);
182
+ const current = CommerceModel.orderPayLinkVersion(order);
162
183
  if (version > current) return {
163
- outcome: NOT_FOUND
184
+ outcome: notFound
164
185
  };
165
186
  if (version < current) {
166
187
  return {
@@ -168,9 +189,10 @@ async function limited(deps, req, key, limit) {
168
189
  status: 410,
169
190
  body: {
170
191
  state: 'replaced',
192
+ kind,
171
193
  storeName,
172
194
  replaced: true,
173
- error: `This quote was updated, and ${storeName || 'the business'} sent you a newer link.`
195
+ error: `This ${nounOf(kind)} was updated, and ${storeName || 'the business'} sent you a newer link.`
174
196
  }
175
197
  }
176
198
  };
@@ -181,11 +203,12 @@ async function limited(deps, req, key, limit) {
181
203
  host,
182
204
  order,
183
205
  storeName,
184
- store
206
+ store,
207
+ kind
185
208
  }
186
209
  };
187
210
  }
188
- function linkParams(req, source) {
211
+ /** The link's site, quote and token from a query or a body, and whether all three are there. */ export function quoteLinkParams(source) {
189
212
  var _source_hostId, _source_q, _source_t;
190
213
  const hostId = String((_source_hostId = source['hostId']) != null ? _source_hostId : '');
191
214
  const quoteId = String((_source_q = source['q']) != null ? _source_q : '').slice(0, 200);
@@ -206,48 +229,60 @@ function linkParams(req, source) {
206
229
  error: 'Method not allowed'
207
230
  }
208
231
  };
209
- const { hostId, quoteId, token, complete } = linkParams(req, (_req_query = req.query) != null ? _req_query : {});
232
+ const { hostId, quoteId, token, complete } = quoteLinkParams((_req_query = req.query) != null ? _req_query : {});
210
233
  if (!complete) return {
211
234
  status: 400,
212
235
  body: {
213
236
  error: 'This link is incomplete.'
214
237
  }
215
238
  };
216
- const refused = await limited(deps, req, `commerce-quote-view:${hostId}`, QUOTE_VIEW_LOOKUPS_PER_WINDOW);
239
+ const refused = await limitedQuoteLink(deps, req, `commerce-quote-view:${hostId}`, QUOTE_VIEW_LOOKUPS_PER_WINDOW);
217
240
  if (refused) return refused;
218
- const linked = await loadLinkedQuote(deps, hostId, quoteId, token);
241
+ const linked = await loadLinkedQuote(deps, hostId, quoteId, token, {
242
+ invoices: true
243
+ });
219
244
  if ('outcome' in linked) {
220
245
  // The replaced page is a page, not a failure: answered as data.
221
246
  if (linked.outcome.status === 410) {
222
- const { storeName } = linked.outcome.body;
247
+ const { storeName, kind } = linked.outcome.body;
223
248
  return {
224
249
  status: 200,
225
250
  body: {
226
251
  state: 'replaced',
252
+ kind,
227
253
  storeName
228
254
  }
229
255
  };
230
256
  }
231
257
  return linked.outcome;
232
258
  }
233
- const { host, order, storeName, store } = linked.loaded;
234
- const view = CommerceModel.buildQuotePageView({
259
+ const { host, order, storeName, store, kind } = linked.loaded;
260
+ const currency = String((_store_get = store.get('currency')) != null ? _store_get : 'USD');
261
+ const receiptUrl = order.status === 'pending' ? null : orderStatusUrl(host, hostId, quoteId);
262
+ const view = kind === 'quote' ? CommerceModel.buildQuotePageView({
235
263
  order,
236
264
  orderId: quoteId,
237
265
  storeName,
238
- currency: String((_store_get = store.get('currency')) != null ? _store_get : 'USD'),
239
- receiptUrl: CommerceModel.isUnpaidQuote(order) ? null : orderStatusUrl(host, hostId, quoteId)
266
+ currency,
267
+ receiptUrl
268
+ }) : CommerceModel.buildInvoicePageView({
269
+ order,
270
+ orderId: quoteId,
271
+ storeName,
272
+ currency,
273
+ receiptUrl
240
274
  });
241
275
  return {
242
276
  status: 200,
243
277
  body: view
244
278
  };
245
279
  }
246
- /** The Checkout Session a quote's Pay opens, as form params. Pure over its inputs. */ export function buildQuoteSessionParams(input) {
247
- var _ref;
280
+ /** The Checkout Session a quote's (or an invoice's) Pay opens, as form params. Pure over its inputs. */ export function buildQuoteSessionParams(input) {
281
+ var _order_quote, _charge_shippingOptions, _ref;
248
282
  var _quote_customer;
249
283
  const { charge, order } = input;
250
- const quote = order.quote;
284
+ const invoice = CommerceModel.orderPayLinkKind(order) === 'invoice';
285
+ const quote = (_order_quote = order.quote) != null ? _order_quote : {};
251
286
  const params = new URLSearchParams(_extends({
252
287
  mode: 'payment'
253
288
  }, checkoutSessionCardAuthenticationParams('payment'), charge.taxAtPayment ? {
@@ -267,7 +302,14 @@ function linkParams(req, source) {
267
302
  // Shipping the business priced for the quote's ship-to, charged as the one
268
303
  // option, with the address asked for where it ships (and where Stripe Tax
269
304
  // reads the tax from). The quote's own address stands when it asks nothing.
270
- if (charge.shippingCents > 0) {
305
+ // An invoiced draft offers the rates it was made with, and the buyer picks
306
+ // one, as on the draft's first link (AGL-4099).
307
+ const rates = (_charge_shippingOptions = charge.shippingOptions) != null ? _charge_shippingOptions : [];
308
+ if (rates.length > 0) {
309
+ var _charge_shippingCountries;
310
+ CommerceModel.appendShippingAddressCollectionParams(params, ((_charge_shippingCountries = charge.shippingCountries) == null ? void 0 : _charge_shippingCountries.length) ? charge.shippingCountries : CommerceModel.CHECKOUT_SHIPPING_COUNTRIES);
311
+ CommerceModel.appendCheckoutShippingParams(params, rates);
312
+ } else if (charge.shippingCents > 0) {
271
313
  var _ref1;
272
314
  var _quote_shipTo;
273
315
  const country = String((_ref1 = (_quote_shipTo = quote.shipTo) == null ? void 0 : _quote_shipTo.country) != null ? _ref1 : '').toUpperCase();
@@ -282,7 +324,7 @@ function linkParams(req, source) {
282
324
  }
283
325
  ]);
284
326
  }
285
- const shippingFloorCents = charge.shippingCents;
327
+ const shippingFloorCents = rates.length > 0 ? CommerceModel.shippingFloorCents(rates) : charge.shippingCents;
286
328
  for (const [key, value] of Object.entries(CommerceModel.destinationChargeParams({
287
329
  accountId: input.accountId,
288
330
  feeCents: input.feeCents,
@@ -292,7 +334,7 @@ function linkParams(req, source) {
292
334
  }))){
293
335
  params.set(key, value);
294
336
  }
295
- const email = String((_ref = (_quote_customer = quote.customer) == null ? void 0 : _quote_customer.email) != null ? _ref : '').trim();
337
+ const email = String((_ref = invoice ? order.customerEmail : (_quote_customer = quote.customer) == null ? void 0 : _quote_customer.email) != null ? _ref : '').trim();
296
338
  if (email) params.set('customer_email', email);
297
339
  const separator = input.returnUrl.includes('?') ? '&' : '?';
298
340
  params.set('success_url', `${input.returnUrl}${separator}session_id={CHECKOUT_SESSION_ID}`);
@@ -303,9 +345,18 @@ function linkParams(req, source) {
303
345
  params.set('metadata[type]', 'commerce-draft');
304
346
  params.set('metadata[hostId]', input.hostId);
305
347
  params.set('metadata[orderId]', input.quoteId);
306
- params.set('metadata[quoteId]', input.quoteId);
307
- params.set('metadata[quoteLinkVersion]', String(CommerceModel.quoteLinkVersion(quote)));
308
- params.set('metadata[quoteUpdatedAtMs]', String(Number(quote.updatedAtMs) || 0));
348
+ if (invoice) {
349
+ // An invoice's link version, which the draft branch compares as a
350
+ // quote's (AGL-4099), and the promotion the draft was priced with, which
351
+ // it counts once the draft is paid (no hold: the draft's lapsed with its
352
+ // first session).
353
+ params.set('metadata[payLinkVersion]', String(CommerceModel.orderPayLinkVersion(order)));
354
+ if (order.discountId) params.set('metadata[discountId]', String(order.discountId));
355
+ } else {
356
+ params.set('metadata[quoteId]', input.quoteId);
357
+ params.set('metadata[quoteLinkVersion]', String(CommerceModel.quoteLinkVersion(quote)));
358
+ params.set('metadata[quoteUpdatedAtMs]', String(Number(quote.updatedAtMs) || 0));
359
+ }
309
360
  params.set('metadata[feeCents]', String(input.feeCents));
310
361
  // A tax charged as its own line is invisible to Stripe's tax total; this
311
362
  // witness is what records it as the business's tax.
@@ -346,7 +397,7 @@ const refusal = (status, reason, error, extra = {})=>({
346
397
  error: 'Payments are not configured.'
347
398
  }
348
399
  };
349
- const { hostId, quoteId, token, complete } = linkParams(req, bodyOf(req));
400
+ const { hostId, quoteId, token, complete } = quoteLinkParams(bodyOf(req));
350
401
  if (!complete) return {
351
402
  status: 400,
352
403
  body: {
@@ -360,22 +411,26 @@ const refusal = (status, reason, error, extra = {})=>({
360
411
  error: 'Paying needs an Idempotency-Key header.'
361
412
  }
362
413
  };
363
- const refused = await limited(deps, req, `commerce-quote-pay:${hostId}`, QUOTE_PAY_PRESSES_PER_WINDOW);
414
+ const refused = await limitedQuoteLink(deps, req, `commerce-quote-pay:${hostId}`, QUOTE_PAY_PRESSES_PER_WINDOW);
364
415
  if (refused) return refused;
365
- const linked = await loadLinkedQuote(deps, hostId, quoteId, token);
416
+ const linked = await loadLinkedQuote(deps, hostId, quoteId, token, {
417
+ invoices: true
418
+ });
366
419
  if ('outcome' in linked) return linked.outcome;
367
- const { hostRef, host, order, storeName, store } = linked.loaded;
420
+ const { hostRef, host, order, storeName, store, kind } = linked.loaded;
421
+ const noun = nounOf(kind);
368
422
  // Every refusal the customer can be given is above the claim, so none of
369
423
  // them spends the press. `v` is the edit of the quote the page showed: a
370
424
  // quote changed since is shown again before it is paid, never charged unseen.
425
+ // An invoice is not edited, so it sends none.
371
426
  const seen = Number(bodyOf(req)['v']);
372
- if (Number.isFinite(seen) && seen > 0 && seen !== (Number((_order_quote = order.quote) == null ? void 0 : _order_quote.updatedAtMs) || 0)) {
427
+ if (kind === 'quote' && Number.isFinite(seen) && seen > 0 && seen !== (Number((_order_quote = order.quote) == null ? void 0 : _order_quote.updatedAtMs) || 0)) {
373
428
  return refusal(409, 'updated', QUOTE_UPDATED_MESSAGE);
374
429
  }
375
- const blocked = CommerceModel.quotePayRefusal(order, quoteId);
376
- if (blocked) return refusal(409, blocked, CommerceModel.quotePayRefusalMessage(blocked, storeName));
430
+ const blocked = CommerceModel.invoicePayRefusal(order, quoteId);
431
+ if (blocked) return refusal(409, blocked, CommerceModel.invoicePayRefusalMessage(blocked, storeName, kind));
377
432
  const owner = await deps.orgForHost(hostId);
378
- const unavailable = refusal(409, 'payments-unavailable', `${storeName || 'This business'} can’t take payments online right now. Contact them to pay this quote.`);
433
+ const unavailable = refusal(409, 'payments-unavailable', `${storeName || 'This business'} can’t take payments online right now. Contact them to pay this ${noun}.`);
379
434
  if (!checkEntitlement(owner == null ? void 0 : owner.org, 'commerce')) return unavailable;
380
435
  const ownerUid = owner == null ? void 0 : (_owner_org = owner.org) == null ? void 0 : _owner_org.ownerUid;
381
436
  const profile = ownerUid ? await deps.firestore().collection('profiles').doc(String(ownerUid)).get() : null;
@@ -385,17 +440,20 @@ const refusal = (status, reason, error, extra = {})=>({
385
440
  chargesEnabled: profile == null ? void 0 : profile.get('stripeChargesEnabled'),
386
441
  accountLivemode: profile == null ? void 0 : profile.get('stripeAccountLivemode')
387
442
  }, {
388
- subject: `quote ${quoteId} on host ${hostId}`
443
+ subject: `${noun} ${quoteId} on host ${hostId}`
389
444
  });
390
445
  if (!ready) return unavailable;
391
446
  const taxSettings = (_store_get = store.get('tax')) != null ? _store_get : {};
392
447
  if (CommerceModel.storefrontTaxDecision({
393
448
  settings: taxSettings
394
449
  }).kind === 'undecided') {
395
- return refusal(409, 'tax-undecided', CommerceModel.quotePayRefusalMessage('tax-undecided', storeName));
450
+ return refusal(409, 'tax-undecided', CommerceModel.invoicePayRefusalMessage('tax-undecided', storeName, kind));
396
451
  }
397
- const charge = CommerceModel.quoteCheckoutCharge(order, quoteId);
398
- const feeCents = resolveTransactionFeeCents(owner == null ? void 0 : owner.org, charge.feeProductType, charge.goodsCents, charge.totalCents);
452
+ const charge = CommerceModel.invoiceCheckoutCharge(order, quoteId);
453
+ // The fee is sized on the most the buyer can pay, as a draft's first link
454
+ // sized it on its dearest rate.
455
+ const dearestRateCents = charge.shippingOptions.reduce((most, rate)=>Math.max(most, rate.amountCents), 0);
456
+ const feeCents = resolveTransactionFeeCents(owner == null ? void 0 : owner.org, charge.feeProductType, charge.goodsCents, charge.totalCents + dearestRateCents);
399
457
  const nativeMode = await deps.nativeMode(String((_ref = owner == null ? void 0 : owner.id) != null ? _ref : ''));
400
458
  const site = host;
401
459
  const origin = siteReturnOrigin({
@@ -407,8 +465,11 @@ const refusal = (status, reason, error, extra = {})=>({
407
465
  requestHost: headerOf(req, 'host')
408
466
  });
409
467
  const returnUrl = `${origin}${quotePageHref(quoteId, token)}`;
468
+ // A draft's first session (the one its paymentLinkUrl opens) is closed
469
+ // here too, so the draft has one way to pay from the first press on.
410
470
  const openedFor = (_order_checkoutSessionId = order.checkoutSessionId) != null ? _order_checkoutSessionId : '';
411
471
  const openedAtEdit = Number((_order_quote1 = order.quote) == null ? void 0 : _order_quote1.updatedAtMs) || 0;
472
+ const openedAtVersion = CommerceModel.orderPayLinkVersion(order);
412
473
  const claimed = await claimAttempt(deps.firestore(), {
413
474
  kind: 'commerce-quote-pay',
414
475
  scopeId: `${hostId}:${quoteId}`,
@@ -438,7 +499,7 @@ const refusal = (status, reason, error, extra = {})=>({
438
499
  const previous = await deps.expireSession(openedFor);
439
500
  if (previous === 'complete') {
440
501
  await claim.release();
441
- return refusal(409, 'paid', 'This quote has just been paid. Your receipt is on its way.', {
502
+ return refusal(409, 'paid', `This ${noun} has just been paid. Your receipt is on its way.`, {
442
503
  confirming: true
443
504
  });
444
505
  }
@@ -519,29 +580,30 @@ const refusal = (status, reason, error, extra = {})=>({
519
580
  var _current_quote;
520
581
  const snapshot = await transaction.get(orderRef);
521
582
  const current = snapshot.exists ? snapshot.data() : null;
522
- if (!current || !CommerceModel.isQuote(current)) return {
523
- outcome: NOT_FOUND,
583
+ if (!current || CommerceModel.orderPayLinkKind(current) !== kind) return {
584
+ outcome: NOT_FOUND_ANY,
524
585
  displaced: ''
525
586
  };
526
- if (!CommerceModel.isUnpaidQuote(current)) {
587
+ if (current.status !== 'pending') {
588
+ const closed = current.status === 'cancelled' || current.status === 'refunded';
527
589
  return {
528
- outcome: refusal(409, 'paid', CommerceModel.quotePayRefusalMessage('paid', storeName)),
590
+ outcome: refusal(409, closed ? 'closed' : 'paid', CommerceModel.invoicePayRefusalMessage(closed ? 'closed' : 'paid', storeName, kind)),
529
591
  displaced: ''
530
592
  };
531
593
  }
532
- if (CommerceModel.quoteLinkVersion(current.quote) !== CommerceModel.quoteLinkVersion(order.quote)) {
594
+ if (CommerceModel.orderPayLinkVersion(current) !== openedAtVersion) {
533
595
  return {
534
596
  outcome: {
535
597
  status: 410,
536
598
  body: {
537
599
  replaced: true,
538
- error: `This quote was updated, and ${storeName || 'the business'} sent you a newer link.`
600
+ error: `This ${noun} was updated, and ${storeName || 'the business'} sent you a newer link.`
539
601
  }
540
602
  },
541
603
  displaced: ''
542
604
  };
543
605
  }
544
- if ((Number((_current_quote = current.quote) == null ? void 0 : _current_quote.updatedAtMs) || 0) !== openedAtEdit) {
606
+ if (kind === 'quote' && (Number((_current_quote = current.quote) == null ? void 0 : _current_quote.updatedAtMs) || 0) !== openedAtEdit) {
545
607
  return {
546
608
  outcome: refusal(409, 'updated', QUOTE_UPDATED_MESSAGE),
547
609
  displaced: ''
@@ -581,7 +643,7 @@ const refusal = (status, reason, error, extra = {})=>({
581
643
  };
582
644
  }
583
645
  }
584
- function send(res, outcome) {
646
+ /** Sends a link route's answer, never cached or indexed. */ export function sendQuoteLinkOutcome(res, outcome) {
585
647
  var _outcome_headers;
586
648
  res.setHeader('Cache-Control', 'private, no-store');
587
649
  res.setHeader('X-Robots-Tag', 'noindex, nofollow');
@@ -590,10 +652,10 @@ function send(res, outcome) {
590
652
  }
591
653
  /** `GET /api/commerce/quote-view`. */ export const quoteViewHandler = async (req, res)=>{
592
654
  try {
593
- return send(res, await handleQuoteView(defaultQuotePayDeps(), req));
655
+ return sendQuoteLinkOutcome(res, await handleQuoteView(defaultQuotePayDeps(), req));
594
656
  } catch (error) {
595
657
  console.error('quote view failed', error);
596
- return send(res, {
658
+ return sendQuoteLinkOutcome(res, {
597
659
  status: 500,
598
660
  body: {
599
661
  error: 'This quote could not be loaded. Try again.'
@@ -601,6 +663,6 @@ function send(res, outcome) {
601
663
  });
602
664
  }
603
665
  };
604
- /** `POST /api/commerce/quote-pay`. */ export const quotePayHandler = async (req, res)=>send(res, await handleQuotePay(defaultQuotePayDeps(), req));
666
+ /** `POST /api/commerce/quote-pay`. */ export const quotePayHandler = async (req, res)=>sendQuoteLinkOutcome(res, await handleQuotePay(defaultQuotePayDeps(), req));
605
667
 
606
668
  //# sourceMappingURL=quote-pay.js.map