@aglyn/plugins-commerce 1.30.0 → 1.32.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.
Files changed (132) hide show
  1. package/README.md +4 -0
  2. package/package.json +13 -13
  3. package/src/lib/components/cart.js +46 -20
  4. package/src/lib/components/cart.js.map +1 -1
  5. package/src/lib/components/commerce-console-page.js +6 -0
  6. package/src/lib/components/commerce-console-page.js.map +1 -1
  7. package/src/lib/components/console/currencies-card.component.d.ts +21 -0
  8. package/src/lib/components/console/currencies-card.component.js +408 -0
  9. package/src/lib/components/console/currencies-card.component.js.map +1 -0
  10. package/src/lib/components/console/product-add-ons-fields.component.d.ts +13 -0
  11. package/src/lib/components/console/product-add-ons-fields.component.js +74 -2
  12. package/src/lib/components/console/product-add-ons-fields.component.js.map +1 -1
  13. package/src/lib/components/console/product-editor-dialog.component.js +47 -1
  14. package/src/lib/components/console/product-editor-dialog.component.js.map +1 -1
  15. package/src/lib/components/console/product-fixed-prices-fields.component.d.ts +25 -0
  16. package/src/lib/components/console/product-fixed-prices-fields.component.js +112 -0
  17. package/src/lib/components/console/product-fixed-prices-fields.component.js.map +1 -0
  18. package/src/lib/components/console/product-traffic-card.component.d.ts +24 -0
  19. package/src/lib/components/console/product-traffic-card.component.js +172 -0
  20. package/src/lib/components/console/product-traffic-card.component.js.map +1 -0
  21. package/src/lib/components/console/products-hub-card.component.js +181 -59
  22. package/src/lib/components/console/products-hub-card.component.js.map +1 -1
  23. package/src/lib/components/console/reservations-card.component.js +32 -15
  24. package/src/lib/components/console/reservations-card.component.js.map +1 -1
  25. package/src/lib/components/console/shipping-settings-card.component.js +131 -10
  26. package/src/lib/components/console/shipping-settings-card.component.js.map +1 -1
  27. package/src/lib/components/console/store-settings-card.component.js +1 -1
  28. package/src/lib/components/console/store-settings-card.component.js.map +1 -1
  29. package/src/lib/components/console/use-currency-setup.d.ts +20 -0
  30. package/src/lib/components/console/use-currency-setup.js +61 -0
  31. package/src/lib/components/console/use-currency-setup.js.map +1 -0
  32. package/src/lib/components/currency-selector.d.ts +36 -0
  33. package/src/lib/components/currency-selector.js +157 -0
  34. package/src/lib/components/currency-selector.js.map +1 -0
  35. package/src/lib/components/product-add-ons.d.ts +9 -1
  36. package/src/lib/components/product-add-ons.js +8 -4
  37. package/src/lib/components/product-add-ons.js.map +1 -1
  38. package/src/lib/components/product-choice-cards.d.ts +13 -1
  39. package/src/lib/components/product-choice-cards.js +5 -4
  40. package/src/lib/components/product-choice-cards.js.map +1 -1
  41. package/src/lib/components/product-detail.d.ts +8 -0
  42. package/src/lib/components/product-detail.js +38 -11
  43. package/src/lib/components/product-detail.js.map +1 -1
  44. package/src/lib/components/product-grid.d.ts +2 -0
  45. package/src/lib/components/product-grid.js +22 -9
  46. package/src/lib/components/product-grid.js.map +1 -1
  47. package/src/lib/components/related-products.js +6 -2
  48. package/src/lib/components/related-products.js.map +1 -1
  49. package/src/lib/components/shopper-currency.d.ts +108 -0
  50. package/src/lib/components/shopper-currency.js +262 -0
  51. package/src/lib/components/shopper-currency.js.map +1 -0
  52. package/src/lib/components/wishlist.js +7 -1
  53. package/src/lib/components/wishlist.js.map +1 -1
  54. package/src/lib/declarations.server.js +39 -0
  55. package/src/lib/declarations.server.js.map +1 -1
  56. package/src/lib/model/commerce-currency-setup.d.ts +79 -0
  57. package/src/lib/model/commerce-currency-setup.js +124 -0
  58. package/src/lib/model/commerce-currency-setup.js.map +1 -0
  59. package/src/lib/model/commerce-currency.d.ts +215 -0
  60. package/src/lib/model/commerce-currency.js +511 -0
  61. package/src/lib/model/commerce-currency.js.map +1 -0
  62. package/src/lib/model/commerce-orders.d.ts +17 -0
  63. package/src/lib/model/commerce-orders.js +10 -5
  64. package/src/lib/model/commerce-orders.js.map +1 -1
  65. package/src/lib/model/commerce-record-routes.js +56 -0
  66. package/src/lib/model/commerce-record-routes.js.map +1 -1
  67. package/src/lib/model/commerce-reservations.d.ts +6 -0
  68. package/src/lib/model/commerce-reservations.js.map +1 -1
  69. package/src/lib/model/commerce-shipping.d.ts +61 -17
  70. package/src/lib/model/commerce-shipping.js +346 -32
  71. package/src/lib/model/commerce-shipping.js.map +1 -1
  72. package/src/lib/model/commerce.d.ts +6 -0
  73. package/src/lib/model/commerce.js.map +1 -1
  74. package/src/lib/model/index.d.ts +2 -0
  75. package/src/lib/model/index.js +2 -0
  76. package/src/lib/model/index.js.map +1 -1
  77. package/src/lib/model/order-figures.js +9 -2
  78. package/src/lib/model/order-figures.js.map +1 -1
  79. package/src/lib/model/product-modifiers.d.ts +5 -0
  80. package/src/lib/model/product-modifiers.js.map +1 -1
  81. package/src/lib/model/product-write.d.ts +10 -1
  82. package/src/lib/model/product-write.js +13 -2
  83. package/src/lib/model/product-write.js.map +1 -1
  84. package/src/lib/server/billing-webhook.js +37 -24
  85. package/src/lib/server/billing-webhook.js.map +1 -1
  86. package/src/lib/server/carrier-shipping.js +3 -2
  87. package/src/lib/server/carrier-shipping.js.map +1 -1
  88. package/src/lib/server/cart-checkout.js +77 -15
  89. package/src/lib/server/cart-checkout.js.map +1 -1
  90. package/src/lib/server/cart.d.ts +12 -0
  91. package/src/lib/server/cart.js +52 -6
  92. package/src/lib/server/cart.js.map +1 -1
  93. package/src/lib/server/catalog.d.ts +5 -0
  94. package/src/lib/server/catalog.js +7 -0
  95. package/src/lib/server/catalog.js.map +1 -1
  96. package/src/lib/server/checkout.js +54 -16
  97. package/src/lib/server/checkout.js.map +1 -1
  98. package/src/lib/server/currency.d.ts +93 -0
  99. package/src/lib/server/currency.js +182 -0
  100. package/src/lib/server/currency.js.map +1 -0
  101. package/src/lib/server/draft-order.js +3 -3
  102. package/src/lib/server/draft-order.js.map +1 -1
  103. package/src/lib/server/order-notifications.js +4 -2
  104. package/src/lib/server/order-notifications.js.map +1 -1
  105. package/src/lib/server/product.d.ts +9 -0
  106. package/src/lib/server/product.js +12 -0
  107. package/src/lib/server/product.js.map +1 -1
  108. package/src/lib/server/products-write.js +10 -2
  109. package/src/lib/server/products-write.js.map +1 -1
  110. package/src/lib/server/refund.js +3 -3
  111. package/src/lib/server/refund.js.map +1 -1
  112. package/src/lib/server/site-chat-tools.d.ts +22 -0
  113. package/src/lib/server/site-chat-tools.js +369 -0
  114. package/src/lib/server/site-chat-tools.js.map +1 -0
  115. package/src/lib/server/site-chat-visitor.d.ts +24 -0
  116. package/src/lib/server/site-chat-visitor.js +39 -0
  117. package/src/lib/server/site-chat-visitor.js.map +1 -0
  118. package/src/lib/server/site-offers.d.ts +31 -0
  119. package/src/lib/server/site-offers.js +91 -0
  120. package/src/lib/server/site-offers.js.map +1 -0
  121. package/src/lib/server/stays-site-search.d.ts +20 -0
  122. package/src/lib/server/stays-site-search.js +78 -0
  123. package/src/lib/server/stays-site-search.js.map +1 -0
  124. package/src/lib/server.js +20 -0
  125. package/src/lib/server.js.map +1 -1
  126. package/src/lib/site-chat-tool-definitions.d.ts +26 -0
  127. package/src/lib/site-chat-tool-definitions.js +141 -0
  128. package/src/lib/site-chat-tool-definitions.js.map +1 -0
  129. package/src/lib/site.js +8 -0
  130. package/src/lib/site.js.map +1 -1
  131. package/src/lib/subprocessors.js +7 -0
  132. package/src/lib/subprocessors.js.map +1 -1
@@ -0,0 +1,511 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Aglyn LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */ /**
17
+ * Multi-currency selling (AGL-4248), the way Shopify Markets does it: a
18
+ * shopper sees, and is CHARGED in, their own currency (Stripe presentment
19
+ * currency on the merchant's account, settled in the store currency), picked
20
+ * from where they browse out of the currencies the merchant turned on, and can
21
+ * switch it themselves.
22
+ *
23
+ * Prices convert from the store currency at a daily reference rate, plus an
24
+ * optional buffer, then round the way the merchant chose (Shopify's
25
+ * "rounding" rule rounds UP to the chosen ending). A product or add-on may
26
+ * carry a fixed price in a currency instead, which wins over conversion.
27
+ *
28
+ * Pure. Settings live on `hosts/{hostId}/settings/store` under `currencies`;
29
+ * rates live on `platform/fxRates`, written by the commerce rates beat.
30
+ */ /** How a converted price is rounded, always UP to the next allowed value. */ import { _ as _extends } from "@swc/helpers/_/_extends";
31
+ export const CURRENCY_ROUNDING_LABELS = {
32
+ none: 'To the cent (no rounding)',
33
+ whole: 'Whole number (29.00)',
34
+ '99': 'Ends in .99 (28.99)',
35
+ '95': 'Ends in .95 (28.95)'
36
+ };
37
+ /**
38
+ * The currencies a store may sell in: the ones the European Central Bank
39
+ * publishes a daily reference rate for, that Stripe charges cards in, and
40
+ * that have two decimal places (every receipt and report formats cents).
41
+ */ export const SELLABLE_CURRENCIES = [
42
+ 'USD',
43
+ 'EUR',
44
+ 'GBP',
45
+ 'AUD',
46
+ 'NZD',
47
+ 'CAD',
48
+ 'CHF',
49
+ 'SEK',
50
+ 'NOK',
51
+ 'DKK',
52
+ 'PLN',
53
+ 'CZK',
54
+ 'MXN',
55
+ 'SGD',
56
+ 'HKD',
57
+ 'ZAR',
58
+ 'ILS',
59
+ 'BRL',
60
+ 'INR',
61
+ 'PHP',
62
+ 'THB',
63
+ 'MYR',
64
+ 'IDR',
65
+ 'RON',
66
+ 'TRY',
67
+ 'CNY'
68
+ ];
69
+ export const CURRENCY_NAMES = {
70
+ USD: 'US dollar',
71
+ EUR: 'Euro',
72
+ GBP: 'British pound',
73
+ AUD: 'Australian dollar',
74
+ NZD: 'New Zealand dollar',
75
+ CAD: 'Canadian dollar',
76
+ JPY: 'Japanese yen',
77
+ CHF: 'Swiss franc',
78
+ SEK: 'Swedish krona',
79
+ NOK: 'Norwegian krone',
80
+ DKK: 'Danish krone',
81
+ PLN: 'Polish złoty',
82
+ CZK: 'Czech koruna',
83
+ MXN: 'Mexican peso',
84
+ SGD: 'Singapore dollar',
85
+ HKD: 'Hong Kong dollar',
86
+ ZAR: 'South African rand',
87
+ ILS: 'Israeli new shekel',
88
+ BRL: 'Brazilian real',
89
+ INR: 'Indian rupee',
90
+ PHP: 'Philippine peso',
91
+ THB: 'Thai baht',
92
+ MYR: 'Malaysian ringgit',
93
+ IDR: 'Indonesian rupiah',
94
+ KRW: 'South Korean won',
95
+ RON: 'Romanian leu',
96
+ TRY: 'Turkish lira',
97
+ CNY: 'Chinese yuan'
98
+ };
99
+ /** Currencies Stripe charges with no minor unit. */ const ZERO_DECIMAL = new Set([
100
+ 'JPY',
101
+ 'KRW'
102
+ ]);
103
+ /** Minor units per major unit: 100 for most, 1 for yen and won. */ export function currencyMinorFactor(code) {
104
+ return ZERO_DECIMAL.has(code.toUpperCase()) ? 1 : 100;
105
+ }
106
+ const EURO_COUNTRIES = [
107
+ 'AT',
108
+ 'BE',
109
+ 'BG',
110
+ 'HR',
111
+ 'CY',
112
+ 'EE',
113
+ 'FI',
114
+ 'FR',
115
+ 'DE',
116
+ 'GR',
117
+ 'IE',
118
+ 'IT',
119
+ 'LV',
120
+ 'LT',
121
+ 'LU',
122
+ 'MT',
123
+ 'NL',
124
+ 'PT',
125
+ 'SK',
126
+ 'SI',
127
+ 'ES',
128
+ 'AD',
129
+ 'MC',
130
+ 'SM',
131
+ 'VA',
132
+ 'ME',
133
+ 'XK',
134
+ 'AX',
135
+ 'GF',
136
+ 'GP',
137
+ 'MQ',
138
+ 'YT',
139
+ 'RE',
140
+ 'BL',
141
+ 'MF',
142
+ 'PM'
143
+ ];
144
+ /**
145
+ * The currency a country's shoppers expect (ISO 4217), for the currencies a
146
+ * store can sell in. A country not listed shops in the store currency.
147
+ */ export const COUNTRY_CURRENCY = _extends({}, Object.fromEntries(EURO_COUNTRIES.map((code)=>[
148
+ code,
149
+ 'EUR'
150
+ ])), {
151
+ US: 'USD',
152
+ PR: 'USD',
153
+ GU: 'USD',
154
+ AS: 'USD',
155
+ VI: 'USD',
156
+ MP: 'USD',
157
+ EC: 'USD',
158
+ SV: 'USD',
159
+ PA: 'USD',
160
+ TL: 'USD',
161
+ FM: 'USD',
162
+ MH: 'USD',
163
+ PW: 'USD',
164
+ TC: 'USD',
165
+ VG: 'USD',
166
+ BQ: 'USD',
167
+ GB: 'GBP',
168
+ IM: 'GBP',
169
+ JE: 'GBP',
170
+ GG: 'GBP',
171
+ AU: 'AUD',
172
+ CX: 'AUD',
173
+ CC: 'AUD',
174
+ NF: 'AUD',
175
+ KI: 'AUD',
176
+ NR: 'AUD',
177
+ TV: 'AUD',
178
+ NZ: 'NZD',
179
+ CK: 'NZD',
180
+ NU: 'NZD',
181
+ PN: 'NZD',
182
+ TK: 'NZD',
183
+ CA: 'CAD',
184
+ JP: 'JPY',
185
+ CH: 'CHF',
186
+ LI: 'CHF',
187
+ SE: 'SEK',
188
+ NO: 'NOK',
189
+ SJ: 'NOK',
190
+ BV: 'NOK',
191
+ DK: 'DKK',
192
+ FO: 'DKK',
193
+ GL: 'DKK',
194
+ PL: 'PLN',
195
+ CZ: 'CZK',
196
+ MX: 'MXN',
197
+ SG: 'SGD',
198
+ HK: 'HKD',
199
+ ZA: 'ZAR',
200
+ IL: 'ILS',
201
+ PS: 'ILS',
202
+ BR: 'BRL',
203
+ IN: 'INR',
204
+ PH: 'PHP',
205
+ TH: 'THB',
206
+ MY: 'MYR',
207
+ ID: 'IDR',
208
+ KR: 'KRW',
209
+ RO: 'RON',
210
+ TR: 'TRY',
211
+ CN: 'CNY'
212
+ });
213
+ const upper = (value)=>typeof value === 'string' ? value.trim().toUpperCase() : '';
214
+ /** The store's own currency, upper case (`settings/store.currency`, default USD). */ export function storeBaseCurrency(store) {
215
+ const code = upper(store == null ? void 0 : store.currency);
216
+ return /^[A-Z]{3}$/.test(code) ? code : 'USD';
217
+ }
218
+ /**
219
+ * The currencies this store sells in, the store currency first, each known
220
+ * and listed once. Off (or nothing added) is the store currency alone.
221
+ */ export function storeCurrencies(settings, base) {
222
+ var _settings_currencies;
223
+ const list = [
224
+ {
225
+ code: base,
226
+ rounding: 'none'
227
+ }
228
+ ];
229
+ if (!(settings == null ? void 0 : settings.enabled)) return list;
230
+ for (const entry of (_settings_currencies = settings.currencies) != null ? _settings_currencies : []){
231
+ const code = upper(entry == null ? void 0 : entry.code);
232
+ if (!SELLABLE_CURRENCIES.includes(code) || list.some((c)=>c.code === code)) continue;
233
+ list.push(_extends({
234
+ code,
235
+ rounding: [
236
+ 'none',
237
+ 'whole',
238
+ '99',
239
+ '95'
240
+ ].includes(entry.rounding) ? entry.rounding : 'none',
241
+ bufferPct: clampBuffer(entry.bufferPct)
242
+ }, Number(entry.manualRate) > 0 ? {
243
+ manualRate: Number(entry.manualRate)
244
+ } : {}, entry.pricesIncludeTax ? {
245
+ pricesIncludeTax: true
246
+ } : {}));
247
+ }
248
+ return list;
249
+ }
250
+ function clampBuffer(value) {
251
+ const n = Number(value);
252
+ if (!Number.isFinite(n) || n === 0) return 0;
253
+ return Math.max(-90, Math.min(100, Math.round(n * 100) / 100));
254
+ }
255
+ /**
256
+ * The rate a store converts into `target` at: its manual rate when set,
257
+ * else the daily cross rate. `undefined` when neither covers it.
258
+ */ export function currencyRate(rates, base, target) {
259
+ if (upper(base) === target.code) return 1;
260
+ if (target.manualRate && target.manualRate > 0) return target.manualRate;
261
+ return crossRate(rates, base, target.code);
262
+ }
263
+ /** Whether `code` is a currency this store sells in. */ export function storeSellsIn(settings, base, code) {
264
+ const want = upper(code);
265
+ return storeCurrencies(settings, base).find((entry)=>entry.code === want);
266
+ }
267
+ /**
268
+ * The currency a shopper sees: their own choice when the store sells in it,
269
+ * else the one their country uses when the store sells in that (and
270
+ * auto-detect is on), else the store currency.
271
+ */ export function resolveShopperCurrency(input) {
272
+ const { settings, base } = input;
273
+ const chosen = storeSellsIn(settings, base, input.chosen);
274
+ if (chosen) return chosen.code;
275
+ if ((settings == null ? void 0 : settings.autoDetect) !== false) {
276
+ const local = COUNTRY_CURRENCY[upper(input.country)];
277
+ if (local && storeSellsIn(settings, base, local)) return local;
278
+ }
279
+ return base;
280
+ }
281
+ /** `rates[to] / rates[from]` through the rates' own base; undefined when unknown. */ export function crossRate(rates, from, to) {
282
+ const f = upper(from);
283
+ const t = upper(to);
284
+ if (f === t) return 1;
285
+ if (!(rates == null ? void 0 : rates.rates)) return undefined;
286
+ const rate = (code)=>code === upper(rates.base) ? 1 : rates.rates[code];
287
+ const a = rate(f);
288
+ const b = rate(t);
289
+ if (!(a > 0) || !(b > 0)) return undefined;
290
+ return b / a;
291
+ }
292
+ /** Round a major amount UP to the ending the merchant chose. */ export function roundMajor(major, rounding, code) {
293
+ const factor = currencyMinorFactor(code);
294
+ // Shave float noise (28.950000000001) before rounding up.
295
+ const clean = Math.round(major * factor * 1e6) / 1e6 / factor;
296
+ switch(rounding){
297
+ case 'whole':
298
+ return Math.ceil(clean - 1e-9);
299
+ case '99':
300
+ case '95':
301
+ {
302
+ if (factor === 1) return Math.ceil(clean - 1e-9);
303
+ const ending = rounding === '99' ? 0.99 : 0.95;
304
+ const floor = Math.floor(clean);
305
+ const candidate = floor + ending;
306
+ return clean <= candidate + 1e-9 ? candidate : floor + 1 + ending;
307
+ }
308
+ case 'none':
309
+ default:
310
+ return Math.ceil(clean * factor - 1e-9) / factor;
311
+ }
312
+ }
313
+ /**
314
+ * A store-currency amount (minor units) in a shopper currency (minor units):
315
+ * converted at the reference rate, plus the buffer, rounded as set.
316
+ * `undefined` when the rates do not cover the pair, and callers then sell in
317
+ * the store currency rather than guessing.
318
+ */ export function convertMinor(amountMinor, from, target, rates, options = {}) {
319
+ var _target_bufferPct, _target_rounding;
320
+ const rate = currencyRate(rates, from, target);
321
+ if (rate == null) return undefined;
322
+ if (upper(from) === target.code) return Math.round(amountMinor);
323
+ if (!amountMinor) return 0;
324
+ const major = amountMinor / currencyMinorFactor(from) * rate * (1 + ((_target_bufferPct = target.bufferPct) != null ? _target_bufferPct : 0) / 100);
325
+ const rounded = options.round === false ? roundMajor(major, 'none', target.code) : roundMajor(major, (_target_rounding = target.rounding) != null ? _target_rounding : 'none', target.code);
326
+ return Math.round(rounded * currencyMinorFactor(target.code));
327
+ }
328
+ /**
329
+ * The price a shopper pays for a variant, in minor units of their currency:
330
+ * the variant's fixed price there when it has one, else its store price
331
+ * converted. `priceMajor` is the store price in major units (`priceUsd`).
332
+ */ export function variantPriceMinor(variant, base, target, rates) {
333
+ var _variant_priceUsd;
334
+ var _variant_fixedPrices;
335
+ const fixed = (_variant_fixedPrices = variant.fixedPrices) == null ? void 0 : _variant_fixedPrices[target.code];
336
+ if (target.code !== base && typeof fixed === 'number' && fixed >= 0) {
337
+ return Math.round(fixed * currencyMinorFactor(target.code));
338
+ }
339
+ const baseMinor = Math.round(Number((_variant_priceUsd = variant.priceUsd) != null ? _variant_priceUsd : 0) * currencyMinorFactor(base));
340
+ return convertMinor(baseMinor, base, target, rates);
341
+ }
342
+ /**
343
+ * An add-on or fee amount in the shopper's currency: a fixed amount there
344
+ * when the merchant set one, else converted at the rate WITHOUT the ending
345
+ * rounding (a $5 add-on is NZ$8.61, not NZ$8.99: rounding is for prices a
346
+ * shopper reads on their own).
347
+ */ export function amountMinorIn(baseMinor, base, target, rates, fixedMinor) {
348
+ const fixed = fixedMinor == null ? void 0 : fixedMinor[target.code];
349
+ if (target.code !== base && typeof fixed === 'number' && fixed >= 0) return Math.round(fixed);
350
+ return convertMinor(baseMinor, base, target, rates, {
351
+ round: false
352
+ });
353
+ }
354
+ /**
355
+ * Money for a shopper. Where two currencies a store sells in share a sign
356
+ * ($ for US, Australian and New Zealand dollars), each says which it is:
357
+ * US$29.00, A$45.00, NZ$49.00.
358
+ */ export function formatShopperMoney(amountMinor, code, options = {}) {
359
+ const currency = upper(code) || 'USD';
360
+ const factor = currencyMinorFactor(currency);
361
+ const value = (Number(amountMinor) || 0) / factor;
362
+ let text;
363
+ try {
364
+ var _options_locale;
365
+ text = new Intl.NumberFormat((_options_locale = options.locale) != null ? _options_locale : 'en-US', {
366
+ style: 'currency',
367
+ currency,
368
+ minimumFractionDigits: factor === 1 ? 0 : 2,
369
+ maximumFractionDigits: factor === 1 ? 0 : 2
370
+ }).format(value);
371
+ } catch (unused) {
372
+ return `${currency} ${value.toFixed(factor === 1 ? 0 : 2)}`;
373
+ }
374
+ if (options.disambiguate && currency === 'USD') text = text.replace(/^(-?)\$/, '$1US$');
375
+ return text;
376
+ }
377
+ /** Whether a store's currencies include two that share a sign. */ export function currenciesShareSign(codes) {
378
+ const dollars = codes.filter((code)=>[
379
+ 'USD',
380
+ 'AUD',
381
+ 'NZD',
382
+ 'CAD',
383
+ 'SGD',
384
+ 'HKD',
385
+ 'MXN'
386
+ ].includes(code));
387
+ return dollars.length > 1;
388
+ }
389
+ /**
390
+ * Money in a timeline line or notice for a sale in `currency` (lower or
391
+ * upper case). A USD sale keeps the exact text it always had (`$1234.50`);
392
+ * any other currency names itself (`NZ$449.00`).
393
+ */ export function saleMoneyText(cents, currency) {
394
+ const code = upper(currency) || 'USD';
395
+ if (code === 'USD') return `$${((Number(cents) || 0) / 100).toFixed(2)}`;
396
+ return formatShopperMoney(cents, code);
397
+ }
398
+ /** The metadata keys a checkout writes the charged line units under (AGL-4248). */ export const CHARGE_UNITS_METADATA_PREFIX = 'chargeUnits';
399
+ /** Line units as metadata entries, each under Stripe's 500-character value cap. */ export function encodeChargeUnitsMetadata(units) {
400
+ const out = {};
401
+ let chunk = [];
402
+ let index = 0;
403
+ const flush = ()=>{
404
+ if (chunk.length) out[`${CHARGE_UNITS_METADATA_PREFIX}${index++}`] = chunk.join(',');
405
+ chunk = [];
406
+ };
407
+ for (const unit of units){
408
+ const text = String(Math.max(0, Math.round(unit)));
409
+ if ([
410
+ ...chunk,
411
+ text
412
+ ].join(',').length > 480) flush();
413
+ chunk.push(text);
414
+ }
415
+ flush();
416
+ return out;
417
+ }
418
+ /** The line units back from session metadata, or undefined when absent. */ export function decodeChargeUnitsMetadata(metadata) {
419
+ if (!metadata) return undefined;
420
+ const units = [];
421
+ for(let index = 0; typeof metadata[`${CHARGE_UNITS_METADATA_PREFIX}${index}`] === 'string'; index++){
422
+ for (const part of String(metadata[`${CHARGE_UNITS_METADATA_PREFIX}${index}`]).split(',')){
423
+ const n = Number(part);
424
+ units.push(Number.isFinite(n) ? n : 0);
425
+ }
426
+ }
427
+ return units.length ? units : undefined;
428
+ }
429
+ /**
430
+ * The order's currency fields (AGL-4248) from a paid session: the charge
431
+ * currency Stripe reports and, when it is not the store's, the store
432
+ * currency, the rate and the totals there.
433
+ */ export function orderCurrencyFields(session, totals) {
434
+ var _ref;
435
+ const currency = typeof (session == null ? void 0 : session.currency) === 'string' ? session.currency.toLowerCase() : undefined;
436
+ const meta = (_ref = session == null ? void 0 : session.metadata) != null ? _ref : {};
437
+ const base = typeof meta['baseCurrency'] === 'string' ? String(meta['baseCurrency']).toLowerCase() : undefined;
438
+ const rate = Number(meta['fxRate']);
439
+ if (!currency) return {};
440
+ if (!base || base === currency || !(rate > 0)) return {
441
+ currency
442
+ };
443
+ const factor = currencyMinorFactor(base) / currencyMinorFactor(currency);
444
+ const baseTotals = Object.fromEntries(Object.entries(totals).map(([key, value])=>[
445
+ key,
446
+ typeof value === 'number' ? Math.round(value / rate * factor) : value
447
+ ]));
448
+ return {
449
+ currency,
450
+ baseCurrency: base,
451
+ fxRate: rate,
452
+ baseTotals
453
+ };
454
+ }
455
+ /** Rates older than this are not used to charge anyone: the store currency is. */ export const FX_RATES_MAX_AGE_MS = 4 * 24 * 60 * 60 * 1000;
456
+ export function ratesAreFresh(rates, nowMs) {
457
+ return Boolean((rates == null ? void 0 : rates.asOfMs) && nowMs - rates.asOfMs <= FX_RATES_MAX_AGE_MS);
458
+ }
459
+ /** The ECB's daily reference rates (free, no key): per 1 EUR, about 16:00 CET. */ export const ECB_DAILY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
460
+ /**
461
+ * Parse the European Central Bank's daily reference XML
462
+ * (`eurofxref-daily.xml`): `<Cube time="2026-11-25">` holding
463
+ * `<Cube currency="USD" rate="1.0521"/>` rows, per 1 EUR.
464
+ */ export function parseEcbDailyXml(xml) {
465
+ var _exec;
466
+ const time = (_exec = /<Cube\s+time=['"](\d{4}-\d{2}-\d{2})['"]/.exec(xml)) == null ? void 0 : _exec[1];
467
+ const rates = {};
468
+ for (const match of xml.matchAll(/<Cube\s+currency=['"]([A-Z]{3})['"]\s+rate=['"]([\d.]+)['"]/g)){
469
+ const value = Number(match[2]);
470
+ if (value > 0) rates[match[1]] = value;
471
+ }
472
+ if (!time || !Object.keys(rates).length) return undefined;
473
+ return {
474
+ base: 'EUR',
475
+ rates,
476
+ asOfMs: Date.parse(`${time}T16:00:00Z`),
477
+ source: 'ecb'
478
+ };
479
+ }
480
+ /**
481
+ * The public answer, from a store's settings and the stored rates. A
482
+ * currency the rates do not cover, or rates older than
483
+ * {@link FX_RATES_MAX_AGE_MS}, drop out: checkout would refuse to charge in
484
+ * them, so the storefront must not show them.
485
+ */ export function publicCurrencyAnswer(context, nowMs) {
486
+ var _context_settings;
487
+ const fresh = ratesAreFresh(context.rates, nowMs);
488
+ const currencies = storeCurrencies(context.settings, context.base).filter((entry)=>entry.code === context.base || Boolean(entry.manualRate) || fresh && crossRate(context.rates, context.base, entry.code) != null);
489
+ const rates = fresh && context.rates && currencies.length > 1 ? {
490
+ base: context.rates.base,
491
+ asOfMs: context.rates.asOfMs,
492
+ rates: Object.fromEntries(currencies.map((entry)=>entry.code).concat(context.base).filter((code)=>{
493
+ var _context_rates, _context_rates1;
494
+ return code !== ((_context_rates = context.rates) == null ? void 0 : _context_rates.base) && ((_context_rates1 = context.rates) == null ? void 0 : _context_rates1.rates[code]);
495
+ }).map((code)=>{
496
+ var _context_rates;
497
+ return [
498
+ code,
499
+ (_context_rates = context.rates) == null ? void 0 : _context_rates.rates[code]
500
+ ];
501
+ }))
502
+ } : null;
503
+ return {
504
+ base: context.base,
505
+ currencies,
506
+ autoDetect: ((_context_settings = context.settings) == null ? void 0 : _context_settings.autoDetect) !== false,
507
+ rates
508
+ };
509
+ }
510
+
511
+ //# sourceMappingURL=commerce-currency.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/model/commerce-currency.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 * Multi-currency selling (AGL-4248), the way Shopify Markets does it: a\n * shopper sees, and is CHARGED in, their own currency (Stripe presentment\n * currency on the merchant's account, settled in the store currency), picked\n * from where they browse out of the currencies the merchant turned on, and can\n * switch it themselves.\n *\n * Prices convert from the store currency at a daily reference rate, plus an\n * optional buffer, then round the way the merchant chose (Shopify's\n * \"rounding\" rule rounds UP to the chosen ending). A product or add-on may\n * carry a fixed price in a currency instead, which wins over conversion.\n *\n * Pure. Settings live on `hosts/{hostId}/settings/store` under `currencies`;\n * rates live on `platform/fxRates`, written by the commerce rates beat.\n */\n\n/** How a converted price is rounded, always UP to the next allowed value. */\nexport type CurrencyRounding = 'none' | 'whole' | '99' | '95'\n\nexport const CURRENCY_ROUNDING_LABELS: Readonly<Record<CurrencyRounding, string>> = {\n none: 'To the cent (no rounding)',\n whole: 'Whole number (29.00)',\n '99': 'Ends in .99 (28.99)',\n '95': 'Ends in .95 (28.95)',\n}\n\n/** One currency a store sells in besides its own. */\nexport interface StoreCurrency {\n /** ISO 4217, upper case: 'NZD'. */\n code: string\n /**\n * Rounding of converted prices, OFF unless the merchant turns it on\n * (Shopify's rule): converted prices round UP to the ending chosen.\n */\n rounding?: CurrencyRounding\n /**\n * Price adjustment, in percent (-90 to +100), on every converted price in\n * this currency: Shopify's per-market adjustment. Positive covers Stripe's\n * conversion fee, which the merchant pays otherwise.\n */\n bufferPct?: number\n /**\n * A manual exchange rate: units of this currency per one unit of the store\n * currency. Set, it replaces the daily rate and never goes stale.\n */\n manualRate?: number\n /**\n * Prices shown and charged in this currency INCLUDE tax (VAT/GST), the way\n * shoppers in the UK, EU, Australia and New Zealand expect. Only meaningful\n * with Stripe Tax registrations in those places.\n */\n pricesIncludeTax?: boolean\n}\n\nexport interface StoreCurrencySettings {\n /** Selling in more than the store currency at all. */\n enabled?: boolean\n currencies?: StoreCurrency[]\n /** Pick the shopper's currency from their country (default on). */\n autoDetect?: boolean\n}\n\n/** The daily reference rates, per 1 unit of `base`. */\nexport interface FxRates {\n base: string\n rates: Record<string, number>\n asOfMs: number\n source?: string\n}\n\n/** Fixed prices a product variant carries per currency, in major units (29.95). */\nexport type FixedPrices = Record<string, number>\n\n/**\n * The currencies a store may sell in: the ones the European Central Bank\n * publishes a daily reference rate for, that Stripe charges cards in, and\n * that have two decimal places (every receipt and report formats cents).\n */\nexport const SELLABLE_CURRENCIES: readonly string[] = [\n 'USD',\n 'EUR',\n 'GBP',\n 'AUD',\n 'NZD',\n 'CAD',\n 'CHF',\n 'SEK',\n 'NOK',\n 'DKK',\n 'PLN',\n 'CZK',\n 'MXN',\n 'SGD',\n 'HKD',\n 'ZAR',\n 'ILS',\n 'BRL',\n 'INR',\n 'PHP',\n 'THB',\n 'MYR',\n 'IDR',\n 'RON',\n 'TRY',\n 'CNY',\n]\n\nexport const CURRENCY_NAMES: Readonly<Record<string, string>> = {\n USD: 'US dollar',\n EUR: 'Euro',\n GBP: 'British pound',\n AUD: 'Australian dollar',\n NZD: 'New Zealand dollar',\n CAD: 'Canadian dollar',\n JPY: 'Japanese yen',\n CHF: 'Swiss franc',\n SEK: 'Swedish krona',\n NOK: 'Norwegian krone',\n DKK: 'Danish krone',\n PLN: 'Polish złoty',\n CZK: 'Czech koruna',\n MXN: 'Mexican peso',\n SGD: 'Singapore dollar',\n HKD: 'Hong Kong dollar',\n ZAR: 'South African rand',\n ILS: 'Israeli new shekel',\n BRL: 'Brazilian real',\n INR: 'Indian rupee',\n PHP: 'Philippine peso',\n THB: 'Thai baht',\n MYR: 'Malaysian ringgit',\n IDR: 'Indonesian rupiah',\n KRW: 'South Korean won',\n RON: 'Romanian leu',\n TRY: 'Turkish lira',\n CNY: 'Chinese yuan',\n}\n\n/** Currencies Stripe charges with no minor unit. */\nconst ZERO_DECIMAL = new Set(['JPY', 'KRW'])\n\n/** Minor units per major unit: 100 for most, 1 for yen and won. */\nexport function currencyMinorFactor(code: string): number {\n return ZERO_DECIMAL.has(code.toUpperCase()) ? 1 : 100\n}\n\nconst EURO_COUNTRIES = [\n 'AT', 'BE', 'BG', 'HR', 'CY', 'EE', 'FI', 'FR', 'DE', 'GR', 'IE', 'IT',\n 'LV', 'LT', 'LU', 'MT', 'NL', 'PT', 'SK', 'SI', 'ES', 'AD', 'MC', 'SM',\n 'VA', 'ME', 'XK', 'AX', 'GF', 'GP', 'MQ', 'YT', 'RE', 'BL', 'MF', 'PM',\n]\n\n/**\n * The currency a country's shoppers expect (ISO 4217), for the currencies a\n * store can sell in. A country not listed shops in the store currency.\n */\nexport const COUNTRY_CURRENCY: Readonly<Record<string, string>> = {\n ...Object.fromEntries(EURO_COUNTRIES.map((code) => [code, 'EUR'])),\n US: 'USD', PR: 'USD', GU: 'USD', AS: 'USD', VI: 'USD', MP: 'USD', EC: 'USD',\n SV: 'USD', PA: 'USD', TL: 'USD', FM: 'USD', MH: 'USD', PW: 'USD', TC: 'USD',\n VG: 'USD', BQ: 'USD',\n GB: 'GBP', IM: 'GBP', JE: 'GBP', GG: 'GBP',\n AU: 'AUD', CX: 'AUD', CC: 'AUD', NF: 'AUD', KI: 'AUD', NR: 'AUD', TV: 'AUD',\n NZ: 'NZD', CK: 'NZD', NU: 'NZD', PN: 'NZD', TK: 'NZD',\n CA: 'CAD',\n JP: 'JPY',\n CH: 'CHF', LI: 'CHF',\n SE: 'SEK',\n NO: 'NOK', SJ: 'NOK', BV: 'NOK',\n DK: 'DKK', FO: 'DKK', GL: 'DKK',\n PL: 'PLN',\n CZ: 'CZK',\n MX: 'MXN',\n SG: 'SGD',\n HK: 'HKD',\n ZA: 'ZAR',\n IL: 'ILS', PS: 'ILS',\n BR: 'BRL',\n IN: 'INR',\n PH: 'PHP',\n TH: 'THB',\n MY: 'MYR',\n ID: 'IDR',\n KR: 'KRW',\n RO: 'RON',\n TR: 'TRY',\n CN: 'CNY',\n}\n\nconst upper = (value: unknown): string =>\n typeof value === 'string' ? value.trim().toUpperCase() : ''\n\n/** The store's own currency, upper case (`settings/store.currency`, default USD). */\nexport function storeBaseCurrency(store: { currency?: unknown } | undefined): string {\n const code = upper(store?.currency)\n return /^[A-Z]{3}$/.test(code) ? code : 'USD'\n}\n\n/**\n * The currencies this store sells in, the store currency first, each known\n * and listed once. Off (or nothing added) is the store currency alone.\n */\nexport function storeCurrencies(\n settings: StoreCurrencySettings | undefined,\n base: string,\n): StoreCurrency[] {\n const list: StoreCurrency[] = [{ code: base, rounding: 'none' }]\n if (!settings?.enabled) return list\n for (const entry of settings.currencies ?? []) {\n const code = upper(entry?.code)\n if (!SELLABLE_CURRENCIES.includes(code) || list.some((c) => c.code === code)) continue\n list.push({\n code,\n rounding: (['none', 'whole', '99', '95'] as const).includes(\n entry.rounding as CurrencyRounding,\n )\n ? entry.rounding\n : 'none',\n bufferPct: clampBuffer(entry.bufferPct),\n ...(Number(entry.manualRate) > 0 ? { manualRate: Number(entry.manualRate) } : {}),\n ...(entry.pricesIncludeTax ? { pricesIncludeTax: true } : {}),\n })\n }\n return list\n}\n\nfunction clampBuffer(value: unknown): number {\n const n = Number(value)\n if (!Number.isFinite(n) || n === 0) return 0\n return Math.max(-90, Math.min(100, Math.round(n * 100) / 100))\n}\n\n/**\n * The rate a store converts into `target` at: its manual rate when set,\n * else the daily cross rate. `undefined` when neither covers it.\n */\nexport function currencyRate(\n rates: FxRates | undefined,\n base: string,\n target: StoreCurrency,\n): number | undefined {\n if (upper(base) === target.code) return 1\n if (target.manualRate && target.manualRate > 0) return target.manualRate\n return crossRate(rates, base, target.code)\n}\n\n/** Whether `code` is a currency this store sells in. */\nexport function storeSellsIn(\n settings: StoreCurrencySettings | undefined,\n base: string,\n code: unknown,\n): StoreCurrency | undefined {\n const want = upper(code)\n return storeCurrencies(settings, base).find((entry) => entry.code === want)\n}\n\n/**\n * The currency a shopper sees: their own choice when the store sells in it,\n * else the one their country uses when the store sells in that (and\n * auto-detect is on), else the store currency.\n */\nexport function resolveShopperCurrency(input: {\n settings: StoreCurrencySettings | undefined\n base: string\n country?: string | null\n chosen?: string | null\n}): string {\n const { settings, base } = input\n const chosen = storeSellsIn(settings, base, input.chosen)\n if (chosen) return chosen.code\n if (settings?.autoDetect !== false) {\n const local = COUNTRY_CURRENCY[upper(input.country)]\n if (local && storeSellsIn(settings, base, local)) return local\n }\n return base\n}\n\n/** `rates[to] / rates[from]` through the rates' own base; undefined when unknown. */\nexport function crossRate(rates: FxRates | undefined, from: string, to: string): number | undefined {\n const f = upper(from)\n const t = upper(to)\n if (f === t) return 1\n if (!rates?.rates) return undefined\n const rate = (code: string) => (code === upper(rates.base) ? 1 : rates.rates[code])\n const a = rate(f)\n const b = rate(t)\n if (!(a > 0) || !(b > 0)) return undefined\n return b / a\n}\n\n/** Round a major amount UP to the ending the merchant chose. */\nexport function roundMajor(major: number, rounding: CurrencyRounding, code: string): number {\n const factor = currencyMinorFactor(code)\n // Shave float noise (28.950000000001) before rounding up.\n const clean = Math.round(major * factor * 1e6) / 1e6 / factor\n switch (rounding) {\n case 'whole':\n return Math.ceil(clean - 1e-9)\n case '99':\n case '95': {\n if (factor === 1) return Math.ceil(clean - 1e-9)\n const ending = rounding === '99' ? 0.99 : 0.95\n const floor = Math.floor(clean)\n const candidate = floor + ending\n return clean <= candidate + 1e-9 ? candidate : floor + 1 + ending\n }\n case 'none':\n default:\n return Math.ceil(clean * factor - 1e-9) / factor\n }\n}\n\n/**\n * A store-currency amount (minor units) in a shopper currency (minor units):\n * converted at the reference rate, plus the buffer, rounded as set.\n * `undefined` when the rates do not cover the pair, and callers then sell in\n * the store currency rather than guessing.\n */\nexport function convertMinor(\n amountMinor: number,\n from: string,\n target: StoreCurrency,\n rates: FxRates | undefined,\n options: { round?: boolean } = {},\n): number | undefined {\n const rate = currencyRate(rates, from, target)\n if (rate == null) return undefined\n if (upper(from) === target.code) return Math.round(amountMinor)\n if (!amountMinor) return 0\n const major =\n (amountMinor / currencyMinorFactor(from)) * rate * (1 + (target.bufferPct ?? 0) / 100)\n const rounded =\n options.round === false\n ? roundMajor(major, 'none', target.code)\n : roundMajor(major, target.rounding ?? 'none', target.code)\n return Math.round(rounded * currencyMinorFactor(target.code))\n}\n\n/**\n * The price a shopper pays for a variant, in minor units of their currency:\n * the variant's fixed price there when it has one, else its store price\n * converted. `priceMajor` is the store price in major units (`priceUsd`).\n */\nexport function variantPriceMinor(\n variant: { priceUsd?: number; fixedPrices?: FixedPrices },\n base: string,\n target: StoreCurrency,\n rates: FxRates | undefined,\n): number | undefined {\n const fixed = variant.fixedPrices?.[target.code]\n if (target.code !== base && typeof fixed === 'number' && fixed >= 0) {\n return Math.round(fixed * currencyMinorFactor(target.code))\n }\n const baseMinor = Math.round(Number(variant.priceUsd ?? 0) * currencyMinorFactor(base))\n return convertMinor(baseMinor, base, target, rates)\n}\n\n/**\n * An add-on or fee amount in the shopper's currency: a fixed amount there\n * when the merchant set one, else converted at the rate WITHOUT the ending\n * rounding (a $5 add-on is NZ$8.61, not NZ$8.99: rounding is for prices a\n * shopper reads on their own).\n */\nexport function amountMinorIn(\n baseMinor: number,\n base: string,\n target: StoreCurrency,\n rates: FxRates | undefined,\n fixedMinor?: Record<string, number>,\n): number | undefined {\n const fixed = fixedMinor?.[target.code]\n if (target.code !== base && typeof fixed === 'number' && fixed >= 0) return Math.round(fixed)\n return convertMinor(baseMinor, base, target, rates, { round: false })\n}\n\n/**\n * Money for a shopper. Where two currencies a store sells in share a sign\n * ($ for US, Australian and New Zealand dollars), each says which it is:\n * US$29.00, A$45.00, NZ$49.00.\n */\nexport function formatShopperMoney(\n amountMinor: number,\n code: string,\n options: { disambiguate?: boolean; locale?: string } = {},\n): string {\n const currency = upper(code) || 'USD'\n const factor = currencyMinorFactor(currency)\n const value = (Number(amountMinor) || 0) / factor\n let text: string\n try {\n text = new Intl.NumberFormat(options.locale ?? 'en-US', {\n style: 'currency',\n currency,\n minimumFractionDigits: factor === 1 ? 0 : 2,\n maximumFractionDigits: factor === 1 ? 0 : 2,\n }).format(value)\n } catch {\n return `${currency} ${value.toFixed(factor === 1 ? 0 : 2)}`\n }\n if (options.disambiguate && currency === 'USD') text = text.replace(/^(-?)\\$/, '$1US$')\n return text\n}\n\n/** Whether a store's currencies include two that share a sign. */\nexport function currenciesShareSign(codes: readonly string[]): boolean {\n const dollars = codes.filter((code) => ['USD', 'AUD', 'NZD', 'CAD', 'SGD', 'HKD', 'MXN'].includes(code))\n return dollars.length > 1\n}\n\n/**\n * Money in a timeline line or notice for a sale in `currency` (lower or\n * upper case). A USD sale keeps the exact text it always had (`$1234.50`);\n * any other currency names itself (`NZ$449.00`).\n */\nexport function saleMoneyText(cents: number, currency: string | null | undefined): string {\n const code = upper(currency) || 'USD'\n if (code === 'USD') return `$${((Number(cents) || 0) / 100).toFixed(2)}`\n return formatShopperMoney(cents, code)\n}\n\n/** The metadata keys a checkout writes the charged line units under (AGL-4248). */\nexport const CHARGE_UNITS_METADATA_PREFIX = 'chargeUnits'\n\n/** Line units as metadata entries, each under Stripe's 500-character value cap. */\nexport function encodeChargeUnitsMetadata(units: readonly number[]): Record<string, string> {\n const out: Record<string, string> = {}\n let chunk: string[] = []\n let index = 0\n const flush = () => {\n if (chunk.length) out[`${CHARGE_UNITS_METADATA_PREFIX}${index++}`] = chunk.join(',')\n chunk = []\n }\n for (const unit of units) {\n const text = String(Math.max(0, Math.round(unit)))\n if ([...chunk, text].join(',').length > 480) flush()\n chunk.push(text)\n }\n flush()\n return out\n}\n\n/** The line units back from session metadata, or undefined when absent. */\nexport function decodeChargeUnitsMetadata(\n metadata: Record<string, unknown> | null | undefined,\n): number[] | undefined {\n if (!metadata) return undefined\n const units: number[] = []\n for (let index = 0; typeof metadata[`${CHARGE_UNITS_METADATA_PREFIX}${index}`] === 'string'; index++) {\n for (const part of String(metadata[`${CHARGE_UNITS_METADATA_PREFIX}${index}`]).split(',')) {\n const n = Number(part)\n units.push(Number.isFinite(n) ? n : 0)\n }\n }\n return units.length ? units : undefined\n}\n\n/**\n * The order's currency fields (AGL-4248) from a paid session: the charge\n * currency Stripe reports and, when it is not the store's, the store\n * currency, the rate and the totals there.\n */\nexport function orderCurrencyFields<T extends object>(\n session: { currency?: unknown; metadata?: Record<string, unknown> | null } | null | undefined,\n totals: T,\n): { currency?: string; baseCurrency?: string; fxRate?: number; baseTotals?: T } {\n const currency = typeof session?.currency === 'string' ? session.currency.toLowerCase() : undefined\n const meta = session?.metadata ?? {}\n const base = typeof meta['baseCurrency'] === 'string' ? String(meta['baseCurrency']).toLowerCase() : undefined\n const rate = Number(meta['fxRate'])\n if (!currency) return {}\n if (!base || base === currency || !(rate > 0)) return { currency }\n const factor = currencyMinorFactor(base) / currencyMinorFactor(currency)\n const baseTotals = Object.fromEntries(\n Object.entries(totals).map(([key, value]) => [\n key,\n typeof value === 'number' ? Math.round((value / rate) * factor) : value,\n ]),\n ) as T\n return { currency, baseCurrency: base, fxRate: rate, baseTotals }\n}\n\n/** Rates older than this are not used to charge anyone: the store currency is. */\nexport const FX_RATES_MAX_AGE_MS = 4 * 24 * 60 * 60 * 1000\n\nexport function ratesAreFresh(rates: FxRates | undefined, nowMs: number): boolean {\n return Boolean(rates?.asOfMs && nowMs - rates.asOfMs <= FX_RATES_MAX_AGE_MS)\n}\n\n/** The ECB's daily reference rates (free, no key): per 1 EUR, about 16:00 CET. */\nexport const ECB_DAILY_URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'\n\n/**\n * Parse the European Central Bank's daily reference XML\n * (`eurofxref-daily.xml`): `<Cube time=\"2026-11-25\">` holding\n * `<Cube currency=\"USD\" rate=\"1.0521\"/>` rows, per 1 EUR.\n */\nexport function parseEcbDailyXml(xml: string): FxRates | undefined {\n const time = /<Cube\\s+time=['\"](\\d{4}-\\d{2}-\\d{2})['\"]/.exec(xml)?.[1]\n const rates: Record<string, number> = {}\n for (const match of xml.matchAll(/<Cube\\s+currency=['\"]([A-Z]{3})['\"]\\s+rate=['\"]([\\d.]+)['\"]/g)) {\n const value = Number(match[2])\n if (value > 0) rates[match[1]] = value\n }\n if (!time || !Object.keys(rates).length) return undefined\n return { base: 'EUR', rates, asOfMs: Date.parse(`${time}T16:00:00Z`), source: 'ecb' }\n}\n\n/** What a storefront prices from (`GET commerce/currency`). */\nexport interface PublicCurrencyAnswer {\n base: string\n /** The store currency first; only currencies the day's rates cover. */\n currencies: StoreCurrency[]\n autoDetect: boolean\n /** The rates for exactly those currencies, or null when selling in the store currency only. */\n rates: FxRates | null\n}\n\n/**\n * The public answer, from a store's settings and the stored rates. A\n * currency the rates do not cover, or rates older than\n * {@link FX_RATES_MAX_AGE_MS}, drop out: checkout would refuse to charge in\n * them, so the storefront must not show them.\n */\nexport function publicCurrencyAnswer(\n context: { base: string; settings: StoreCurrencySettings | undefined; rates: FxRates | undefined },\n nowMs: number,\n): PublicCurrencyAnswer {\n const fresh = ratesAreFresh(context.rates, nowMs)\n const currencies = storeCurrencies(context.settings, context.base).filter(\n (entry) =>\n entry.code === context.base ||\n Boolean(entry.manualRate) ||\n (fresh && crossRate(context.rates, context.base, entry.code) != null),\n )\n const rates =\n fresh && context.rates && currencies.length > 1\n ? {\n base: context.rates.base,\n asOfMs: context.rates.asOfMs,\n rates: Object.fromEntries(\n currencies\n .map((entry) => entry.code)\n .concat(context.base)\n .filter((code) => code !== context.rates?.base && context.rates?.rates[code])\n .map((code) => [code, context.rates?.rates[code] as number]),\n ),\n }\n : null\n return {\n base: context.base,\n currencies,\n autoDetect: context.settings?.autoDetect !== false,\n rates,\n }\n}\n"],"names":["CURRENCY_ROUNDING_LABELS","none","whole","SELLABLE_CURRENCIES","CURRENCY_NAMES","USD","EUR","GBP","AUD","NZD","CAD","JPY","CHF","SEK","NOK","DKK","PLN","CZK","MXN","SGD","HKD","ZAR","ILS","BRL","INR","PHP","THB","MYR","IDR","KRW","RON","TRY","CNY","ZERO_DECIMAL","Set","currencyMinorFactor","code","has","toUpperCase","EURO_COUNTRIES","COUNTRY_CURRENCY","Object","fromEntries","map","US","PR","GU","AS","VI","MP","EC","SV","PA","TL","FM","MH","PW","TC","VG","BQ","GB","IM","JE","GG","AU","CX","CC","NF","KI","NR","TV","NZ","CK","NU","PN","TK","CA","JP","CH","LI","SE","NO","SJ","BV","DK","FO","GL","PL","CZ","MX","SG","HK","ZA","IL","PS","BR","IN","PH","TH","MY","ID","KR","RO","TR","CN","upper","value","trim","storeBaseCurrency","store","currency","test","storeCurrencies","settings","base","list","rounding","enabled","entry","currencies","includes","some","c","push","bufferPct","clampBuffer","Number","manualRate","pricesIncludeTax","n","isFinite","Math","max","min","round","currencyRate","rates","target","crossRate","storeSellsIn","want","find","resolveShopperCurrency","input","chosen","autoDetect","local","country","from","to","f","t","undefined","rate","a","b","roundMajor","major","factor","clean","ceil","ending","floor","candidate","convertMinor","amountMinor","options","rounded","variantPriceMinor","variant","fixed","fixedPrices","baseMinor","priceUsd","amountMinorIn","fixedMinor","formatShopperMoney","text","Intl","NumberFormat","locale","style","minimumFractionDigits","maximumFractionDigits","format","toFixed","disambiguate","replace","currenciesShareSign","codes","dollars","filter","length","saleMoneyText","cents","CHARGE_UNITS_METADATA_PREFIX","encodeChargeUnitsMetadata","units","out","chunk","index","flush","join","unit","String","decodeChargeUnitsMetadata","metadata","part","split","orderCurrencyFields","session","totals","toLowerCase","meta","baseTotals","entries","key","baseCurrency","fxRate","FX_RATES_MAX_AGE_MS","ratesAreFresh","nowMs","Boolean","asOfMs","ECB_DAILY_URL","parseEcbDailyXml","xml","time","exec","match","matchAll","keys","Date","parse","source","publicCurrencyAnswer","context","fresh","concat"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;CAcC,GAED,2EAA2E;AAG3E,OAAO,MAAMA,2BAAuE;IAClFC,MAAM;IACNC,OAAO;IACP,MAAM;IACN,MAAM;AACR,EAAC;AAiDD;;;;CAIC,GACD,OAAO,MAAMC,sBAAyC;IACpD;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD,CAAA;AAED,OAAO,MAAMC,iBAAmD;IAC9DC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;IACLC,KAAK;AACP,EAAC;AAED,kDAAkD,GAClD,MAAMC,eAAe,IAAIC,IAAI;IAAC;IAAO;CAAM;AAE3C,iEAAiE,GACjE,OAAO,SAASC,oBAAoBC,IAAY;IAC9C,OAAOH,aAAaI,GAAG,CAACD,KAAKE,WAAW,MAAM,IAAI;AACpD;AAEA,MAAMC,iBAAiB;IACrB;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAClE;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;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,aAAaP,gBAAgB4H,QAAQ3H,QAAQ,EAAE2H,QAAQ1H,IAAI,EAAE6E,MAAM,CACvE,CAACzE,QACCA,MAAMlF,IAAI,KAAKwM,QAAQ1H,IAAI,IAC3B4G,QAAQxG,MAAMS,UAAU,KACvB8G,SAASnG,UAAUkG,QAAQpG,KAAK,EAAEoG,QAAQ1H,IAAI,EAAEI,MAAMlF,IAAI,KAAK;IAEpE,MAAMoG,QACJqG,SAASD,QAAQpG,KAAK,IAAIjB,WAAWyE,MAAM,GAAG,IAC1C;QACE9E,MAAM0H,QAAQpG,KAAK,CAACtB,IAAI;QACxB6G,QAAQa,QAAQpG,KAAK,CAACuF,MAAM;QAC5BvF,OAAO/F,OAAOC,WAAW,CACvB6E,WACG5E,GAAG,CAAC,CAAC2E,QAAUA,MAAMlF,IAAI,EACzB0M,MAAM,CAACF,QAAQ1H,IAAI,EACnB6E,MAAM,CAAC,CAAC3J;gBAAkBwM,gBAAuBA;mBAAhCxM,WAASwM,iBAAAA,QAAQpG,KAAK,qBAAboG,eAAe1H,IAAI,OAAI0H,kBAAAA,QAAQpG,KAAK,qBAAboG,gBAAepG,KAAK,CAACpG,KAAK;WAC3EO,GAAG,CAAC,CAACP;gBAAgBwM;mBAAP;gBAACxM;iBAAMwM,iBAAAA,QAAQpG,KAAK,qBAAboG,eAAepG,KAAK,CAACpG,KAAK;aAAW;;IAEjE,IACA;IACN,OAAO;QACL8E,MAAM0H,QAAQ1H,IAAI;QAClBK;QACA0B,YAAY2F,EAAAA,oBAAAA,QAAQ3H,QAAQ,qBAAhB2H,kBAAkB3F,UAAU,MAAK;QAC7CT;IACF;AACF"}
@@ -438,6 +438,23 @@ export interface OrderHardwareBuyer {
438
438
  export interface HostOrder {
439
439
  /** Human order number, sequential per host (e.g. #1042). */
440
440
  number?: number;
441
+ /**
442
+ * The currency the shopper paid in (AGL-4248), lower case as Stripe
443
+ * writes it: every amount on the order, `totals` and lines included, is in
444
+ * it. Absent on orders from before multi-currency, which are in the store
445
+ * currency.
446
+ */
447
+ currency?: string;
448
+ /** The store's own currency when the shopper paid in another (AGL-4248), lower case. */
449
+ baseCurrency?: string;
450
+ /** Units of `currency` per unit of `baseCurrency` the order was priced at, before any buffer. */
451
+ fxRate?: number;
452
+ /**
453
+ * `totals` in the store currency (AGL-4248), at `fxRate`, for the
454
+ * merchant's reports. What Stripe actually settled can differ by its own
455
+ * conversion; the payout report is the record of that.
456
+ */
457
+ baseTotals?: OrderTotals;
441
458
  /**
442
459
  * The distinct use days its lines name (`OrderLineItem.useDayKey`,
443
460
  * AGL-4043), stamped by every creator through `withOrderListFields` so the
@@ -23,6 +23,7 @@
23
23
  */ import { _ as _extends } from "@swc/helpers/_/_extends";
24
24
  import { stripeIdIsTestMode } from "@aglyn/aglyn/app-utils/stripe-deployment-mode";
25
25
  import { isCheckInDayKey } from "./commerce-check-in-product.js";
26
+ import { formatOrderMoney } from "./buyer-notifications.js";
26
27
  import { lineRequiresShipping, orderLineFulfillmentStates } from "./order-fulfillment.js";
27
28
  /**
28
29
  * Legal status transitions. Refund/cancel policies: anything paid can
@@ -335,7 +336,7 @@ export function computeCheckoutSessionTotals(lineItems, session, parts) {
335
336
  * document rather than as an order; the arithmetic is the same either way,
336
337
  * which is why it lives here and is not duplicated there.
337
338
  */ export function computeBuyNowOrder(session, snapshot) {
338
- var _ref, _ref1, _metadata_quantity, _ref2, _ref3, _ref4, _ref5, _metadata_variantId, _metadata_productId;
339
+ var _ref, _ref1, _metadata_quantity, _ref2, _ref3, _ref4, _ref5, _metadata_variantId, _metadata_productId, _metadata_chargeFeeCents;
339
340
  var _snapshot_modifiers;
340
341
  const metadata = (_ref = session == null ? void 0 : session.metadata) != null ? _ref : {};
341
342
  const details = (_ref1 = session == null ? void 0 : session.total_details) != null ? _ref1 : {};
@@ -379,7 +380,8 @@ export function computeCheckoutSessionTotals(lineItems, session, parts) {
379
380
  return {
380
381
  lineItems,
381
382
  totals: computeCheckoutSessionTotals(lineItems, session, {
382
- feeCents: metadataCents(metadata.feeCents),
383
+ // The take as charged when the sale was in another currency (AGL-4248).
384
+ feeCents: metadataCents((_metadata_chargeFeeCents = metadata.chargeFeeCents) != null ? _metadata_chargeFeeCents : metadata.feeCents),
383
385
  lineItemTaxCents,
384
386
  pricedInDiscountCents
385
387
  })
@@ -815,17 +817,20 @@ export function computeCheckoutSessionTotals(lineItems, session, parts) {
815
817
  * must not mean different things on two of them. Empty for an order with
816
818
  * nothing refunded — there is nothing to say.
817
819
  */ export function orderRefundSummary(order) {
818
- var _order_refundedCents, _ref, _order_amountCents, _order_refundedLineItemIds, _order_lineItems;
820
+ var _order_refundedCents, _ref, _order_amountCents, _order_currency, _order_refundedLineItemIds, _order_lineItems;
819
821
  var _order_totals;
820
822
  const state = orderRefundState(order);
821
823
  if (state === 'none') return '';
822
824
  const refundedCents = Math.max(0, Number((_order_refundedCents = order.refundedCents) != null ? _order_refundedCents : 0));
823
825
  const totalCents = (_ref = (_order_totals = order.totals) == null ? void 0 : _order_totals.totalCents) != null ? _ref : Number((_order_amountCents = order.amountCents) != null ? _order_amountCents : 0);
824
- if (state === 'full') return `Refunded in full ($${usd(refundedCents)})`;
826
+ // In the currency it was paid in (AGL-4248); a USD order reads as before.
827
+ const currency = String((_order_currency = order.currency) != null ? _order_currency : 'usd').toUpperCase();
828
+ const money = (cents)=>currency === 'USD' ? `$${usd(cents)}` : formatOrderMoney(cents, currency);
829
+ if (state === 'full') return `Refunded in full (${money(refundedCents)})`;
825
830
  const revoked = ((_order_refundedLineItemIds = order.refundedLineItemIds) != null ? _order_refundedLineItemIds : []).length;
826
831
  const lines = ((_order_lineItems = order.lineItems) != null ? _order_lineItems : []).length;
827
832
  const scope = revoked > 0 ? `${revoked} of ${lines} line${lines === 1 ? '' : 's'} withdrawn` : 'no lines withdrawn — refunded by amount';
828
- return `Partially refunded ($${usd(refundedCents)} of $${usd(totalCents)}) — ${scope}`;
833
+ return `Partially refunded (${money(refundedCents)} of ${money(totalCents)}) — ${scope}`;
829
834
  }
830
835
  /** `12345` -> `123.45`, the CSV's money format. */ function usd(cents) {
831
836
  return (cents / 100).toFixed(2);