@wdft/micropayments-sdk 0.0.4 → 0.0.6

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/src/client.ts CHANGED
@@ -1,402 +1,412 @@
1
- import { type IServiceOptions, Service } from "./service.js";
1
+ import { type IServiceOptions, type ICommitmentMetadata, Service } from "./service.js";
2
2
 
3
3
  interface ICommitmentPayload {
4
- commitment_id: string;
5
- amount: number;
6
- currency: string;
7
- reference_id: string;
8
- expires_at: number;
9
- click_url: string;
10
- retry_ms: number;
11
- message: string;
12
- status: CommitmentStatus;
4
+ commitment_id: string;
5
+ amount: number;
6
+ currency: string;
7
+ reference_id: string;
8
+ expires_at: number;
9
+ click_url: string;
10
+ retry_ms: number;
11
+ message: string;
12
+ status: CommitmentStatus;
13
13
  }
14
14
 
15
15
  export type CommitmentStatus =
16
- | "initializing"
17
- | "created"
18
- | "waiting"
19
- | "expired"
20
- | "resolved"
21
- | "rejected"
22
- | "duplicated";
16
+ | "initializing"
17
+ | "created"
18
+ | "waiting"
19
+ | "expired"
20
+ | "resolved"
21
+ | "rejected"
22
+ | "duplicated";
23
23
 
24
24
  export type ICommitmentPayloadSimple = Omit<
25
- ICommitmentPayload,
26
- "status" | "message"
25
+ ICommitmentPayload,
26
+ "status" | "message"
27
27
  >;
28
28
 
29
+ const DEFAULT_RETRY_TIME_MS = 5000;
30
+
29
31
  class Commitment {
30
- private readonly payload: ICommitmentPayload;
31
-
32
- constructor(
33
- private readonly service: Service,
34
- payload: ICommitmentPayloadSimple,
35
- private readonly options: ICommitmentOptions,
36
- ) {
37
- this.payload = {
38
- message: "Creating...",
39
- status: "initializing",
40
- ...payload,
41
- };
42
-
43
- if (this.options.onCreated) {
44
- this.options.onCreated(this);
45
- }
46
- }
47
-
48
- isExpired() {
49
- return Date.now() > this.payload.expires_at;
50
- }
51
-
52
- isRejected() {
53
- return this.getStatus() === "rejected";
54
- }
55
-
56
- isResolved() {
57
- return this.getStatus() === "resolved";
58
- }
59
-
60
- getStatus(): CommitmentStatus {
61
- return this.payload.status;
62
- }
63
-
64
- getClickUrl() {
65
- return this.payload.click_url;
66
- }
67
-
68
- getAmount() {
69
- return this.payload.amount;
70
- }
71
-
72
- getCurrency() {
73
- return this.payload.currency;
74
- }
75
-
76
- getReferenceId() {
77
- return this.payload.reference_id;
78
- }
79
-
80
- getCommitmentId() {
81
- return this.payload.commitment_id;
82
- }
83
-
84
- markAsCreated() {
85
- if (this.payload.status === "created") {
86
- return;
87
- }
88
-
89
- this.payload.status = "created";
90
-
91
- if (this.options.onCreated) {
92
- this.options.onCreated(this);
93
- }
94
- }
95
-
96
- markAsDuplicated() {
97
- if (this.payload.status === "duplicated") {
98
- this.stopChecking();
99
- return;
100
- }
101
-
102
- this.payload.status = "duplicated";
103
-
104
- this.stopChecking();
105
- if (this.options.onDuplicated) {
106
- this.options.onDuplicated(this);
107
- }
108
- }
109
-
110
- markAsWaiting() {
111
- if (this.payload.status === "waiting") {
112
- return;
113
- }
114
-
115
- this.payload.status = "waiting";
116
-
117
- if (this.options.onWaiting) {
118
- this.options.onWaiting(this);
119
- }
120
- }
121
-
122
- markAsExpired() {
123
- if (this.payload.status === "expired") {
124
- this.stopChecking();
125
- return;
126
- }
127
-
128
- this.payload.status = "expired";
129
-
130
- if (this.options.onExpired) {
131
- this.options.onExpired(this);
132
- }
133
-
134
- this.stopChecking();
135
- }
136
-
137
- markAsRejected() {
138
- if (this.payload.status === "rejected") {
139
- this.stopChecking();
140
- return;
141
- }
142
-
143
- this.payload.status = "rejected";
144
-
145
- if (this.options.onRejected) {
146
- this.options.onRejected(this);
147
- }
148
-
149
- this.stopChecking();
150
- }
151
-
152
- markAsResolved() {
153
- if (this.payload.status === "resolved") {
154
- this.stopChecking();
155
- return;
156
- }
157
-
158
- this.payload.status = "resolved";
159
-
160
- this.stopChecking();
161
-
162
- if (this.options.onResolved) {
163
- this.options.onResolved(this);
164
- }
165
- }
166
-
167
- private retries = 0;
168
- private currentTimer: number | null = null;
169
-
170
- getNextCallTime() {
171
- this.retries++;
172
- return Math.min(
173
- 5000,
174
- this.payload.retry_ms + this.payload.retry_ms * (this.retries / 10),
175
- );
176
- }
177
-
178
- startChecking() {
179
- if (this.currentCheck) {
180
- return;
181
- }
182
-
183
- this.currentCheck = this.check()
184
- .then((status) => {
185
- const nextTime = this.getNextCallTime();
186
- if (status === "waiting") {
187
- this.currentTimer = setTimeout(
188
- () => this.startChecking(),
189
- nextTime,
190
- ) as unknown as number;
191
- }
192
- return status;
193
- })
194
- .finally(() => {
195
- this.stopChecking(false);
196
- });
197
- }
198
-
199
- stopChecking(withTimer = true) {
200
- this.currentCheck = null;
201
- if (this.currentTimer && withTimer) {
202
- clearTimeout(this.currentTimer);
203
- }
204
- }
205
-
206
- private currentCheck: Promise<CommitmentStatus> | null = null;
207
-
208
- async check(): Promise<CommitmentStatus> {
209
- if (this.isExpired()) {
210
- this.markAsExpired();
211
- return this.getStatus();
212
- }
213
-
214
- if (this.isRejected()) {
215
- this.markAsRejected();
216
- return this.getStatus();
217
- }
218
-
219
- if (this.isResolved()) {
220
- this.markAsResolved();
221
- return this.getStatus();
222
- }
223
-
224
- try {
225
- const status = await this.service.checkCommitment(this.getCommitmentId());
226
-
227
- switch (status.status) {
228
- case "duplicated": {
229
- this.markAsDuplicated();
230
- break;
231
- }
232
- case "expired": {
233
- this.markAsExpired();
234
- break;
235
- }
236
- case "resolved": {
237
- this.markAsResolved();
238
- break;
239
- }
240
- case "waiting": {
241
- this.markAsWaiting();
242
- break;
243
- }
244
- case "rejected": {
245
- this.markAsRejected();
246
- break;
247
- }
248
- }
249
-
250
- if (this.options.onCheck) {
251
- this.options.onCheck(this);
252
- }
253
- } catch (error) {
254
- if (this.options.onError) {
255
- this.options.onError(error as Error, this);
256
- this.stopChecking();
257
- } else {
258
- throw error;
259
- }
260
- }
261
-
262
- return this.getStatus();
263
- }
32
+ private readonly payload: ICommitmentPayload;
33
+
34
+ constructor(
35
+ private readonly service: Service,
36
+ payload: ICommitmentPayloadSimple,
37
+ private readonly options: ICommitmentOptions,
38
+ ) {
39
+ this.payload = {
40
+ message: "Creating...",
41
+ status: "initializing",
42
+ ...payload,
43
+ };
44
+
45
+ if (this.options.onCreated) {
46
+ this.options.onCreated(this);
47
+ }
48
+ }
49
+
50
+ isExpired() {
51
+ return Date.now() > this.payload.expires_at;
52
+ }
53
+
54
+ isRejected() {
55
+ return this.getStatus() === "rejected";
56
+ }
57
+
58
+ isResolved() {
59
+ return this.getStatus() === "resolved";
60
+ }
61
+
62
+ getStatus(): CommitmentStatus {
63
+ return this.payload.status;
64
+ }
65
+
66
+ getClickUrl() {
67
+ return this.payload.click_url;
68
+ }
69
+
70
+ getAmount() {
71
+ return this.payload.amount;
72
+ }
73
+
74
+ getCurrency() {
75
+ return this.payload.currency;
76
+ }
77
+
78
+ getReferenceId() {
79
+ return this.payload.reference_id;
80
+ }
81
+
82
+ getCommitmentId() {
83
+ return this.payload.commitment_id;
84
+ }
85
+
86
+ markAsCreated() {
87
+ if (this.payload.status === "created") {
88
+ return;
89
+ }
90
+
91
+ this.payload.status = "created";
92
+
93
+ if (this.options.onCreated) {
94
+ this.options.onCreated(this);
95
+ }
96
+ }
97
+
98
+ markAsDuplicated() {
99
+ if (this.payload.status === "duplicated") {
100
+ this.stopChecking();
101
+ return;
102
+ }
103
+
104
+ this.payload.status = "duplicated";
105
+
106
+ this.stopChecking();
107
+ if (this.options.onDuplicated) {
108
+ this.options.onDuplicated(this);
109
+ }
110
+ }
111
+
112
+ markAsWaiting() {
113
+ if (this.payload.status === "waiting") {
114
+ return;
115
+ }
116
+
117
+ this.payload.status = "waiting";
118
+
119
+ if (this.options.onWaiting) {
120
+ this.options.onWaiting(this);
121
+ }
122
+ }
123
+
124
+ markAsExpired() {
125
+ if (this.payload.status === "expired") {
126
+ this.stopChecking();
127
+ return;
128
+ }
129
+
130
+ this.payload.status = "expired";
131
+
132
+ if (this.options.onExpired) {
133
+ this.options.onExpired(this);
134
+ }
135
+
136
+ this.stopChecking();
137
+ }
138
+
139
+ markAsRejected() {
140
+ if (this.payload.status === "rejected") {
141
+ this.stopChecking();
142
+ return;
143
+ }
144
+
145
+ this.payload.status = "rejected";
146
+
147
+ if (this.options.onRejected) {
148
+ this.options.onRejected(this);
149
+ }
150
+
151
+ this.stopChecking();
152
+ }
153
+
154
+ markAsResolved() {
155
+ if (this.payload.status === "resolved") {
156
+ this.stopChecking();
157
+ return;
158
+ }
159
+
160
+ this.payload.status = "resolved";
161
+
162
+ this.stopChecking();
163
+
164
+ if (this.options.onResolved) {
165
+ this.options.onResolved(this);
166
+ }
167
+ }
168
+
169
+ private retries = 0;
170
+ private currentTimer: number | null = null;
171
+
172
+ getNextCallTime() {
173
+ this.retries++;
174
+ return Math.min(
175
+ 5000,
176
+ this.payload.retry_ms + this.payload.retry_ms * (this.retries / 10),
177
+ );
178
+ }
179
+
180
+ startChecking() {
181
+ if (this.currentCheck) {
182
+ return;
183
+ }
184
+
185
+ this.currentCheck = this.check()
186
+ .then((status) => {
187
+ const nextTime = this.getNextCallTime();
188
+ if (status === "waiting") {
189
+ this.currentTimer = setTimeout(
190
+ () => this.startChecking(),
191
+ nextTime,
192
+ ) as unknown as number;
193
+ }
194
+ return status;
195
+ })
196
+ .finally(() => {
197
+ this.stopChecking(false);
198
+ });
199
+ }
200
+
201
+ stopChecking(withTimer = true) {
202
+ this.currentCheck = null;
203
+ if (this.currentTimer && withTimer) {
204
+ clearTimeout(this.currentTimer);
205
+ }
206
+ }
207
+
208
+ private currentCheck: Promise<CommitmentStatus> | null = null;
209
+
210
+ async check(): Promise<CommitmentStatus> {
211
+ if (this.isExpired()) {
212
+ this.markAsExpired();
213
+ return this.getStatus();
214
+ }
215
+
216
+ if (this.isRejected()) {
217
+ this.markAsRejected();
218
+ return this.getStatus();
219
+ }
220
+
221
+ if (this.isResolved()) {
222
+ this.markAsResolved();
223
+ return this.getStatus();
224
+ }
225
+
226
+ try {
227
+ const status = await this.service.checkCommitment(this.getCommitmentId());
228
+
229
+ if (status.retry_after) {
230
+ this.options.retry_ms = Math.max(this.options.retry_ms || DEFAULT_RETRY_TIME_MS, status.retry_after)
231
+ }
232
+
233
+ switch (status.status) {
234
+ case "duplicated": {
235
+ this.markAsDuplicated();
236
+ break;
237
+ }
238
+ case "expired": {
239
+ this.markAsExpired();
240
+ break;
241
+ }
242
+ case "resolved": {
243
+ this.markAsResolved();
244
+ break;
245
+ }
246
+ case "waiting": {
247
+ this.markAsWaiting();
248
+ break;
249
+ }
250
+ case "rejected": {
251
+ this.markAsRejected();
252
+ break;
253
+ }
254
+ }
255
+
256
+ if (this.options.onCheck) {
257
+ this.options.onCheck(this);
258
+ }
259
+ } catch (error) {
260
+ if (this.options.onError) {
261
+ this.options.onError(error as Error, this);
262
+ this.stopChecking();
263
+ } else {
264
+ throw error;
265
+ }
266
+ }
267
+
268
+ return this.getStatus();
269
+ }
264
270
  }
265
271
 
266
272
  interface IClientOptions {
267
- currency: string;
268
- retry_ms?: number;
273
+ currency: string;
274
+ retry_ms?: number;
269
275
  }
270
276
 
271
277
  interface INewClientOptions extends IClientOptions, IServiceOptions {}
272
278
 
273
279
  interface ICommitmentOptions {
274
- currency?: string;
275
- retry_ms?: number;
276
- onCreated?: (commitment: Commitment) => void;
277
- onResolved?: (commitment: Commitment) => void;
278
- onError?: (error: Error, commitment: Commitment) => void;
279
- onExpired?: (commitment: Commitment) => void;
280
- onRejected?: (commitment: Commitment) => void;
281
- onDuplicated?: (commitment: Commitment) => void;
282
- onCheck?: (commitment: Commitment) => void;
283
- onWaiting?: (commitment: Commitment) => void;
280
+ currency?: string;
281
+ retry_ms?: number;
282
+ metadata?: ICommitmentMetadata;
283
+ onCreated?: (commitment: Commitment) => void;
284
+ onResolved?: (commitment: Commitment) => void;
285
+ onError?: (error: Error, commitment: Commitment) => void;
286
+ onExpired?: (commitment: Commitment) => void;
287
+ onRejected?: (commitment: Commitment) => void;
288
+ onDuplicated?: (commitment: Commitment) => void;
289
+ onCheck?: (commitment: Commitment) => void;
290
+ onWaiting?: (commitment: Commitment) => void;
284
291
  }
285
292
 
286
293
  export class Client {
287
- static new(opts: INewClientOptions) {
288
- return new Client(new Service(opts), opts);
289
- }
290
-
291
- constructor(
292
- private readonly svc: Service,
293
- private readonly options: IClientOptions,
294
- ) {}
295
-
296
- async createDirect(
297
- reference_id: string,
298
- amount: number,
299
- options: ICommitmentOptions = {},
300
- ): Promise<Commitment> {
301
- const curr = options.currency ?? this.options.currency;
302
- const response = await this.svc.createCommitment(
303
- reference_id,
304
- amount,
305
- curr,
306
- );
307
-
308
- return new Commitment(
309
- this.svc,
310
- {
311
- commitment_id: response.commitment_id,
312
- amount: amount,
313
- currency: curr,
314
- reference_id: reference_id,
315
- expires_at: response.expires_at,
316
- click_url: response.click_url,
317
- retry_ms: this.options.retry_ms ?? 1000,
318
- },
319
- options,
320
- );
321
- }
322
-
323
- create(reference_id: string, amount: number, on: ICallback) {
324
- const builder = this.commitmentBuilder()
325
- .setAmount(amount)
326
- .setCurrency(this.options.currency)
327
- .setReference(reference_id);
328
-
329
- if (this.options.retry_ms) {
330
- builder.setRetryTime(this.options.retry_ms);
331
- }
332
-
333
- builder
334
- .onCreated((commitment) => {
335
- commitment.startChecking();
336
- return on(null, {
337
- status: commitment.getStatus(),
338
- reference_id,
339
- commitment_id: commitment.getCommitmentId(),
340
- click_url: commitment.getClickUrl(),
341
- });
342
- })
343
- .onWaiting((commitment) =>
344
- on(null, {
345
- status: commitment.getStatus(),
346
- reference_id,
347
- commitment_id: commitment.getCommitmentId(),
348
- click_url: commitment.getClickUrl(),
349
- }),
350
- )
351
- .onDuplicated((commitment) =>
352
- on(null, {
353
- status: commitment.getStatus(),
354
- reference_id,
355
- commitment_id: commitment.getCommitmentId(),
356
- click_url: commitment.getClickUrl(),
357
- }),
358
- )
359
- .onExpired((commitment) =>
360
- on(null, {
361
- status: commitment.getStatus(),
362
- reference_id,
363
- commitment_id: commitment.getCommitmentId(),
364
- click_url: commitment.getClickUrl(),
365
- }),
366
- )
367
- .onRejected((commitment) =>
368
- on(null, {
369
- status: commitment.getStatus(),
370
- reference_id,
371
- commitment_id: commitment.getCommitmentId(),
372
- click_url: commitment.getClickUrl(),
373
- }),
374
- )
375
- .onResolved((commitment) =>
376
- on(null, {
377
- status: commitment.getStatus(),
378
- reference_id,
379
- commitment_id: commitment.getCommitmentId(),
380
- click_url: commitment.getClickUrl(),
381
- }),
382
- )
383
- .onError((error, commitment) => {
384
- commitment?.stopChecking();
385
- on(error, null);
386
- })
387
- .exec();
388
- }
389
-
390
- commitmentBuilder(): CommitmentObservabileBuilder {
391
- return new CommitmentObservabileBuilder(this);
392
- }
294
+ static new(opts: INewClientOptions) {
295
+ return new Client(new Service(opts), opts);
296
+ }
297
+
298
+ constructor(
299
+ private readonly svc: Service,
300
+ private readonly options: IClientOptions,
301
+ ) {}
302
+
303
+ async createDirect(
304
+ reference_id: string,
305
+ amount: number,
306
+ options: ICommitmentOptions = {},
307
+ ): Promise<Commitment> {
308
+ const curr = options.currency ?? this.options.currency;
309
+ const response = await this.svc.createCommitment(
310
+ reference_id,
311
+ amount,
312
+ curr,
313
+ options.metadata,
314
+ );
315
+
316
+ return new Commitment(
317
+ this.svc,
318
+ {
319
+ commitment_id: response.commitment_id,
320
+ amount: amount,
321
+ currency: curr,
322
+ reference_id: reference_id,
323
+ expires_at: response.expires_at,
324
+ click_url: response.click_url,
325
+ retry_ms: this.options.retry_ms ?? DEFAULT_RETRY_TIME_MS,
326
+ },
327
+ options,
328
+ );
329
+ }
330
+
331
+ create(reference_id: string, amount: number, on: ICallback) {
332
+ const builder = this.commitmentBuilder()
333
+ .setAmount(amount)
334
+ .setCurrency(this.options.currency)
335
+ .setMetadataUrl(globalThis?.location?.href ?? "")
336
+ .setMetadataTitle(globalThis?.document?.title ?? "")
337
+ .setReference(reference_id);
338
+
339
+ if (this.options.retry_ms) {
340
+ builder.setRetryTime(this.options.retry_ms);
341
+ }
342
+
343
+ builder
344
+ .onCreated((commitment) => {
345
+ commitment.startChecking();
346
+ return on(null, {
347
+ status: commitment.getStatus(),
348
+ reference_id,
349
+ commitment_id: commitment.getCommitmentId(),
350
+ click_url: commitment.getClickUrl(),
351
+ });
352
+ })
353
+ .onWaiting((commitment) =>
354
+ on(null, {
355
+ status: commitment.getStatus(),
356
+ reference_id,
357
+ commitment_id: commitment.getCommitmentId(),
358
+ click_url: commitment.getClickUrl(),
359
+ })
360
+ )
361
+ .onDuplicated((commitment) =>
362
+ on(null, {
363
+ status: commitment.getStatus(),
364
+ reference_id,
365
+ commitment_id: commitment.getCommitmentId(),
366
+ click_url: commitment.getClickUrl(),
367
+ })
368
+ )
369
+ .onExpired((commitment) =>
370
+ on(null, {
371
+ status: commitment.getStatus(),
372
+ reference_id,
373
+ commitment_id: commitment.getCommitmentId(),
374
+ click_url: commitment.getClickUrl(),
375
+ })
376
+ )
377
+ .onRejected((commitment) =>
378
+ on(null, {
379
+ status: commitment.getStatus(),
380
+ reference_id,
381
+ commitment_id: commitment.getCommitmentId(),
382
+ click_url: commitment.getClickUrl(),
383
+ })
384
+ )
385
+ .onResolved((commitment) =>
386
+ on(null, {
387
+ status: commitment.getStatus(),
388
+ reference_id,
389
+ commitment_id: commitment.getCommitmentId(),
390
+ click_url: commitment.getClickUrl(),
391
+ })
392
+ )
393
+ .onError((error, commitment) => {
394
+ commitment?.stopChecking();
395
+ on(error, null);
396
+ })
397
+ .exec();
398
+ }
399
+
400
+ commitmentBuilder(): CommitmentObservabileBuilder {
401
+ return new CommitmentObservabileBuilder(this);
402
+ }
393
403
  }
394
404
 
395
405
  export interface CallbackPayload {
396
- status: CommitmentStatus;
397
- reference_id: string;
398
- commitment_id: string;
399
- click_url: string;
406
+ status: CommitmentStatus;
407
+ reference_id: string;
408
+ commitment_id: string;
409
+ click_url: string;
400
410
  }
401
411
 
402
412
  type OnStatusCallback = (error: null, payload: CallbackPayload) => void;
@@ -404,117 +414,128 @@ type OnStatusErrorCallback = (error: Error, payload: null) => void;
404
414
  export type ICallback = OnStatusCallback & OnStatusErrorCallback;
405
415
 
406
416
  class CommitmentObservabileBuilder {
407
- private _reference_id: string | undefined;
408
- private _amount: number | undefined;
409
- private _retry_ms = 1000;
410
- private _currency: string | undefined;
411
-
412
- private _onCreatedFn: (commitment: Commitment) => void = () => {};
413
- private _onResolvedFn: (commitment: Commitment) => void = () => {};
414
- private _onErrorFn: (error: Error, commitment?: Commitment) => void =
415
- () => {};
416
- private _onExpiredFn: (commitment: Commitment) => void = () => {};
417
- private _onRejectedFn: (commitment: Commitment) => void = () => {};
418
- private _onDuplicatedFn: (commitment: Commitment) => void = () => {};
419
- private _onCheckFn: (commitment: Commitment) => void = () => {};
420
- private _onWaitingFn: (commitment: Commitment) => void = () => {};
421
-
422
- constructor(private readonly client: Client) {}
423
-
424
- setRetryTime(retry: number) {
425
- this._retry_ms = retry;
426
- return this;
427
- }
428
-
429
- setAmount(amount: number) {
430
- this._amount = amount;
431
- return this;
432
- }
433
-
434
- setReference(reference: string) {
435
- this._reference_id = reference;
436
- return this;
437
- }
438
-
439
- setCurrency(currency: string) {
440
- this._currency = currency;
441
- return this;
442
- }
443
-
444
- onCreated(cb: (commitment: Commitment) => void) {
445
- this._onCreatedFn = cb;
446
- return this;
447
- }
448
-
449
- onResolved(cb: (commitment: Commitment) => void) {
450
- this._onResolvedFn = cb;
451
- return this;
452
- }
453
-
454
- onExpired(cb: (commitment: Commitment) => void) {
455
- this._onExpiredFn = cb;
456
- return this;
457
- }
458
-
459
- onRejected(cb: (commitment: Commitment) => void) {
460
- this._onRejectedFn = cb;
461
- return this;
462
- }
463
-
464
- onError(cb: (error: Error, commitment?: Commitment) => void) {
465
- this._onErrorFn = cb;
466
- return this;
467
- }
468
-
469
- onDuplicated(cb: (commitment: Commitment) => void) {
470
- this._onDuplicatedFn = cb;
471
- return this;
472
- }
473
-
474
- onCheck(cb: (commitment: Commitment) => void) {
475
- this._onCheckFn = cb;
476
- return this;
477
- }
478
-
479
- onWaiting(cb: (commitment: Commitment) => void) {
480
- this._onWaitingFn = cb;
481
- return this;
482
- }
483
-
484
- exec() {
485
- if (!this._reference_id) {
486
- throw new Error("reference_id must be defined");
487
- }
488
-
489
- if (!this._amount) {
490
- throw new Error("amount must be defined");
491
- }
492
-
493
- if (!this._currency) {
494
- throw new Error("currency must be defined");
495
- }
496
-
497
- this.client
498
- .createDirect(this._reference_id, this._amount, {
499
- retry_ms: this._retry_ms,
500
- currency: this._currency,
501
- onCheck: this._onCheckFn,
502
- onCreated: this._onCreatedFn,
503
- onDuplicated: this._onDuplicatedFn,
504
- onError: this._onErrorFn,
505
- onExpired: this._onExpiredFn,
506
- onWaiting: this._onWaitingFn,
507
- onRejected: this._onRejectedFn,
508
- onResolved: this._onResolvedFn,
509
- })
510
- .catch((error) => {
511
- if (this._onErrorFn) {
512
- this._onErrorFn(error);
513
- } else {
514
- throw error;
515
- }
516
- });
517
- }
417
+ private _reference_id: string | undefined;
418
+ private _amount: number | undefined;
419
+ private _retry_ms = DEFAULT_RETRY_TIME_MS;
420
+ private _currency: string | undefined;
421
+ private _metadata: ICommitmentMetadata = { title: "", url: "" };
422
+ private _onCreatedFn: (commitment: Commitment) => void = () => {};
423
+ private _onResolvedFn: (commitment: Commitment) => void = () => {};
424
+ private _onErrorFn: (error: Error, commitment?: Commitment) => void =
425
+ () => {};
426
+ private _onExpiredFn: (commitment: Commitment) => void = () => {};
427
+ private _onRejectedFn: (commitment: Commitment) => void = () => {};
428
+ private _onDuplicatedFn: (commitment: Commitment) => void = () => {};
429
+ private _onCheckFn: (commitment: Commitment) => void = () => {};
430
+ private _onWaitingFn: (commitment: Commitment) => void = () => {};
431
+
432
+ constructor(private readonly client: Client) {}
433
+
434
+ setMetadataTitle(title: string = "") {
435
+ this._metadata.title = title;
436
+ return this;
437
+ }
438
+
439
+ setMetadataUrl(url: string = "") {
440
+ this._metadata.url = url;
441
+ return this;
442
+ }
443
+
444
+ setRetryTime(retry: number) {
445
+ this._retry_ms = retry;
446
+ return this;
447
+ }
448
+
449
+ setAmount(amount: number) {
450
+ this._amount = amount;
451
+ return this;
452
+ }
453
+
454
+ setReference(reference: string) {
455
+ this._reference_id = reference;
456
+ return this;
457
+ }
458
+
459
+ setCurrency(currency: string) {
460
+ this._currency = currency;
461
+ return this;
462
+ }
463
+
464
+ onCreated(cb: (commitment: Commitment) => void) {
465
+ this._onCreatedFn = cb;
466
+ return this;
467
+ }
468
+
469
+ onResolved(cb: (commitment: Commitment) => void) {
470
+ this._onResolvedFn = cb;
471
+ return this;
472
+ }
473
+
474
+ onExpired(cb: (commitment: Commitment) => void) {
475
+ this._onExpiredFn = cb;
476
+ return this;
477
+ }
478
+
479
+ onRejected(cb: (commitment: Commitment) => void) {
480
+ this._onRejectedFn = cb;
481
+ return this;
482
+ }
483
+
484
+ onError(cb: (error: Error, commitment?: Commitment) => void) {
485
+ this._onErrorFn = cb;
486
+ return this;
487
+ }
488
+
489
+ onDuplicated(cb: (commitment: Commitment) => void) {
490
+ this._onDuplicatedFn = cb;
491
+ return this;
492
+ }
493
+
494
+ onCheck(cb: (commitment: Commitment) => void) {
495
+ this._onCheckFn = cb;
496
+ return this;
497
+ }
498
+
499
+ onWaiting(cb: (commitment: Commitment) => void) {
500
+ this._onWaitingFn = cb;
501
+ return this;
502
+ }
503
+
504
+ exec() {
505
+ if (!this._reference_id) {
506
+ throw new Error("reference_id must be defined");
507
+ }
508
+
509
+ if (!this._amount) {
510
+ throw new Error("amount must be defined");
511
+ }
512
+
513
+ if (!this._currency) {
514
+ throw new Error("currency must be defined");
515
+ }
516
+
517
+ this.client
518
+ .createDirect(this._reference_id, this._amount, {
519
+ retry_ms: this._retry_ms,
520
+ currency: this._currency,
521
+ metadata: this._metadata,
522
+ onCheck: this._onCheckFn,
523
+ onCreated: this._onCreatedFn,
524
+ onDuplicated: this._onDuplicatedFn,
525
+ onError: this._onErrorFn,
526
+ onExpired: this._onExpiredFn,
527
+ onWaiting: this._onWaitingFn,
528
+ onRejected: this._onRejectedFn,
529
+ onResolved: this._onResolvedFn,
530
+ })
531
+ .catch((error) => {
532
+ if (this._onErrorFn) {
533
+ this._onErrorFn(error);
534
+ } else {
535
+ throw error;
536
+ }
537
+ });
538
+ }
518
539
  }
519
540
 
520
541
  export type { Commitment };