@vesant-sdk/transaction 0.1.1-next.f504af3 → 0.1.2-dev.0520b46
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/dist/index.d.mts +40 -6
- package/dist/index.d.ts +40 -6
- package/dist/index.js +25 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseClient, RequestOptions, BaseClientConfig, VesantError } from '@vesant-sdk/core';
|
|
1
|
+
import { BaseClient, RequestOptions, BaseClientConfig, VesantError, WebhookDedupStore } from '@vesant-sdk/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* TypeScript types for the Transaction Monitoring (TM) Service
|
|
@@ -15,15 +15,24 @@ interface TransactionCreateDTO {
|
|
|
15
15
|
transaction_mode: TransactionMode;
|
|
16
16
|
sub_type?: TransactionSubType;
|
|
17
17
|
amount: string;
|
|
18
|
-
|
|
18
|
+
/** Defaults server-side to `amount` when omitted and `currency` is USD. */
|
|
19
|
+
amount_usd?: number;
|
|
19
20
|
currency: string;
|
|
21
|
+
status?: TransactionStatus;
|
|
20
22
|
source_account?: string;
|
|
21
23
|
destination_account?: string;
|
|
22
24
|
country?: string;
|
|
23
25
|
ip_address?: string;
|
|
26
|
+
/** Spelling matches the backend's JSON tag — `beneficiary_comment` is ignored by the API. */
|
|
24
27
|
benificiary_comment?: string;
|
|
25
28
|
metadata?: Record<string, unknown>;
|
|
26
29
|
transaction_date: string;
|
|
30
|
+
/** Device fingerprint ID from the Sift beacon. */
|
|
31
|
+
device_id?: string;
|
|
32
|
+
/** Session ID for session-level fraud signals. */
|
|
33
|
+
session_id?: string;
|
|
34
|
+
/** Raw User-Agent string forwarded to Sift for device classification. Falls back to the request's own header. */
|
|
35
|
+
user_agent?: string;
|
|
27
36
|
}
|
|
28
37
|
type WithholdingType = "none" | "backup_us" | "backup_non_us" | "treaty";
|
|
29
38
|
/** Reason backup withholding or treaty rate was applied. */
|
|
@@ -84,7 +93,7 @@ interface TransactionClientConfig {
|
|
|
84
93
|
environment?: 'production' | 'sandbox';
|
|
85
94
|
}
|
|
86
95
|
/**
|
|
87
|
-
* Response from POST /api/v1/
|
|
96
|
+
* Response from POST /api/v1/transactions.
|
|
88
97
|
*
|
|
89
98
|
* Status → outcome mapping (withdrawal transactions only):
|
|
90
99
|
* "pending" — Vesant accepted; trigger the payment gateway (full release,
|
|
@@ -216,7 +225,7 @@ interface CustomerTaxProfileRecord {
|
|
|
216
225
|
created_at: string;
|
|
217
226
|
updated_at: string;
|
|
218
227
|
}
|
|
219
|
-
type TaxFormTrigger = 'trigger_account_creation' | 'trigger_first_withdrawal' | 'trigger_threshold' | 'trigger_manual' | 'trigger_tin_invalid' | 'trigger_w8ben_expiry' | 'trigger_tin_expired';
|
|
228
|
+
type TaxFormTrigger = 'trigger_account_creation' | 'trigger_first_withdrawal' | 'trigger_threshold' | 'trigger_manual' | 'trigger_self_request' | 'trigger_tin_invalid' | 'trigger_w8ben_expiry' | 'trigger_tin_expired';
|
|
220
229
|
interface CustomerTaxProfileDocuments {
|
|
221
230
|
w_form: string;
|
|
222
231
|
'1099_form': string;
|
|
@@ -236,8 +245,19 @@ interface RequestTaxFormWithProfileInput {
|
|
|
236
245
|
address?: string;
|
|
237
246
|
ip_address?: string;
|
|
238
247
|
form_type?: string;
|
|
248
|
+
/** Required only when the customer's TIN is already `verified` and this is a re-request. */
|
|
249
|
+
reason?: string;
|
|
239
250
|
}
|
|
240
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Response shape depends on the tenant's `email_sending_method`:
|
|
253
|
+
* - `tenant_email`: form metadata (`form_id`, `public_url`, `reference_id`, `request_id`)
|
|
254
|
+
* plus the nested `profile`.
|
|
255
|
+
* - `avalara_direct`: Avalara emails the customer directly, so there's no `public_url` —
|
|
256
|
+
* the response body *is* the bare `CustomerTaxProfileRecord`, not wrapped under `profile`.
|
|
257
|
+
* Callers must branch on `email_sending_method` (or check for `public_url`) rather than
|
|
258
|
+
* assuming one shape.
|
|
259
|
+
*/
|
|
260
|
+
interface RequestTaxFormWithProfileOutput extends Partial<CustomerTaxProfileRecord> {
|
|
241
261
|
profile?: CustomerTaxProfileRecord;
|
|
242
262
|
request_id?: string;
|
|
243
263
|
reference_id?: string;
|
|
@@ -247,6 +267,11 @@ interface RequestTaxFormWithProfileOutput {
|
|
|
247
267
|
|
|
248
268
|
declare class TaxTransactionClient extends BaseClient {
|
|
249
269
|
getCustomerDocuments(customerID: string): Promise<CustomerTaxProfileDocuments>;
|
|
270
|
+
/**
|
|
271
|
+
* A disabled trigger (e.g. `trigger_manual` off in tenant rules) returns 200 with
|
|
272
|
+
* the customer's unchanged profile and no form sent — check `public_url` presence
|
|
273
|
+
* or compare `form_status`/`tin_status` rather than trusting a 200 alone.
|
|
274
|
+
*/
|
|
250
275
|
requestTaxFormWithProfile(input: RequestTaxFormWithProfileInput, requestOptions?: RequestOptions): Promise<RequestTaxFormWithProfileOutput>;
|
|
251
276
|
}
|
|
252
277
|
|
|
@@ -257,6 +282,13 @@ declare class TaxTransactionClient extends BaseClient {
|
|
|
257
282
|
declare class TransactionClient extends BaseClient {
|
|
258
283
|
readonly tax: TaxTransactionClient;
|
|
259
284
|
constructor(config: BaseClientConfig);
|
|
285
|
+
/**
|
|
286
|
+
* Submit a transaction via the unified ingestion endpoint.
|
|
287
|
+
*
|
|
288
|
+
* POST /api/v1/transactions is gated on ANY of transaction monitoring, tax,
|
|
289
|
+
* or fraud being enabled for the tenant, so fraud-only and tax-only tenants
|
|
290
|
+
* can ingest even with TM switched off.
|
|
291
|
+
*/
|
|
260
292
|
createTransaction(request: TransactionCreateDTO, requestOptions?: RequestOptions): Promise<TransactionCreateResponse>;
|
|
261
293
|
submitWithdrawal(request: TransactionCreateDTO, requestOptions?: RequestOptions): Promise<PendingWithdrawal>;
|
|
262
294
|
getTransaction(transactionId: string, requestOptions?: RequestOptions): Promise<Transaction>;
|
|
@@ -282,13 +314,15 @@ interface TransactionWebhookHandlerConfig {
|
|
|
282
314
|
replayProtection?: boolean;
|
|
283
315
|
/** How long (ms) to remember seen events for replay detection. Default: 300_000 (5 min). */
|
|
284
316
|
replayWindow?: number;
|
|
317
|
+
/** Pluggable dedup store for replay protection (default: in-memory, process-local). */
|
|
318
|
+
dedupStore?: WebhookDedupStore;
|
|
285
319
|
}
|
|
286
320
|
declare class TransactionWebhookHandler {
|
|
287
321
|
private readonly handlers;
|
|
288
322
|
private readonly secret;
|
|
289
323
|
private readonly replayProtection;
|
|
290
324
|
private readonly replayWindow;
|
|
291
|
-
private readonly
|
|
325
|
+
private readonly dedupStore;
|
|
292
326
|
private readonly pendingWithdrawals;
|
|
293
327
|
private callbackHandler;
|
|
294
328
|
constructor(config: TransactionWebhookHandlerConfig);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseClient, RequestOptions, BaseClientConfig, VesantError } from '@vesant-sdk/core';
|
|
1
|
+
import { BaseClient, RequestOptions, BaseClientConfig, VesantError, WebhookDedupStore } from '@vesant-sdk/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* TypeScript types for the Transaction Monitoring (TM) Service
|
|
@@ -15,15 +15,24 @@ interface TransactionCreateDTO {
|
|
|
15
15
|
transaction_mode: TransactionMode;
|
|
16
16
|
sub_type?: TransactionSubType;
|
|
17
17
|
amount: string;
|
|
18
|
-
|
|
18
|
+
/** Defaults server-side to `amount` when omitted and `currency` is USD. */
|
|
19
|
+
amount_usd?: number;
|
|
19
20
|
currency: string;
|
|
21
|
+
status?: TransactionStatus;
|
|
20
22
|
source_account?: string;
|
|
21
23
|
destination_account?: string;
|
|
22
24
|
country?: string;
|
|
23
25
|
ip_address?: string;
|
|
26
|
+
/** Spelling matches the backend's JSON tag — `beneficiary_comment` is ignored by the API. */
|
|
24
27
|
benificiary_comment?: string;
|
|
25
28
|
metadata?: Record<string, unknown>;
|
|
26
29
|
transaction_date: string;
|
|
30
|
+
/** Device fingerprint ID from the Sift beacon. */
|
|
31
|
+
device_id?: string;
|
|
32
|
+
/** Session ID for session-level fraud signals. */
|
|
33
|
+
session_id?: string;
|
|
34
|
+
/** Raw User-Agent string forwarded to Sift for device classification. Falls back to the request's own header. */
|
|
35
|
+
user_agent?: string;
|
|
27
36
|
}
|
|
28
37
|
type WithholdingType = "none" | "backup_us" | "backup_non_us" | "treaty";
|
|
29
38
|
/** Reason backup withholding or treaty rate was applied. */
|
|
@@ -84,7 +93,7 @@ interface TransactionClientConfig {
|
|
|
84
93
|
environment?: 'production' | 'sandbox';
|
|
85
94
|
}
|
|
86
95
|
/**
|
|
87
|
-
* Response from POST /api/v1/
|
|
96
|
+
* Response from POST /api/v1/transactions.
|
|
88
97
|
*
|
|
89
98
|
* Status → outcome mapping (withdrawal transactions only):
|
|
90
99
|
* "pending" — Vesant accepted; trigger the payment gateway (full release,
|
|
@@ -216,7 +225,7 @@ interface CustomerTaxProfileRecord {
|
|
|
216
225
|
created_at: string;
|
|
217
226
|
updated_at: string;
|
|
218
227
|
}
|
|
219
|
-
type TaxFormTrigger = 'trigger_account_creation' | 'trigger_first_withdrawal' | 'trigger_threshold' | 'trigger_manual' | 'trigger_tin_invalid' | 'trigger_w8ben_expiry' | 'trigger_tin_expired';
|
|
228
|
+
type TaxFormTrigger = 'trigger_account_creation' | 'trigger_first_withdrawal' | 'trigger_threshold' | 'trigger_manual' | 'trigger_self_request' | 'trigger_tin_invalid' | 'trigger_w8ben_expiry' | 'trigger_tin_expired';
|
|
220
229
|
interface CustomerTaxProfileDocuments {
|
|
221
230
|
w_form: string;
|
|
222
231
|
'1099_form': string;
|
|
@@ -236,8 +245,19 @@ interface RequestTaxFormWithProfileInput {
|
|
|
236
245
|
address?: string;
|
|
237
246
|
ip_address?: string;
|
|
238
247
|
form_type?: string;
|
|
248
|
+
/** Required only when the customer's TIN is already `verified` and this is a re-request. */
|
|
249
|
+
reason?: string;
|
|
239
250
|
}
|
|
240
|
-
|
|
251
|
+
/**
|
|
252
|
+
* Response shape depends on the tenant's `email_sending_method`:
|
|
253
|
+
* - `tenant_email`: form metadata (`form_id`, `public_url`, `reference_id`, `request_id`)
|
|
254
|
+
* plus the nested `profile`.
|
|
255
|
+
* - `avalara_direct`: Avalara emails the customer directly, so there's no `public_url` —
|
|
256
|
+
* the response body *is* the bare `CustomerTaxProfileRecord`, not wrapped under `profile`.
|
|
257
|
+
* Callers must branch on `email_sending_method` (or check for `public_url`) rather than
|
|
258
|
+
* assuming one shape.
|
|
259
|
+
*/
|
|
260
|
+
interface RequestTaxFormWithProfileOutput extends Partial<CustomerTaxProfileRecord> {
|
|
241
261
|
profile?: CustomerTaxProfileRecord;
|
|
242
262
|
request_id?: string;
|
|
243
263
|
reference_id?: string;
|
|
@@ -247,6 +267,11 @@ interface RequestTaxFormWithProfileOutput {
|
|
|
247
267
|
|
|
248
268
|
declare class TaxTransactionClient extends BaseClient {
|
|
249
269
|
getCustomerDocuments(customerID: string): Promise<CustomerTaxProfileDocuments>;
|
|
270
|
+
/**
|
|
271
|
+
* A disabled trigger (e.g. `trigger_manual` off in tenant rules) returns 200 with
|
|
272
|
+
* the customer's unchanged profile and no form sent — check `public_url` presence
|
|
273
|
+
* or compare `form_status`/`tin_status` rather than trusting a 200 alone.
|
|
274
|
+
*/
|
|
250
275
|
requestTaxFormWithProfile(input: RequestTaxFormWithProfileInput, requestOptions?: RequestOptions): Promise<RequestTaxFormWithProfileOutput>;
|
|
251
276
|
}
|
|
252
277
|
|
|
@@ -257,6 +282,13 @@ declare class TaxTransactionClient extends BaseClient {
|
|
|
257
282
|
declare class TransactionClient extends BaseClient {
|
|
258
283
|
readonly tax: TaxTransactionClient;
|
|
259
284
|
constructor(config: BaseClientConfig);
|
|
285
|
+
/**
|
|
286
|
+
* Submit a transaction via the unified ingestion endpoint.
|
|
287
|
+
*
|
|
288
|
+
* POST /api/v1/transactions is gated on ANY of transaction monitoring, tax,
|
|
289
|
+
* or fraud being enabled for the tenant, so fraud-only and tax-only tenants
|
|
290
|
+
* can ingest even with TM switched off.
|
|
291
|
+
*/
|
|
260
292
|
createTransaction(request: TransactionCreateDTO, requestOptions?: RequestOptions): Promise<TransactionCreateResponse>;
|
|
261
293
|
submitWithdrawal(request: TransactionCreateDTO, requestOptions?: RequestOptions): Promise<PendingWithdrawal>;
|
|
262
294
|
getTransaction(transactionId: string, requestOptions?: RequestOptions): Promise<Transaction>;
|
|
@@ -282,13 +314,15 @@ interface TransactionWebhookHandlerConfig {
|
|
|
282
314
|
replayProtection?: boolean;
|
|
283
315
|
/** How long (ms) to remember seen events for replay detection. Default: 300_000 (5 min). */
|
|
284
316
|
replayWindow?: number;
|
|
317
|
+
/** Pluggable dedup store for replay protection (default: in-memory, process-local). */
|
|
318
|
+
dedupStore?: WebhookDedupStore;
|
|
285
319
|
}
|
|
286
320
|
declare class TransactionWebhookHandler {
|
|
287
321
|
private readonly handlers;
|
|
288
322
|
private readonly secret;
|
|
289
323
|
private readonly replayProtection;
|
|
290
324
|
private readonly replayWindow;
|
|
291
|
-
private readonly
|
|
325
|
+
private readonly dedupStore;
|
|
292
326
|
private readonly pendingWithdrawals;
|
|
293
327
|
private callbackHandler;
|
|
294
328
|
constructor(config: TransactionWebhookHandlerConfig);
|
package/dist/index.js
CHANGED
|
@@ -95,6 +95,11 @@ var TaxTransactionClient = class extends core.BaseClient {
|
|
|
95
95
|
`/api/v1/tm/customer-tax-profiles/${customerID}/documents`
|
|
96
96
|
);
|
|
97
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* A disabled trigger (e.g. `trigger_manual` off in tenant rules) returns 200 with
|
|
100
|
+
* the customer's unchanged profile and no form sent — check `public_url` presence
|
|
101
|
+
* or compare `form_status`/`tin_status` rather than trusting a 200 alone.
|
|
102
|
+
*/
|
|
98
103
|
async requestTaxFormWithProfile(input, requestOptions) {
|
|
99
104
|
return this.requestWithRetry(
|
|
100
105
|
"/api/v1/tax/customer-tax-profiles/request-form-with-profile",
|
|
@@ -110,14 +115,30 @@ var TaxTransactionClient = class extends core.BaseClient {
|
|
|
110
115
|
};
|
|
111
116
|
|
|
112
117
|
// src/client.ts
|
|
118
|
+
function assertWellFormedCurrency(currency) {
|
|
119
|
+
if (!/^[A-Za-z]{3}$/.test(currency ?? "")) {
|
|
120
|
+
throw new core.ValidationError(
|
|
121
|
+
`Invalid currency code: ${JSON.stringify(currency)}. Expected a 3-letter ISO 4217 code.`,
|
|
122
|
+
["currency"]
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
113
126
|
var TransactionClient = class extends core.BaseClient {
|
|
114
127
|
constructor(config) {
|
|
115
128
|
super(config);
|
|
116
129
|
this.tax = new TaxTransactionClient(config);
|
|
117
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Submit a transaction via the unified ingestion endpoint.
|
|
133
|
+
*
|
|
134
|
+
* POST /api/v1/transactions is gated on ANY of transaction monitoring, tax,
|
|
135
|
+
* or fraud being enabled for the tenant, so fraud-only and tax-only tenants
|
|
136
|
+
* can ingest even with TM switched off.
|
|
137
|
+
*/
|
|
118
138
|
async createTransaction(request, requestOptions) {
|
|
139
|
+
assertWellFormedCurrency(request.currency);
|
|
119
140
|
try {
|
|
120
|
-
return await this.requestWithRetry("/api/v1/
|
|
141
|
+
return await this.requestWithRetry("/api/v1/transactions", {
|
|
121
142
|
method: "POST",
|
|
122
143
|
body: JSON.stringify(request)
|
|
123
144
|
}, void 0, void 0, requestOptions);
|
|
@@ -154,12 +175,12 @@ var TransactionWebhookHandler = class {
|
|
|
154
175
|
"tm.transaction.callback": [],
|
|
155
176
|
"tm.transaction.held": []
|
|
156
177
|
};
|
|
157
|
-
this.seenEvents = /* @__PURE__ */ new Map();
|
|
158
178
|
this.pendingWithdrawals = /* @__PURE__ */ new Map();
|
|
159
179
|
this.callbackHandler = null;
|
|
160
180
|
this.secret = config.secret;
|
|
161
181
|
this.replayProtection = config.replayProtection ?? true;
|
|
162
182
|
this.replayWindow = config.replayWindow ?? 3e5;
|
|
183
|
+
this.dedupStore = config.dedupStore ?? new core.InMemoryDedupStore();
|
|
163
184
|
}
|
|
164
185
|
on(eventType, handler) {
|
|
165
186
|
this.handlers[eventType].push(handler);
|
|
@@ -204,21 +225,13 @@ var TransactionWebhookHandler = class {
|
|
|
204
225
|
const event = this.parse(body);
|
|
205
226
|
if (this.replayProtection) {
|
|
206
227
|
const key = `${event.event_type}:${event.tx_id}`;
|
|
207
|
-
if (this.
|
|
228
|
+
if (await this.dedupStore.seen(key)) {
|
|
208
229
|
throw new core.ValidationError(
|
|
209
230
|
`Duplicate webhook event: ${event.event_type} for ${event.tx_id} has already been processed`,
|
|
210
231
|
["tx_id"]
|
|
211
232
|
);
|
|
212
233
|
}
|
|
213
|
-
|
|
214
|
-
this.seenEvents.set(key, now);
|
|
215
|
-
if (this.seenEvents.size > 1e3) {
|
|
216
|
-
for (const [k, seenAt] of this.seenEvents) {
|
|
217
|
-
if (now - seenAt > this.replayWindow) {
|
|
218
|
-
this.seenEvents.delete(k);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
234
|
+
await this.dedupStore.mark(key, this.replayWindow);
|
|
222
235
|
}
|
|
223
236
|
return event;
|
|
224
237
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/pending-withdrawal.ts","../src/tax/client.ts","../src/client.ts","../src/webhook-handler.ts"],"names":["VesantError","BaseClient","verifyWebhookSignature","ValidationError"],"mappings":";;;;;AAGO,IAAM,yBAAA,GAAN,MAAM,0BAAA,SAAkCA,gBAAA,CAAY;AAAA,EAGzD,WAAA,CAAY,KAAA,EAAe,OAAA,GAAU,CAAA,uBAAA,EAA0B,KAAK,CAAA,eAAA,CAAA,EAAmB;AACrF,IAAA,KAAA,CAAM,OAAA,EAAS,yBAAyB,GAAG,CAAA;AAC3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AC/C,CAAA,MAAO;AACL,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,UAAA,GAAa,KAAK,CAAA;AAAA,QAC/C;AAAA,MACF;AAAA,IACF;AAEA,IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA;AAC/C,IAAA,KAAA,MAAW,WAAW,QAAA,EAAU;AAC9B,MAAA,MAAM,QAAQ,KAAK,CAAA;AAAA,IACrB;AAAA,EACF;AACF","file":"index.js","sourcesContent":["import { VesantError } from '@vesant-sdk/core';\nimport type { TransactionCallbackEvent, HoldReason } from './types';\n\nexport class DuplicateTransactionError extends VesantError {\n readonly tx_id: string;\n\n constructor(tx_id: string, message = `Duplicate transaction: ${tx_id} already exists`) {\n super(message, 'DUPLICATE_TRANSACTION', 409);\n this.name = 'DuplicateTransactionError';\n this.tx_id = tx_id;\n Object.setPrototypeOf(this, DuplicateTransactionError.prototype);\n }\n}\n\nexport class TaxHoldError extends Error {\n readonly tx_id: string;\n readonly hold_reason: HoldReason;\n readonly event: TransactionCallbackEvent;\n\n constructor(event: TransactionCallbackEvent) {\n super(`Transaction ${event.tx_id} held: ${event.hold_reason}`);\n this.name = 'TaxHoldError';\n this.tx_id = event.tx_id;\n this.hold_reason = event.hold_reason as HoldReason;\n this.event = event;\n Object.setPrototypeOf(this, TaxHoldError.prototype);\n }\n}\n","import { VesantError } from '@vesant-sdk/core';\nimport { TaxHoldError } from './errors';\nimport type { Transaction, TransactionCallbackEvent } from './types';\n\nexport type WithdrawalState = 'vesant_pending' | 'withheld' | 'suspend' | 'released' | 'failed';\n\nexport class PendingWithdrawal {\n private _state: WithdrawalState = 'vesant_pending';\n private _event: TransactionCallbackEvent | null = null;\n private _error: unknown = undefined;\n\n // Deferred promise — created lazily only when wait() is called.\n private _waitPromise: Promise<TransactionCallbackEvent> | null = null;\n private _resolveWait?: (event: TransactionCallbackEvent) => void;\n private _rejectWait?: (err: unknown) => void;\n\n readonly tx_id: string;\n readonly initialTransaction: Transaction;\n\n constructor(tx_id: string, transaction: Transaction) {\n this.tx_id = tx_id;\n this.initialTransaction = transaction;\n }\n\n status(): WithdrawalState {\n return this._state;\n }\n\n callbackEvent(): TransactionCallbackEvent | null {\n return this._event;\n }\n\n wait(signal?: AbortSignal): Promise<TransactionCallbackEvent> {\n if (this._state !== 'vesant_pending') {\n if (this._state === 'suspend' || this._state === 'failed') {\n return Promise.reject(this._error);\n }\n if (this._event !== null) {\n return Promise.resolve(this._event);\n }\n }\n\n if (!this._waitPromise) {\n this._waitPromise = new Promise<TransactionCallbackEvent>((resolve, reject) => {\n this._resolveWait = resolve;\n this._rejectWait = reject;\n });\n }\n\n if (signal) {\n if (signal.aborted) {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n } else {\n signal.addEventListener('abort', () => {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n }, { once: true });\n }\n }\n\n return this._waitPromise;\n }\n\n /** Called by TransactionWebhookHandler when the tm.transaction.callback arrives for this tx_id. */\n _settle(event: TransactionCallbackEvent): void {\n if (this._state !== 'vesant_pending') return;\n this._event = event;\n if (event.status === 'hold' || event.status === 'suspend') {\n this._state = 'suspend';\n this._error = new TaxHoldError(event);\n this._rejectWait?.(this._error);\n } else if (event.withholding_type === 'none') {\n this._state = 'released';\n this._resolveWait?.(event);\n } else {\n this._state = 'withheld';\n this._resolveWait?.(event);\n }\n }\n\n /** Called to fail the pending withdrawal with a generic error (e.g. abort, network loss). */\n _fail(err: unknown): void {\n if (this._state !== 'vesant_pending') return;\n this._state = 'failed';\n this._error = err;\n this._rejectWait?.(err);\n }\n}\n","import { BaseClient } from '@vesant-sdk/core';\nimport type { RequestOptions } from '@vesant-sdk/core';\nimport type { RequestTaxFormWithProfileInput, RequestTaxFormWithProfileOutput, CustomerTaxProfileDocuments } from './types';\n\nexport class TaxTransactionClient extends BaseClient {\n async getCustomerDocuments(customerID: string): Promise<CustomerTaxProfileDocuments> {\n return this.request<CustomerTaxProfileDocuments>(\n `/api/v1/tm/customer-tax-profiles/${customerID}/documents`\n );\n }\n\n async requestTaxFormWithProfile(\n input: RequestTaxFormWithProfileInput,\n requestOptions?: RequestOptions\n ): Promise<RequestTaxFormWithProfileOutput> {\n return this.requestWithRetry<RequestTaxFormWithProfileOutput>(\n '/api/v1/tax/customer-tax-profiles/request-form-with-profile',\n {\n method: 'POST',\n body: JSON.stringify(input),\n },\n undefined,\n undefined,\n requestOptions\n );\n }\n}\n","/**\n * TransactionClient — SDK for the Transaction Monitoring service\n */\nimport { BaseClient, VesantError } from '@vesant-sdk/core';\nimport type { BaseClientConfig, RequestOptions } from '@vesant-sdk/core';\nimport type { Transaction, TransactionCreateDTO, TransactionCreateResponse, TmTenantSettings } from './types';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport { TaxTransactionClient } from './tax/client';\n\nexport class TransactionClient extends BaseClient {\n readonly tax: TaxTransactionClient;\n\n constructor(config: BaseClientConfig) {\n super(config);\n this.tax = new TaxTransactionClient(config);\n }\n\n async createTransaction(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<TransactionCreateResponse> {\n try {\n return await this.requestWithRetry<TransactionCreateResponse>('/api/v1/tm/transactions', {\n method: 'POST',\n body: JSON.stringify(request),\n }, undefined, undefined, requestOptions);\n } catch (error) {\n if (error instanceof VesantError && error.statusCode === 409) {\n const transaction = await this.getTransaction(request.tx_id, requestOptions);\n return { transaction, message: 'Transaction already exists' };\n }\n throw error;\n }\n }\n\n async submitWithdrawal(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<PendingWithdrawal> {\n const response = await this.createTransaction(\n { ...request, transaction_type: 'withdrawal' },\n requestOptions\n );\n return new PendingWithdrawal(response.transaction.tx_id, response.transaction);\n }\n\n async getTransaction(transactionId: string, requestOptions?: RequestOptions): Promise<Transaction> {\n return this.requestWithRetry<Transaction>(`/api/v1/tm/transactions/status/${transactionId}`, {\n method: 'GET',\n }, undefined, undefined, requestOptions);\n }\n\n async getSettings(): Promise<TmTenantSettings> {\n return this.requestWithRetry<TmTenantSettings>('/api/v1/tm/settings', {\n method: 'GET',\n });\n }\n}\n","import { verifyWebhookSignature, ValidationError } from '@vesant-sdk/core';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport type {\n TransactionCallbackEvent,\n TransactionCallbackHandler,\n TransactionHeldEvent,\n TransactionWebhookEvent,\n} from './types';\n\ntype EventHandler<T> = (event: T) => void | Promise<void>;\n\ntype HandlerMap = {\n 'tm.transaction.callback': Array<EventHandler<TransactionCallbackEvent>>;\n 'tm.transaction.held': Array<EventHandler<TransactionHeldEvent>>;\n};\n\nexport const TRANSACTION_SIGNATURE_HEADER = 'x-webhook-signature';\n\nexport interface TransactionWebhookHandlerConfig {\n secret: string;\n /** Reject duplicate event_type+tx_id pairs within replayWindow. Default: true. */\n replayProtection?: boolean;\n /** How long (ms) to remember seen events for replay detection. Default: 300_000 (5 min). */\n replayWindow?: number;\n}\n\nexport class TransactionWebhookHandler {\n private readonly handlers: HandlerMap = {\n 'tm.transaction.callback': [],\n 'tm.transaction.held': [],\n };\n private readonly secret: string;\n private readonly replayProtection: boolean;\n private readonly replayWindow: number;\n private readonly seenEvents = new Map<string, number>();\n private readonly pendingWithdrawals = new Map<string, PendingWithdrawal>();\n private callbackHandler: TransactionCallbackHandler | null = null;\n\n constructor(config: TransactionWebhookHandlerConfig) {\n this.secret = config.secret;\n this.replayProtection = config.replayProtection ?? true;\n this.replayWindow = config.replayWindow ?? 300_000;\n }\n\n on(eventType: 'tm.transaction.callback', handler: EventHandler<TransactionCallbackEvent>): this;\n on(eventType: 'tm.transaction.held', handler: EventHandler<TransactionHeldEvent>): this;\n on(eventType: keyof HandlerMap, handler: EventHandler<TransactionCallbackEvent> | EventHandler<TransactionHeldEvent>): this {\n (this.handlers[eventType] as Array<typeof handler>).push(handler);\n return this;\n }\n\n /** Register a semantic callback handler for the three withholding outcomes. Replaces any previous handler. */\n registerCallbackHandler(handler: TransactionCallbackHandler): this {\n this.callbackHandler = handler;\n return this;\n }\n\n /** Track a PendingWithdrawal so it is auto-settled when its tm.transaction.callback arrives. */\n link(pending: PendingWithdrawal): this {\n this.pendingWithdrawals.set(pending.tx_id, pending);\n return this;\n }\n\n /** Stop tracking a PendingWithdrawal by tx_id. */\n unlink(tx_id: string): this {\n this.pendingWithdrawals.delete(tx_id);\n return this;\n }\n\n /** Verify HMAC-SHA256 signature only. Returns false instead of throwing. */\n async verify(rawBody: string, signature: string): Promise<boolean> {\n return verifyWebhookSignature(rawBody, signature, this.secret);\n }\n\n /** Parse and validate required fields. Does not verify signature — use verifyAndParse in production. */\n parse(body: string): TransactionWebhookEvent {\n const event = JSON.parse(body) as TransactionWebhookEvent;\n if (!event.event_type || !event.tx_id) {\n throw new ValidationError(\n 'Invalid TM webhook event: missing required fields (event_type, tx_id)',\n ['event_type', 'tx_id']\n );\n }\n return event;\n }\n\n /** Verify signature, parse, and check for replays. Throws ValidationError on any failure. */\n async verifyAndParse(body: string, signature: string): Promise<TransactionWebhookEvent> {\n const isValid = await verifyWebhookSignature(body, signature, this.secret);\n if (!isValid) {\n throw new ValidationError('Invalid webhook signature', ['signature']);\n }\n\n const event = this.parse(body);\n\n if (this.replayProtection) {\n const key = `${event.event_type}:${event.tx_id}`;\n if (this.seenEvents.has(key)) {\n throw new ValidationError(\n `Duplicate webhook event: ${event.event_type} for ${event.tx_id} has already been processed`,\n ['tx_id']\n );\n }\n const now = Date.now();\n this.seenEvents.set(key, now);\n if (this.seenEvents.size > 1000) {\n for (const [k, seenAt] of this.seenEvents) {\n if (now - seenAt > this.replayWindow) {\n this.seenEvents.delete(k);\n }\n }\n }\n }\n\n return event;\n }\n\n /** Verify signature, parse, and dispatch to registered handlers. */\n async handle(body: string, signature: string): Promise<void> {\n const event = await this.verifyAndParse(body, signature);\n\n if (event.event_type === 'tm.transaction.callback') {\n const pending = this.pendingWithdrawals.get(event.tx_id);\n if (pending) {\n pending._settle(event);\n this.pendingWithdrawals.delete(event.tx_id);\n }\n\n if (this.callbackHandler) {\n if (event.status === 'hold' || event.status === 'suspend') {\n await this.callbackHandler.onHeld?.(event);\n } else if (event.withholding_type === 'none') {\n await this.callbackHandler.onReleased?.(event);\n } else {\n await this.callbackHandler.onWithheld?.(event);\n }\n }\n }\n\n const handlers = this.handlers[event.event_type] as Array<EventHandler<typeof event>>;\n for (const handler of handlers) {\n await handler(event);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/pending-withdrawal.ts","../src/tax/client.ts","../src/client.ts","../src/webhook-handler.ts"],"names":["VesantError","BaseClient","ValidationError","InMemoryDedupStore","verifyWebhookSignature"],"mappings":";;;;;AAGO,IAAM,yBAAA,GAAN,MAAM,0BAAA,SAAkCA,gBAAA,CAAY;AAAA,EAGzD,WAAA,CAAY,KAAA,EAAe,OAAA,GAAU,CAAA,uBAAA,EAA0B,KAAK,CAAA,eAAA,CAAA,EAAmB;AACrF,IAAA,KAAA,CAAM,OAAA,EAAS,yBAAyB,GAAG,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAA,GAAO,2BAAA;AACZ,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,MAAA,CAAO,cAAA,CAAe,IAAA,EAAM,0BAAA,CAA0B,SAAS,CAAA;AAAA,EACjE;AACF;AAEO,IAAM,YAAA,GAAN,MAAM,aAAA,SAAqB,KAAA,CAAM;AAAA,EAKtC,YAAY,KAAA,EAAiC;AAC3C,IAAA,KAAA,CAAM,eAAe,KAAA,CAAM,KAAK,CAAA,OAAA,EAAU,KAAA,CAAM,WAAW,CAAA,CAAE,CAAA;AAC7D,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,QAAQ,KAAA,CAAM,KAAA;AACnB,IAAA,IAAA,CAAK,cAAc,KAAA,CAAM,WAAA;AACzB,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,MAAA,CAAO,cAAA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M,MAAA,CAAO,IAAA,EAAc,SAAA,EAAkC;AAC3D,IAAA,MAAM,KAAA,GAAQ,MAAM,IAAA,CAAK,cAAA,CAAe,MAAM,SAAS,CAAA;AAEvD,IAAA,IAAI,KAAA,CAAM,eAAe,yBAAA,EAA2B;AAClD,MAAA,MAAM,OAAA,GAAU,IAAA,CAAK,kBAAA,CAAmB,GAAA,CAAI,MAAM,KAAK,CAAA;AACvD,MAAA,IAAI,OAAA,EAAS;AACX,QAAA,OAAA,CAAQ,QAAQ,KAAK,CAAA;AACrB,QAAA,IAAA,CAAK,kBAAA,CAAmB,MAAA,CAAO,KAAA,CAAM,KAAK,CAAA;AAAA,MAC5C;AAEA,MAAA,IAAI,KAAK,eAAA,EAAiB;AACxB,QAAA,IAAI,KAAA,CAAM,MAAA,KAAW,MAAA,IAAU,KAAA,CAAM,WAAW,SAAA,EAAW;AACzD,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,MAAA,GAAS,KAAK,CAAA;AAAA,QAC3C,CAAA,MAAA,IAAW,KAAA,CAAM,gBAAA,KAAqB,MAAA,EAAQ;AAC5C,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,UAAA,GAAa,KAAK,CAAA;AAAA,QAC/C,CAAA,MAAO;AACL,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,UAAA,GAAa,KAAK,CAAA;AAAA,QAC/C;AAAA,MACF;AAAA,IACF;AAEA,IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA;AAC/C,IAAA,KAAA,MAAW,WAAW,QAAA,EAAU;AAC9B,MAAA,MAAM,QAAQ,KAAK,CAAA;AAAA,IACrB;AAAA,EACF;AACF","file":"index.js","sourcesContent":["import { VesantError } from '@vesant-sdk/core';\nimport type { TransactionCallbackEvent, HoldReason } from './types';\n\nexport class DuplicateTransactionError extends VesantError {\n readonly tx_id: string;\n\n constructor(tx_id: string, message = `Duplicate transaction: ${tx_id} already exists`) {\n super(message, 'DUPLICATE_TRANSACTION', 409);\n this.name = 'DuplicateTransactionError';\n this.tx_id = tx_id;\n Object.setPrototypeOf(this, DuplicateTransactionError.prototype);\n }\n}\n\nexport class TaxHoldError extends Error {\n readonly tx_id: string;\n readonly hold_reason: HoldReason;\n readonly event: TransactionCallbackEvent;\n\n constructor(event: TransactionCallbackEvent) {\n super(`Transaction ${event.tx_id} held: ${event.hold_reason}`);\n this.name = 'TaxHoldError';\n this.tx_id = event.tx_id;\n this.hold_reason = event.hold_reason as HoldReason;\n this.event = event;\n Object.setPrototypeOf(this, TaxHoldError.prototype);\n }\n}\n","import { VesantError } from '@vesant-sdk/core';\nimport { TaxHoldError } from './errors';\nimport type { Transaction, TransactionCallbackEvent } from './types';\n\nexport type WithdrawalState = 'vesant_pending' | 'withheld' | 'suspend' | 'released' | 'failed';\n\nexport class PendingWithdrawal {\n private _state: WithdrawalState = 'vesant_pending';\n private _event: TransactionCallbackEvent | null = null;\n private _error: unknown = undefined;\n\n // Deferred promise — created lazily only when wait() is called.\n private _waitPromise: Promise<TransactionCallbackEvent> | null = null;\n private _resolveWait?: (event: TransactionCallbackEvent) => void;\n private _rejectWait?: (err: unknown) => void;\n\n readonly tx_id: string;\n readonly initialTransaction: Transaction;\n\n constructor(tx_id: string, transaction: Transaction) {\n this.tx_id = tx_id;\n this.initialTransaction = transaction;\n }\n\n status(): WithdrawalState {\n return this._state;\n }\n\n callbackEvent(): TransactionCallbackEvent | null {\n return this._event;\n }\n\n wait(signal?: AbortSignal): Promise<TransactionCallbackEvent> {\n if (this._state !== 'vesant_pending') {\n if (this._state === 'suspend' || this._state === 'failed') {\n return Promise.reject(this._error);\n }\n if (this._event !== null) {\n return Promise.resolve(this._event);\n }\n }\n\n if (!this._waitPromise) {\n this._waitPromise = new Promise<TransactionCallbackEvent>((resolve, reject) => {\n this._resolveWait = resolve;\n this._rejectWait = reject;\n });\n }\n\n if (signal) {\n if (signal.aborted) {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n } else {\n signal.addEventListener('abort', () => {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n }, { once: true });\n }\n }\n\n return this._waitPromise;\n }\n\n /** Called by TransactionWebhookHandler when the tm.transaction.callback arrives for this tx_id. */\n _settle(event: TransactionCallbackEvent): void {\n if (this._state !== 'vesant_pending') return;\n this._event = event;\n if (event.status === 'hold' || event.status === 'suspend') {\n this._state = 'suspend';\n this._error = new TaxHoldError(event);\n this._rejectWait?.(this._error);\n } else if (event.withholding_type === 'none') {\n this._state = 'released';\n this._resolveWait?.(event);\n } else {\n this._state = 'withheld';\n this._resolveWait?.(event);\n }\n }\n\n /** Called to fail the pending withdrawal with a generic error (e.g. abort, network loss). */\n _fail(err: unknown): void {\n if (this._state !== 'vesant_pending') return;\n this._state = 'failed';\n this._error = err;\n this._rejectWait?.(err);\n }\n}\n","import { BaseClient } from '@vesant-sdk/core';\nimport type { RequestOptions } from '@vesant-sdk/core';\nimport type { RequestTaxFormWithProfileInput, RequestTaxFormWithProfileOutput, CustomerTaxProfileDocuments } from './types';\n\nexport class TaxTransactionClient extends BaseClient {\n async getCustomerDocuments(customerID: string): Promise<CustomerTaxProfileDocuments> {\n return this.request<CustomerTaxProfileDocuments>(\n `/api/v1/tm/customer-tax-profiles/${customerID}/documents`\n );\n }\n\n /**\n * A disabled trigger (e.g. `trigger_manual` off in tenant rules) returns 200 with\n * the customer's unchanged profile and no form sent — check `public_url` presence\n * or compare `form_status`/`tin_status` rather than trusting a 200 alone.\n */\n async requestTaxFormWithProfile(\n input: RequestTaxFormWithProfileInput,\n requestOptions?: RequestOptions\n ): Promise<RequestTaxFormWithProfileOutput> {\n return this.requestWithRetry<RequestTaxFormWithProfileOutput>(\n '/api/v1/tax/customer-tax-profiles/request-form-with-profile',\n {\n method: 'POST',\n body: JSON.stringify(input),\n },\n undefined,\n undefined,\n requestOptions\n );\n }\n}\n","/**\n * TransactionClient — SDK for the Transaction Monitoring service\n */\nimport { BaseClient, VesantError, ValidationError } from '@vesant-sdk/core';\nimport type { BaseClientConfig, RequestOptions } from '@vesant-sdk/core';\nimport type { Transaction, TransactionCreateDTO, TransactionCreateResponse, TmTenantSettings } from './types';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport { TaxTransactionClient } from './tax/client';\n\n/**\n * Ingestion requires an ISO 4217 style currency — exactly three ASCII letters,\n * the same shape the backend persists and the frontend's Intl.NumberFormat\n * accepts. Reject anything else (empty, a country code, or a social-casino\n * ticker like \"SC\"/\"GC\") up front so a malformed code can never reach the\n * ingestion endpoint and crash a downstream currency formatter. This is a\n * structural guard only — it does not rewrite the code; callers own any\n * business mapping (e.g. Sweeps Coins → USD) before submitting.\n */\nfunction assertWellFormedCurrency(currency: string): void {\n if (!/^[A-Za-z]{3}$/.test(currency ?? '')) {\n throw new ValidationError(\n `Invalid currency code: ${JSON.stringify(currency)}. Expected a 3-letter ISO 4217 code.`,\n ['currency']\n );\n }\n}\n\nexport class TransactionClient extends BaseClient {\n readonly tax: TaxTransactionClient;\n\n constructor(config: BaseClientConfig) {\n super(config);\n this.tax = new TaxTransactionClient(config);\n }\n\n /**\n * Submit a transaction via the unified ingestion endpoint.\n *\n * POST /api/v1/transactions is gated on ANY of transaction monitoring, tax,\n * or fraud being enabled for the tenant, so fraud-only and tax-only tenants\n * can ingest even with TM switched off.\n */\n async createTransaction(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<TransactionCreateResponse> {\n assertWellFormedCurrency(request.currency);\n try {\n return await this.requestWithRetry<TransactionCreateResponse>('/api/v1/transactions', {\n method: 'POST',\n body: JSON.stringify(request),\n }, undefined, undefined, requestOptions);\n } catch (error) {\n if (error instanceof VesantError && error.statusCode === 409) {\n const transaction = await this.getTransaction(request.tx_id, requestOptions);\n return { transaction, message: 'Transaction already exists' };\n }\n throw error;\n }\n }\n\n async submitWithdrawal(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<PendingWithdrawal> {\n const response = await this.createTransaction(\n { ...request, transaction_type: 'withdrawal' },\n requestOptions\n );\n return new PendingWithdrawal(response.transaction.tx_id, response.transaction);\n }\n\n async getTransaction(transactionId: string, requestOptions?: RequestOptions): Promise<Transaction> {\n return this.requestWithRetry<Transaction>(`/api/v1/tm/transactions/status/${transactionId}`, {\n method: 'GET',\n }, undefined, undefined, requestOptions);\n }\n\n async getSettings(): Promise<TmTenantSettings> {\n return this.requestWithRetry<TmTenantSettings>('/api/v1/tm/settings', {\n method: 'GET',\n });\n }\n}\n","import { verifyWebhookSignature, ValidationError, InMemoryDedupStore, type WebhookDedupStore } from '@vesant-sdk/core';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport type {\n TransactionCallbackEvent,\n TransactionCallbackHandler,\n TransactionHeldEvent,\n TransactionWebhookEvent,\n} from './types';\n\ntype EventHandler<T> = (event: T) => void | Promise<void>;\n\ntype HandlerMap = {\n 'tm.transaction.callback': Array<EventHandler<TransactionCallbackEvent>>;\n 'tm.transaction.held': Array<EventHandler<TransactionHeldEvent>>;\n};\n\nexport const TRANSACTION_SIGNATURE_HEADER = 'x-webhook-signature';\n\nexport interface TransactionWebhookHandlerConfig {\n secret: string;\n /** Reject duplicate event_type+tx_id pairs within replayWindow. Default: true. */\n replayProtection?: boolean;\n /** How long (ms) to remember seen events for replay detection. Default: 300_000 (5 min). */\n replayWindow?: number;\n /** Pluggable dedup store for replay protection (default: in-memory, process-local). */\n dedupStore?: WebhookDedupStore;\n}\n\nexport class TransactionWebhookHandler {\n private readonly handlers: HandlerMap = {\n 'tm.transaction.callback': [],\n 'tm.transaction.held': [],\n };\n private readonly secret: string;\n private readonly replayProtection: boolean;\n private readonly replayWindow: number;\n private readonly dedupStore: WebhookDedupStore;\n private readonly pendingWithdrawals = new Map<string, PendingWithdrawal>();\n private callbackHandler: TransactionCallbackHandler | null = null;\n\n constructor(config: TransactionWebhookHandlerConfig) {\n this.secret = config.secret;\n this.replayProtection = config.replayProtection ?? true;\n this.replayWindow = config.replayWindow ?? 300_000;\n this.dedupStore = config.dedupStore ?? new InMemoryDedupStore();\n }\n\n on(eventType: 'tm.transaction.callback', handler: EventHandler<TransactionCallbackEvent>): this;\n on(eventType: 'tm.transaction.held', handler: EventHandler<TransactionHeldEvent>): this;\n on(eventType: keyof HandlerMap, handler: EventHandler<TransactionCallbackEvent> | EventHandler<TransactionHeldEvent>): this {\n (this.handlers[eventType] as Array<typeof handler>).push(handler);\n return this;\n }\n\n /** Register a semantic callback handler for the three withholding outcomes. Replaces any previous handler. */\n registerCallbackHandler(handler: TransactionCallbackHandler): this {\n this.callbackHandler = handler;\n return this;\n }\n\n /** Track a PendingWithdrawal so it is auto-settled when its tm.transaction.callback arrives. */\n link(pending: PendingWithdrawal): this {\n this.pendingWithdrawals.set(pending.tx_id, pending);\n return this;\n }\n\n /** Stop tracking a PendingWithdrawal by tx_id. */\n unlink(tx_id: string): this {\n this.pendingWithdrawals.delete(tx_id);\n return this;\n }\n\n /** Verify HMAC-SHA256 signature only. Returns false instead of throwing. */\n async verify(rawBody: string, signature: string): Promise<boolean> {\n return verifyWebhookSignature(rawBody, signature, this.secret);\n }\n\n /** Parse and validate required fields. Does not verify signature — use verifyAndParse in production. */\n parse(body: string): TransactionWebhookEvent {\n const event = JSON.parse(body) as TransactionWebhookEvent;\n if (!event.event_type || !event.tx_id) {\n throw new ValidationError(\n 'Invalid TM webhook event: missing required fields (event_type, tx_id)',\n ['event_type', 'tx_id']\n );\n }\n return event;\n }\n\n /** Verify signature, parse, and check for replays. Throws ValidationError on any failure. */\n async verifyAndParse(body: string, signature: string): Promise<TransactionWebhookEvent> {\n const isValid = await verifyWebhookSignature(body, signature, this.secret);\n if (!isValid) {\n throw new ValidationError('Invalid webhook signature', ['signature']);\n }\n\n const event = this.parse(body);\n\n if (this.replayProtection) {\n const key = `${event.event_type}:${event.tx_id}`;\n if (await this.dedupStore.seen(key)) {\n throw new ValidationError(\n `Duplicate webhook event: ${event.event_type} for ${event.tx_id} has already been processed`,\n ['tx_id']\n );\n }\n await this.dedupStore.mark(key, this.replayWindow);\n }\n\n return event;\n }\n\n /** Verify signature, parse, and dispatch to registered handlers. */\n async handle(body: string, signature: string): Promise<void> {\n const event = await this.verifyAndParse(body, signature);\n\n if (event.event_type === 'tm.transaction.callback') {\n const pending = this.pendingWithdrawals.get(event.tx_id);\n if (pending) {\n pending._settle(event);\n this.pendingWithdrawals.delete(event.tx_id);\n }\n\n if (this.callbackHandler) {\n if (event.status === 'hold' || event.status === 'suspend') {\n await this.callbackHandler.onHeld?.(event);\n } else if (event.withholding_type === 'none') {\n await this.callbackHandler.onReleased?.(event);\n } else {\n await this.callbackHandler.onWithheld?.(event);\n }\n }\n }\n\n const handlers = this.handlers[event.event_type] as Array<EventHandler<typeof event>>;\n for (const handler of handlers) {\n await handler(event);\n }\n }\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { VesantError, BaseClient, verifyWebhookSignature, ValidationError } from '@vesant-sdk/core';
|
|
1
|
+
import { VesantError, BaseClient, InMemoryDedupStore, verifyWebhookSignature, ValidationError } from '@vesant-sdk/core';
|
|
2
2
|
|
|
3
3
|
// src/client.ts
|
|
4
4
|
var DuplicateTransactionError = class _DuplicateTransactionError extends VesantError {
|
|
@@ -93,6 +93,11 @@ var TaxTransactionClient = class extends BaseClient {
|
|
|
93
93
|
`/api/v1/tm/customer-tax-profiles/${customerID}/documents`
|
|
94
94
|
);
|
|
95
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* A disabled trigger (e.g. `trigger_manual` off in tenant rules) returns 200 with
|
|
98
|
+
* the customer's unchanged profile and no form sent — check `public_url` presence
|
|
99
|
+
* or compare `form_status`/`tin_status` rather than trusting a 200 alone.
|
|
100
|
+
*/
|
|
96
101
|
async requestTaxFormWithProfile(input, requestOptions) {
|
|
97
102
|
return this.requestWithRetry(
|
|
98
103
|
"/api/v1/tax/customer-tax-profiles/request-form-with-profile",
|
|
@@ -108,14 +113,30 @@ var TaxTransactionClient = class extends BaseClient {
|
|
|
108
113
|
};
|
|
109
114
|
|
|
110
115
|
// src/client.ts
|
|
116
|
+
function assertWellFormedCurrency(currency) {
|
|
117
|
+
if (!/^[A-Za-z]{3}$/.test(currency ?? "")) {
|
|
118
|
+
throw new ValidationError(
|
|
119
|
+
`Invalid currency code: ${JSON.stringify(currency)}. Expected a 3-letter ISO 4217 code.`,
|
|
120
|
+
["currency"]
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
111
124
|
var TransactionClient = class extends BaseClient {
|
|
112
125
|
constructor(config) {
|
|
113
126
|
super(config);
|
|
114
127
|
this.tax = new TaxTransactionClient(config);
|
|
115
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Submit a transaction via the unified ingestion endpoint.
|
|
131
|
+
*
|
|
132
|
+
* POST /api/v1/transactions is gated on ANY of transaction monitoring, tax,
|
|
133
|
+
* or fraud being enabled for the tenant, so fraud-only and tax-only tenants
|
|
134
|
+
* can ingest even with TM switched off.
|
|
135
|
+
*/
|
|
116
136
|
async createTransaction(request, requestOptions) {
|
|
137
|
+
assertWellFormedCurrency(request.currency);
|
|
117
138
|
try {
|
|
118
|
-
return await this.requestWithRetry("/api/v1/
|
|
139
|
+
return await this.requestWithRetry("/api/v1/transactions", {
|
|
119
140
|
method: "POST",
|
|
120
141
|
body: JSON.stringify(request)
|
|
121
142
|
}, void 0, void 0, requestOptions);
|
|
@@ -152,12 +173,12 @@ var TransactionWebhookHandler = class {
|
|
|
152
173
|
"tm.transaction.callback": [],
|
|
153
174
|
"tm.transaction.held": []
|
|
154
175
|
};
|
|
155
|
-
this.seenEvents = /* @__PURE__ */ new Map();
|
|
156
176
|
this.pendingWithdrawals = /* @__PURE__ */ new Map();
|
|
157
177
|
this.callbackHandler = null;
|
|
158
178
|
this.secret = config.secret;
|
|
159
179
|
this.replayProtection = config.replayProtection ?? true;
|
|
160
180
|
this.replayWindow = config.replayWindow ?? 3e5;
|
|
181
|
+
this.dedupStore = config.dedupStore ?? new InMemoryDedupStore();
|
|
161
182
|
}
|
|
162
183
|
on(eventType, handler) {
|
|
163
184
|
this.handlers[eventType].push(handler);
|
|
@@ -202,21 +223,13 @@ var TransactionWebhookHandler = class {
|
|
|
202
223
|
const event = this.parse(body);
|
|
203
224
|
if (this.replayProtection) {
|
|
204
225
|
const key = `${event.event_type}:${event.tx_id}`;
|
|
205
|
-
if (this.
|
|
226
|
+
if (await this.dedupStore.seen(key)) {
|
|
206
227
|
throw new ValidationError(
|
|
207
228
|
`Duplicate webhook event: ${event.event_type} for ${event.tx_id} has already been processed`,
|
|
208
229
|
["tx_id"]
|
|
209
230
|
);
|
|
210
231
|
}
|
|
211
|
-
|
|
212
|
-
this.seenEvents.set(key, now);
|
|
213
|
-
if (this.seenEvents.size > 1e3) {
|
|
214
|
-
for (const [k, seenAt] of this.seenEvents) {
|
|
215
|
-
if (now - seenAt > this.replayWindow) {
|
|
216
|
-
this.seenEvents.delete(k);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
232
|
+
await this.dedupStore.mark(key, this.replayWindow);
|
|
220
233
|
}
|
|
221
234
|
return event;
|
|
222
235
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts","../src/pending-withdrawal.ts","../src/tax/client.ts","../src/client.ts","../src/webhook-handler.ts"],"names":["VesantError","BaseClient"],"mappings":";;;AAGO,IAAM,yBAAA,GAAN,MAAM,0BAAA,SAAkC,WAAA,CAAY;AAAA,EAGzD,WAAA,CAAY,KAAA,EAAe,OAAA,GAAU,CAAA,uBAAA,EAA0B,KAAK,CAAA,eAAA,CAAA,EAAmB;AACrF,IAAA,KAAA,CAAM,OAAA,EAAS,yBAAyB,GAAG,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAA,GAAO,2BAAA;AACZ,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,MAAA,CAAO,cAAA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IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA;AAC/C,IAAA,KAAA,MAAW,WAAW,QAAA,EAAU;AAC9B,MAAA,MAAM,QAAQ,KAAK,CAAA;AAAA,IACrB;AAAA,EACF;AACF","file":"index.mjs","sourcesContent":["import { VesantError } from '@vesant-sdk/core';\nimport type { TransactionCallbackEvent, HoldReason } from './types';\n\nexport class DuplicateTransactionError extends VesantError {\n readonly tx_id: string;\n\n constructor(tx_id: string, message = `Duplicate transaction: ${tx_id} already exists`) {\n super(message, 'DUPLICATE_TRANSACTION', 409);\n this.name = 'DuplicateTransactionError';\n this.tx_id = tx_id;\n Object.setPrototypeOf(this, DuplicateTransactionError.prototype);\n }\n}\n\nexport class TaxHoldError extends Error {\n readonly tx_id: string;\n readonly hold_reason: HoldReason;\n readonly event: TransactionCallbackEvent;\n\n constructor(event: TransactionCallbackEvent) {\n super(`Transaction ${event.tx_id} held: ${event.hold_reason}`);\n this.name = 'TaxHoldError';\n this.tx_id = event.tx_id;\n this.hold_reason = event.hold_reason as HoldReason;\n this.event = event;\n Object.setPrototypeOf(this, TaxHoldError.prototype);\n }\n}\n","import { VesantError } from '@vesant-sdk/core';\nimport { TaxHoldError } from './errors';\nimport type { Transaction, TransactionCallbackEvent } from './types';\n\nexport type WithdrawalState = 'vesant_pending' | 'withheld' | 'suspend' | 'released' | 'failed';\n\nexport class PendingWithdrawal {\n private _state: WithdrawalState = 'vesant_pending';\n private _event: TransactionCallbackEvent | null = null;\n private _error: unknown = undefined;\n\n // Deferred promise — created lazily only when wait() is called.\n private _waitPromise: Promise<TransactionCallbackEvent> | null = null;\n private _resolveWait?: (event: TransactionCallbackEvent) => void;\n private _rejectWait?: (err: unknown) => void;\n\n readonly tx_id: string;\n readonly initialTransaction: Transaction;\n\n constructor(tx_id: string, transaction: Transaction) {\n this.tx_id = tx_id;\n this.initialTransaction = transaction;\n }\n\n status(): WithdrawalState {\n return this._state;\n }\n\n callbackEvent(): TransactionCallbackEvent | null {\n return this._event;\n }\n\n wait(signal?: AbortSignal): Promise<TransactionCallbackEvent> {\n if (this._state !== 'vesant_pending') {\n if (this._state === 'suspend' || this._state === 'failed') {\n return Promise.reject(this._error);\n }\n if (this._event !== null) {\n return Promise.resolve(this._event);\n }\n }\n\n if (!this._waitPromise) {\n this._waitPromise = new Promise<TransactionCallbackEvent>((resolve, reject) => {\n this._resolveWait = resolve;\n this._rejectWait = reject;\n });\n }\n\n if (signal) {\n if (signal.aborted) {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n } else {\n signal.addEventListener('abort', () => {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n }, { once: true });\n }\n }\n\n return this._waitPromise;\n }\n\n /** Called by TransactionWebhookHandler when the tm.transaction.callback arrives for this tx_id. */\n _settle(event: TransactionCallbackEvent): void {\n if (this._state !== 'vesant_pending') return;\n this._event = event;\n if (event.status === 'hold' || event.status === 'suspend') {\n this._state = 'suspend';\n this._error = new TaxHoldError(event);\n this._rejectWait?.(this._error);\n } else if (event.withholding_type === 'none') {\n this._state = 'released';\n this._resolveWait?.(event);\n } else {\n this._state = 'withheld';\n this._resolveWait?.(event);\n }\n }\n\n /** Called to fail the pending withdrawal with a generic error (e.g. abort, network loss). */\n _fail(err: unknown): void {\n if (this._state !== 'vesant_pending') return;\n this._state = 'failed';\n this._error = err;\n this._rejectWait?.(err);\n }\n}\n","import { BaseClient } from '@vesant-sdk/core';\nimport type { RequestOptions } from '@vesant-sdk/core';\nimport type { RequestTaxFormWithProfileInput, RequestTaxFormWithProfileOutput, CustomerTaxProfileDocuments } from './types';\n\nexport class TaxTransactionClient extends BaseClient {\n async getCustomerDocuments(customerID: string): Promise<CustomerTaxProfileDocuments> {\n return this.request<CustomerTaxProfileDocuments>(\n `/api/v1/tm/customer-tax-profiles/${customerID}/documents`\n );\n }\n\n async requestTaxFormWithProfile(\n input: RequestTaxFormWithProfileInput,\n requestOptions?: RequestOptions\n ): Promise<RequestTaxFormWithProfileOutput> {\n return this.requestWithRetry<RequestTaxFormWithProfileOutput>(\n '/api/v1/tax/customer-tax-profiles/request-form-with-profile',\n {\n method: 'POST',\n body: JSON.stringify(input),\n },\n undefined,\n undefined,\n requestOptions\n );\n }\n}\n","/**\n * TransactionClient — SDK for the Transaction Monitoring service\n */\nimport { BaseClient, VesantError } from '@vesant-sdk/core';\nimport type { BaseClientConfig, RequestOptions } from '@vesant-sdk/core';\nimport type { Transaction, TransactionCreateDTO, TransactionCreateResponse, TmTenantSettings } from './types';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport { TaxTransactionClient } from './tax/client';\n\nexport class TransactionClient extends BaseClient {\n readonly tax: TaxTransactionClient;\n\n constructor(config: BaseClientConfig) {\n super(config);\n this.tax = new TaxTransactionClient(config);\n }\n\n async createTransaction(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<TransactionCreateResponse> {\n try {\n return await this.requestWithRetry<TransactionCreateResponse>('/api/v1/tm/transactions', {\n method: 'POST',\n body: JSON.stringify(request),\n }, undefined, undefined, requestOptions);\n } catch (error) {\n if (error instanceof VesantError && error.statusCode === 409) {\n const transaction = await this.getTransaction(request.tx_id, requestOptions);\n return { transaction, message: 'Transaction already exists' };\n }\n throw error;\n }\n }\n\n async submitWithdrawal(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<PendingWithdrawal> {\n const response = await this.createTransaction(\n { ...request, transaction_type: 'withdrawal' },\n requestOptions\n );\n return new PendingWithdrawal(response.transaction.tx_id, response.transaction);\n }\n\n async getTransaction(transactionId: string, requestOptions?: RequestOptions): Promise<Transaction> {\n return this.requestWithRetry<Transaction>(`/api/v1/tm/transactions/status/${transactionId}`, {\n method: 'GET',\n }, undefined, undefined, requestOptions);\n }\n\n async getSettings(): Promise<TmTenantSettings> {\n return this.requestWithRetry<TmTenantSettings>('/api/v1/tm/settings', {\n method: 'GET',\n });\n }\n}\n","import { verifyWebhookSignature, ValidationError } from '@vesant-sdk/core';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport type {\n TransactionCallbackEvent,\n TransactionCallbackHandler,\n TransactionHeldEvent,\n TransactionWebhookEvent,\n} from './types';\n\ntype EventHandler<T> = (event: T) => void | Promise<void>;\n\ntype HandlerMap = {\n 'tm.transaction.callback': Array<EventHandler<TransactionCallbackEvent>>;\n 'tm.transaction.held': Array<EventHandler<TransactionHeldEvent>>;\n};\n\nexport const TRANSACTION_SIGNATURE_HEADER = 'x-webhook-signature';\n\nexport interface TransactionWebhookHandlerConfig {\n secret: string;\n /** Reject duplicate event_type+tx_id pairs within replayWindow. Default: true. */\n replayProtection?: boolean;\n /** How long (ms) to remember seen events for replay detection. Default: 300_000 (5 min). */\n replayWindow?: number;\n}\n\nexport class TransactionWebhookHandler {\n private readonly handlers: HandlerMap = {\n 'tm.transaction.callback': [],\n 'tm.transaction.held': [],\n };\n private readonly secret: string;\n private readonly replayProtection: boolean;\n private readonly replayWindow: number;\n private readonly seenEvents = new Map<string, number>();\n private readonly pendingWithdrawals = new Map<string, PendingWithdrawal>();\n private callbackHandler: TransactionCallbackHandler | null = null;\n\n constructor(config: TransactionWebhookHandlerConfig) {\n this.secret = config.secret;\n this.replayProtection = config.replayProtection ?? true;\n this.replayWindow = config.replayWindow ?? 300_000;\n }\n\n on(eventType: 'tm.transaction.callback', handler: EventHandler<TransactionCallbackEvent>): this;\n on(eventType: 'tm.transaction.held', handler: EventHandler<TransactionHeldEvent>): this;\n on(eventType: keyof HandlerMap, handler: EventHandler<TransactionCallbackEvent> | EventHandler<TransactionHeldEvent>): this {\n (this.handlers[eventType] as Array<typeof handler>).push(handler);\n return this;\n }\n\n /** Register a semantic callback handler for the three withholding outcomes. Replaces any previous handler. */\n registerCallbackHandler(handler: TransactionCallbackHandler): this {\n this.callbackHandler = handler;\n return this;\n }\n\n /** Track a PendingWithdrawal so it is auto-settled when its tm.transaction.callback arrives. */\n link(pending: PendingWithdrawal): this {\n this.pendingWithdrawals.set(pending.tx_id, pending);\n return this;\n }\n\n /** Stop tracking a PendingWithdrawal by tx_id. */\n unlink(tx_id: string): this {\n this.pendingWithdrawals.delete(tx_id);\n return this;\n }\n\n /** Verify HMAC-SHA256 signature only. Returns false instead of throwing. */\n async verify(rawBody: string, signature: string): Promise<boolean> {\n return verifyWebhookSignature(rawBody, signature, this.secret);\n }\n\n /** Parse and validate required fields. Does not verify signature — use verifyAndParse in production. */\n parse(body: string): TransactionWebhookEvent {\n const event = JSON.parse(body) as TransactionWebhookEvent;\n if (!event.event_type || !event.tx_id) {\n throw new ValidationError(\n 'Invalid TM webhook event: missing required fields (event_type, tx_id)',\n ['event_type', 'tx_id']\n );\n }\n return event;\n }\n\n /** Verify signature, parse, and check for replays. Throws ValidationError on any failure. */\n async verifyAndParse(body: string, signature: string): Promise<TransactionWebhookEvent> {\n const isValid = await verifyWebhookSignature(body, signature, this.secret);\n if (!isValid) {\n throw new ValidationError('Invalid webhook signature', ['signature']);\n }\n\n const event = this.parse(body);\n\n if (this.replayProtection) {\n const key = `${event.event_type}:${event.tx_id}`;\n if (this.seenEvents.has(key)) {\n throw new ValidationError(\n `Duplicate webhook event: ${event.event_type} for ${event.tx_id} has already been processed`,\n ['tx_id']\n );\n }\n const now = Date.now();\n this.seenEvents.set(key, now);\n if (this.seenEvents.size > 1000) {\n for (const [k, seenAt] of this.seenEvents) {\n if (now - seenAt > this.replayWindow) {\n this.seenEvents.delete(k);\n }\n }\n }\n }\n\n return event;\n }\n\n /** Verify signature, parse, and dispatch to registered handlers. */\n async handle(body: string, signature: string): Promise<void> {\n const event = await this.verifyAndParse(body, signature);\n\n if (event.event_type === 'tm.transaction.callback') {\n const pending = this.pendingWithdrawals.get(event.tx_id);\n if (pending) {\n pending._settle(event);\n this.pendingWithdrawals.delete(event.tx_id);\n }\n\n if (this.callbackHandler) {\n if (event.status === 'hold' || event.status === 'suspend') {\n await this.callbackHandler.onHeld?.(event);\n } else if (event.withholding_type === 'none') {\n await this.callbackHandler.onReleased?.(event);\n } else {\n await this.callbackHandler.onWithheld?.(event);\n }\n }\n }\n\n const handlers = this.handlers[event.event_type] as Array<EventHandler<typeof event>>;\n for (const handler of handlers) {\n await handler(event);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/pending-withdrawal.ts","../src/tax/client.ts","../src/client.ts","../src/webhook-handler.ts"],"names":["VesantError","BaseClient","ValidationError"],"mappings":";;;AAGO,IAAM,yBAAA,GAAN,MAAM,0BAAA,SAAkC,WAAA,CAAY;AAAA,EAGzD,WAAA,CAAY,KAAA,EAAe,OAAA,GAAU,CAAA,uBAAA,EAA0B,KAAK,CAAA,eAAA,CAAA,EAAmB;AACrF,IAAA,KAAA,CAAM,OAAA,EAAS,yBAAyB,GAAG,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAA,GAAO,2BAAA;AACZ,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,MAAA,CAAO,cAAA,CAAe,IAAA,EAAM,0BAAA,CAA0B,SAAS,CAAA;AAAA,EACjE;AACF;AAEO,IAAM,YAAA,GAAN,MAAM,aAAA,SAAqB,KAAA,CAAM;AAAA,EAKtC,YAAY,KAAA,EAAiC;AAC3C,IAAA,KAAA,CAAM,eAAe,KAAA,CAAM,KAAK,CAAA,OAAA,EAAU,KAAA,CAAM,WAAW,CAAA,CAAE,CAAA;AAC7D,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,QAAQ,KAAA,CAAM,KAAA;AACnB,IAAA,IAAA,CAAK,cAAc,KAAA,CAAM,WAAA;AACzB,IAAA,IAAA,CAAK,KAAA,GAAQ,KAAA;AACb,IAAA,MAAA,CAAO,cAAA,CAAe,IAAA,EAAM,aAAA,CAAa,SAAS,CAAA;AAAA,EACpD;AACF;;;ACrBO,IAAM,oBAAN,MAAwB;AAAA,EAa7B,WAAA,CAAY,OAAe,WAAA,EAA0B;AAZrD,IAAA,IAAA,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A2B;AAClD,MAAA,MAAM,OAAA,GAAU,IAAA,CAAK,kBAAA,CAAmB,GAAA,CAAI,MAAM,KAAK,CAAA;AACvD,MAAA,IAAI,OAAA,EAAS;AACX,QAAA,OAAA,CAAQ,QAAQ,KAAK,CAAA;AACrB,QAAA,IAAA,CAAK,kBAAA,CAAmB,MAAA,CAAO,KAAA,CAAM,KAAK,CAAA;AAAA,MAC5C;AAEA,MAAA,IAAI,KAAK,eAAA,EAAiB;AACxB,QAAA,IAAI,KAAA,CAAM,MAAA,KAAW,MAAA,IAAU,KAAA,CAAM,WAAW,SAAA,EAAW;AACzD,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,MAAA,GAAS,KAAK,CAAA;AAAA,QAC3C,CAAA,MAAA,IAAW,KAAA,CAAM,gBAAA,KAAqB,MAAA,EAAQ;AAC5C,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,UAAA,GAAa,KAAK,CAAA;AAAA,QAC/C,CAAA,MAAO;AACL,UAAA,MAAM,IAAA,CAAK,eAAA,CAAgB,UAAA,GAAa,KAAK,CAAA;AAAA,QAC/C;AAAA,MACF;AAAA,IACF;AAEA,IAAA,MAAM,QAAA,GAAW,IAAA,CAAK,QAAA,CAAS,KAAA,CAAM,UAAU,CAAA;AAC/C,IAAA,KAAA,MAAW,WAAW,QAAA,EAAU;AAC9B,MAAA,MAAM,QAAQ,KAAK,CAAA;AAAA,IACrB;AAAA,EACF;AACF","file":"index.mjs","sourcesContent":["import { VesantError } from '@vesant-sdk/core';\nimport type { TransactionCallbackEvent, HoldReason } from './types';\n\nexport class DuplicateTransactionError extends VesantError {\n readonly tx_id: string;\n\n constructor(tx_id: string, message = `Duplicate transaction: ${tx_id} already exists`) {\n super(message, 'DUPLICATE_TRANSACTION', 409);\n this.name = 'DuplicateTransactionError';\n this.tx_id = tx_id;\n Object.setPrototypeOf(this, DuplicateTransactionError.prototype);\n }\n}\n\nexport class TaxHoldError extends Error {\n readonly tx_id: string;\n readonly hold_reason: HoldReason;\n readonly event: TransactionCallbackEvent;\n\n constructor(event: TransactionCallbackEvent) {\n super(`Transaction ${event.tx_id} held: ${event.hold_reason}`);\n this.name = 'TaxHoldError';\n this.tx_id = event.tx_id;\n this.hold_reason = event.hold_reason as HoldReason;\n this.event = event;\n Object.setPrototypeOf(this, TaxHoldError.prototype);\n }\n}\n","import { VesantError } from '@vesant-sdk/core';\nimport { TaxHoldError } from './errors';\nimport type { Transaction, TransactionCallbackEvent } from './types';\n\nexport type WithdrawalState = 'vesant_pending' | 'withheld' | 'suspend' | 'released' | 'failed';\n\nexport class PendingWithdrawal {\n private _state: WithdrawalState = 'vesant_pending';\n private _event: TransactionCallbackEvent | null = null;\n private _error: unknown = undefined;\n\n // Deferred promise — created lazily only when wait() is called.\n private _waitPromise: Promise<TransactionCallbackEvent> | null = null;\n private _resolveWait?: (event: TransactionCallbackEvent) => void;\n private _rejectWait?: (err: unknown) => void;\n\n readonly tx_id: string;\n readonly initialTransaction: Transaction;\n\n constructor(tx_id: string, transaction: Transaction) {\n this.tx_id = tx_id;\n this.initialTransaction = transaction;\n }\n\n status(): WithdrawalState {\n return this._state;\n }\n\n callbackEvent(): TransactionCallbackEvent | null {\n return this._event;\n }\n\n wait(signal?: AbortSignal): Promise<TransactionCallbackEvent> {\n if (this._state !== 'vesant_pending') {\n if (this._state === 'suspend' || this._state === 'failed') {\n return Promise.reject(this._error);\n }\n if (this._event !== null) {\n return Promise.resolve(this._event);\n }\n }\n\n if (!this._waitPromise) {\n this._waitPromise = new Promise<TransactionCallbackEvent>((resolve, reject) => {\n this._resolveWait = resolve;\n this._rejectWait = reject;\n });\n }\n\n if (signal) {\n if (signal.aborted) {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n } else {\n signal.addEventListener('abort', () => {\n this._fail(new VesantError('Request aborted', 'REQUEST_ABORTED'));\n }, { once: true });\n }\n }\n\n return this._waitPromise;\n }\n\n /** Called by TransactionWebhookHandler when the tm.transaction.callback arrives for this tx_id. */\n _settle(event: TransactionCallbackEvent): void {\n if (this._state !== 'vesant_pending') return;\n this._event = event;\n if (event.status === 'hold' || event.status === 'suspend') {\n this._state = 'suspend';\n this._error = new TaxHoldError(event);\n this._rejectWait?.(this._error);\n } else if (event.withholding_type === 'none') {\n this._state = 'released';\n this._resolveWait?.(event);\n } else {\n this._state = 'withheld';\n this._resolveWait?.(event);\n }\n }\n\n /** Called to fail the pending withdrawal with a generic error (e.g. abort, network loss). */\n _fail(err: unknown): void {\n if (this._state !== 'vesant_pending') return;\n this._state = 'failed';\n this._error = err;\n this._rejectWait?.(err);\n }\n}\n","import { BaseClient } from '@vesant-sdk/core';\nimport type { RequestOptions } from '@vesant-sdk/core';\nimport type { RequestTaxFormWithProfileInput, RequestTaxFormWithProfileOutput, CustomerTaxProfileDocuments } from './types';\n\nexport class TaxTransactionClient extends BaseClient {\n async getCustomerDocuments(customerID: string): Promise<CustomerTaxProfileDocuments> {\n return this.request<CustomerTaxProfileDocuments>(\n `/api/v1/tm/customer-tax-profiles/${customerID}/documents`\n );\n }\n\n /**\n * A disabled trigger (e.g. `trigger_manual` off in tenant rules) returns 200 with\n * the customer's unchanged profile and no form sent — check `public_url` presence\n * or compare `form_status`/`tin_status` rather than trusting a 200 alone.\n */\n async requestTaxFormWithProfile(\n input: RequestTaxFormWithProfileInput,\n requestOptions?: RequestOptions\n ): Promise<RequestTaxFormWithProfileOutput> {\n return this.requestWithRetry<RequestTaxFormWithProfileOutput>(\n '/api/v1/tax/customer-tax-profiles/request-form-with-profile',\n {\n method: 'POST',\n body: JSON.stringify(input),\n },\n undefined,\n undefined,\n requestOptions\n );\n }\n}\n","/**\n * TransactionClient — SDK for the Transaction Monitoring service\n */\nimport { BaseClient, VesantError, ValidationError } from '@vesant-sdk/core';\nimport type { BaseClientConfig, RequestOptions } from '@vesant-sdk/core';\nimport type { Transaction, TransactionCreateDTO, TransactionCreateResponse, TmTenantSettings } from './types';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport { TaxTransactionClient } from './tax/client';\n\n/**\n * Ingestion requires an ISO 4217 style currency — exactly three ASCII letters,\n * the same shape the backend persists and the frontend's Intl.NumberFormat\n * accepts. Reject anything else (empty, a country code, or a social-casino\n * ticker like \"SC\"/\"GC\") up front so a malformed code can never reach the\n * ingestion endpoint and crash a downstream currency formatter. This is a\n * structural guard only — it does not rewrite the code; callers own any\n * business mapping (e.g. Sweeps Coins → USD) before submitting.\n */\nfunction assertWellFormedCurrency(currency: string): void {\n if (!/^[A-Za-z]{3}$/.test(currency ?? '')) {\n throw new ValidationError(\n `Invalid currency code: ${JSON.stringify(currency)}. Expected a 3-letter ISO 4217 code.`,\n ['currency']\n );\n }\n}\n\nexport class TransactionClient extends BaseClient {\n readonly tax: TaxTransactionClient;\n\n constructor(config: BaseClientConfig) {\n super(config);\n this.tax = new TaxTransactionClient(config);\n }\n\n /**\n * Submit a transaction via the unified ingestion endpoint.\n *\n * POST /api/v1/transactions is gated on ANY of transaction monitoring, tax,\n * or fraud being enabled for the tenant, so fraud-only and tax-only tenants\n * can ingest even with TM switched off.\n */\n async createTransaction(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<TransactionCreateResponse> {\n assertWellFormedCurrency(request.currency);\n try {\n return await this.requestWithRetry<TransactionCreateResponse>('/api/v1/transactions', {\n method: 'POST',\n body: JSON.stringify(request),\n }, undefined, undefined, requestOptions);\n } catch (error) {\n if (error instanceof VesantError && error.statusCode === 409) {\n const transaction = await this.getTransaction(request.tx_id, requestOptions);\n return { transaction, message: 'Transaction already exists' };\n }\n throw error;\n }\n }\n\n async submitWithdrawal(\n request: TransactionCreateDTO,\n requestOptions?: RequestOptions\n ): Promise<PendingWithdrawal> {\n const response = await this.createTransaction(\n { ...request, transaction_type: 'withdrawal' },\n requestOptions\n );\n return new PendingWithdrawal(response.transaction.tx_id, response.transaction);\n }\n\n async getTransaction(transactionId: string, requestOptions?: RequestOptions): Promise<Transaction> {\n return this.requestWithRetry<Transaction>(`/api/v1/tm/transactions/status/${transactionId}`, {\n method: 'GET',\n }, undefined, undefined, requestOptions);\n }\n\n async getSettings(): Promise<TmTenantSettings> {\n return this.requestWithRetry<TmTenantSettings>('/api/v1/tm/settings', {\n method: 'GET',\n });\n }\n}\n","import { verifyWebhookSignature, ValidationError, InMemoryDedupStore, type WebhookDedupStore } from '@vesant-sdk/core';\nimport { PendingWithdrawal } from './pending-withdrawal';\nimport type {\n TransactionCallbackEvent,\n TransactionCallbackHandler,\n TransactionHeldEvent,\n TransactionWebhookEvent,\n} from './types';\n\ntype EventHandler<T> = (event: T) => void | Promise<void>;\n\ntype HandlerMap = {\n 'tm.transaction.callback': Array<EventHandler<TransactionCallbackEvent>>;\n 'tm.transaction.held': Array<EventHandler<TransactionHeldEvent>>;\n};\n\nexport const TRANSACTION_SIGNATURE_HEADER = 'x-webhook-signature';\n\nexport interface TransactionWebhookHandlerConfig {\n secret: string;\n /** Reject duplicate event_type+tx_id pairs within replayWindow. Default: true. */\n replayProtection?: boolean;\n /** How long (ms) to remember seen events for replay detection. Default: 300_000 (5 min). */\n replayWindow?: number;\n /** Pluggable dedup store for replay protection (default: in-memory, process-local). */\n dedupStore?: WebhookDedupStore;\n}\n\nexport class TransactionWebhookHandler {\n private readonly handlers: HandlerMap = {\n 'tm.transaction.callback': [],\n 'tm.transaction.held': [],\n };\n private readonly secret: string;\n private readonly replayProtection: boolean;\n private readonly replayWindow: number;\n private readonly dedupStore: WebhookDedupStore;\n private readonly pendingWithdrawals = new Map<string, PendingWithdrawal>();\n private callbackHandler: TransactionCallbackHandler | null = null;\n\n constructor(config: TransactionWebhookHandlerConfig) {\n this.secret = config.secret;\n this.replayProtection = config.replayProtection ?? true;\n this.replayWindow = config.replayWindow ?? 300_000;\n this.dedupStore = config.dedupStore ?? new InMemoryDedupStore();\n }\n\n on(eventType: 'tm.transaction.callback', handler: EventHandler<TransactionCallbackEvent>): this;\n on(eventType: 'tm.transaction.held', handler: EventHandler<TransactionHeldEvent>): this;\n on(eventType: keyof HandlerMap, handler: EventHandler<TransactionCallbackEvent> | EventHandler<TransactionHeldEvent>): this {\n (this.handlers[eventType] as Array<typeof handler>).push(handler);\n return this;\n }\n\n /** Register a semantic callback handler for the three withholding outcomes. Replaces any previous handler. */\n registerCallbackHandler(handler: TransactionCallbackHandler): this {\n this.callbackHandler = handler;\n return this;\n }\n\n /** Track a PendingWithdrawal so it is auto-settled when its tm.transaction.callback arrives. */\n link(pending: PendingWithdrawal): this {\n this.pendingWithdrawals.set(pending.tx_id, pending);\n return this;\n }\n\n /** Stop tracking a PendingWithdrawal by tx_id. */\n unlink(tx_id: string): this {\n this.pendingWithdrawals.delete(tx_id);\n return this;\n }\n\n /** Verify HMAC-SHA256 signature only. Returns false instead of throwing. */\n async verify(rawBody: string, signature: string): Promise<boolean> {\n return verifyWebhookSignature(rawBody, signature, this.secret);\n }\n\n /** Parse and validate required fields. Does not verify signature — use verifyAndParse in production. */\n parse(body: string): TransactionWebhookEvent {\n const event = JSON.parse(body) as TransactionWebhookEvent;\n if (!event.event_type || !event.tx_id) {\n throw new ValidationError(\n 'Invalid TM webhook event: missing required fields (event_type, tx_id)',\n ['event_type', 'tx_id']\n );\n }\n return event;\n }\n\n /** Verify signature, parse, and check for replays. Throws ValidationError on any failure. */\n async verifyAndParse(body: string, signature: string): Promise<TransactionWebhookEvent> {\n const isValid = await verifyWebhookSignature(body, signature, this.secret);\n if (!isValid) {\n throw new ValidationError('Invalid webhook signature', ['signature']);\n }\n\n const event = this.parse(body);\n\n if (this.replayProtection) {\n const key = `${event.event_type}:${event.tx_id}`;\n if (await this.dedupStore.seen(key)) {\n throw new ValidationError(\n `Duplicate webhook event: ${event.event_type} for ${event.tx_id} has already been processed`,\n ['tx_id']\n );\n }\n await this.dedupStore.mark(key, this.replayWindow);\n }\n\n return event;\n }\n\n /** Verify signature, parse, and dispatch to registered handlers. */\n async handle(body: string, signature: string): Promise<void> {\n const event = await this.verifyAndParse(body, signature);\n\n if (event.event_type === 'tm.transaction.callback') {\n const pending = this.pendingWithdrawals.get(event.tx_id);\n if (pending) {\n pending._settle(event);\n this.pendingWithdrawals.delete(event.tx_id);\n }\n\n if (this.callbackHandler) {\n if (event.status === 'hold' || event.status === 'suspend') {\n await this.callbackHandler.onHeld?.(event);\n } else if (event.withholding_type === 'none') {\n await this.callbackHandler.onReleased?.(event);\n } else {\n await this.callbackHandler.onWithheld?.(event);\n }\n }\n }\n\n const handlers = this.handlers[event.event_type] as Array<EventHandler<typeof event>>;\n for (const handler of handlers) {\n await handler(event);\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vesant-sdk/transaction",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Transaction monitoring client for the Vesant Compliance Platform",
|
|
3
|
+
"version": "0.1.2-dev.0520b46",
|
|
4
|
+
"description": "Transaction monitoring and unified transaction ingestion client for the Vesant Compliance Platform",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -45,5 +45,8 @@
|
|
|
45
45
|
"type": "git",
|
|
46
46
|
"url": "https://github.com/botcalm-compliance/vesant-sdk.git",
|
|
47
47
|
"directory": "typescript/packages/transaction"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18"
|
|
48
51
|
}
|
|
49
52
|
}
|