@wdft/micropayments-sdk 0.0.4 → 0.0.5

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,406 @@
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
29
  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
- }
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
+ }
264
264
  }
265
265
 
266
266
  interface IClientOptions {
267
- currency: string;
268
- retry_ms?: number;
267
+ currency: string;
268
+ retry_ms?: number;
269
269
  }
270
270
 
271
271
  interface INewClientOptions extends IClientOptions, IServiceOptions {}
272
272
 
273
273
  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;
274
+ currency?: string;
275
+ retry_ms?: number;
276
+ metadata?: ICommitmentMetadata;
277
+ onCreated?: (commitment: Commitment) => void;
278
+ onResolved?: (commitment: Commitment) => void;
279
+ onError?: (error: Error, commitment: Commitment) => void;
280
+ onExpired?: (commitment: Commitment) => void;
281
+ onRejected?: (commitment: Commitment) => void;
282
+ onDuplicated?: (commitment: Commitment) => void;
283
+ onCheck?: (commitment: Commitment) => void;
284
+ onWaiting?: (commitment: Commitment) => void;
284
285
  }
285
286
 
286
287
  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
- }
288
+ static new(opts: INewClientOptions) {
289
+ return new Client(new Service(opts), opts);
290
+ }
291
+
292
+ constructor(
293
+ private readonly svc: Service,
294
+ private readonly options: IClientOptions,
295
+ ) {}
296
+
297
+ async createDirect(
298
+ reference_id: string,
299
+ amount: number,
300
+ options: ICommitmentOptions = {},
301
+ ): Promise<Commitment> {
302
+ const curr = options.currency ?? this.options.currency;
303
+ const response = await this.svc.createCommitment(
304
+ reference_id,
305
+ amount,
306
+ curr,
307
+ options.metadata,
308
+ );
309
+
310
+ return new Commitment(
311
+ this.svc,
312
+ {
313
+ commitment_id: response.commitment_id,
314
+ amount: amount,
315
+ currency: curr,
316
+ reference_id: reference_id,
317
+ expires_at: response.expires_at,
318
+ click_url: response.click_url,
319
+ retry_ms: this.options.retry_ms ?? 1000,
320
+ },
321
+ options,
322
+ );
323
+ }
324
+
325
+ create(reference_id: string, amount: number, on: ICallback) {
326
+ const builder = this.commitmentBuilder()
327
+ .setAmount(amount)
328
+ .setCurrency(this.options.currency)
329
+ .setMetadataUrl(globalThis?.location?.href ?? "")
330
+ .setMetadataTitle(globalThis?.document?.title ?? "")
331
+ .setReference(reference_id);
332
+
333
+ if (this.options.retry_ms) {
334
+ builder.setRetryTime(this.options.retry_ms);
335
+ }
336
+
337
+ builder
338
+ .onCreated((commitment) => {
339
+ commitment.startChecking();
340
+ return on(null, {
341
+ status: commitment.getStatus(),
342
+ reference_id,
343
+ commitment_id: commitment.getCommitmentId(),
344
+ click_url: commitment.getClickUrl(),
345
+ });
346
+ })
347
+ .onWaiting((commitment) =>
348
+ on(null, {
349
+ status: commitment.getStatus(),
350
+ reference_id,
351
+ commitment_id: commitment.getCommitmentId(),
352
+ click_url: commitment.getClickUrl(),
353
+ })
354
+ )
355
+ .onDuplicated((commitment) =>
356
+ on(null, {
357
+ status: commitment.getStatus(),
358
+ reference_id,
359
+ commitment_id: commitment.getCommitmentId(),
360
+ click_url: commitment.getClickUrl(),
361
+ })
362
+ )
363
+ .onExpired((commitment) =>
364
+ on(null, {
365
+ status: commitment.getStatus(),
366
+ reference_id,
367
+ commitment_id: commitment.getCommitmentId(),
368
+ click_url: commitment.getClickUrl(),
369
+ })
370
+ )
371
+ .onRejected((commitment) =>
372
+ on(null, {
373
+ status: commitment.getStatus(),
374
+ reference_id,
375
+ commitment_id: commitment.getCommitmentId(),
376
+ click_url: commitment.getClickUrl(),
377
+ })
378
+ )
379
+ .onResolved((commitment) =>
380
+ on(null, {
381
+ status: commitment.getStatus(),
382
+ reference_id,
383
+ commitment_id: commitment.getCommitmentId(),
384
+ click_url: commitment.getClickUrl(),
385
+ })
386
+ )
387
+ .onError((error, commitment) => {
388
+ commitment?.stopChecking();
389
+ on(error, null);
390
+ })
391
+ .exec();
392
+ }
393
+
394
+ commitmentBuilder(): CommitmentObservabileBuilder {
395
+ return new CommitmentObservabileBuilder(this);
396
+ }
393
397
  }
394
398
 
395
399
  export interface CallbackPayload {
396
- status: CommitmentStatus;
397
- reference_id: string;
398
- commitment_id: string;
399
- click_url: string;
400
+ status: CommitmentStatus;
401
+ reference_id: string;
402
+ commitment_id: string;
403
+ click_url: string;
400
404
  }
401
405
 
402
406
  type OnStatusCallback = (error: null, payload: CallbackPayload) => void;
@@ -404,117 +408,128 @@ type OnStatusErrorCallback = (error: Error, payload: null) => void;
404
408
  export type ICallback = OnStatusCallback & OnStatusErrorCallback;
405
409
 
406
410
  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
- }
411
+ private _reference_id: string | undefined;
412
+ private _amount: number | undefined;
413
+ private _retry_ms = 1000;
414
+ private _currency: string | undefined;
415
+ private _metadata: ICommitmentMetadata = { title: "", url: "" };
416
+ private _onCreatedFn: (commitment: Commitment) => void = () => {};
417
+ private _onResolvedFn: (commitment: Commitment) => void = () => {};
418
+ private _onErrorFn: (error: Error, commitment?: Commitment) => void =
419
+ () => {};
420
+ private _onExpiredFn: (commitment: Commitment) => void = () => {};
421
+ private _onRejectedFn: (commitment: Commitment) => void = () => {};
422
+ private _onDuplicatedFn: (commitment: Commitment) => void = () => {};
423
+ private _onCheckFn: (commitment: Commitment) => void = () => {};
424
+ private _onWaitingFn: (commitment: Commitment) => void = () => {};
425
+
426
+ constructor(private readonly client: Client) {}
427
+
428
+ setMetadataTitle(title: string = "") {
429
+ this._metadata.title = title;
430
+ return this;
431
+ }
432
+
433
+ setMetadataUrl(url: string = "") {
434
+ this._metadata.url = url;
435
+ return this;
436
+ }
437
+
438
+ setRetryTime(retry: number) {
439
+ this._retry_ms = retry;
440
+ return this;
441
+ }
442
+
443
+ setAmount(amount: number) {
444
+ this._amount = amount;
445
+ return this;
446
+ }
447
+
448
+ setReference(reference: string) {
449
+ this._reference_id = reference;
450
+ return this;
451
+ }
452
+
453
+ setCurrency(currency: string) {
454
+ this._currency = currency;
455
+ return this;
456
+ }
457
+
458
+ onCreated(cb: (commitment: Commitment) => void) {
459
+ this._onCreatedFn = cb;
460
+ return this;
461
+ }
462
+
463
+ onResolved(cb: (commitment: Commitment) => void) {
464
+ this._onResolvedFn = cb;
465
+ return this;
466
+ }
467
+
468
+ onExpired(cb: (commitment: Commitment) => void) {
469
+ this._onExpiredFn = cb;
470
+ return this;
471
+ }
472
+
473
+ onRejected(cb: (commitment: Commitment) => void) {
474
+ this._onRejectedFn = cb;
475
+ return this;
476
+ }
477
+
478
+ onError(cb: (error: Error, commitment?: Commitment) => void) {
479
+ this._onErrorFn = cb;
480
+ return this;
481
+ }
482
+
483
+ onDuplicated(cb: (commitment: Commitment) => void) {
484
+ this._onDuplicatedFn = cb;
485
+ return this;
486
+ }
487
+
488
+ onCheck(cb: (commitment: Commitment) => void) {
489
+ this._onCheckFn = cb;
490
+ return this;
491
+ }
492
+
493
+ onWaiting(cb: (commitment: Commitment) => void) {
494
+ this._onWaitingFn = cb;
495
+ return this;
496
+ }
497
+
498
+ exec() {
499
+ if (!this._reference_id) {
500
+ throw new Error("reference_id must be defined");
501
+ }
502
+
503
+ if (!this._amount) {
504
+ throw new Error("amount must be defined");
505
+ }
506
+
507
+ if (!this._currency) {
508
+ throw new Error("currency must be defined");
509
+ }
510
+
511
+ this.client
512
+ .createDirect(this._reference_id, this._amount, {
513
+ retry_ms: this._retry_ms,
514
+ currency: this._currency,
515
+ metadata: this._metadata,
516
+ onCheck: this._onCheckFn,
517
+ onCreated: this._onCreatedFn,
518
+ onDuplicated: this._onDuplicatedFn,
519
+ onError: this._onErrorFn,
520
+ onExpired: this._onExpiredFn,
521
+ onWaiting: this._onWaitingFn,
522
+ onRejected: this._onRejectedFn,
523
+ onResolved: this._onResolvedFn,
524
+ })
525
+ .catch((error) => {
526
+ if (this._onErrorFn) {
527
+ this._onErrorFn(error);
528
+ } else {
529
+ throw error;
530
+ }
531
+ });
532
+ }
518
533
  }
519
534
 
520
535
  export type { Commitment };