@open-mercato/checkout 0.10.1-develop.8289.1.9a25111d49 → 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.
- package/.turbo/turbo-build.log +1 -1
- package/dist/modules/checkout/__integration__/TC-CHK-CRUDFORM-001.spec.js +3 -3
- package/dist/modules/checkout/__integration__/TC-CHK-CRUDFORM-001.spec.js.map +2 -2
- package/dist/modules/checkout/__integration__/TC-CHKT-037-template-sync.spec.js +3 -1
- package/dist/modules/checkout/__integration__/TC-CHKT-037-template-sync.spec.js.map +2 -2
- package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-001.spec.js +259 -0
- package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-001.spec.js.map +7 -0
- package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-002-admin-round-trip.spec.js +85 -0
- package/dist/modules/checkout/__integration__/TC-CHKT-PRECISION-002-admin-round-trip.spec.js.map +7 -0
- package/dist/modules/checkout/api/pay/[slug]/submit/route.js +17 -2
- package/dist/modules/checkout/api/pay/[slug]/submit/route.js.map +2 -2
- package/dist/modules/checkout/backend/checkout/pay-links/page.js +9 -3
- package/dist/modules/checkout/backend/checkout/pay-links/page.js.map +2 -2
- package/dist/modules/checkout/commands/links.js +7 -10
- package/dist/modules/checkout/commands/links.js.map +2 -2
- package/dist/modules/checkout/commands/shared.js +43 -7
- package/dist/modules/checkout/commands/shared.js.map +2 -2
- package/dist/modules/checkout/commands/templates.js +12 -10
- package/dist/modules/checkout/commands/templates.js.map +2 -2
- package/dist/modules/checkout/commands/transactions.js +13 -1
- package/dist/modules/checkout/commands/transactions.js.map +2 -2
- package/dist/modules/checkout/components/LinkTemplateForm.js +47 -20
- package/dist/modules/checkout/components/LinkTemplateForm.js.map +2 -2
- package/dist/modules/checkout/components/PayPage.js +59 -55
- package/dist/modules/checkout/components/PayPage.js.map +2 -2
- package/dist/modules/checkout/data/entities.js +6 -5
- package/dist/modules/checkout/data/entities.js.map +2 -2
- package/dist/modules/checkout/data/validators.js +19 -2
- package/dist/modules/checkout/data/validators.js.map +2 -2
- package/dist/modules/checkout/i18n/de.json +1 -0
- package/dist/modules/checkout/i18n/en.json +1 -0
- package/dist/modules/checkout/i18n/es.json +1 -0
- package/dist/modules/checkout/i18n/ko.json +1 -0
- package/dist/modules/checkout/i18n/pl.json +1 -0
- package/dist/modules/checkout/lib/amountPrecision.js +98 -0
- package/dist/modules/checkout/lib/amountPrecision.js.map +7 -0
- package/dist/modules/checkout/lib/payPageAmount.js +56 -0
- package/dist/modules/checkout/lib/payPageAmount.js.map +7 -0
- package/dist/modules/checkout/lib/utils.js +63 -20
- package/dist/modules/checkout/lib/utils.js.map +2 -2
- package/dist/modules/checkout/migrations/Migration20261014082519_checkout.js +33 -0
- package/dist/modules/checkout/migrations/Migration20261014082519_checkout.js.map +7 -0
- package/dist/modules/checkout/workers/send-email.worker.js +8 -1
- package/dist/modules/checkout/workers/send-email.worker.js.map +2 -2
- package/package.json +10 -7
- package/src/modules/checkout/__integration__/TC-CHK-CRUDFORM-001.spec.ts +3 -3
- package/src/modules/checkout/__integration__/TC-CHKT-037-template-sync.spec.ts +2 -0
- package/src/modules/checkout/__integration__/TC-CHKT-PRECISION-001.spec.ts +320 -0
- package/src/modules/checkout/__integration__/TC-CHKT-PRECISION-002-admin-round-trip.spec.ts +112 -0
- package/src/modules/checkout/api/pay/[slug]/__tests__/route.test.ts +4 -0
- package/src/modules/checkout/api/pay/[slug]/submit/__tests__/route.test.ts +145 -0
- package/src/modules/checkout/api/pay/[slug]/submit/route.ts +19 -2
- package/src/modules/checkout/backend/checkout/pay-links/page.tsx +13 -3
- package/src/modules/checkout/commands/__tests__/configured-amount-precision.test.ts +180 -0
- package/src/modules/checkout/commands/__tests__/create-transaction-amount.test.ts +77 -0
- package/src/modules/checkout/commands/__tests__/template-sync.test.ts +145 -0
- package/src/modules/checkout/commands/links.ts +7 -10
- package/src/modules/checkout/commands/shared.ts +56 -7
- package/src/modules/checkout/commands/templates.ts +12 -10
- package/src/modules/checkout/commands/transactions.ts +15 -1
- package/src/modules/checkout/components/LinkTemplateForm.tsx +52 -21
- package/src/modules/checkout/components/PayPage.tsx +76 -58
- package/src/modules/checkout/components/__tests__/LinkTemplateForm.localeDecimalSeparator.test.tsx +35 -1
- package/src/modules/checkout/components/__tests__/LinkTemplateForm.priceListExactAmount.test.tsx +196 -0
- package/src/modules/checkout/components/__tests__/PayPage.amountPrecision.test.tsx +115 -0
- package/src/modules/checkout/components/__tests__/PayPagePricing.amountDisplay.test.tsx +98 -0
- package/src/modules/checkout/components/__tests__/__fixtures__/legacyPayPageContracts.ts +13 -0
- package/src/modules/checkout/components/__tests__/payPageContracts.backwardCompat.test.ts +32 -0
- package/src/modules/checkout/data/__tests__/validators.test.ts +91 -0
- package/src/modules/checkout/data/entities.ts +7 -6
- package/src/modules/checkout/data/validators.ts +26 -2
- package/src/modules/checkout/i18n/de.json +1 -0
- package/src/modules/checkout/i18n/en.json +1 -0
- package/src/modules/checkout/i18n/es.json +1 -0
- package/src/modules/checkout/i18n/ko.json +1 -0
- package/src/modules/checkout/i18n/pl.json +1 -0
- package/src/modules/checkout/lib/__tests__/amountPrecision.test.ts +169 -0
- package/src/modules/checkout/lib/__tests__/payPageAmount.test.ts +127 -0
- package/src/modules/checkout/lib/__tests__/utils.test.ts +72 -0
- package/src/modules/checkout/lib/amountPrecision.ts +152 -0
- package/src/modules/checkout/lib/payPageAmount.ts +99 -0
- package/src/modules/checkout/lib/utils.ts +85 -28
- package/src/modules/checkout/migrations/.snapshot-open-mercato.json +158 -27
- package/src/modules/checkout/migrations/Migration20261014082519_checkout.ts +35 -0
- package/src/modules/checkout/migrations/__tests__/Migration20261014082519_checkout.integration.test.ts +6 -0
- package/src/modules/checkout/workers/__tests__/send-email.worker.test.ts +43 -0
- package/src/modules/checkout/workers/send-email.worker.ts +8 -1
|
@@ -17,13 +17,17 @@ import {
|
|
|
17
17
|
isCheckoutLinkPublic,
|
|
18
18
|
mapGatewayStatusToCheckoutStatus,
|
|
19
19
|
resolveSubmittedAmount,
|
|
20
|
+
toMoneyString,
|
|
20
21
|
validateDescriptorCurrencies,
|
|
21
22
|
} from '../../../../lib/utils'
|
|
22
23
|
import { validateCheckoutCustomerData } from '../../../../lib/customerDataValidation'
|
|
24
|
+
import { findCheckoutChargePrecisionError } from '../../../../lib/amountPrecision'
|
|
23
25
|
import { checkoutSubmitRateLimitConfig, enforceCheckoutRateLimit } from '../../../../lib/rateLimiter'
|
|
24
26
|
import { rateLimitErrorSchema } from '@open-mercato/shared/lib/ratelimit/helpers'
|
|
25
27
|
import { checkoutTag } from '../../../openapi'
|
|
26
28
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
29
|
+
import { withExactAmounts } from '@open-mercato/shared/lib/decimal'
|
|
30
|
+
import { readJsonSafe } from '@open-mercato/shared/lib/http/readJsonSafe'
|
|
27
31
|
|
|
28
32
|
const logger = createLogger('checkout')
|
|
29
33
|
|
|
@@ -335,7 +339,8 @@ export async function POST(req: Request, { params }: { params: Promise<{ slug: s
|
|
|
335
339
|
return NextResponse.json({ error: 'Idempotency-Key must be between 16 and 128 characters' }, { status: 400 })
|
|
336
340
|
}
|
|
337
341
|
|
|
338
|
-
const
|
|
342
|
+
const rawBody = await readJsonSafe<unknown>(req, {})
|
|
343
|
+
const body = withExactAmounts(publicSubmitSchema.parse(rawBody), rawBody, ['amount'] as const)
|
|
339
344
|
const em = container.resolve('em')
|
|
340
345
|
const commandBus = container.resolve('commandBus') as CommandBus
|
|
341
346
|
const paymentGatewayService = container.resolve('paymentGatewayService') as PaymentGatewayService
|
|
@@ -402,10 +407,20 @@ export async function POST(req: Request, { params }: { params: Promise<{ slug: s
|
|
|
402
407
|
await buildSubmitResponse(req, em, link, existingTransaction, link.gatewayProviderKey),
|
|
403
408
|
)
|
|
404
409
|
}
|
|
410
|
+
const precisionError = await findCheckoutChargePrecisionError(
|
|
411
|
+
container,
|
|
412
|
+
{ organizationId: link.organizationId, tenantId: link.tenantId },
|
|
413
|
+
existingTransaction
|
|
414
|
+
? { amount: existingTransaction.amount, currencyCode: existingTransaction.currencyCode }
|
|
415
|
+
: { amount: resolvedAmount.amountExact, currencyCode: resolvedAmount.currencyCode },
|
|
416
|
+
)
|
|
417
|
+
if (precisionError) {
|
|
418
|
+
return NextResponse.json(precisionError, { status: 400 })
|
|
419
|
+
}
|
|
405
420
|
|
|
406
421
|
const transactionInput = {
|
|
407
422
|
linkId: link.id,
|
|
408
|
-
amount: resolvedAmount.
|
|
423
|
+
amount: resolvedAmount.amountExact,
|
|
409
424
|
currencyCode: resolvedAmount.currencyCode,
|
|
410
425
|
idempotencyKey,
|
|
411
426
|
customerData: collectedCustomerData,
|
|
@@ -498,6 +513,7 @@ export async function POST(req: Request, { params }: { params: Promise<{ slug: s
|
|
|
498
513
|
paymentId: transactionId,
|
|
499
514
|
idempotencyKey,
|
|
500
515
|
amount: sessionAmount,
|
|
516
|
+
amountExact: toMoneyString(transaction.amount) ?? undefined,
|
|
501
517
|
currencyCode: sessionCurrencyCode,
|
|
502
518
|
paymentTypes: configuredPaymentTypes.length > 0 ? configuredPaymentTypes : undefined,
|
|
503
519
|
description: link.title ?? link.name,
|
|
@@ -590,6 +606,7 @@ export async function POST(req: Request, { params }: { params: Promise<{ slug: s
|
|
|
590
606
|
status: refreshedTransaction.status,
|
|
591
607
|
paymentStatus: refreshedTransaction.paymentStatus ?? null,
|
|
592
608
|
amount: Number(refreshedTransaction.amount),
|
|
609
|
+
amountExact: toMoneyString(refreshedTransaction.amount),
|
|
593
610
|
currency: refreshedTransaction.currencyCode,
|
|
594
611
|
gatewayProvider: link.gatewayProviderKey,
|
|
595
612
|
gatewayTransactionId: refreshedTransaction.gatewayTransactionId ?? null,
|
|
@@ -16,6 +16,7 @@ import { Badge } from '@open-mercato/ui/primitives/badge'
|
|
|
16
16
|
import { apiCallOrThrow, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'
|
|
17
17
|
import { normalizeCrudServerError } from '@open-mercato/ui/backend/utils/serverErrors'
|
|
18
18
|
import { ListEmptyState } from '@open-mercato/ui/backend/filters/ListEmptyState'
|
|
19
|
+
import { countDecimalPlaces, resolveExactDecimal, toDecimal } from '@open-mercato/shared/lib/decimal'
|
|
19
20
|
|
|
20
21
|
type LinkRow = {
|
|
21
22
|
id: string
|
|
@@ -23,9 +24,12 @@ type LinkRow = {
|
|
|
23
24
|
slug: string
|
|
24
25
|
pricingMode: 'fixed' | 'custom_amount' | 'price_list'
|
|
25
26
|
fixedPriceAmount?: number | null
|
|
27
|
+
fixedPriceAmountExact?: string | null
|
|
26
28
|
fixedPriceCurrencyCode?: string | null
|
|
27
29
|
customAmountMin?: number | null
|
|
30
|
+
customAmountMinExact?: string | null
|
|
28
31
|
customAmountMax?: number | null
|
|
32
|
+
customAmountMaxExact?: string | null
|
|
29
33
|
customAmountCurrencyCode?: string | null
|
|
30
34
|
status: 'draft' | 'active' | 'inactive'
|
|
31
35
|
completionCount: number
|
|
@@ -33,6 +37,12 @@ type LinkRow = {
|
|
|
33
37
|
createdAt?: string | null
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
function formatLinkAmount(exact: string | null | undefined, legacy: number | null | undefined): string {
|
|
41
|
+
const amount = resolveExactDecimal(exact, legacy)
|
|
42
|
+
if (amount === null) return '0.00'
|
|
43
|
+
return toDecimal(amount).toFixed(Math.max(2, countDecimalPlaces(amount)))
|
|
44
|
+
}
|
|
45
|
+
|
|
36
46
|
type ListResponse = {
|
|
37
47
|
items: LinkRow[]
|
|
38
48
|
total: number
|
|
@@ -150,14 +160,14 @@ export default function CheckoutPayLinksPage() {
|
|
|
150
160
|
cell: ({ row }) => {
|
|
151
161
|
if (row.original.pricingMode === 'fixed') {
|
|
152
162
|
return t('checkout.admin.payLinks.pricing.fixed', {
|
|
153
|
-
amount: row.original.fixedPriceAmount
|
|
163
|
+
amount: formatLinkAmount(row.original.fixedPriceAmountExact, row.original.fixedPriceAmount),
|
|
154
164
|
currency: row.original.fixedPriceCurrencyCode ?? '',
|
|
155
165
|
}).trim()
|
|
156
166
|
}
|
|
157
167
|
if (row.original.pricingMode === 'custom_amount') {
|
|
158
168
|
return t('checkout.admin.payLinks.pricing.customAmount', {
|
|
159
|
-
min: row.original.customAmountMin
|
|
160
|
-
max: row.original.customAmountMax
|
|
169
|
+
min: formatLinkAmount(row.original.customAmountMinExact, row.original.customAmountMin),
|
|
170
|
+
max: formatLinkAmount(row.original.customAmountMaxExact, row.original.customAmountMax),
|
|
161
171
|
currency: row.original.customAmountCurrencyCode ?? '',
|
|
162
172
|
}).trim()
|
|
163
173
|
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/** @jest-environment node */
|
|
2
|
+
|
|
3
|
+
import { commandRegistry } from '@open-mercato/shared/lib/commands/registry'
|
|
4
|
+
import type { CommandRuntimeContext } from '@open-mercato/shared/lib/commands/types'
|
|
5
|
+
|
|
6
|
+
const ORG_ID = '123e4567-e89b-12d3-a456-426614174000'
|
|
7
|
+
const TENANT_ID = '123e4567-e89b-12d3-a456-426614174001'
|
|
8
|
+
const LINK_ID = '123e4567-e89b-12d3-a456-426614174010'
|
|
9
|
+
const TEMPLATE_ID = '123e4567-e89b-12d3-a456-426614174020'
|
|
10
|
+
const FLUSH_SENTINEL = 'FLUSH_REACHED'
|
|
11
|
+
|
|
12
|
+
const mockFindOneWithDecryption = jest.fn()
|
|
13
|
+
|
|
14
|
+
jest.mock('@open-mercato/shared/lib/encryption/find', () => ({
|
|
15
|
+
findOneWithDecryption: jest.fn((...args: unknown[]) => mockFindOneWithDecryption(...args)),
|
|
16
|
+
findWithDecryption: jest.fn(async () => []),
|
|
17
|
+
}))
|
|
18
|
+
|
|
19
|
+
jest.mock('../../lib/gatewayProviderAvailability', () => ({
|
|
20
|
+
ensureGatewayProviderConfigured: jest.fn(async () => undefined),
|
|
21
|
+
getGatewayProviderConfigurationMessageKey: jest.fn(() => null),
|
|
22
|
+
}))
|
|
23
|
+
|
|
24
|
+
jest.mock('@open-mercato/shared/lib/commands/helpers', () => ({
|
|
25
|
+
setCustomFieldsIfAny: jest.fn(async () => undefined),
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
jest.mock('@open-mercato/shared/lib/commands/customFieldSnapshots', () => ({
|
|
29
|
+
loadCustomFieldSnapshot: jest.fn(async () => ({})),
|
|
30
|
+
buildCustomFieldResetMap: jest.fn(() => ({})),
|
|
31
|
+
}))
|
|
32
|
+
|
|
33
|
+
jest.mock('@open-mercato/shared/lib/crud/custom-fields', () => ({
|
|
34
|
+
loadCustomFieldValues: jest.fn(async () => ({})),
|
|
35
|
+
}))
|
|
36
|
+
|
|
37
|
+
jest.mock('../../events', () => ({
|
|
38
|
+
emitCheckoutEvent: jest.fn(async () => undefined),
|
|
39
|
+
}))
|
|
40
|
+
|
|
41
|
+
import '../links'
|
|
42
|
+
import '../templates'
|
|
43
|
+
|
|
44
|
+
const CURRENCY_DECIMALS: Record<string, number> = { USD: 2, JPY: 0, ETH: 18 }
|
|
45
|
+
|
|
46
|
+
function makeContext() {
|
|
47
|
+
const created: Array<Record<string, unknown>> = []
|
|
48
|
+
const em = {
|
|
49
|
+
create: jest.fn((_entity: unknown, data: Record<string, unknown>) => {
|
|
50
|
+
created.push(data)
|
|
51
|
+
return { id: 'created-1', ...data }
|
|
52
|
+
}),
|
|
53
|
+
persist: jest.fn(),
|
|
54
|
+
findOne: jest.fn(async () => null),
|
|
55
|
+
flush: jest.fn(async () => {
|
|
56
|
+
throw new Error(FLUSH_SENTINEL)
|
|
57
|
+
}),
|
|
58
|
+
}
|
|
59
|
+
const services: Record<string, unknown> = {
|
|
60
|
+
em,
|
|
61
|
+
dataEngine: {},
|
|
62
|
+
paymentGatewayDescriptorService: {},
|
|
63
|
+
currencyPrecisionService: {
|
|
64
|
+
getDecimalPlaces: async ({ code }: { code: string }) => CURRENCY_DECIMALS[code] ?? null,
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
const ctx = {
|
|
68
|
+
container: {
|
|
69
|
+
resolve: (name: string) => {
|
|
70
|
+
if (!(name in services)) throw new Error(`[internal] missing ${name}`)
|
|
71
|
+
return services[name]
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
auth: { orgId: ORG_ID, tenantId: TENANT_ID },
|
|
75
|
+
organizationScope: null,
|
|
76
|
+
selectedOrganizationId: ORG_ID,
|
|
77
|
+
organizationIds: [ORG_ID],
|
|
78
|
+
} as unknown as CommandRuntimeContext
|
|
79
|
+
return { ctx, created }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function storedRecord(overrides: Record<string, unknown> = {}) {
|
|
83
|
+
return {
|
|
84
|
+
organizationId: ORG_ID,
|
|
85
|
+
tenantId: TENANT_ID,
|
|
86
|
+
name: 'Pay link',
|
|
87
|
+
title: null,
|
|
88
|
+
slug: 'pay-link',
|
|
89
|
+
templateId: null,
|
|
90
|
+
status: 'draft',
|
|
91
|
+
isLocked: false,
|
|
92
|
+
pricingMode: 'fixed',
|
|
93
|
+
gatewayProviderKey: 'mock',
|
|
94
|
+
gatewaySettings: {},
|
|
95
|
+
fixedPriceAmount: '10',
|
|
96
|
+
fixedPriceCurrencyCode: 'USD',
|
|
97
|
+
fixedPriceOriginalAmount: null,
|
|
98
|
+
customAmountMin: null,
|
|
99
|
+
customAmountMax: null,
|
|
100
|
+
customAmountCurrencyCode: null,
|
|
101
|
+
priceListItems: null,
|
|
102
|
+
passwordHash: null,
|
|
103
|
+
updatedAt: new Date('2026-06-01T10:00:00.000Z'),
|
|
104
|
+
...overrides,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function run(commandId: string, input: Record<string, unknown>, ctx: CommandRuntimeContext) {
|
|
109
|
+
const handler = commandRegistry.get(commandId)
|
|
110
|
+
if (!handler) throw new Error(`[internal] command ${commandId} not registered`)
|
|
111
|
+
await expect(handler.execute(input, ctx)).rejects.toThrow(FLUSH_SENTINEL)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const customAmountInput = {
|
|
115
|
+
name: 'Donation',
|
|
116
|
+
pricingMode: 'custom_amount',
|
|
117
|
+
customAmountMin: '10.001',
|
|
118
|
+
customAmountMax: '10.005',
|
|
119
|
+
customAmountCurrencyCode: 'USD',
|
|
120
|
+
gatewayProviderKey: 'mock',
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
describe('configured checkout amounts are rounded to the currency decimals', () => {
|
|
124
|
+
beforeEach(() => {
|
|
125
|
+
jest.clearAllMocks()
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('rounds custom amount bounds on template create (USD)', async () => {
|
|
129
|
+
const { ctx, created } = makeContext()
|
|
130
|
+
await run('checkout.template.create', customAmountInput, ctx)
|
|
131
|
+
expect(created[0]).toMatchObject({ customAmountMin: '10', customAmountMax: '10.01' })
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('rounds the fixed price and price-list items on link create (JPY)', async () => {
|
|
135
|
+
const { ctx, created } = makeContext()
|
|
136
|
+
await run('checkout.link.create', {
|
|
137
|
+
name: 'Yen link',
|
|
138
|
+
pricingMode: 'price_list',
|
|
139
|
+
priceListItems: [{ id: 'a', description: 'A', amount: '99.5', currencyCode: 'JPY' }],
|
|
140
|
+
gatewayProviderKey: 'mock',
|
|
141
|
+
}, ctx)
|
|
142
|
+
expect(created[0]?.priceListItems).toEqual([
|
|
143
|
+
{ id: 'a', description: 'A', amount: 100, amountExact: '100', currencyCode: 'JPY' },
|
|
144
|
+
])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('keeps every digit for an 18-decimal currency on link create', async () => {
|
|
148
|
+
const { ctx, created } = makeContext()
|
|
149
|
+
await run('checkout.link.create', {
|
|
150
|
+
name: 'ETH link',
|
|
151
|
+
pricingMode: 'fixed',
|
|
152
|
+
fixedPriceAmount: '1234.567890123456789012',
|
|
153
|
+
fixedPriceCurrencyCode: 'ETH',
|
|
154
|
+
gatewayProviderKey: 'mock',
|
|
155
|
+
}, ctx)
|
|
156
|
+
expect(created[0]).toMatchObject({ fixedPriceAmount: '1234.567890123456789012' })
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('rounds a new fixed price on link update using the stored currency', async () => {
|
|
160
|
+
const { ctx } = makeContext()
|
|
161
|
+
const link = storedRecord({ id: LINK_ID })
|
|
162
|
+
mockFindOneWithDecryption.mockResolvedValue(link)
|
|
163
|
+
await run('checkout.link.update', { id: LINK_ID, fixedPriceAmount: '10.005' }, ctx)
|
|
164
|
+
expect(link.fixedPriceAmount).toBe('10.01')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
it('rounds custom amount bounds and price-list items on template update', async () => {
|
|
168
|
+
const { ctx } = makeContext()
|
|
169
|
+
const template = storedRecord({ id: TEMPLATE_ID, pricingMode: 'price_list', fixedPriceAmount: null, fixedPriceCurrencyCode: null })
|
|
170
|
+
mockFindOneWithDecryption.mockResolvedValue(template)
|
|
171
|
+
await run('checkout.template.update', {
|
|
172
|
+
id: TEMPLATE_ID,
|
|
173
|
+
pricingMode: 'price_list',
|
|
174
|
+
priceListItems: [{ id: 'a', description: 'A', amount: '10.005', currencyCode: 'USD' }],
|
|
175
|
+
}, ctx)
|
|
176
|
+
expect(template.priceListItems).toEqual([
|
|
177
|
+
{ id: 'a', description: 'A', amount: 10.01, amountExact: '10.01', currencyCode: 'USD' },
|
|
178
|
+
])
|
|
179
|
+
})
|
|
180
|
+
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** @jest-environment node */
|
|
2
|
+
|
|
3
|
+
import { commandRegistry } from '@open-mercato/shared/lib/commands/registry'
|
|
4
|
+
import type { CommandRuntimeContext } from '@open-mercato/shared/lib/commands/types'
|
|
5
|
+
|
|
6
|
+
import '../transactions'
|
|
7
|
+
|
|
8
|
+
jest.mock('../../events', () => ({
|
|
9
|
+
emitCheckoutEvent: jest.fn(async () => undefined),
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
const ORG_ID = '123e4567-e89b-12d3-a456-426614174000'
|
|
13
|
+
const TENANT_ID = '123e4567-e89b-12d3-a456-426614174001'
|
|
14
|
+
const LINK_ID = '123e4567-e89b-12d3-a456-426614174010'
|
|
15
|
+
|
|
16
|
+
function makeContext(currencyDecimalPlaces: number | null) {
|
|
17
|
+
const created: Array<Record<string, unknown>> = []
|
|
18
|
+
const tx = {
|
|
19
|
+
findOne: jest.fn(async () => ({
|
|
20
|
+
id: LINK_ID,
|
|
21
|
+
status: 'active',
|
|
22
|
+
slug: 'link',
|
|
23
|
+
templateId: null,
|
|
24
|
+
gatewayProviderKey: 'stripe',
|
|
25
|
+
isLocked: true,
|
|
26
|
+
})),
|
|
27
|
+
execute: jest.fn(async () => [{ active_reservation_count: 1 }]),
|
|
28
|
+
create: jest.fn((_entity: unknown, data: Record<string, unknown>) => {
|
|
29
|
+
const record = { id: 'tx-1', ...data }
|
|
30
|
+
created.push(record)
|
|
31
|
+
return record
|
|
32
|
+
}),
|
|
33
|
+
persist: jest.fn(),
|
|
34
|
+
flush: jest.fn(async () => undefined),
|
|
35
|
+
}
|
|
36
|
+
const em = { transactional: async (run: (manager: typeof tx) => Promise<unknown>) => run(tx) }
|
|
37
|
+
const services: Record<string, unknown> = {
|
|
38
|
+
em,
|
|
39
|
+
currencyPrecisionService: { getDecimalPlaces: async () => currencyDecimalPlaces },
|
|
40
|
+
}
|
|
41
|
+
const ctx = {
|
|
42
|
+
container: {
|
|
43
|
+
resolve: (name: string) => {
|
|
44
|
+
if (!(name in services)) throw new Error(`[internal] missing ${name}`)
|
|
45
|
+
return services[name]
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
} as unknown as CommandRuntimeContext
|
|
49
|
+
return { ctx, created }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function input(amount: string) {
|
|
53
|
+
return {
|
|
54
|
+
linkId: LINK_ID,
|
|
55
|
+
amount,
|
|
56
|
+
currencyCode: 'USD',
|
|
57
|
+
idempotencyKey: 'key-1',
|
|
58
|
+
organizationId: ORG_ID,
|
|
59
|
+
tenantId: TENANT_ID,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe('checkout.transaction.create charged amount', () => {
|
|
64
|
+
const handler = commandRegistry.get('checkout.transaction.create')!
|
|
65
|
+
|
|
66
|
+
it('stores the amount rounded to the currency precision the gateway charges', async () => {
|
|
67
|
+
const { ctx, created } = makeContext(2)
|
|
68
|
+
await handler.execute(input('10.004'), ctx)
|
|
69
|
+
expect(created[0]?.amount).toBe('10')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('keeps every digit for a high-precision currency', async () => {
|
|
73
|
+
const { ctx, created } = makeContext(18)
|
|
74
|
+
await handler.execute(input('0.000000000000000001'), ctx)
|
|
75
|
+
expect(created[0]?.amount).toBe('0.000000000000000001')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import type { CheckoutLink } from '../../data/entities'
|
|
1
2
|
import {
|
|
2
3
|
buildSelectiveLinkedCustomFieldUpdates,
|
|
3
4
|
buildSelectiveLinkedLinkSnapshot,
|
|
5
|
+
restoreLinkFromSnapshot,
|
|
4
6
|
type CheckoutLinkSnapshot,
|
|
5
7
|
type CheckoutTemplateSnapshot,
|
|
6
8
|
} from '../shared'
|
|
@@ -23,11 +25,15 @@ function createTemplateSnapshot(overrides: Partial<CheckoutTemplateSnapshot> = {
|
|
|
23
25
|
themeMode: 'auto',
|
|
24
26
|
pricingMode: 'fixed',
|
|
25
27
|
fixedPriceAmount: 49.99,
|
|
28
|
+
fixedPriceAmountExact: '49.99',
|
|
26
29
|
fixedPriceCurrencyCode: 'USD',
|
|
27
30
|
fixedPriceIncludesTax: true,
|
|
28
31
|
fixedPriceOriginalAmount: 69.99,
|
|
32
|
+
fixedPriceOriginalAmountExact: '69.99',
|
|
29
33
|
customAmountMin: null,
|
|
34
|
+
customAmountMinExact: null,
|
|
30
35
|
customAmountMax: null,
|
|
36
|
+
customAmountMaxExact: null,
|
|
31
37
|
customAmountCurrencyCode: null,
|
|
32
38
|
priceListItems: [],
|
|
33
39
|
gatewayProviderKey: 'mock',
|
|
@@ -119,4 +125,143 @@ describe('template link sync helpers', () => {
|
|
|
119
125
|
removed: null,
|
|
120
126
|
})
|
|
121
127
|
})
|
|
128
|
+
|
|
129
|
+
it('propagates changed template amounts together with their exact values', () => {
|
|
130
|
+
const before = createTemplateSnapshot({
|
|
131
|
+
fixedPriceAmount: 10,
|
|
132
|
+
fixedPriceAmountExact: '10',
|
|
133
|
+
fixedPriceOriginalAmount: 15,
|
|
134
|
+
fixedPriceOriginalAmountExact: '15',
|
|
135
|
+
})
|
|
136
|
+
const after = createTemplateSnapshot({
|
|
137
|
+
fixedPriceAmount: 20,
|
|
138
|
+
fixedPriceAmountExact: '20',
|
|
139
|
+
fixedPriceOriginalAmount: 25,
|
|
140
|
+
fixedPriceOriginalAmountExact: '25',
|
|
141
|
+
})
|
|
142
|
+
const link = createLinkSnapshot({
|
|
143
|
+
fixedPriceAmount: 10,
|
|
144
|
+
fixedPriceAmountExact: '10',
|
|
145
|
+
fixedPriceOriginalAmount: 30,
|
|
146
|
+
fixedPriceOriginalAmountExact: '30',
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
const result = buildSelectiveLinkedLinkSnapshot(link, before, after)
|
|
150
|
+
|
|
151
|
+
expect(result.changed).toBe(true)
|
|
152
|
+
expect(result.snapshot.fixedPriceAmount).toBe(20)
|
|
153
|
+
expect(result.snapshot.fixedPriceAmountExact).toBe('20')
|
|
154
|
+
expect(result.snapshot.fixedPriceOriginalAmount).toBe(30)
|
|
155
|
+
expect(result.snapshot.fixedPriceOriginalAmountExact).toBe('30')
|
|
156
|
+
|
|
157
|
+
const target = {} as CheckoutLink
|
|
158
|
+
restoreLinkFromSnapshot(target, result.snapshot)
|
|
159
|
+
expect(target.fixedPriceAmount).toBe('20')
|
|
160
|
+
expect(target.fixedPriceOriginalAmount).toBe('30')
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('compares template amounts on their exact values beyond float precision', () => {
|
|
164
|
+
const before = createTemplateSnapshot({
|
|
165
|
+
pricingMode: 'custom_amount',
|
|
166
|
+
customAmountMin: Number('1.123456789012345678'),
|
|
167
|
+
customAmountMinExact: '1.123456789012345678',
|
|
168
|
+
customAmountMax: Number('9.999999999999999998'),
|
|
169
|
+
customAmountMaxExact: '9.999999999999999998',
|
|
170
|
+
})
|
|
171
|
+
const after = createTemplateSnapshot({
|
|
172
|
+
pricingMode: 'custom_amount',
|
|
173
|
+
customAmountMin: Number('1.123456789012345679'),
|
|
174
|
+
customAmountMinExact: '1.123456789012345679',
|
|
175
|
+
customAmountMax: Number('9.999999999999999999'),
|
|
176
|
+
customAmountMaxExact: '9.999999999999999999',
|
|
177
|
+
})
|
|
178
|
+
const link = createLinkSnapshot({
|
|
179
|
+
pricingMode: 'custom_amount',
|
|
180
|
+
customAmountMin: Number('1.123456789012345678'),
|
|
181
|
+
customAmountMinExact: '1.123456789012345678',
|
|
182
|
+
customAmountMax: Number('9.999999999999999997'),
|
|
183
|
+
customAmountMaxExact: '9.999999999999999997',
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
const result = buildSelectiveLinkedLinkSnapshot(link, before, after)
|
|
187
|
+
|
|
188
|
+
expect(result.changed).toBe(true)
|
|
189
|
+
expect(result.snapshot.customAmountMinExact).toBe('1.123456789012345679')
|
|
190
|
+
expect(result.snapshot.customAmountMaxExact).toBe('9.999999999999999997')
|
|
191
|
+
|
|
192
|
+
const target = {} as CheckoutLink
|
|
193
|
+
restoreLinkFromSnapshot(target, result.snapshot)
|
|
194
|
+
expect(target.customAmountMin).toBe('1.123456789012345679')
|
|
195
|
+
expect(target.customAmountMax).toBe('9.999999999999999997')
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('reports no change when only float-equal amounts match on their exact values', () => {
|
|
199
|
+
const template = createTemplateSnapshot({
|
|
200
|
+
fixedPriceAmount: Number('1.123456789012345678'),
|
|
201
|
+
fixedPriceAmountExact: '1.123456789012345678',
|
|
202
|
+
})
|
|
203
|
+
const link = createLinkSnapshot({
|
|
204
|
+
fixedPriceAmount: Number('1.123456789012345678'),
|
|
205
|
+
fixedPriceAmountExact: '1.123456789012345678',
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
const result = buildSelectiveLinkedLinkSnapshot(link, template, createTemplateSnapshot({
|
|
209
|
+
fixedPriceAmount: Number('1.123456789012345678'),
|
|
210
|
+
fixedPriceAmountExact: '1.1234567890123456780',
|
|
211
|
+
}))
|
|
212
|
+
|
|
213
|
+
expect(result.changed).toBe(false)
|
|
214
|
+
expect(result.snapshot.fixedPriceAmountExact).toBe('1.123456789012345678')
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('still propagates amounts from legacy snapshots without exact values', () => {
|
|
218
|
+
const before = createTemplateSnapshot({ fixedPriceAmount: 10, fixedPriceAmountExact: undefined })
|
|
219
|
+
const after = createTemplateSnapshot({ fixedPriceAmount: 20, fixedPriceAmountExact: undefined })
|
|
220
|
+
const link = createLinkSnapshot({ fixedPriceAmount: 10, fixedPriceAmountExact: '10' })
|
|
221
|
+
|
|
222
|
+
const result = buildSelectiveLinkedLinkSnapshot(link, before, after)
|
|
223
|
+
|
|
224
|
+
expect(result.changed).toBe(true)
|
|
225
|
+
expect(result.snapshot.fixedPriceAmount).toBe(20)
|
|
226
|
+
expect(result.snapshot.fixedPriceAmountExact).toBe('20')
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('keeps syncing price-list items after the link is re-saved with exact amounts', () => {
|
|
230
|
+
const before = createTemplateSnapshot({
|
|
231
|
+
pricingMode: 'price_list',
|
|
232
|
+
priceListItems: [{ id: 'basic', description: 'Basic', amount: 10, currencyCode: 'USD' }],
|
|
233
|
+
})
|
|
234
|
+
const after = createTemplateSnapshot({
|
|
235
|
+
pricingMode: 'price_list',
|
|
236
|
+
priceListItems: [{ id: 'basic', description: 'Basic', amount: 12.5, amountExact: '12.5', currencyCode: 'USD' }],
|
|
237
|
+
})
|
|
238
|
+
const resavedLink = createLinkSnapshot({
|
|
239
|
+
pricingMode: 'price_list',
|
|
240
|
+
priceListItems: [{ id: 'basic', description: 'Basic', amount: 10, amountExact: '10.00', currencyCode: 'USD' }],
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
const result = buildSelectiveLinkedLinkSnapshot(resavedLink, before, after)
|
|
244
|
+
|
|
245
|
+
expect(result.changed).toBe(true)
|
|
246
|
+
expect(result.snapshot.priceListItems).toEqual(after.priceListItems)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
it('treats price-list items with a different exact amount as a link override', () => {
|
|
250
|
+
const before = createTemplateSnapshot({
|
|
251
|
+
pricingMode: 'price_list',
|
|
252
|
+
priceListItems: [{ id: 'eth', description: 'ETH', amount: 1, amountExact: '1.000000000000000001', currencyCode: 'ETH' }],
|
|
253
|
+
})
|
|
254
|
+
const after = createTemplateSnapshot({
|
|
255
|
+
pricingMode: 'price_list',
|
|
256
|
+
priceListItems: [{ id: 'eth', description: 'ETH', amount: 2, amountExact: '2', currencyCode: 'ETH' }],
|
|
257
|
+
})
|
|
258
|
+
const overriddenLink = createLinkSnapshot({
|
|
259
|
+
pricingMode: 'price_list',
|
|
260
|
+
priceListItems: [{ id: 'eth', description: 'ETH', amount: 1, amountExact: '1.000000000000000002', currencyCode: 'ETH' }],
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
const result = buildSelectiveLinkedLinkSnapshot(overriddenLink, before, after)
|
|
264
|
+
|
|
265
|
+
expect(result.snapshot.priceListItems).toEqual(overriddenLink.priceListItems)
|
|
266
|
+
})
|
|
122
267
|
})
|
|
@@ -28,10 +28,10 @@ import {
|
|
|
28
28
|
pickExplicitParsedOverrides,
|
|
29
29
|
resolveLoadedCheckoutCustomFields,
|
|
30
30
|
serializeTemplateOrLink,
|
|
31
|
-
toMoneyString,
|
|
32
31
|
toTemplateOrLinkMutationInput,
|
|
33
32
|
validateDescriptorCurrencies,
|
|
34
33
|
} from '../lib/utils'
|
|
34
|
+
import { roundCheckoutConfiguredAmounts, toStoredCheckoutAmounts } from '../lib/amountPrecision'
|
|
35
35
|
import {
|
|
36
36
|
captureLinkSnapshot,
|
|
37
37
|
createLinkFromSnapshot,
|
|
@@ -112,17 +112,15 @@ const createLinkCommand: CommandHandler<Record<string, unknown>, { id: string; s
|
|
|
112
112
|
sourceValues.slug ?? null,
|
|
113
113
|
sourceValues.title ?? sourceValues.name,
|
|
114
114
|
)
|
|
115
|
+
const roundedValues = await roundCheckoutConfiguredAmounts(ctx.container, scope, sourceValues)
|
|
115
116
|
const link = em.create(CheckoutLink, {
|
|
116
|
-
...
|
|
117
|
+
...roundedValues,
|
|
117
118
|
organizationId: scope.organizationId,
|
|
118
119
|
tenantId: scope.tenantId,
|
|
119
120
|
completionCount: 0,
|
|
120
121
|
activeReservationCount: 0,
|
|
121
122
|
isLocked: false,
|
|
122
|
-
|
|
123
|
-
fixedPriceOriginalAmount: toMoneyString(sourceValues.fixedPriceOriginalAmount),
|
|
124
|
-
customAmountMin: toMoneyString(sourceValues.customAmountMin),
|
|
125
|
-
customAmountMax: toMoneyString(sourceValues.customAmountMax),
|
|
123
|
+
...toStoredCheckoutAmounts(roundedValues),
|
|
126
124
|
slug,
|
|
127
125
|
passwordHash: await hashCheckoutPassword(sourceValues.password),
|
|
128
126
|
} as any)
|
|
@@ -313,12 +311,11 @@ const updateLinkCommand: CommandHandler<Record<string, unknown>, { ok: true; slu
|
|
|
313
311
|
: link.slug
|
|
314
312
|
const passwordHash = parsed.password !== undefined ? await hashCheckoutPassword(parsed.password) : link.passwordHash
|
|
315
313
|
const previousStatus = link.status
|
|
314
|
+
const roundedValues = await roundCheckoutConfiguredAmounts(ctx.container, scope, nextValues)
|
|
316
315
|
Object.assign(link, {
|
|
317
316
|
...parsed,
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
customAmountMin: parsed.customAmountMin !== undefined ? toMoneyString(parsed.customAmountMin) : link.customAmountMin,
|
|
321
|
-
customAmountMax: parsed.customAmountMax !== undefined ? toMoneyString(parsed.customAmountMax) : link.customAmountMax,
|
|
317
|
+
...toStoredCheckoutAmounts(roundedValues),
|
|
318
|
+
priceListItems: roundedValues.priceListItems,
|
|
322
319
|
slug,
|
|
323
320
|
passwordHash,
|
|
324
321
|
})
|