@open-mercato/checkout 0.10.1-develop.8287.1.7313a0fe67 → 0.10.1-develop.8291.1.6ec3aee41c

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 (87) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/checkout/__integration__/TC-CHK-CRUDFORM-001.spec.js +3 -3
  3. package/dist/modules/checkout/__integration__/TC-CHK-CRUDFORM-001.spec.js.map +2 -2
  4. package/dist/modules/checkout/__integration__/TC-CHKT-037-template-sync.spec.js +3 -1
  5. package/dist/modules/checkout/__integration__/TC-CHKT-037-template-sync.spec.js.map +2 -2
  6. package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-001.spec.js +259 -0
  7. package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-001.spec.js.map +7 -0
  8. package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-002-admin-round-trip.spec.js +85 -0
  9. package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-002-admin-round-trip.spec.js.map +7 -0
  10. package/dist/modules/checkout/api/pay/[slug]/submit/route.js +17 -2
  11. package/dist/modules/checkout/api/pay/[slug]/submit/route.js.map +2 -2
  12. package/dist/modules/checkout/backend/checkout/pay-links/page.js +9 -3
  13. package/dist/modules/checkout/backend/checkout/pay-links/page.js.map +2 -2
  14. package/dist/modules/checkout/commands/links.js +7 -10
  15. package/dist/modules/checkout/commands/links.js.map +2 -2
  16. package/dist/modules/checkout/commands/shared.js +43 -7
  17. package/dist/modules/checkout/commands/shared.js.map +2 -2
  18. package/dist/modules/checkout/commands/templates.js +12 -10
  19. package/dist/modules/checkout/commands/templates.js.map +2 -2
  20. package/dist/modules/checkout/commands/transactions.js +13 -1
  21. package/dist/modules/checkout/commands/transactions.js.map +2 -2
  22. package/dist/modules/checkout/components/LinkTemplateForm.js +47 -20
  23. package/dist/modules/checkout/components/LinkTemplateForm.js.map +2 -2
  24. package/dist/modules/checkout/components/PayPage.js +59 -55
  25. package/dist/modules/checkout/components/PayPage.js.map +2 -2
  26. package/dist/modules/checkout/data/entities.js +6 -5
  27. package/dist/modules/checkout/data/entities.js.map +2 -2
  28. package/dist/modules/checkout/data/validators.js +19 -2
  29. package/dist/modules/checkout/data/validators.js.map +2 -2
  30. package/dist/modules/checkout/i18n/de.json +1 -0
  31. package/dist/modules/checkout/i18n/en.json +1 -0
  32. package/dist/modules/checkout/i18n/es.json +1 -0
  33. package/dist/modules/checkout/i18n/ko.json +1 -0
  34. package/dist/modules/checkout/i18n/pl.json +1 -0
  35. package/dist/modules/checkout/lib/amountPrecision.js +98 -0
  36. package/dist/modules/checkout/lib/amountPrecision.js.map +7 -0
  37. package/dist/modules/checkout/lib/payPageAmount.js +56 -0
  38. package/dist/modules/checkout/lib/payPageAmount.js.map +7 -0
  39. package/dist/modules/checkout/lib/utils.js +63 -20
  40. package/dist/modules/checkout/lib/utils.js.map +2 -2
  41. package/dist/modules/checkout/migrations/Migration20261014082519_checkout.js +33 -0
  42. package/dist/modules/checkout/migrations/Migration20261014082519_checkout.js.map +7 -0
  43. package/dist/modules/checkout/workers/send-email.worker.js +8 -1
  44. package/dist/modules/checkout/workers/send-email.worker.js.map +2 -2
  45. package/package.json +10 -7
  46. package/src/modules/checkout/__integration__/TC-CHK-CRUDFORM-001.spec.ts +3 -3
  47. package/src/modules/checkout/__integration__/TC-CHKT-037-template-sync.spec.ts +2 -0
  48. package/src/modules/checkout/__integration__/TC-CHKT-PRECISION-001.spec.ts +320 -0
  49. package/src/modules/checkout/__integration__/TC-CHKT-PRECISION-002-admin-round-trip.spec.ts +112 -0
  50. package/src/modules/checkout/api/pay/[slug]/__tests__/route.test.ts +4 -0
  51. package/src/modules/checkout/api/pay/[slug]/submit/__tests__/route.test.ts +145 -0
  52. package/src/modules/checkout/api/pay/[slug]/submit/route.ts +19 -2
  53. package/src/modules/checkout/backend/checkout/pay-links/page.tsx +13 -3
  54. package/src/modules/checkout/commands/__tests__/configured-amount-precision.test.ts +180 -0
  55. package/src/modules/checkout/commands/__tests__/create-transaction-amount.test.ts +77 -0
  56. package/src/modules/checkout/commands/__tests__/template-sync.test.ts +145 -0
  57. package/src/modules/checkout/commands/links.ts +7 -10
  58. package/src/modules/checkout/commands/shared.ts +56 -7
  59. package/src/modules/checkout/commands/templates.ts +12 -10
  60. package/src/modules/checkout/commands/transactions.ts +15 -1
  61. package/src/modules/checkout/components/LinkTemplateForm.tsx +52 -21
  62. package/src/modules/checkout/components/PayPage.tsx +76 -58
  63. package/src/modules/checkout/components/__tests__/LinkTemplateForm.localeDecimalSeparator.test.tsx +35 -1
  64. package/src/modules/checkout/components/__tests__/LinkTemplateForm.priceListExactAmount.test.tsx +196 -0
  65. package/src/modules/checkout/components/__tests__/PayPage.amountPrecision.test.tsx +115 -0
  66. package/src/modules/checkout/components/__tests__/PayPagePricing.amountDisplay.test.tsx +98 -0
  67. package/src/modules/checkout/components/__tests__/__fixtures__/legacyPayPageContracts.ts +13 -0
  68. package/src/modules/checkout/components/__tests__/payPageContracts.backwardCompat.test.ts +32 -0
  69. package/src/modules/checkout/data/__tests__/validators.test.ts +91 -0
  70. package/src/modules/checkout/data/entities.ts +7 -6
  71. package/src/modules/checkout/data/validators.ts +26 -2
  72. package/src/modules/checkout/i18n/de.json +1 -0
  73. package/src/modules/checkout/i18n/en.json +1 -0
  74. package/src/modules/checkout/i18n/es.json +1 -0
  75. package/src/modules/checkout/i18n/ko.json +1 -0
  76. package/src/modules/checkout/i18n/pl.json +1 -0
  77. package/src/modules/checkout/lib/__tests__/amountPrecision.test.ts +169 -0
  78. package/src/modules/checkout/lib/__tests__/payPageAmount.test.ts +127 -0
  79. package/src/modules/checkout/lib/__tests__/utils.test.ts +72 -0
  80. package/src/modules/checkout/lib/amountPrecision.ts +152 -0
  81. package/src/modules/checkout/lib/payPageAmount.ts +99 -0
  82. package/src/modules/checkout/lib/utils.ts +85 -28
  83. package/src/modules/checkout/migrations/.snapshot-open-mercato.json +158 -27
  84. package/src/modules/checkout/migrations/Migration20261014082519_checkout.ts +35 -0
  85. package/src/modules/checkout/migrations/__tests__/Migration20261014082519_checkout.integration.test.ts +6 -0
  86. package/src/modules/checkout/workers/__tests__/send-email.worker.test.ts +43 -0
  87. package/src/modules/checkout/workers/send-email.worker.ts +8 -1
@@ -2,18 +2,27 @@ import { createHmac, timingSafeEqual } from 'crypto'
2
2
  import bcrypt from 'bcryptjs'
3
3
  import { slugify } from '@open-mercato/shared/lib/slugify'
4
4
  import { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'
5
+ import {
6
+ decimalToNumber,
7
+ decimalToString,
8
+ parseDecimal,
9
+ resolveExactDecimal,
10
+ withExactAmounts,
11
+ type WithExactAmounts,
12
+ } from '@open-mercato/shared/lib/decimal'
5
13
  import { normalizeCustomFieldResponse } from '@open-mercato/shared/lib/custom-fields/normalize'
6
14
  import { parseDecryptedFieldValue } from '@open-mercato/shared/lib/encryption/tenantDataEncryptionService'
7
15
  import type { EntityManager } from '@mikro-orm/postgresql'
8
16
  import { getPaymentGatewayDescriptor } from '@open-mercato/shared/modules/payment_gateways/types'
9
17
  import { CheckoutLink, CheckoutLinkTemplate, CheckoutTransaction } from '../data/entities'
10
18
  import { buildCheckoutAttachmentPreviewUrl, normalizeOptionalString } from './client-utils'
11
- import type {
12
- CreateLinkInput,
13
- CreateTemplateInput,
14
- PublicSubmitInput,
15
- UpdateLinkInput,
16
- UpdateTemplateInput,
19
+ import {
20
+ assertExactCustomAmountRange,
21
+ type CreateLinkInput,
22
+ type CreateTemplateInput,
23
+ type PublicSubmitInput,
24
+ type UpdateLinkInput,
25
+ type UpdateTemplateInput,
17
26
  } from '../data/validators'
18
27
  import { CHECKOUT_TERMINAL_STATUSES } from './constants'
19
28
  export {
@@ -30,8 +39,18 @@ export type CheckoutScope = {
30
39
 
31
40
  export type CheckoutLinkStatus = 'draft' | 'active' | 'inactive'
32
41
 
42
+ export const CHECKOUT_MONEY_FIELDS = [
43
+ 'fixedPriceAmount',
44
+ 'fixedPriceOriginalAmount',
45
+ 'customAmountMin',
46
+ 'customAmountMax',
47
+ 'amount',
48
+ ] as const
49
+
50
+ export type CheckoutMoneyField = (typeof CHECKOUT_MONEY_FIELDS)[number]
51
+
33
52
  export type CheckoutPayloadWithCustomFields<TInput> = {
34
- parsed: TInput
53
+ parsed: WithExactAmounts<TInput, CheckoutMoneyField>
35
54
  customFields: Record<string, unknown>
36
55
  }
37
56
 
@@ -43,6 +62,10 @@ type TemplateOrLinkInput =
43
62
 
44
63
  type TemplateOrLinkMutationInput = Omit<CreateLinkInput, 'password'> & {
45
64
  password?: string | null
65
+ fixedPriceAmountExact?: string | null
66
+ fixedPriceOriginalAmountExact?: string | null
67
+ customAmountMinExact?: string | null
68
+ customAmountMaxExact?: string | null
46
69
  }
47
70
 
48
71
  function isRecord(value: unknown): value is Record<string, unknown> {
@@ -68,8 +91,9 @@ export function pickExplicitParsedOverrides<TInput extends Record<string, unknow
68
91
  if (!isRecord(rawInput)) return {}
69
92
 
70
93
  const overrides: Partial<TInput> = {}
71
- for (const key of Object.keys(parsed) as Array<keyof TInput>) {
72
- if (!Object.prototype.hasOwnProperty.call(rawInput, key)) continue
94
+ for (const key of Object.keys(parsed) as Array<keyof TInput & string>) {
95
+ const sourceKey = key.endsWith('Exact') ? key.slice(0, -'Exact'.length) : key
96
+ if (!Object.prototype.hasOwnProperty.call(rawInput, key) && !Object.prototype.hasOwnProperty.call(rawInput, sourceKey)) continue
73
97
  overrides[key] = parsed[key]
74
98
  }
75
99
 
@@ -89,10 +113,16 @@ export function parseCheckoutInput<TInput>(raw: unknown, parser: (value: unknown
89
113
  const source = isRecord(raw) ? { ...raw } : {}
90
114
  const customFields = isRecord(source.customFields) ? source.customFields : {}
91
115
  delete source.customFields
92
- return {
93
- parsed: parser(source),
94
- customFields,
116
+ const parsed = withExactAmounts(parser(source) as TInput & object, source, CHECKOUT_MONEY_FIELDS)
117
+ const items = (parsed as { priceListItems?: unknown }).priceListItems
118
+ if (Array.isArray(items)) {
119
+ const rawItems = Array.isArray(source.priceListItems) ? source.priceListItems : []
120
+ ;(parsed as { priceListItems?: unknown }).priceListItems = items.map((item, index) =>
121
+ isRecord(item) ? withExactAmounts(item, rawItems[index], ['amount'] as const) : item,
122
+ )
95
123
  }
124
+ assertExactCustomAmountRange(parsed)
125
+ return { parsed, customFields }
96
126
  }
97
127
 
98
128
  export function resolveLoadedCheckoutCustomFields(
@@ -114,9 +144,10 @@ export function toMoneyNumber(value: string | number | null | undefined): number
114
144
  return Number.isFinite(numeric) ? numeric : null
115
145
  }
116
146
 
147
+ /** Exact decimal string for a money column (no rounding, no padding), or `null`. */
117
148
  export function toMoneyString(value: string | number | null | undefined): string | null {
118
- const numeric = toMoneyNumber(value)
119
- return numeric == null ? null : numeric.toFixed(2)
149
+ const parsed = parseDecimal(value)
150
+ return parsed ? decimalToString(parsed) : null
120
151
  }
121
152
 
122
153
  export { normalizeOptionalString, buildCheckoutAttachmentPreviewUrl } from './client-utils'
@@ -162,11 +193,15 @@ export function toTemplateOrLinkMutationInput(
162
193
  themeMode: record.themeMode,
163
194
  pricingMode: record.pricingMode,
164
195
  fixedPriceAmount: toMoneyNumber(record.fixedPriceAmount),
196
+ fixedPriceAmountExact: toMoneyString(record.fixedPriceAmount),
165
197
  fixedPriceCurrencyCode: record.fixedPriceCurrencyCode ?? null,
166
198
  fixedPriceIncludesTax: record.fixedPriceIncludesTax,
167
199
  fixedPriceOriginalAmount: toMoneyNumber(record.fixedPriceOriginalAmount),
200
+ fixedPriceOriginalAmountExact: toMoneyString(record.fixedPriceOriginalAmount),
168
201
  customAmountMin: toMoneyNumber(record.customAmountMin),
202
+ customAmountMinExact: toMoneyString(record.customAmountMin),
169
203
  customAmountMax: toMoneyNumber(record.customAmountMax),
204
+ customAmountMaxExact: toMoneyString(record.customAmountMax),
170
205
  customAmountCurrencyCode: record.customAmountCurrencyCode ?? null,
171
206
  priceListItems: record.priceListItems ?? null,
172
207
  gatewayProviderKey: record.gatewayProviderKey ?? '',
@@ -396,34 +431,41 @@ export function buildConsentProof(link: CheckoutLink, acceptedLegalConsents: Pub
396
431
  return proof
397
432
  }
398
433
 
399
- export function resolveSubmittedAmount(link: CheckoutLink, input: PublicSubmitInput): { amount: number; currencyCode: string; selectedPriceItemId: string | null } {
434
+ /**
435
+ * The amount a payer is charged, compared exactly against the link configuration.
436
+ * `input.amountExact` (raw request digits) wins over the coerced float when present.
437
+ */
438
+ export function resolveSubmittedAmount(
439
+ link: CheckoutLink,
440
+ input: PublicSubmitInput & { amountExact?: string | null },
441
+ ): { amount: number; amountExact: string; currencyCode: string; selectedPriceItemId: string | null } {
442
+ const submitted = input.amount == null ? null : parseDecimal(resolveExactDecimal(input.amountExact, input.amount))
400
443
  if (link.pricingMode === 'fixed') {
401
- const expected = toMoneyNumber(link.fixedPriceAmount)
444
+ const expected = parseDecimal(link.fixedPriceAmount)
402
445
  if (expected == null || !link.fixedPriceCurrencyCode) {
403
446
  throw new CrudHttpError(422, { error: 'checkout.payPage.errors.submit' })
404
447
  }
405
- if (input.amount != null && Number(input.amount) !== expected) {
448
+ if (input.amount != null && (!submitted || !submitted.eq(expected))) {
406
449
  throw new CrudHttpError(422, { error: 'checkout.payPage.errors.submit' })
407
450
  }
408
- return { amount: expected, currencyCode: link.fixedPriceCurrencyCode, selectedPriceItemId: null }
451
+ return withAmount(expected, link.fixedPriceCurrencyCode, null)
409
452
  }
410
453
  if (link.pricingMode === 'custom_amount') {
411
- if (input.amount == null || !link.customAmountCurrencyCode) {
454
+ if (input.amount == null || !submitted || !link.customAmountCurrencyCode) {
412
455
  throw new CrudHttpError(422, {
413
456
  error: 'checkout.payPage.validation.fixErrors',
414
457
  fieldErrors: { amount: 'checkout.payPage.validation.amountRequired' },
415
458
  })
416
459
  }
417
- const min = toMoneyNumber(link.customAmountMin) ?? 0
418
- const max = toMoneyNumber(link.customAmountMax)
419
- const amount = Number(input.amount)
420
- if (amount < min || (max != null && amount > max)) {
460
+ const min = parseDecimal(link.customAmountMin)
461
+ const max = parseDecimal(link.customAmountMax)
462
+ if ((min && submitted.lt(min)) || (!min && submitted.lt(0)) || (max && submitted.gt(max))) {
421
463
  throw new CrudHttpError(422, {
422
464
  error: 'checkout.payPage.validation.fixErrors',
423
465
  fieldErrors: { amount: 'checkout.payPage.errors.submit' },
424
466
  })
425
467
  }
426
- return { amount, currencyCode: link.customAmountCurrencyCode, selectedPriceItemId: null }
468
+ return withAmount(submitted, link.customAmountCurrencyCode, null)
427
469
  }
428
470
  const selectedPriceItem = (link.priceListItems ?? []).find((item) => item.id === input.selectedPriceItemId)
429
471
  if (!selectedPriceItem) {
@@ -432,13 +474,19 @@ export function resolveSubmittedAmount(link: CheckoutLink, input: PublicSubmitIn
432
474
  fieldErrors: { selectedPriceItemId: 'checkout.payPage.validation.priceSelectionRequired' },
433
475
  })
434
476
  }
435
- if (input.amount != null && Number(input.amount) !== Number(selectedPriceItem.amount)) {
477
+ const itemAmount = parseDecimal(resolveExactDecimal(selectedPriceItem.amountExact, selectedPriceItem.amount))
478
+ if (!itemAmount || (input.amount != null && (!submitted || !submitted.eq(itemAmount)))) {
436
479
  throw new CrudHttpError(422, { error: 'checkout.payPage.errors.submit' })
437
480
  }
481
+ return withAmount(itemAmount, selectedPriceItem.currencyCode, selectedPriceItem.id)
482
+ }
483
+
484
+ function withAmount(amount: NonNullable<ReturnType<typeof parseDecimal>>, currencyCode: string, selectedPriceItemId: string | null) {
438
485
  return {
439
- amount: Number(selectedPriceItem.amount),
440
- currencyCode: selectedPriceItem.currencyCode,
441
- selectedPriceItemId: selectedPriceItem.id,
486
+ amount: decimalToNumber(amount),
487
+ amountExact: decimalToString(amount),
488
+ currencyCode,
489
+ selectedPriceItemId,
442
490
  }
443
491
  }
444
492
 
@@ -459,11 +507,15 @@ export function serializeTemplateOrLink(record: CheckoutLinkTemplate | CheckoutL
459
507
  themeMode: record.themeMode,
460
508
  pricingMode: record.pricingMode,
461
509
  fixedPriceAmount: toMoneyNumber(record.fixedPriceAmount),
510
+ fixedPriceAmountExact: toMoneyString(record.fixedPriceAmount),
462
511
  fixedPriceCurrencyCode: record.fixedPriceCurrencyCode ?? null,
463
512
  fixedPriceIncludesTax: record.fixedPriceIncludesTax,
464
513
  fixedPriceOriginalAmount: toMoneyNumber(record.fixedPriceOriginalAmount),
514
+ fixedPriceOriginalAmountExact: toMoneyString(record.fixedPriceOriginalAmount),
465
515
  customAmountMin: toMoneyNumber(record.customAmountMin),
516
+ customAmountMinExact: toMoneyString(record.customAmountMin),
466
517
  customAmountMax: toMoneyNumber(record.customAmountMax),
518
+ customAmountMaxExact: toMoneyString(record.customAmountMax),
467
519
  customAmountCurrencyCode: record.customAmountCurrencyCode ?? null,
468
520
  priceListItems: record.priceListItems ?? [],
469
521
  gatewayProviderKey: record.gatewayProviderKey ?? null,
@@ -519,11 +571,15 @@ export function serializePublicPayLink(record: CheckoutLink) {
519
571
  themeMode: record.themeMode,
520
572
  pricingMode: record.pricingMode,
521
573
  fixedPriceAmount: toMoneyNumber(record.fixedPriceAmount),
574
+ fixedPriceAmountExact: toMoneyString(record.fixedPriceAmount),
522
575
  fixedPriceCurrencyCode: record.fixedPriceCurrencyCode ?? null,
523
576
  fixedPriceIncludesTax: record.fixedPriceIncludesTax,
524
577
  fixedPriceOriginalAmount: toMoneyNumber(record.fixedPriceOriginalAmount),
578
+ fixedPriceOriginalAmountExact: toMoneyString(record.fixedPriceOriginalAmount),
525
579
  customAmountMin: toMoneyNumber(record.customAmountMin),
580
+ customAmountMinExact: toMoneyString(record.customAmountMin),
526
581
  customAmountMax: toMoneyNumber(record.customAmountMax),
582
+ customAmountMaxExact: toMoneyString(record.customAmountMax),
527
583
  customAmountCurrencyCode: record.customAmountCurrencyCode ?? null,
528
584
  priceListItems: record.priceListItems ?? [],
529
585
  gatewayProviderKey: record.gatewayProviderKey ?? null,
@@ -549,6 +605,7 @@ export function serializeTransaction(record: CheckoutTransaction, link?: Checkou
549
605
  linkName: link?.name ?? null,
550
606
  linkSlug: link?.slug ?? null,
551
607
  amount: toMoneyNumber(record.amount),
608
+ amountExact: toMoneyString(record.amount),
552
609
  currencyCode: record.currencyCode,
553
610
  status: record.status,
554
611
  paymentStatus: record.paymentStatus ?? null,