@open-mercato/gateway-tpay 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.
|
@@ -6,9 +6,9 @@ import {
|
|
|
6
6
|
TpayNotificationError
|
|
7
7
|
} from "./notification-form.js";
|
|
8
8
|
import { resolveTpayEnvironment } from "./tpay-client.js";
|
|
9
|
+
import { countDecimalPlaces, decimalToNumber, multiplyDecimals, parseDecimal } from "@open-mercato/shared/lib/decimal";
|
|
9
10
|
const TPAY_JWS_HEADER = "x-jws-signature";
|
|
10
11
|
const TPAY_WEBHOOK_MAX_BODY_BYTES = 64 * 1024;
|
|
11
|
-
const STORED_AMOUNT_PATTERN = /^(\d+)(?:\.(\d{1,4}))?$/;
|
|
12
12
|
function readSecurityCode(credentials) {
|
|
13
13
|
const value = credentials.notificationSecurityCode;
|
|
14
14
|
if (typeof value !== "string" || value.trim().length === 0) {
|
|
@@ -22,11 +22,9 @@ function readSignatureHeader(headers) {
|
|
|
22
22
|
return value;
|
|
23
23
|
}
|
|
24
24
|
function storedAmountToGrosze(amount) {
|
|
25
|
-
const
|
|
26
|
-
if (!
|
|
27
|
-
const
|
|
28
|
-
if (fraction.slice(2) !== "00") throw new TpayNotificationError("amountMismatch");
|
|
29
|
-
const grosze = Number(match[1]) * 100 + Number(fraction.slice(0, 2));
|
|
25
|
+
const exact = parseDecimal(amount.trim());
|
|
26
|
+
if (!exact || exact.lt(0) || countDecimalPlaces(exact) > 2) throw new TpayNotificationError("amountMismatch");
|
|
27
|
+
const grosze = decimalToNumber(multiplyDecimals(exact, 100));
|
|
30
28
|
if (!Number.isSafeInteger(grosze)) throw new TpayNotificationError("amountMismatch");
|
|
31
29
|
return grosze;
|
|
32
30
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/gateway_tpay/lib/webhook-handler.ts"],
|
|
4
|
-
"sourcesContent": ["import type {\n VerifyWebhookInput,\n WebhookEvent,\n WebhookLocatorContext,\n} from '@open-mercato/shared/modules/payment_gateways/types'\nimport { computeTpayMd5, verifyTpayMd5 } from './checksum'\nimport { verifyTpayJws } from './jws'\nimport {\n parseTpayNotificationForm,\n TPAY_CRC_PATTERN,\n TpayNotificationError,\n type TpayNotification,\n} from './notification-form'\nimport { resolveTpayEnvironment } from './tpay-client'\n\nexport const TPAY_JWS_HEADER = 'x-jws-signature'\nexport const TPAY_WEBHOOK_MAX_BODY_BYTES = 64 * 1024\n\
|
|
5
|
-
"mappings": "AAKA,SAAS,gBAAgB,qBAAqB;AAC9C,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,8BAA8B;
|
|
4
|
+
"sourcesContent": ["import type {\n VerifyWebhookInput,\n WebhookEvent,\n WebhookLocatorContext,\n} from '@open-mercato/shared/modules/payment_gateways/types'\nimport { computeTpayMd5, verifyTpayMd5 } from './checksum'\nimport { verifyTpayJws } from './jws'\nimport {\n parseTpayNotificationForm,\n TPAY_CRC_PATTERN,\n TpayNotificationError,\n type TpayNotification,\n} from './notification-form'\nimport { resolveTpayEnvironment } from './tpay-client'\nimport { countDecimalPlaces, decimalToNumber, multiplyDecimals, parseDecimal } from '@open-mercato/shared/lib/decimal'\n\nexport const TPAY_JWS_HEADER = 'x-jws-signature'\nexport const TPAY_WEBHOOK_MAX_BODY_BYTES = 64 * 1024\n\nexport type TpayNotificationVerifyOptions = {\n now?: () => Date\n}\n\nfunction readSecurityCode(credentials: Record<string, unknown>): string {\n const value = credentials.notificationSecurityCode\n if (typeof value !== 'string' || value.trim().length === 0) {\n throw new TpayNotificationError('missingSecurityCode')\n }\n return value.trim()\n}\n\nfunction readSignatureHeader(headers: VerifyWebhookInput['headers']): string {\n const value = headers[TPAY_JWS_HEADER]\n if (typeof value !== 'string' || value.trim().length === 0) throw new TpayNotificationError('missingSignature')\n return value\n}\n\nfunction storedAmountToGrosze(amount: string): number {\n const exact = parseDecimal(amount.trim())\n if (!exact || exact.lt(0) || countDecimalPlaces(exact) > 2) throw new TpayNotificationError('amountMismatch')\n const grosze = decimalToNumber(multiplyDecimals(exact, 100))\n if (!Number.isSafeInteger(grosze)) throw new TpayNotificationError('amountMismatch')\n return grosze\n}\n\nfunction assertAmounts(notification: TpayNotification, candidateAmount: string): number {\n if (notification.trAmountGrosze !== storedAmountToGrosze(candidateAmount)) {\n throw new TpayNotificationError('amountMismatch')\n }\n if (notification.trStatus !== 'true') return 0\n if (notification.trPaidGrosze < notification.trAmountGrosze) throw new TpayNotificationError('underpaid')\n return notification.trPaidGrosze - notification.trAmountGrosze\n}\n\nexport async function verifyTpayNotification(\n input: VerifyWebhookInput,\n options: TpayNotificationVerifyOptions = {},\n): Promise<WebhookEvent> {\n if (!Buffer.isBuffer(input.rawBody)) throw new TpayNotificationError('unsupportedBody')\n const rawBody = input.rawBody\n const notification = parseTpayNotificationForm(rawBody)\n const environment = resolveTpayEnvironment(input.credentials.environment ?? 'sandbox')\n const securityCode = readSecurityCode(input.credentials)\n const candidate = input.candidate\n if (!candidate) throw new TpayNotificationError('missingCandidate')\n if (candidate.paymentId.toLowerCase() !== notification.trCrc.toLowerCase()) {\n throw new TpayNotificationError('paymentMismatch')\n }\n\n const expectedMd5 = computeTpayMd5({\n id: notification.id,\n trId: notification.trId,\n trAmount: notification.trAmount,\n trCrc: notification.trCrc,\n securityCode,\n })\n if (!verifyTpayMd5(expectedMd5, notification.md5sum)) throw new TpayNotificationError('checksumMismatch')\n\n await verifyTpayJws({ header: readSignatureHeader(input.headers), rawBody, environment })\n\n if (candidate.currencyCode.trim().toUpperCase() !== 'PLN') throw new TpayNotificationError('currencyMismatch')\n const overpaidByGrosze = assertAmounts(notification, candidate.amount)\n\n const settled = notification.trStatus === 'true'\n const identity = `${notification.trId}:${notification.trStatus}:${notification.trDate}`\n const data: Record<string, unknown> = {\n status: settled ? 'correct' : 'refund',\n title: notification.trId,\n trDate: notification.trDate,\n paid: notification.trPaid,\n amount: notification.trAmount,\n testMode: notification.testMode === '1',\n }\n if (overpaidByGrosze > 0) data.overpaidByGrosze = overpaidByGrosze\n\n return {\n eventType: settled ? 'tpay.transaction.settled' : 'tpay.transaction.chargeback',\n eventId: identity,\n idempotencyKey: `tpay:transaction:${identity}`,\n data,\n timestamp: (options.now ?? (() => new Date()))(),\n }\n}\n\nexport function readTpayPaymentIdHint(\n _payload: Record<string, unknown> | null,\n context?: WebhookLocatorContext,\n): string | null {\n try {\n const rawBody = context?.rawBody\n if (rawBody === undefined) return null\n const text = Buffer.isBuffer(rawBody)\n ? new TextDecoder('utf-8', { fatal: true }).decode(rawBody)\n : rawBody\n const values = new URLSearchParams(text).getAll('tr_crc')\n if (values.length !== 1 || !TPAY_CRC_PATTERN.test(values[0])) return null\n return values[0]\n } catch {\n return null\n }\n}\n"],
|
|
5
|
+
"mappings": "AAKA,SAAS,gBAAgB,qBAAqB;AAC9C,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,8BAA8B;AACvC,SAAS,oBAAoB,iBAAiB,kBAAkB,oBAAoB;AAE7E,MAAM,kBAAkB;AACxB,MAAM,8BAA8B,KAAK;AAMhD,SAAS,iBAAiB,aAA8C;AACtE,QAAM,QAAQ,YAAY;AAC1B,MAAI,OAAO,UAAU,YAAY,MAAM,KAAK,EAAE,WAAW,GAAG;AAC1D,UAAM,IAAI,sBAAsB,qBAAqB;AAAA,EACvD;AACA,SAAO,MAAM,KAAK;AACpB;AAEA,SAAS,oBAAoB,SAAgD;AAC3E,QAAM,QAAQ,QAAQ,eAAe;AACrC,MAAI,OAAO,UAAU,YAAY,MAAM,KAAK,EAAE,WAAW,EAAG,OAAM,IAAI,sBAAsB,kBAAkB;AAC9G,SAAO;AACT;AAEA,SAAS,qBAAqB,QAAwB;AACpD,QAAM,QAAQ,a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tpay",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -62,21 +62,21 @@
|
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@open-mercato/core": "0.10.1-develop.
|
|
65
|
+
"@open-mercato/core": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
66
66
|
"zod": "^4.6.6"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"@mikro-orm/postgresql": "^7.0.14",
|
|
70
|
-
"@open-mercato/ai-assistant": "0.10.1-develop.
|
|
71
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
72
|
-
"@open-mercato/ui": "0.10.1-develop.
|
|
70
|
+
"@open-mercato/ai-assistant": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
71
|
+
"@open-mercato/shared": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
72
|
+
"@open-mercato/ui": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
73
73
|
"react": "^19.0.0",
|
|
74
74
|
"react-dom": "^19.0.0"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@open-mercato/ai-assistant": "0.10.1-develop.
|
|
78
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
79
|
-
"@open-mercato/ui": "0.10.1-develop.
|
|
77
|
+
"@open-mercato/ai-assistant": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
78
|
+
"@open-mercato/shared": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
79
|
+
"@open-mercato/ui": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
80
80
|
"@types/jest": "^30.0.0",
|
|
81
81
|
"esbuild": "^0.28.2",
|
|
82
82
|
"glob": "^14.0.6",
|
|
@@ -185,13 +185,13 @@ describe('verifyTpayNotification', () => {
|
|
|
185
185
|
await expect(verify(input({ candidate: candidate({ paymentId: CRC.toUpperCase() }) }))).resolves.toBeDefined()
|
|
186
186
|
})
|
|
187
187
|
|
|
188
|
-
it.each(['12.35', '12.3450', 'abc', '12.34567'])('rejects a stored amount mismatch (%s)', async (amount) => {
|
|
188
|
+
it.each(['12.35', '12.3450', 'abc', '12.34567', '12.340000000000000001', '-12.34'])('rejects a stored amount mismatch (%s)', async (amount) => {
|
|
189
189
|
await expect(verify(input({ candidate: candidate({ amount }) }))).rejects.toMatchObject({
|
|
190
190
|
reason: 'amountMismatch',
|
|
191
191
|
})
|
|
192
192
|
})
|
|
193
193
|
|
|
194
|
-
it.each(['12.34', '12.340', '12.3400'])('accepts the equivalent stored amount %s', async (amount) => {
|
|
194
|
+
it.each(['12.34', '12.340', '12.3400', '12.340000000000000000'])('accepts the equivalent stored amount %s', async (amount) => {
|
|
195
195
|
await expect(verify(input({ candidate: candidate({ amount }) }))).resolves.toBeDefined()
|
|
196
196
|
})
|
|
197
197
|
|
|
@@ -12,12 +12,11 @@ import {
|
|
|
12
12
|
type TpayNotification,
|
|
13
13
|
} from './notification-form'
|
|
14
14
|
import { resolveTpayEnvironment } from './tpay-client'
|
|
15
|
+
import { countDecimalPlaces, decimalToNumber, multiplyDecimals, parseDecimal } from '@open-mercato/shared/lib/decimal'
|
|
15
16
|
|
|
16
17
|
export const TPAY_JWS_HEADER = 'x-jws-signature'
|
|
17
18
|
export const TPAY_WEBHOOK_MAX_BODY_BYTES = 64 * 1024
|
|
18
19
|
|
|
19
|
-
const STORED_AMOUNT_PATTERN = /^(\d+)(?:\.(\d{1,4}))?$/
|
|
20
|
-
|
|
21
20
|
export type TpayNotificationVerifyOptions = {
|
|
22
21
|
now?: () => Date
|
|
23
22
|
}
|
|
@@ -37,11 +36,9 @@ function readSignatureHeader(headers: VerifyWebhookInput['headers']): string {
|
|
|
37
36
|
}
|
|
38
37
|
|
|
39
38
|
function storedAmountToGrosze(amount: string): number {
|
|
40
|
-
const
|
|
41
|
-
if (!
|
|
42
|
-
const
|
|
43
|
-
if (fraction.slice(2) !== '00') throw new TpayNotificationError('amountMismatch')
|
|
44
|
-
const grosze = Number(match[1]) * 100 + Number(fraction.slice(0, 2))
|
|
39
|
+
const exact = parseDecimal(amount.trim())
|
|
40
|
+
if (!exact || exact.lt(0) || countDecimalPlaces(exact) > 2) throw new TpayNotificationError('amountMismatch')
|
|
41
|
+
const grosze = decimalToNumber(multiplyDecimals(exact, 100))
|
|
45
42
|
if (!Number.isSafeInteger(grosze)) throw new TpayNotificationError('amountMismatch')
|
|
46
43
|
return grosze
|
|
47
44
|
}
|