|
1
|
import { IPaymentModuleService } from "@medusajs/framework/types" |
|
2
|
import { |
|
3
|
Module, |
|
4
|
Modules, |
|
5
|
PaymentActions, |
|
6
|
promiseAll, |
|
7
|
} from "@medusajs/framework/utils" |
|
8
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
9
|
import { PaymentModuleService } from "@services" |
|
10
|
import { |
|
11
|
createPaymentCollections, |
|
12
|
createPayments, |
|
13
|
createPaymentSessions, |
|
14
|
} from "../../../__fixtures__" |
|
15
|
|
|
16
|
jest.setTimeout(30000) |
|
17
|
|
|
18
|
moduleIntegrationTestRunner<IPaymentModuleService>({ |
|
19
|
moduleName: Modules.PAYMENT, |
|
20
|
testSuite: ({ MikroOrmWrapper, service }) => { |
|
21
|
describe("Payment Module Service", () => { |
|
22
|
beforeEach(() => { |
|
23
|
jest.clearAllMocks() |
|
24
|
}) |
|
25
|
|
|
26
|
it(`should export the appropriate linkable configuration`, () => { |
|
27
|
const linkable = Module(Modules.PAYMENT, { |
|
28
|
service: PaymentModuleService, |
|
29
|
}).linkable |
|
30
|
|
|
31
|
expect(Object.keys(linkable)).toHaveLength(8) |
|
32
|
expect(Object.keys(linkable)).toEqual([ |
|
33
|
"paymentCollection", |
|
34
|
"paymentSession", |
|
35
|
"payment", |
|
36
|
"capture", |
|
37
|
"refund", |
|
38
|
"refundReason", |
|
39
|
"accountHolder", |
|
40
|
"paymentProvider", |
|
41
|
]) |
|
42
|
|
|
43
|
Object.keys(linkable).forEach((key) => { |
|
44
|
delete linkable[key].toJSON |
|
45
|
}) |
|
46
|
|
|
47
|
expect(linkable).toEqual({ |
|
48
|
paymentCollection: { |
|
49
|
id: { |
|
50
|
linkable: "payment_collection_id", |
|
51
|
primaryKey: "id", |
|
52
|
serviceName: "payment", |
|
53
|
field: "paymentCollection", |
|
54
|
entity: "PaymentCollection", |
|
55
|
}, |
|
56
|
}, |
|
57
|
paymentSession: { |
|
58
|
id: { |
|
59
|
linkable: "payment_session_id", |
|
60
|
primaryKey: "id", |
|
61
|
serviceName: "payment", |
|
62
|
field: "paymentSession", |
|
63
|
entity: "PaymentSession", |
|
64
|
}, |
|
65
|
}, |
|
66
|
payment: { |
|
67
|
id: { |
|
68
|
linkable: "payment_id", |
|
69
|
primaryKey: "id", |
|
70
|
serviceName: "payment", |
|
71
|
field: "payment", |
|
72
|
entity: "Payment", |
|
73
|
}, |
|
74
|
}, |
|
75
|
capture: { |
|
76
|
id: { |
|
77
|
linkable: "capture_id", |
|
78
|
primaryKey: "id", |
|
79
|
serviceName: "payment", |
|
80
|
field: "capture", |
|
81
|
entity: "Capture", |
|
82
|
}, |
|
83
|
}, |
|
84
|
refund: { |
|
85
|
id: { |
|
86
|
linkable: "refund_id", |
|
87
|
primaryKey: "id", |
|
88
|
serviceName: "payment", |
|
89
|
field: "refund", |
|
90
|
entity: "Refund", |
|
91
|
}, |
|
92
|
}, |
|
93
|
refundReason: { |
|
94
|
id: { |
|
95
|
linkable: "refund_reason_id", |
|
96
|
primaryKey: "id", |
|
97
|
serviceName: "payment", |
|
98
|
field: "refundReason", |
|
99
|
entity: "RefundReason", |
|
100
|
}, |
|
101
|
}, |
|
102
|
accountHolder: { |
|
103
|
id: { |
|
104
|
linkable: "account_holder_id", |
|
105
|
primaryKey: "id", |
|
106
|
serviceName: "payment", |
|
107
|
field: "accountHolder", |
|
108
|
entity: "AccountHolder", |
|
109
|
}, |
|
110
|
}, |
|
111
|
paymentProvider: { |
|
112
|
id: { |
|
113
|
linkable: "payment_provider_id", |
|
114
|
primaryKey: "id", |
|
115
|
serviceName: "payment", |
|
116
|
field: "paymentProvider", |
|
117
|
entity: "PaymentProvider", |
|
118
|
}, |
|
119
|
}, |
|
120
|
}) |
|
121
|
}) |
|
122
|
|
|
123
|
describe("Payment Flow", () => { |
|
124
|
it("complete payment flow successfully", async () => { |
|
125
|
let paymentCollection = await service.createPaymentCollections({ |
|
126
|
currency_code: "usd", |
|
127
|
amount: 200, |
|
128
|
}) |
|
129
|
|
|
130
|
const paymentSession = await service.createPaymentSession( |
|
131
|
paymentCollection.id, |
|
132
|
{ |
|
133
|
provider_id: "pp_system_default", |
|
134
|
amount: 200, |
|
135
|
currency_code: "usd", |
|
136
|
data: {}, |
|
137
|
context: { |
|
138
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
139
|
}, |
|
140
|
} |
|
141
|
) |
|
142
|
|
|
143
|
const payment = await service.authorizePaymentSession( |
|
144
|
paymentSession.id, |
|
145
|
{} |
|
146
|
) |
|
147
|
|
|
148
|
await service.capturePayment({ |
|
149
|
amount: 200, |
|
150
|
payment_id: payment.id, |
|
151
|
}) |
|
152
|
|
|
153
|
await service.completePaymentCollections(paymentCollection.id) |
|
154
|
|
|
155
|
paymentCollection = await service.retrievePaymentCollection( |
|
156
|
paymentCollection.id, |
|
157
|
{ relations: ["payment_sessions", "payments.captures"] } |
|
158
|
) |
|
159
|
|
|
160
|
expect(paymentCollection).toEqual( |
|
161
|
expect.objectContaining({ |
|
162
|
id: expect.any(String), |
|
163
|
currency_code: "usd", |
|
164
|
amount: 200, |
|
165
|
authorized_amount: 200, |
|
166
|
captured_amount: 200, |
|
167
|
status: "completed", |
|
168
|
deleted_at: null, |
|
169
|
completed_at: expect.any(Date), |
|
170
|
payment_sessions: [ |
|
171
|
expect.objectContaining({ |
|
172
|
id: expect.any(String), |
|
173
|
currency_code: "usd", |
|
174
|
amount: 200, |
|
175
|
provider_id: "pp_system_default", |
|
176
|
status: "authorized", |
|
177
|
authorized_at: expect.any(Date), |
|
178
|
}), |
|
179
|
], |
|
180
|
payments: [ |
|
181
|
expect.objectContaining({ |
|
182
|
id: expect.any(String), |
|
183
|
amount: 200, |
|
184
|
currency_code: "usd", |
|
185
|
provider_id: "pp_system_default", |
|
186
|
captures: [ |
|
187
|
expect.objectContaining({ |
|
188
|
amount: 200, |
|
189
|
}), |
|
190
|
], |
|
191
|
}), |
|
192
|
], |
|
193
|
}) |
|
194
|
) |
|
195
|
}) |
|
196
|
|
|
197
|
it("complete payment flow successfully when rounded numbers are equal", async () => { |
|
198
|
let paymentCollection = await service.createPaymentCollections({ |
|
199
|
currency_code: "usd", |
|
200
|
amount: 200.129, |
|
201
|
}) |
|
202
|
|
|
203
|
const paymentSession = await service.createPaymentSession( |
|
204
|
paymentCollection.id, |
|
205
|
{ |
|
206
|
provider_id: "pp_system_default", |
|
207
|
amount: 200.129, |
|
208
|
currency_code: "usd", |
|
209
|
data: {}, |
|
210
|
context: { |
|
211
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
212
|
}, |
|
213
|
} |
|
214
|
) |
|
215
|
|
|
216
|
const payment = await service.authorizePaymentSession( |
|
217
|
paymentSession.id, |
|
218
|
{} |
|
219
|
) |
|
220
|
|
|
221
|
await service.capturePayment({ |
|
222
|
amount: 200.13, // rounded from payment provider |
|
223
|
payment_id: payment.id, |
|
224
|
}) |
|
225
|
|
|
226
|
await service.completePaymentCollections(paymentCollection.id) |
|
227
|
|
|
228
|
paymentCollection = await service.retrievePaymentCollection( |
|
229
|
paymentCollection.id, |
|
230
|
{ relations: ["payment_sessions", "payments.captures"] } |
|
231
|
) |
|
232
|
|
|
233
|
expect(paymentCollection).toEqual( |
|
234
|
expect.objectContaining({ |
|
235
|
id: expect.any(String), |
|
236
|
currency_code: "usd", |
|
237
|
amount: 200.129, |
|
238
|
authorized_amount: 200.129, |
|
239
|
captured_amount: 200.13, |
|
240
|
status: "completed", |
|
241
|
deleted_at: null, |
|
242
|
completed_at: expect.any(Date), |
|
243
|
payment_sessions: [ |
|
244
|
expect.objectContaining({ |
|
245
|
id: expect.any(String), |
|
246
|
currency_code: "usd", |
|
247
|
amount: 200.129, |
|
248
|
provider_id: "pp_system_default", |
|
249
|
status: "authorized", |
|
250
|
authorized_at: expect.any(Date), |
|
251
|
}), |
|
252
|
], |
|
253
|
payments: [ |
|
254
|
expect.objectContaining({ |
|
255
|
id: expect.any(String), |
|
256
|
amount: 200.129, |
|
257
|
currency_code: "usd", |
|
258
|
provider_id: "pp_system_default", |
|
259
|
captures: [ |
|
260
|
expect.objectContaining({ |
|
261
|
amount: 200.13, |
|
262
|
}), |
|
263
|
], |
|
264
|
}), |
|
265
|
], |
|
266
|
}) |
|
267
|
) |
|
268
|
}) |
|
269
|
}) |
|
270
|
|
|
271
|
describe("PaymentCollection", () => { |
|
272
|
beforeEach(async () => { |
|
273
|
const repositoryManager = await MikroOrmWrapper.forkManager() |
|
274
|
|
|
275
|
await createPaymentCollections(repositoryManager) |
|
276
|
await createPaymentSessions(repositoryManager) |
|
277
|
await createPayments(repositoryManager) |
|
278
|
}) |
|
279
|
|
|
280
|
describe("create", () => { |
|
281
|
it("should throw an error when required params are not passed", async () => { |
|
282
|
let error = await service |
|
283
|
.createPaymentCollections([ |
|
284
|
{ |
|
285
|
amount: 200, |
|
286
|
} as any, |
|
287
|
]) |
|
288
|
.catch((e) => e) |
|
289
|
|
|
290
|
expect(error.message).toContain( |
|
291
|
"Value for PaymentCollection.currency_code is required, 'undefined' found" |
|
292
|
) |
|
293
|
|
|
294
|
error = await service |
|
295
|
.createPaymentCollections([ |
|
296
|
{ |
|
297
|
currency_code: "USD", |
|
298
|
} as any, |
|
299
|
]) |
|
300
|
.catch((e) => e) |
|
301
|
|
|
302
|
// TODO: Change error thrown by Mikro for BigNumber fields |
|
303
|
expect(error.message).toContain( |
|
304
|
"Value for PaymentCollection.amount is required, 'undefined' found" |
|
305
|
) |
|
306
|
}) |
|
307
|
|
|
308
|
it("should create a payment collection successfully", async () => { |
|
309
|
const [createdPaymentCollection] = |
|
310
|
await service.createPaymentCollections([ |
|
311
|
{ currency_code: "USD", amount: 200 }, |
|
312
|
]) |
|
313
|
|
|
314
|
expect(createdPaymentCollection).toEqual( |
|
315
|
expect.objectContaining({ |
|
316
|
id: expect.any(String), |
|
317
|
status: "not_paid", |
|
318
|
payment_providers: [], |
|
319
|
payment_sessions: [], |
|
320
|
payments: [], |
|
321
|
currency_code: "usd", |
|
322
|
amount: 200, |
|
323
|
}) |
|
324
|
) |
|
325
|
}) |
|
326
|
}) |
|
327
|
|
|
328
|
describe("delete", () => { |
|
329
|
it("should delete a Payment Collection", async () => { |
|
330
|
let collection = await service.listPaymentCollections({ |
|
331
|
id: ["pay-col-id-1"], |
|
332
|
}) |
|
333
|
|
|
334
|
expect(collection.length).toEqual(1) |
|
335
|
|
|
336
|
await service.deletePaymentCollections("pay-col-id-1") |
|
337
|
|
|
338
|
collection = await service.listPaymentCollections({ |
|
339
|
id: ["pay-col-id-1"], |
|
340
|
}) |
|
341
|
|
|
342
|
expect(collection.length).toEqual(0) |
|
343
|
}) |
|
344
|
}) |
|
345
|
|
|
346
|
describe("retrieve", () => { |
|
347
|
it("should retrieve a Payment Collection", async () => { |
|
348
|
let collection = await service.retrievePaymentCollection( |
|
349
|
"pay-col-id-2" |
|
350
|
) |
|
351
|
|
|
352
|
expect(collection).toEqual( |
|
353
|
expect.objectContaining({ |
|
354
|
id: "pay-col-id-2", |
|
355
|
amount: 200, |
|
356
|
currency_code: "usd", |
|
357
|
}) |
|
358
|
) |
|
359
|
}) |
|
360
|
|
|
361
|
it("should fail to retrieve a non existent Payment Collection", async () => { |
|
362
|
let error = await service |
|
363
|
.retrievePaymentCollection("pay-col-id-not-exists") |
|
364
|
.catch((e) => e) |
|
365
|
|
|
366
|
expect(error.message).toContain( |
|
367
|
"PaymentCollection with id: pay-col-id-not-exists was not found" |
|
368
|
) |
|
369
|
}) |
|
370
|
}) |
|
371
|
|
|
372
|
describe("list", () => { |
|
373
|
it("should list and count Payment Collection", async () => { |
|
374
|
let [collections, count] = |
|
375
|
await service.listAndCountPaymentCollections() |
|
376
|
|
|
377
|
expect(count).toEqual(3) |
|
378
|
|
|
379
|
expect(collections).toEqual( |
|
380
|
expect.arrayContaining([ |
|
381
|
expect.objectContaining({ |
|
382
|
id: "pay-col-id-1", |
|
383
|
amount: 100, |
|
384
|
currency_code: "usd", |
|
385
|
}), |
|
386
|
expect.objectContaining({ |
|
387
|
id: "pay-col-id-2", |
|
388
|
amount: 200, |
|
389
|
currency_code: "usd", |
|
390
|
}), |
|
391
|
expect.objectContaining({ |
|
392
|
id: "pay-col-id-3", |
|
393
|
amount: 300, |
|
394
|
currency_code: "usd", |
|
395
|
}), |
|
396
|
]) |
|
397
|
) |
|
398
|
}) |
|
399
|
}) |
|
400
|
|
|
401
|
describe("update", () => { |
|
402
|
it("should update a Payment Collection", async () => { |
|
403
|
await service.updatePaymentCollections("pay-col-id-2", { |
|
404
|
currency_code: "eur", |
|
405
|
}) |
|
406
|
|
|
407
|
const collection = await service.retrievePaymentCollection( |
|
408
|
"pay-col-id-2" |
|
409
|
) |
|
410
|
|
|
411
|
expect(collection).toEqual( |
|
412
|
expect.objectContaining({ |
|
413
|
id: "pay-col-id-2", |
|
414
|
currency_code: "eur", |
|
415
|
}) |
|
416
|
) |
|
417
|
}) |
|
418
|
|
|
419
|
it("should not reset currency_code to empty string when updating other fields", async () => { |
|
420
|
await service.updatePaymentCollections("pay-col-id-2", { |
|
421
|
amount: 9, |
|
422
|
}) |
|
423
|
|
|
424
|
const collection = await service.retrievePaymentCollection( |
|
425
|
"pay-col-id-2" |
|
426
|
) |
|
427
|
|
|
428
|
expect(collection).toEqual( |
|
429
|
expect.objectContaining({ |
|
430
|
id: "pay-col-id-2", |
|
431
|
amount: 9, |
|
432
|
currency_code: "usd", |
|
433
|
}) |
|
434
|
) |
|
435
|
}) |
|
436
|
}) |
|
437
|
|
|
438
|
describe("complete", () => { |
|
439
|
it("should complete a Payment Collection", async () => { |
|
440
|
await service.completePaymentCollections("pay-col-id-1") |
|
441
|
|
|
442
|
const collection = await service.retrievePaymentCollection( |
|
443
|
"pay-col-id-1" |
|
444
|
) |
|
445
|
|
|
446
|
expect(collection).toEqual( |
|
447
|
expect.objectContaining({ |
|
448
|
id: "pay-col-id-1", |
|
449
|
completed_at: expect.any(Date), |
|
450
|
}) |
|
451
|
) |
|
452
|
}) |
|
453
|
}) |
|
454
|
}) |
|
455
|
|
|
456
|
describe("PaymentSession", () => { |
|
457
|
beforeEach(async () => { |
|
458
|
const repositoryManager = await MikroOrmWrapper.forkManager() |
|
459
|
|
|
460
|
await createPaymentCollections(repositoryManager) |
|
461
|
await createPaymentSessions(repositoryManager) |
|
462
|
await createPayments(repositoryManager) |
|
463
|
}) |
|
464
|
|
|
465
|
describe("create", () => { |
|
466
|
it("should create a payment session successfully", async () => { |
|
467
|
await service.createPaymentSession("pay-col-id-1", { |
|
468
|
provider_id: "pp_system_default", |
|
469
|
amount: 200, |
|
470
|
currency_code: "usd", |
|
471
|
data: {}, |
|
472
|
context: { |
|
473
|
customer: { |
|
474
|
id: "cus-id-1", |
|
475
|
email: "test@test.test.com", |
|
476
|
}, |
|
477
|
}, |
|
478
|
}) |
|
479
|
|
|
480
|
const paymentCollection = await service.retrievePaymentCollection( |
|
481
|
"pay-col-id-1", |
|
482
|
{ relations: ["payment_sessions"] } |
|
483
|
) |
|
484
|
|
|
485
|
expect(paymentCollection).toEqual( |
|
486
|
expect.objectContaining({ |
|
487
|
id: "pay-col-id-1", |
|
488
|
status: "not_paid", |
|
489
|
payment_sessions: expect.arrayContaining([ |
|
490
|
expect.objectContaining({ |
|
491
|
id: expect.any(String), |
|
492
|
data: {}, |
|
493
|
status: "pending", |
|
494
|
authorized_at: null, |
|
495
|
currency_code: "usd", |
|
496
|
amount: 200, |
|
497
|
provider_id: "pp_system_default", |
|
498
|
}), |
|
499
|
]), |
|
500
|
}) |
|
501
|
) |
|
502
|
}) |
|
503
|
|
|
504
|
it("should gracefully handle payment session creation fails from external provider", async () => { |
|
505
|
jest |
|
506
|
.spyOn((service as any).paymentProviderService_, "createSession") |
|
507
|
.mockImplementationOnce(() => { |
|
508
|
throw new Error("Create session error") |
|
509
|
}) |
|
510
|
|
|
511
|
const deleteProviderSessionMock = jest.spyOn( |
|
512
|
(service as any).paymentProviderService_, |
|
513
|
"deleteSession" |
|
514
|
) |
|
515
|
|
|
516
|
const deletePaymentSessionMock = jest.spyOn( |
|
517
|
(service as any).paymentSessionService_, |
|
518
|
"delete" |
|
519
|
) |
|
520
|
|
|
521
|
const error = await service |
|
522
|
.createPaymentSession("pay-col-id-1", { |
|
523
|
provider_id: "pp_system_default", |
|
524
|
amount: 200, |
|
525
|
currency_code: "usd", |
|
526
|
data: {}, |
|
527
|
context: { |
|
528
|
customer: { id: "cus-id-1", email: "test@test.test.com" }, |
|
529
|
}, |
|
530
|
}) |
|
531
|
.catch((e) => e) |
|
532
|
|
|
533
|
expect(deleteProviderSessionMock).toHaveBeenCalledTimes(0) |
|
534
|
expect(deletePaymentSessionMock).toHaveBeenCalledTimes(1) |
|
535
|
expect(error.message).toEqual("Create session error") |
|
536
|
}) |
|
537
|
|
|
538
|
it("should gracefully handle payment session creation fails from internal failure", async () => { |
|
539
|
jest |
|
540
|
.spyOn((service as any).paymentSessionService_, "update") |
|
541
|
.mockImplementationOnce(() => { |
|
542
|
throw new Error("Update session error") |
|
543
|
}) |
|
544
|
|
|
545
|
const deleteProviderSessionMock = jest.spyOn( |
|
546
|
(service as any).paymentProviderService_, |
|
547
|
"deleteSession" |
|
548
|
) |
|
549
|
|
|
550
|
const deletePaymentSessionMock = jest.spyOn( |
|
551
|
(service as any).paymentSessionService_, |
|
552
|
"delete" |
|
553
|
) |
|
554
|
|
|
555
|
const error = await service |
|
556
|
.createPaymentSession("pay-col-id-1", { |
|
557
|
provider_id: "pp_system_default", |
|
558
|
amount: 200, |
|
559
|
currency_code: "usd", |
|
560
|
data: {}, |
|
561
|
context: { |
|
562
|
customer: { id: "cus-id-1", email: "test@test.test.com" }, |
|
563
|
}, |
|
564
|
}) |
|
565
|
.catch((e) => e) |
|
566
|
|
|
567
|
expect(deleteProviderSessionMock).toHaveBeenCalledTimes(1) |
|
568
|
expect(deletePaymentSessionMock).toHaveBeenCalledTimes(1) |
|
569
|
expect(error.message).toEqual("Update session error") |
|
570
|
}) |
|
571
|
}) |
|
572
|
|
|
573
|
describe("update", () => { |
|
574
|
it("should update a payment session successfully", async () => { |
|
575
|
let session = await service.createPaymentSession("pay-col-id-1", { |
|
576
|
provider_id: "pp_system_default", |
|
577
|
amount: 200, |
|
578
|
currency_code: "usd", |
|
579
|
data: {}, |
|
580
|
context: { |
|
581
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
582
|
}, |
|
583
|
}) |
|
584
|
|
|
585
|
session = await service.updatePaymentSession({ |
|
586
|
id: session.id, |
|
587
|
amount: 200, |
|
588
|
currency_code: "eur", |
|
589
|
data: {}, |
|
590
|
context: { |
|
591
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
592
|
}, |
|
593
|
}) |
|
594
|
|
|
595
|
expect(session).toEqual( |
|
596
|
expect.objectContaining({ |
|
597
|
id: expect.any(String), |
|
598
|
status: "pending", |
|
599
|
currency_code: "eur", |
|
600
|
amount: 200, |
|
601
|
}) |
|
602
|
) |
|
603
|
}) |
|
604
|
}) |
|
605
|
|
|
606
|
describe("authorize", () => { |
|
607
|
it("should authorize a payment session", async () => { |
|
608
|
const collection = await service.createPaymentCollections({ |
|
609
|
amount: 200, |
|
610
|
currency_code: "usd", |
|
611
|
}) |
|
612
|
|
|
613
|
const session = await service.createPaymentSession(collection.id, { |
|
614
|
provider_id: "pp_system_default", |
|
615
|
amount: 100, |
|
616
|
currency_code: "usd", |
|
617
|
data: {}, |
|
618
|
context: { |
|
619
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
620
|
}, |
|
621
|
}) |
|
622
|
|
|
623
|
const payment = await service.authorizePaymentSession( |
|
624
|
session.id, |
|
625
|
{} |
|
626
|
) |
|
627
|
|
|
628
|
expect(payment).toEqual( |
|
629
|
expect.objectContaining({ |
|
630
|
id: expect.any(String), |
|
631
|
amount: 100, |
|
632
|
currency_code: "usd", |
|
633
|
provider_id: "pp_system_default", |
|
634
|
refunds: [], |
|
635
|
captures: [], |
|
636
|
data: {}, |
|
637
|
deleted_at: null, |
|
638
|
captured_at: null, |
|
639
|
canceled_at: null, |
|
640
|
payment_collection_id: expect.any(String), |
|
641
|
payment_session: expect.objectContaining({ |
|
642
|
id: expect.any(String), |
|
643
|
updated_at: expect.any(Date), |
|
644
|
created_at: expect.any(Date), |
|
645
|
currency_code: "usd", |
|
646
|
amount: 100, |
|
647
|
raw_amount: { value: "100", precision: 20 }, |
|
648
|
provider_id: "pp_system_default", |
|
649
|
data: {}, |
|
650
|
status: "authorized", |
|
651
|
authorized_at: expect.any(Date), |
|
652
|
payment_collection_id: expect.any(String), |
|
653
|
}), |
|
654
|
}) |
|
655
|
) |
|
656
|
}) |
|
657
|
|
|
658
|
it("should auto capture payment when provider returns captured status", async () => { |
|
659
|
const collection = await service.createPaymentCollections({ |
|
660
|
amount: 200, |
|
661
|
currency_code: "usd", |
|
662
|
}) |
|
663
|
|
|
664
|
const session = await service.createPaymentSession(collection.id, { |
|
665
|
provider_id: "pp_system_default", |
|
666
|
amount: 100, |
|
667
|
currency_code: "usd", |
|
668
|
data: {}, |
|
669
|
context: { |
|
670
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
671
|
}, |
|
672
|
}) |
|
673
|
|
|
674
|
// Mock the provider to return CAPTURED status |
|
675
|
const authorizePaymentMock = jest |
|
676
|
.spyOn( |
|
677
|
(service as any).paymentProviderService_, |
|
678
|
"authorizePayment" |
|
679
|
) |
|
680
|
.mockResolvedValueOnce({ |
|
681
|
data: { payment_id: "external_payment_id" }, |
|
682
|
status: "captured", |
|
683
|
}) |
|
684
|
|
|
685
|
const capturePaymentMock = jest.spyOn( |
|
686
|
(service as any).paymentProviderService_, |
|
687
|
"capturePayment" |
|
688
|
) |
|
689
|
|
|
690
|
const payment = await service.authorizePaymentSession( |
|
691
|
session.id, |
|
692
|
{} |
|
693
|
) |
|
694
|
|
|
695
|
expect(authorizePaymentMock).toHaveBeenCalledTimes(1) |
|
696
|
expect(capturePaymentMock).not.toHaveBeenCalled() |
|
697
|
|
|
698
|
expect(payment).toEqual( |
|
699
|
expect.objectContaining({ |
|
700
|
id: expect.any(String), |
|
701
|
amount: 100, |
|
702
|
currency_code: "usd", |
|
703
|
provider_id: "pp_system_default", |
|
704
|
captured_at: expect.any(Date), |
|
705
|
captures: [expect.objectContaining({})], |
|
706
|
payment_session: expect.objectContaining({ |
|
707
|
status: "authorized", |
|
708
|
authorized_at: expect.any(Date), |
|
709
|
}), |
|
710
|
}) |
|
711
|
) |
|
712
|
}) |
|
713
|
|
|
714
|
it("should return null and not create payment when provider returns pending_authorization", async () => { |
|
715
|
const collection = await service.createPaymentCollections({ |
|
716
|
amount: 200, |
|
717
|
currency_code: "usd", |
|
718
|
}) |
|
719
|
|
|
720
|
const session = await service.createPaymentSession(collection.id, { |
|
721
|
provider_id: "pp_system_default", |
|
722
|
amount: 100, |
|
723
|
currency_code: "usd", |
|
724
|
data: {}, |
|
725
|
context: { |
|
726
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
727
|
}, |
|
728
|
}) |
|
729
|
|
|
730
|
jest |
|
731
|
.spyOn( |
|
732
|
(service as any).paymentProviderService_, |
|
733
|
"authorizePayment" |
|
734
|
) |
|
735
|
.mockResolvedValueOnce({ |
|
736
|
data: { some: "data" }, |
|
737
|
status: "pending_authorization", |
|
738
|
}) |
|
739
|
|
|
740
|
const result = await service.authorizePaymentSession( |
|
741
|
session.id, |
|
742
|
{} |
|
743
|
) |
|
744
|
|
|
745
|
expect(result).toBeNull() |
|
746
|
|
|
747
|
// Verify session status is updated |
|
748
|
const updatedSession = await service.retrievePaymentSession( |
|
749
|
session.id |
|
750
|
) |
|
751
|
expect(updatedSession.status).toBe("pending_authorization") |
|
752
|
expect(updatedSession.authorized_at).toBeNull() |
|
753
|
|
|
754
|
// Verify no payment record was created |
|
755
|
const payments = await service.listPayments({ |
|
756
|
payment_session_id: session.id, |
|
757
|
}) |
|
758
|
expect(payments).toHaveLength(0) |
|
759
|
|
|
760
|
// Verify payment collection stays in awaiting |
|
761
|
const updatedCollection = |
|
762
|
await service.retrievePaymentCollection(collection.id) |
|
763
|
expect(updatedCollection.status).toBe("awaiting") |
|
764
|
}) |
|
765
|
|
|
766
|
it("should create payment on re-authorization after pending_authorization when provider returns authorized", async () => { |
|
767
|
const collection = await service.createPaymentCollections({ |
|
768
|
amount: 100, |
|
769
|
currency_code: "usd", |
|
770
|
}) |
|
771
|
|
|
772
|
const session = await service.createPaymentSession(collection.id, { |
|
773
|
provider_id: "pp_system_default", |
|
774
|
amount: 100, |
|
775
|
currency_code: "usd", |
|
776
|
data: {}, |
|
777
|
context: { |
|
778
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
779
|
}, |
|
780
|
}) |
|
781
|
|
|
782
|
// First call: provider returns pending_authorization |
|
783
|
const authorizeMock = jest |
|
784
|
.spyOn( |
|
785
|
(service as any).paymentProviderService_, |
|
786
|
"authorizePayment" |
|
787
|
) |
|
788
|
.mockResolvedValueOnce({ |
|
789
|
data: { some: "data" }, |
|
790
|
status: "pending_authorization", |
|
791
|
}) |
|
792
|
|
|
793
|
const firstResult = await service.authorizePaymentSession( |
|
794
|
session.id, |
|
795
|
{} |
|
796
|
) |
|
797
|
expect(firstResult).toBeNull() |
|
798
|
|
|
799
|
// Second call: provider returns authorized (payment came through) |
|
800
|
authorizeMock.mockResolvedValueOnce({ |
|
801
|
data: { payment_id: "external_id" }, |
|
802
|
status: "authorized", |
|
803
|
}) |
|
804
|
|
|
805
|
const payment = await service.authorizePaymentSession( |
|
806
|
session.id, |
|
807
|
{} |
|
808
|
) |
|
809
|
|
|
810
|
expect(payment).toEqual( |
|
811
|
expect.objectContaining({ |
|
812
|
id: expect.any(String), |
|
813
|
amount: 100, |
|
814
|
currency_code: "usd", |
|
815
|
provider_id: "pp_system_default", |
|
816
|
payment_session: expect.objectContaining({ |
|
817
|
status: "authorized", |
|
818
|
authorized_at: expect.any(Date), |
|
819
|
}), |
|
820
|
}) |
|
821
|
) |
|
822
|
|
|
823
|
// Verify payment collection is now authorized |
|
824
|
const updatedCollection = |
|
825
|
await service.retrievePaymentCollection(collection.id) |
|
826
|
expect(updatedCollection.status).toBe("authorized") |
|
827
|
}) |
|
828
|
|
|
829
|
it("should return null again when re-authorizing and provider still returns pending_authorization", async () => { |
|
830
|
const collection = await service.createPaymentCollections({ |
|
831
|
amount: 100, |
|
832
|
currency_code: "usd", |
|
833
|
}) |
|
834
|
|
|
835
|
const session = await service.createPaymentSession(collection.id, { |
|
836
|
provider_id: "pp_system_default", |
|
837
|
amount: 100, |
|
838
|
currency_code: "usd", |
|
839
|
data: {}, |
|
840
|
context: { |
|
841
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
842
|
}, |
|
843
|
}) |
|
844
|
|
|
845
|
const authorizeMock = jest |
|
846
|
.spyOn( |
|
847
|
(service as any).paymentProviderService_, |
|
848
|
"authorizePayment" |
|
849
|
) |
|
850
|
.mockResolvedValue({ |
|
851
|
data: { some: "data" }, |
|
852
|
status: "pending_authorization", |
|
853
|
}) |
|
854
|
|
|
855
|
// First call |
|
856
|
const result1 = await service.authorizePaymentSession( |
|
857
|
session.id, |
|
858
|
{} |
|
859
|
) |
|
860
|
expect(result1).toBeNull() |
|
861
|
|
|
862
|
// Second call - still pending |
|
863
|
const result2 = await service.authorizePaymentSession( |
|
864
|
session.id, |
|
865
|
{} |
|
866
|
) |
|
867
|
expect(result2).toBeNull() |
|
868
|
|
|
869
|
expect(authorizeMock).toHaveBeenCalledTimes(2) |
|
870
|
|
|
871
|
// Still no payment |
|
872
|
const payments = await service.listPayments({ |
|
873
|
payment_session_id: session.id, |
|
874
|
}) |
|
875
|
expect(payments).toHaveLength(0) |
|
876
|
}) |
|
877
|
}) |
|
878
|
}) |
|
879
|
|
|
880
|
describe("Payment", () => { |
|
881
|
beforeEach(async () => { |
|
882
|
const repositoryManager = await MikroOrmWrapper.forkManager() |
|
883
|
|
|
884
|
await createPaymentCollections(repositoryManager) |
|
885
|
await createPaymentSessions(repositoryManager) |
|
886
|
await createPayments(repositoryManager) |
|
887
|
}) |
|
888
|
|
|
889
|
describe("update", () => { |
|
890
|
it("should update a payment successfully", async () => { |
|
891
|
const updatedPayment = await service.updatePayment({ |
|
892
|
id: "pay-id-1", |
|
893
|
}) |
|
894
|
|
|
895
|
expect(updatedPayment).toEqual( |
|
896
|
expect.objectContaining({ |
|
897
|
id: "pay-id-1", |
|
898
|
}) |
|
899
|
) |
|
900
|
}) |
|
901
|
}) |
|
902
|
|
|
903
|
describe("capture", () => { |
|
904
|
it("should capture a payment successfully and update captured_at", async () => { |
|
905
|
const capturedPayment = await service.capturePayment({ |
|
906
|
amount: 100, |
|
907
|
payment_id: "pay-id-1", |
|
908
|
}) |
|
909
|
|
|
910
|
expect(capturedPayment).toEqual( |
|
911
|
expect.objectContaining({ |
|
912
|
id: "pay-id-1", |
|
913
|
amount: 100, |
|
914
|
|
|
915
|
captures: [ |
|
916
|
expect.objectContaining({ |
|
917
|
created_by: null, |
|
918
|
amount: 100, |
|
919
|
}), |
|
920
|
], |
|
921
|
captured_at: expect.any(Date), |
|
922
|
}) |
|
923
|
) |
|
924
|
}) |
|
925
|
|
|
926
|
it("should split a payment in two captures a payment successfully", async () => { |
|
927
|
await service.capturePayment({ |
|
928
|
amount: 50, |
|
929
|
payment_id: "pay-id-1", |
|
930
|
}) |
|
931
|
|
|
932
|
const capturedPayment = await service.capturePayment({ |
|
933
|
amount: 50, |
|
934
|
payment_id: "pay-id-1", |
|
935
|
}) |
|
936
|
|
|
937
|
expect(capturedPayment).toEqual( |
|
938
|
expect.objectContaining({ |
|
939
|
id: "pay-id-1", |
|
940
|
amount: 100, |
|
941
|
|
|
942
|
captures: [ |
|
943
|
expect.objectContaining({ |
|
944
|
created_by: null, |
|
945
|
amount: 50, |
|
946
|
}), |
|
947
|
expect.objectContaining({ |
|
948
|
created_by: null, |
|
949
|
amount: 50, |
|
950
|
}), |
|
951
|
], |
|
952
|
}) |
|
953
|
) |
|
954
|
}) |
|
955
|
|
|
956
|
it("should fail to capture amount greater than authorized", async () => { |
|
957
|
const error = await service |
|
958
|
.capturePayment({ |
|
959
|
amount: 200, |
|
960
|
payment_id: "pay-id-1", |
|
961
|
}) |
|
962
|
.catch((e) => e) |
|
963
|
|
|
964
|
expect(error.message).toEqual( |
|
965
|
"You cannot capture more than the authorized amount substracted by what is already captured." |
|
966
|
) |
|
967
|
}) |
|
968
|
|
|
969
|
it("should fail to capture amount greater than what is already captured", async () => { |
|
970
|
await service.capturePayment({ |
|
971
|
amount: 99, |
|
972
|
payment_id: "pay-id-1", |
|
973
|
}) |
|
974
|
|
|
975
|
const error = await service |
|
976
|
.capturePayment({ |
|
977
|
amount: 2, |
|
978
|
payment_id: "pay-id-1", |
|
979
|
}) |
|
980
|
.catch((e) => e) |
|
981
|
|
|
982
|
expect(error.message).toEqual( |
|
983
|
"You cannot capture more than the authorized amount substracted by what is already captured." |
|
984
|
) |
|
985
|
}) |
|
986
|
|
|
987
|
it("should return payment if payment is already captured", async () => { |
|
988
|
await service.capturePayment({ |
|
989
|
amount: 100, |
|
990
|
payment_id: "pay-id-1", |
|
991
|
}) |
|
992
|
|
|
993
|
const capturedPayment = await service.capturePayment({ |
|
994
|
amount: 100, |
|
995
|
payment_id: "pay-id-1", |
|
996
|
}) |
|
997
|
|
|
998
|
expect(capturedPayment).toEqual( |
|
999
|
expect.objectContaining({ |
|
1000
|
id: "pay-id-1", |
|
1001
|
amount: 100, |
|
1002
|
captures: [ |
|
1003
|
expect.objectContaining({ |
|
1004
|
amount: 100, |
|
1005
|
}), |
|
1006
|
], |
|
1007
|
captured_at: expect.any(Date), |
|
1008
|
}) |
|
1009
|
) |
|
1010
|
}) |
|
1011
|
|
|
1012
|
it("should fail to capture a canceled payment", async () => { |
|
1013
|
await service.cancelPayment("pay-id-1") |
|
1014
|
|
|
1015
|
const error = await service |
|
1016
|
.capturePayment({ |
|
1017
|
amount: 100, |
|
1018
|
payment_id: "pay-id-1", |
|
1019
|
}) |
|
1020
|
.catch((e) => e) |
|
1021
|
|
|
1022
|
expect(error.message).toEqual( |
|
1023
|
"The payment: pay-id-1 has been canceled." |
|
1024
|
) |
|
1025
|
}) |
|
1026
|
|
|
1027
|
it("should not call provider capturePayment for auto-captured payments", async () => { |
|
1028
|
const collection = await service.createPaymentCollections({ |
|
1029
|
amount: 200, |
|
1030
|
currency_code: "usd", |
|
1031
|
}) |
|
1032
|
|
|
1033
|
const session = await service.createPaymentSession(collection.id, { |
|
1034
|
provider_id: "pp_system_default", |
|
1035
|
amount: 100, |
|
1036
|
currency_code: "usd", |
|
1037
|
data: {}, |
|
1038
|
context: { |
|
1039
|
customer: { id: "cus-id-1", email: "new@test.tsst" }, |
|
1040
|
}, |
|
1041
|
}) |
|
1042
|
|
|
1043
|
// Mock the provider to return CAPTURED status for auto-capture |
|
1044
|
jest |
|
1045
|
.spyOn( |
|
1046
|
(service as any).paymentProviderService_, |
|
1047
|
"authorizePayment" |
|
1048
|
) |
|
1049
|
.mockResolvedValueOnce({ |
|
1050
|
data: { payment_id: "external_payment_id" }, |
|
1051
|
status: "captured", |
|
1052
|
}) |
|
1053
|
|
|
1054
|
const payment = await service.authorizePaymentSession( |
|
1055
|
session.id, |
|
1056
|
{} |
|
1057
|
) |
|
1058
|
|
|
1059
|
// Spy on capturePayment provider method |
|
1060
|
const capturePaymentMock = jest.spyOn( |
|
1061
|
(service as any).paymentProviderService_, |
|
1062
|
"capturePayment" |
|
1063
|
) |
|
1064
|
|
|
1065
|
// Try to capture the already auto-captured payment |
|
1066
|
const capturedPayment = await service.capturePayment({ |
|
1067
|
amount: 100, |
|
1068
|
payment_id: payment.id, |
|
1069
|
}) |
|
1070
|
|
|
1071
|
// Provider's capturePayment should NOT be called since it was auto-captured |
|
1072
|
expect(capturePaymentMock).not.toHaveBeenCalled() |
|
1073
|
|
|
1074
|
// Verify data consistency |
|
1075
|
expect(capturedPayment).toEqual( |
|
1076
|
expect.objectContaining({ |
|
1077
|
id: payment.id, |
|
1078
|
amount: 100, |
|
1079
|
captured_at: expect.any(Date), |
|
1080
|
captures: [ |
|
1081
|
expect.objectContaining({ |
|
1082
|
amount: 100, |
|
1083
|
}), |
|
1084
|
], |
|
1085
|
}) |
|
1086
|
) |
|
1087
|
}) |
|
1088
|
}) |
|
1089
|
|
|
1090
|
describe("refund", () => { |
|
1091
|
it("should refund a payments successfully", async () => { |
|
1092
|
await service.capturePayment({ |
|
1093
|
amount: 100, |
|
1094
|
payment_id: "pay-id-2", |
|
1095
|
}) |
|
1096
|
|
|
1097
|
const refundedPayment = await service.refundPayment({ |
|
1098
|
amount: 100, |
|
1099
|
payment_id: "pay-id-2", |
|
1100
|
}) |
|
1101
|
|
|
1102
|
expect(refundedPayment).toEqual( |
|
1103
|
expect.objectContaining({ |
|
1104
|
id: "pay-id-2", |
|
1105
|
amount: 100, |
|
1106
|
refunds: [ |
|
1107
|
expect.objectContaining({ |
|
1108
|
created_by: null, |
|
1109
|
amount: 100, |
|
1110
|
}), |
|
1111
|
], |
|
1112
|
}) |
|
1113
|
) |
|
1114
|
}) |
|
1115
|
|
|
1116
|
it("should persist metadata passed when refunding a payment", async () => { |
|
1117
|
await service.capturePayment({ |
|
1118
|
amount: 100, |
|
1119
|
payment_id: "pay-id-2", |
|
1120
|
}) |
|
1121
|
|
|
1122
|
const refundedPayment = await service.refundPayment({ |
|
1123
|
amount: 100, |
|
1124
|
payment_id: "pay-id-2", |
|
1125
|
metadata: { reason: "customer-request", ticket_id: "42" }, |
|
1126
|
}) |
|
1127
|
|
|
1128
|
expect(refundedPayment).toEqual( |
|
1129
|
expect.objectContaining({ |
|
1130
|
id: "pay-id-2", |
|
1131
|
refunds: [ |
|
1132
|
expect.objectContaining({ |
|
1133
|
amount: 100, |
|
1134
|
metadata: { |
|
1135
|
reason: "customer-request", |
|
1136
|
ticket_id: "42", |
|
1137
|
}, |
|
1138
|
}), |
|
1139
|
], |
|
1140
|
}) |
|
1141
|
) |
|
1142
|
}) |
|
1143
|
|
|
1144
|
it("should fully refund a payment through two refunds", async () => { |
|
1145
|
await service.capturePayment({ |
|
1146
|
amount: 100, |
|
1147
|
payment_id: "pay-id-2", |
|
1148
|
}) |
|
1149
|
|
|
1150
|
const refundedPaymentOne = await service.refundPayment({ |
|
1151
|
amount: 50, |
|
1152
|
payment_id: "pay-id-2", |
|
1153
|
}) |
|
1154
|
|
|
1155
|
const refundedPaymentTwo = await service.refundPayment({ |
|
1156
|
amount: 50, |
|
1157
|
payment_id: "pay-id-2", |
|
1158
|
}) |
|
1159
|
|
|
1160
|
expect(refundedPaymentOne).toEqual( |
|
1161
|
expect.objectContaining({ |
|
1162
|
id: "pay-id-2", |
|
1163
|
amount: 100, |
|
1164
|
refunds: [ |
|
1165
|
expect.objectContaining({ |
|
1166
|
created_by: null, |
|
1167
|
amount: 50, |
|
1168
|
}), |
|
1169
|
], |
|
1170
|
}) |
|
1171
|
) |
|
1172
|
expect(refundedPaymentTwo).toEqual( |
|
1173
|
expect.objectContaining({ |
|
1174
|
id: "pay-id-2", |
|
1175
|
amount: 100, |
|
1176
|
refunds: [ |
|
1177
|
expect.objectContaining({ |
|
1178
|
created_by: null, |
|
1179
|
amount: 50, |
|
1180
|
}), |
|
1181
|
expect.objectContaining({ |
|
1182
|
created_by: null, |
|
1183
|
amount: 50, |
|
1184
|
}), |
|
1185
|
], |
|
1186
|
}) |
|
1187
|
) |
|
1188
|
}) |
|
1189
|
|
|
1190
|
it("should throw if refund is greater than captured amount", async () => { |
|
1191
|
await service.capturePayment({ |
|
1192
|
amount: 50, |
|
1193
|
payment_id: "pay-id-1", |
|
1194
|
}) |
|
1195
|
|
|
1196
|
const error = await service |
|
1197
|
.refundPayment({ |
|
1198
|
amount: 100, |
|
1199
|
payment_id: "pay-id-1", |
|
1200
|
}) |
|
1201
|
.catch((e) => e) |
|
1202
|
|
|
1203
|
expect(error.message).toEqual( |
|
1204
|
"You cannot refund more than what is captured on the payment." |
|
1205
|
) |
|
1206
|
}) |
|
1207
|
|
|
1208
|
it("should throw if refund amount is 0", async () => { |
|
1209
|
await service.capturePayment({ |
|
1210
|
amount: 100, |
|
1211
|
payment_id: "pay-id-2", |
|
1212
|
}) |
|
1213
|
|
|
1214
|
const error = await service |
|
1215
|
.refundPayment({ |
|
1216
|
amount: 0, |
|
1217
|
payment_id: "pay-id-2", |
|
1218
|
}) |
|
1219
|
.catch((e) => e) |
|
1220
|
|
|
1221
|
expect(error.message).toEqual( |
|
1222
|
"Refund amount must be greater than 0." |
|
1223
|
) |
|
1224
|
}) |
|
1225
|
|
|
1226
|
it("should throw if refund amount is negative", async () => { |
|
1227
|
await service.capturePayment({ |
|
1228
|
amount: 100, |
|
1229
|
payment_id: "pay-id-2", |
|
1230
|
}) |
|
1231
|
|
|
1232
|
const error = await service |
|
1233
|
.refundPayment({ |
|
1234
|
amount: -50, |
|
1235
|
payment_id: "pay-id-2", |
|
1236
|
}) |
|
1237
|
.catch((e) => e) |
|
1238
|
|
|
1239
|
expect(error.message).toEqual( |
|
1240
|
"Refund amount must be greater than 0." |
|
1241
|
) |
|
1242
|
}) |
|
1243
|
|
|
1244
|
it("should fully refund the captured amount when no amount is passed", async () => { |
|
1245
|
await service.capturePayment({ |
|
1246
|
amount: 100, |
|
1247
|
payment_id: "pay-id-2", |
|
1248
|
}) |
|
1249
|
|
|
1250
|
const refundedPayment = await service.refundPayment({ |
|
1251
|
payment_id: "pay-id-2", |
|
1252
|
}) |
|
1253
|
|
|
1254
|
expect(refundedPayment).toEqual( |
|
1255
|
expect.objectContaining({ |
|
1256
|
id: "pay-id-2", |
|
1257
|
amount: 100, |
|
1258
|
refunds: [ |
|
1259
|
expect.objectContaining({ |
|
1260
|
amount: 100, |
|
1261
|
}), |
|
1262
|
], |
|
1263
|
}) |
|
1264
|
) |
|
1265
|
}) |
|
1266
|
|
|
1267
|
it("should throw if total refunded amount is greater than captured amount", async () => { |
|
1268
|
await service.capturePayment({ |
|
1269
|
amount: 100, |
|
1270
|
payment_id: "pay-id-1", |
|
1271
|
}) |
|
1272
|
|
|
1273
|
const refundedPayment1 = await service.refundPayment({ |
|
1274
|
amount: 50, |
|
1275
|
payment_id: "pay-id-1", |
|
1276
|
}) |
|
1277
|
|
|
1278
|
expect(refundedPayment1).toEqual( |
|
1279
|
expect.objectContaining({ |
|
1280
|
id: "pay-id-1", |
|
1281
|
amount: 100, |
|
1282
|
refunds: [ |
|
1283
|
expect.objectContaining({ |
|
1284
|
amount: 50, |
|
1285
|
}), |
|
1286
|
], |
|
1287
|
}) |
|
1288
|
) |
|
1289
|
|
|
1290
|
const error = await service |
|
1291
|
.refundPayment({ |
|
1292
|
amount: 60, |
|
1293
|
payment_id: "pay-id-1", |
|
1294
|
}) |
|
1295
|
.catch((e) => e) |
|
1296
|
|
|
1297
|
expect(error.message).toEqual( |
|
1298
|
"You cannot refund more than what is captured on the payment." |
|
1299
|
) |
|
1300
|
}) |
|
1301
|
|
|
1302
|
it("should allow refund slightly exceeding captured amount within currency epsilon", async () => { |
|
1303
|
// USD has 2 decimal digits, so epsilon = 0.01 |
|
1304
|
// Provider captures 87.957975 (sub-cent precision), user requests refund for 87.96 |
|
1305
|
// Difference is 0.002025 which is within epsilon → refund should succeed |
|
1306
|
await service.capturePayment({ |
|
1307
|
amount: 87.957975, |
|
1308
|
payment_id: "pay-id-1", |
|
1309
|
}) |
|
1310
|
|
|
1311
|
const refundedPayment = await service.refundPayment({ |
|
1312
|
amount: 87.96, |
|
1313
|
payment_id: "pay-id-1", |
|
1314
|
}) |
|
1315
|
|
|
1316
|
expect(refundedPayment).toEqual( |
|
1317
|
expect.objectContaining({ |
|
1318
|
id: "pay-id-1", |
|
1319
|
refunds: [ |
|
1320
|
expect.objectContaining({ |
|
1321
|
amount: 87.96, |
|
1322
|
}), |
|
1323
|
], |
|
1324
|
}) |
|
1325
|
) |
|
1326
|
}) |
|
1327
|
|
|
1328
|
it("should throw if refund exceeds captured amount beyond currency epsilon", async () => { |
|
1329
|
// USD epsilon = 0.01; refunding 0.02 more than captured should still throw |
|
1330
|
await service.capturePayment({ |
|
1331
|
amount: 87.957975, |
|
1332
|
payment_id: "pay-id-1", |
|
1333
|
}) |
|
1334
|
|
|
1335
|
const error = await service |
|
1336
|
.refundPayment({ |
|
1337
|
amount: 87.98, |
|
1338
|
payment_id: "pay-id-1", |
|
1339
|
}) |
|
1340
|
.catch((e) => e) |
|
1341
|
|
|
1342
|
expect(error.message).toEqual( |
|
1343
|
"You cannot refund more than what is captured on the payment." |
|
1344
|
) |
|
1345
|
}) |
|
1346
|
|
|
1347
|
describe("idempotency", () => { |
|
1348
|
const spyOnProviderRefund = () => |
|
1349
|
jest.spyOn( |
|
1350
|
(service as any).paymentProviderService_, |
|
1351
|
"refundPayment" |
|
1352
|
) |
|
1353
|
|
|
1354
|
const capture = () => |
|
1355
|
service.capturePayment({ amount: 100, payment_id: "pay-id-2" }) |
|
1356
|
|
|
1357
|
const listAllRefunds = (paymentId = "pay-id-2") => |
|
1358
|
service.listRefunds( |
|
1359
|
{ payment_id: paymentId }, |
|
1360
|
{ withDeleted: true } |
|
1361
|
) |
|
1362
|
|
|
1363
|
it("should derive the idempotency key from the refund when the caller doesn't supply one", async () => { |
|
1364
|
await capture() |
|
1365
|
|
|
1366
|
const refundPaymentMock = spyOnProviderRefund() |
|
1367
|
|
|
1368
|
const payment = await service.refundPayment({ |
|
1369
|
amount: 100, |
|
1370
|
payment_id: "pay-id-2", |
|
1371
|
}) |
|
1372
|
|
|
1373
|
expect(refundPaymentMock).toHaveBeenCalledTimes(1) |
|
1374
|
expect(refundPaymentMock.mock.calls[0][1]).toEqual( |
|
1375
|
expect.objectContaining({ |
|
1376
|
context: expect.objectContaining({ |
|
1377
|
idempotency_key: payment.refunds![0].id, |
|
1378
|
}), |
|
1379
|
}) |
|
1380
|
) |
|
1381
|
}) |
|
1382
|
|
|
1383
|
it("should forward a caller-supplied idempotency key to the provider", async () => { |
|
1384
|
await capture() |
|
1385
|
|
|
1386
|
const refundPaymentMock = spyOnProviderRefund() |
|
1387
|
|
|
1388
|
await service.refundPayment({ |
|
1389
|
amount: 100, |
|
1390
|
payment_id: "pay-id-2", |
|
1391
|
idempotency_key: "rma_42_refund", |
|
1392
|
}) |
|
1393
|
|
|
1394
|
expect(refundPaymentMock.mock.calls[0][1]).toEqual( |
|
1395
|
expect.objectContaining({ |
|
1396
|
context: expect.objectContaining({ |
|
1397
|
idempotency_key: "rma_42_refund", |
|
1398
|
}), |
|
1399
|
}) |
|
1400
|
) |
|
1401
|
}) |
|
1402
|
|
|
1403
|
it("should soft-delete the refund record when the provider call fails", async () => { |
|
1404
|
await capture() |
|
1405
|
|
|
1406
|
spyOnProviderRefund().mockRejectedValueOnce( |
|
1407
|
new Error("provider timed out") |
|
1408
|
) |
|
1409
|
|
|
1410
|
await expect( |
|
1411
|
service.refundPayment({ |
|
1412
|
amount: 100, |
|
1413
|
payment_id: "pay-id-2", |
|
1414
|
}) |
|
1415
|
).rejects.toThrow("provider timed out") |
|
1416
|
|
|
1417
|
// Invisible to every refund total, but still on record. |
|
1418
|
expect( |
|
1419
|
await service.listRefunds({ payment_id: "pay-id-2" }) |
|
1420
|
).toHaveLength(0) |
|
1421
|
|
|
1422
|
const refunds = await listAllRefunds() |
|
1423
|
|
|
1424
|
expect(refunds).toHaveLength(1) |
|
1425
|
expect(refunds[0]).toEqual( |
|
1426
|
expect.objectContaining({ |
|
1427
|
amount: 100, |
|
1428
|
deleted_at: expect.any(Date), |
|
1429
|
}) |
|
1430
|
) |
|
1431
|
}) |
|
1432
|
|
|
1433
|
it("should not count a failed refund toward the refunded total", async () => { |
|
1434
|
await capture() |
|
1435
|
|
|
1436
|
spyOnProviderRefund().mockRejectedValueOnce( |
|
1437
|
new Error("provider timed out") |
|
1438
|
) |
|
1439
|
|
|
1440
|
await expect( |
|
1441
|
service.refundPayment({ |
|
1442
|
amount: 100, |
|
1443
|
payment_id: "pay-id-2", |
|
1444
|
}) |
|
1445
|
).rejects.toThrow("provider timed out") |
|
1446
|
|
|
1447
|
const payment = await service.refundPayment({ |
|
1448
|
amount: 100, |
|
1449
|
payment_id: "pay-id-2", |
|
1450
|
}) |
|
1451
|
|
|
1452
|
expect(payment.refunds).toEqual([ |
|
1453
|
expect.objectContaining({ amount: 100, deleted_at: null }), |
|
1454
|
]) |
|
1455
|
}) |
|
1456
|
|
|
1457
|
it("should start a new refund when retrying without an idempotency key", async () => { |
|
1458
|
await capture() |
|
1459
|
|
|
1460
|
const refundPaymentMock = spyOnProviderRefund() |
|
1461
|
refundPaymentMock.mockRejectedValueOnce( |
|
1462
|
new Error("provider timed out") |
|
1463
|
) |
|
1464
|
|
|
1465
|
await expect( |
|
1466
|
service.refundPayment({ |
|
1467
|
amount: 100, |
|
1468
|
payment_id: "pay-id-2", |
|
1469
|
}) |
|
1470
|
).rejects.toThrow("provider timed out") |
|
1471
|
|
|
1472
|
await service.refundPayment({ |
|
1473
|
amount: 100, |
|
1474
|
payment_id: "pay-id-2", |
|
1475
|
}) |
|
1476
|
|
|
1477
|
// A keyless request is never matched against an earlier attempt, |
|
1478
|
// so the retry is a separate refund with its own key. |
|
1479
|
expect(refundPaymentMock).toHaveBeenCalledTimes(2) |
|
1480
|
expect( |
|
1481
|
refundPaymentMock.mock.calls[1][1].context.idempotency_key |
|
1482
|
).not.toEqual( |
|
1483
|
refundPaymentMock.mock.calls[0][1].context.idempotency_key |
|
1484
|
) |
|
1485
|
expect(await listAllRefunds()).toHaveLength(2) |
|
1486
|
expect( |
|
1487
|
await service.listRefunds({ payment_id: "pay-id-2" }) |
|
1488
|
).toHaveLength(1) |
|
1489
|
}) |
|
1490
|
|
|
1491
|
it("should reuse the caller-supplied idempotency key when retrying a failed refund", async () => { |
|
1492
|
await capture() |
|
1493
|
|
|
1494
|
const refundPaymentMock = spyOnProviderRefund() |
|
1495
|
refundPaymentMock.mockRejectedValueOnce( |
|
1496
|
new Error("provider timed out") |
|
1497
|
) |
|
1498
|
|
|
1499
|
const input = { |
|
1500
|
amount: 100, |
|
1501
|
payment_id: "pay-id-2", |
|
1502
|
idempotency_key: "rma_42_refund", |
|
1503
|
} |
|
1504
|
|
|
1505
|
await expect(service.refundPayment(input)).rejects.toThrow( |
|
1506
|
"provider timed out" |
|
1507
|
) |
|
1508
|
|
|
1509
|
await service.refundPayment(input) |
|
1510
|
|
|
1511
|
expect(refundPaymentMock).toHaveBeenCalledTimes(2) |
|
1512
|
expect( |
|
1513
|
refundPaymentMock.mock.calls.map( |
|
1514
|
(call) => call[1].context.idempotency_key |
|
1515
|
) |
|
1516
|
).toEqual(["rma_42_refund", "rma_42_refund"]) |
|
1517
|
}) |
|
1518
|
|
|
1519
|
it("should not create a second refund when retrying with the same caller-supplied key", async () => { |
|
1520
|
await capture() |
|
1521
|
|
|
1522
|
spyOnProviderRefund().mockRejectedValueOnce( |
|
1523
|
new Error("provider timed out") |
|
1524
|
) |
|
1525
|
|
|
1526
|
const input = { |
|
1527
|
amount: 100, |
|
1528
|
payment_id: "pay-id-2", |
|
1529
|
idempotency_key: "rma_42_refund", |
|
1530
|
} |
|
1531
|
|
|
1532
|
await expect(service.refundPayment(input)).rejects.toThrow( |
|
1533
|
"provider timed out" |
|
1534
|
) |
|
1535
|
|
|
1536
|
const payment = await service.refundPayment(input) |
|
1537
|
|
|
1538
|
expect(payment.refunds).toHaveLength(1) |
|
1539
|
|
|
1540
|
const refunds = await listAllRefunds() |
|
1541
|
|
|
1542
|
expect(refunds).toHaveLength(1) |
|
1543
|
expect(refunds[0]).toEqual( |
|
1544
|
expect.objectContaining({ deleted_at: null }) |
|
1545
|
) |
|
1546
|
}) |
|
1547
|
|
|
1548
|
it("should not call the provider twice for a refund that already succeeded under the same key", async () => { |
|
1549
|
await capture() |
|
1550
|
|
|
1551
|
const refundPaymentMock = spyOnProviderRefund() |
|
1552
|
|
|
1553
|
const input = { |
|
1554
|
amount: 50, |
|
1555
|
payment_id: "pay-id-2", |
|
1556
|
idempotency_key: "rma_42_refund", |
|
1557
|
} |
|
1558
|
|
|
1559
|
await service.refundPayment(input) |
|
1560
|
const payment = await service.refundPayment(input) |
|
1561
|
|
|
1562
|
expect(refundPaymentMock).toHaveBeenCalledTimes(1) |
|
1563
|
expect(payment.refunds).toHaveLength(1) |
|
1564
|
}) |
|
1565
|
|
|
1566
|
it("should not let a resumed refund exceed the captured amount", async () => { |
|
1567
|
await capture() |
|
1568
|
|
|
1569
|
spyOnProviderRefund().mockRejectedValueOnce( |
|
1570
|
new Error("provider timed out") |
|
1571
|
) |
|
1572
|
|
|
1573
|
await expect( |
|
1574
|
service.refundPayment({ amount: 50, payment_id: "pay-id-2" }) |
|
1575
|
).rejects.toThrow("provider timed out") |
|
1576
|
|
|
1577
|
await service.refundPayment({ |
|
1578
|
amount: 100, |
|
1579
|
payment_id: "pay-id-2", |
|
1580
|
}) |
|
1581
|
|
|
1582
|
// Resuming the failed 50 would put 150 against 100 captured. |
|
1583
|
await expect( |
|
1584
|
service.refundPayment({ amount: 50, payment_id: "pay-id-2" }) |
|
1585
|
).rejects.toThrow( |
|
1586
|
"You cannot refund more than what is captured on the payment." |
|
1587
|
) |
|
1588
|
|
|
1589
|
const live = await service.listRefunds({ |
|
1590
|
payment_id: "pay-id-2", |
|
1591
|
}) |
|
1592
|
expect(live).toHaveLength(1) |
|
1593
|
expect(live[0].amount).toEqual(100) |
|
1594
|
}) |
|
1595
|
|
|
1596
|
it("should not let a resumed refund exceed the captured amount with a caller key", async () => { |
|
1597
|
await capture() |
|
1598
|
|
|
1599
|
spyOnProviderRefund().mockRejectedValueOnce( |
|
1600
|
new Error("provider timed out") |
|
1601
|
) |
|
1602
|
|
|
1603
|
await expect( |
|
1604
|
service.refundPayment({ |
|
1605
|
amount: 50, |
|
1606
|
payment_id: "pay-id-2", |
|
1607
|
idempotency_key: "rma_1_refund", |
|
1608
|
}) |
|
1609
|
).rejects.toThrow("provider timed out") |
|
1610
|
|
|
1611
|
await service.refundPayment({ |
|
1612
|
amount: 100, |
|
1613
|
payment_id: "pay-id-2", |
|
1614
|
idempotency_key: "rma_2_refund", |
|
1615
|
}) |
|
1616
|
|
|
1617
|
await expect( |
|
1618
|
service.refundPayment({ |
|
1619
|
amount: 50, |
|
1620
|
payment_id: "pay-id-2", |
|
1621
|
idempotency_key: "rma_1_refund", |
|
1622
|
}) |
|
1623
|
).rejects.toThrow( |
|
1624
|
"You cannot refund more than what is captured on the payment." |
|
1625
|
) |
|
1626
|
}) |
|
1627
|
|
|
1628
|
it("should not let an unrelated refund take over a failed one", async () => { |
|
1629
|
await capture() |
|
1630
|
|
|
1631
|
spyOnProviderRefund().mockRejectedValueOnce( |
|
1632
|
new Error("provider timed out") |
|
1633
|
) |
|
1634
|
|
|
1635
|
await expect( |
|
1636
|
service.refundPayment({ |
|
1637
|
amount: 30, |
|
1638
|
payment_id: "pay-id-2", |
|
1639
|
note: "old attempt", |
|
1640
|
}) |
|
1641
|
).rejects.toThrow("provider timed out") |
|
1642
|
|
|
1643
|
const payment = await service.refundPayment({ |
|
1644
|
amount: 30, |
|
1645
|
payment_id: "pay-id-2", |
|
1646
|
note: "new unrelated refund", |
|
1647
|
}) |
|
1648
|
|
|
1649
|
expect(payment.refunds).toEqual([ |
|
1650
|
expect.objectContaining({ |
|
1651
|
amount: 30, |
|
1652
|
note: "new unrelated refund", |
|
1653
|
}), |
|
1654
|
]) |
|
1655
|
|
|
1656
|
// The old attempt is left behind, not adopted. |
|
1657
|
const all = await listAllRefunds() |
|
1658
|
expect(all).toHaveLength(2) |
|
1659
|
}) |
|
1660
|
|
|
1661
|
it("should not resume a failed refund even when the request is identical", async () => { |
|
1662
|
await capture() |
|
1663
|
|
|
1664
|
const refundPaymentMock = spyOnProviderRefund() |
|
1665
|
refundPaymentMock.mockRejectedValueOnce( |
|
1666
|
new Error("provider timed out") |
|
1667
|
) |
|
1668
|
|
|
1669
|
const input = { |
|
1670
|
amount: 30, |
|
1671
|
payment_id: "pay-id-2", |
|
1672
|
note: "same request", |
|
1673
|
} |
|
1674
|
|
|
1675
|
await expect(service.refundPayment(input)).rejects.toThrow( |
|
1676
|
"provider timed out" |
|
1677
|
) |
|
1678
|
|
|
1679
|
await service.refundPayment(input) |
|
1680
|
|
|
1681
|
// Two identical requests are indistinguishable from one request |
|
1682
|
// sent twice, so resuming is only ever driven by an explicit key. |
|
1683
|
expect(await listAllRefunds()).toHaveLength(2) |
|
1684
|
expect( |
|
1685
|
refundPaymentMock.mock.calls[1][1].context.idempotency_key |
|
1686
|
).not.toEqual( |
|
1687
|
refundPaymentMock.mock.calls[0][1].context.idempotency_key |
|
1688
|
) |
|
1689
|
}) |
|
1690
|
|
|
1691
|
it("should reject a replay while the first attempt is still in flight", async () => { |
|
1692
|
await capture() |
|
1693
|
|
|
1694
|
let releaseProvider: () => void |
|
1695
|
const providerCall = new Promise<void>((resolve) => { |
|
1696
|
releaseProvider = resolve |
|
1697
|
}) |
|
1698
|
|
|
1699
|
spyOnProviderRefund().mockImplementationOnce(async () => { |
|
1700
|
await providerCall |
|
1701
|
throw new Error("provider timed out") |
|
1702
|
}) |
|
1703
|
|
|
1704
|
const input = { |
|
1705
|
amount: 50, |
|
1706
|
payment_id: "pay-id-2", |
|
1707
|
idempotency_key: "rma_42_refund", |
|
1708
|
} |
|
1709
|
|
|
1710
|
const first = service.refundPayment(input).catch((e) => e) |
|
1711
|
|
|
1712
|
// The row is committed before the provider is called, so the |
|
1713
|
// replay below sees an attempt whose outcome isn't known yet. |
|
1714
|
await new Promise((resolve) => setTimeout(resolve, 100)) |
|
1715
|
|
|
1716
|
await expect(service.refundPayment(input)).rejects.toThrow( |
|
1717
|
"is still in progress" |
|
1718
|
) |
|
1719
|
|
|
1720
|
releaseProvider!() |
|
1721
|
await first |
|
1722
|
|
|
1723
|
expect( |
|
1724
|
await service.listRefunds({ payment_id: "pay-id-2" }) |
|
1725
|
).toHaveLength(0) |
|
1726
|
}) |
|
1727
|
|
|
1728
|
it("should treat refunds with different idempotency keys as distinct", async () => { |
|
1729
|
await capture() |
|
1730
|
|
|
1731
|
await service.refundPayment({ |
|
1732
|
amount: 40, |
|
1733
|
payment_id: "pay-id-2", |
|
1734
|
idempotency_key: "rma_1_refund", |
|
1735
|
}) |
|
1736
|
|
|
1737
|
const payment = await service.refundPayment({ |
|
1738
|
amount: 60, |
|
1739
|
payment_id: "pay-id-2", |
|
1740
|
idempotency_key: "rma_2_refund", |
|
1741
|
}) |
|
1742
|
|
|
1743
|
expect(payment.refunds).toHaveLength(2) |
|
1744
|
}) |
|
1745
|
}) |
|
1746
|
}) |
|
1747
|
|
|
1748
|
describe("cancel", () => { |
|
1749
|
it("should cancel a payment", async () => { |
|
1750
|
const payment = await service.cancelPayment("pay-id-2") |
|
1751
|
|
|
1752
|
expect(payment).toEqual( |
|
1753
|
expect.objectContaining({ |
|
1754
|
id: "pay-id-2", |
|
1755
|
canceled_at: expect.any(Date), |
|
1756
|
}) |
|
1757
|
) |
|
1758
|
}) |
|
1759
|
|
|
1760
|
it("should persist the data returned by the payment provider on cancel", async () => { |
|
1761
|
const providerData = { canceled: true, provider_cancel_id: "ext-cancel-123" } |
|
1762
|
|
|
1763
|
jest |
|
1764
|
.spyOn( |
|
1765
|
(service as any).paymentProviderService_, |
|
1766
|
"cancelPayment" |
|
1767
|
) |
|
1768
|
.mockResolvedValueOnce({ data: providerData }) |
|
1769
|
|
|
1770
|
const payment = await service.cancelPayment("pay-id-2") |
|
1771
|
|
|
1772
|
expect(payment).toEqual( |
|
1773
|
expect.objectContaining({ |
|
1774
|
id: "pay-id-2", |
|
1775
|
canceled_at: expect.any(Date), |
|
1776
|
data: providerData, |
|
1777
|
}) |
|
1778
|
) |
|
1779
|
}) |
|
1780
|
|
|
1781
|
// TODO: revisit when totals are implemented |
|
1782
|
// it("should throw if trying to cancel a captured payment", async () => { |
|
1783
|
// await service.capturePayment({ payment_id: "pay-id-2", amount: 100 }) |
|
1784
|
// |
|
1785
|
// const error = await service |
|
1786
|
// .cancelPayment("pay-id-2") |
|
1787
|
// .catch((e) => e.message) |
|
1788
|
// |
|
1789
|
// expect(error).toEqual( |
|
1790
|
// "Cannot cancel a payment: pay-id-2 that has been captured." |
|
1791
|
// ) |
|
1792
|
// }) |
|
1793
|
}) |
|
1794
|
|
|
1795
|
describe("concurrency", () => { |
|
1796
|
it("should authorize, capture and refund multiple payment sessions", async () => { |
|
1797
|
const collection = await service.createPaymentCollections({ |
|
1798
|
amount: 500, |
|
1799
|
currency_code: "usd", |
|
1800
|
}) |
|
1801
|
|
|
1802
|
const session1 = await service.createPaymentSession(collection.id, { |
|
1803
|
provider_id: "pp_system_default", |
|
1804
|
amount: 120, |
|
1805
|
currency_code: "usd", |
|
1806
|
data: {}, |
|
1807
|
}) |
|
1808
|
|
|
1809
|
const session2 = await service.createPaymentSession(collection.id, { |
|
1810
|
provider_id: "pp_system_default", |
|
1811
|
amount: 180, |
|
1812
|
currency_code: "usd", |
|
1813
|
data: {}, |
|
1814
|
}) |
|
1815
|
|
|
1816
|
const session3 = await service.createPaymentSession(collection.id, { |
|
1817
|
provider_id: "pp_system_default", |
|
1818
|
amount: 200, |
|
1819
|
currency_code: "usd", |
|
1820
|
data: {}, |
|
1821
|
}) |
|
1822
|
|
|
1823
|
const session4 = await service.createPaymentSession(collection.id, { |
|
1824
|
provider_id: "pp_system_default", |
|
1825
|
amount: 500, |
|
1826
|
currency_code: "eur", |
|
1827
|
data: {}, |
|
1828
|
}) |
|
1829
|
|
|
1830
|
// authorize |
|
1831
|
const [payment1, payment2, payment3, payment4] = await promiseAll([ |
|
1832
|
service.authorizePaymentSession(session1.id, {}), |
|
1833
|
service.authorizePaymentSession(session2.id, {}), |
|
1834
|
service.authorizePaymentSession(session3.id, {}), |
|
1835
|
service.authorizePaymentSession(session4.id, {}), |
|
1836
|
]) |
|
1837
|
|
|
1838
|
// capture |
|
1839
|
await promiseAll([ |
|
1840
|
service.capturePayment({ |
|
1841
|
amount: 60, |
|
1842
|
payment_id: payment1.id, |
|
1843
|
}), |
|
1844
|
service.capturePayment({ |
|
1845
|
amount: 60, |
|
1846
|
payment_id: payment1.id, |
|
1847
|
}), |
|
1848
|
service.capturePayment({ |
|
1849
|
amount: 180, |
|
1850
|
payment_id: payment2.id, |
|
1851
|
}), |
|
1852
|
service.capturePayment({ |
|
1853
|
amount: 100, |
|
1854
|
payment_id: payment3.id, |
|
1855
|
}), |
|
1856
|
service.capturePayment({ |
|
1857
|
amount: 40, |
|
1858
|
payment_id: payment3.id, |
|
1859
|
}), |
|
1860
|
service.capturePayment({ |
|
1861
|
amount: 60, |
|
1862
|
payment_id: payment3.id, |
|
1863
|
}), |
|
1864
|
service.capturePayment({ |
|
1865
|
amount: 200, |
|
1866
|
payment_id: payment4.id, |
|
1867
|
}), |
|
1868
|
service.capturePayment({ |
|
1869
|
amount: 200, |
|
1870
|
payment_id: payment4.id, |
|
1871
|
}), |
|
1872
|
service.capturePayment({ |
|
1873
|
amount: 100, |
|
1874
|
payment_id: payment4.id, |
|
1875
|
}), |
|
1876
|
]) |
|
1877
|
|
|
1878
|
// refund |
|
1879
|
await promiseAll([ |
|
1880
|
service.refundPayment({ |
|
1881
|
amount: 70, |
|
1882
|
payment_id: payment1.id, |
|
1883
|
}), |
|
1884
|
service.refundPayment({ |
|
1885
|
amount: 50, |
|
1886
|
payment_id: payment1.id, |
|
1887
|
}), |
|
1888
|
service.refundPayment({ |
|
1889
|
amount: 180, |
|
1890
|
payment_id: payment2.id, |
|
1891
|
}), |
|
1892
|
service.refundPayment({ |
|
1893
|
amount: 100, |
|
1894
|
payment_id: payment3.id, |
|
1895
|
}), |
|
1896
|
service.refundPayment({ |
|
1897
|
amount: 40, |
|
1898
|
payment_id: payment3.id, |
|
1899
|
}), |
|
1900
|
service.refundPayment({ |
|
1901
|
amount: 60, |
|
1902
|
payment_id: payment3.id, |
|
1903
|
}), |
|
1904
|
service.refundPayment({ |
|
1905
|
amount: 400, |
|
1906
|
payment_id: payment4.id, |
|
1907
|
}), |
|
1908
|
service.refundPayment({ |
|
1909
|
amount: 99, |
|
1910
|
payment_id: payment4.id, |
|
1911
|
}), |
|
1912
|
]) |
|
1913
|
|
|
1914
|
expect(payment1).toEqual( |
|
1915
|
expect.objectContaining({ |
|
1916
|
amount: 120, |
|
1917
|
currency_code: "usd", |
|
1918
|
provider_id: "pp_system_default", |
|
1919
|
payment_session: expect.objectContaining({ |
|
1920
|
currency_code: "usd", |
|
1921
|
amount: 120, |
|
1922
|
raw_amount: { value: "120", precision: 20 }, |
|
1923
|
provider_id: "pp_system_default", |
|
1924
|
status: "authorized", |
|
1925
|
authorized_at: expect.any(Date), |
|
1926
|
}), |
|
1927
|
}) |
|
1928
|
) |
|
1929
|
|
|
1930
|
expect(payment2).toEqual( |
|
1931
|
expect.objectContaining({ |
|
1932
|
amount: 180, |
|
1933
|
currency_code: "usd", |
|
1934
|
provider_id: "pp_system_default", |
|
1935
|
payment_session: expect.objectContaining({ |
|
1936
|
currency_code: "usd", |
|
1937
|
amount: 180, |
|
1938
|
raw_amount: { value: "180", precision: 20 }, |
|
1939
|
provider_id: "pp_system_default", |
|
1940
|
status: "authorized", |
|
1941
|
authorized_at: expect.any(Date), |
|
1942
|
}), |
|
1943
|
}) |
|
1944
|
) |
|
1945
|
|
|
1946
|
expect(payment3).toEqual( |
|
1947
|
expect.objectContaining({ |
|
1948
|
amount: 200, |
|
1949
|
currency_code: "usd", |
|
1950
|
provider_id: "pp_system_default", |
|
1951
|
payment_session: expect.objectContaining({ |
|
1952
|
currency_code: "usd", |
|
1953
|
amount: 200, |
|
1954
|
raw_amount: { value: "200", precision: 20 }, |
|
1955
|
provider_id: "pp_system_default", |
|
1956
|
status: "authorized", |
|
1957
|
authorized_at: expect.any(Date), |
|
1958
|
}), |
|
1959
|
}) |
|
1960
|
) |
|
1961
|
|
|
1962
|
expect(payment4).toEqual( |
|
1963
|
expect.objectContaining({ |
|
1964
|
amount: 500, |
|
1965
|
currency_code: "eur", |
|
1966
|
provider_id: "pp_system_default", |
|
1967
|
payment_session: expect.objectContaining({ |
|
1968
|
currency_code: "eur", |
|
1969
|
amount: 500, |
|
1970
|
raw_amount: { value: "500", precision: 20 }, |
|
1971
|
provider_id: "pp_system_default", |
|
1972
|
status: "authorized", |
|
1973
|
authorized_at: expect.any(Date), |
|
1974
|
}), |
|
1975
|
}) |
|
1976
|
) |
|
1977
|
|
|
1978
|
const finalCollection = ( |
|
1979
|
await service.listPaymentCollections({ |
|
1980
|
id: collection.id, |
|
1981
|
}) |
|
1982
|
)[0] |
|
1983
|
|
|
1984
|
expect(finalCollection).toEqual( |
|
1985
|
expect.objectContaining({ |
|
1986
|
status: "completed", |
|
1987
|
amount: 500, |
|
1988
|
authorized_amount: 1000, |
|
1989
|
captured_amount: 1000, |
|
1990
|
refunded_amount: 999, |
|
1991
|
}) |
|
1992
|
) |
|
1993
|
}) |
|
1994
|
|
|
1995
|
it("should not allow concurrent captures to exceed the authorized amount", async () => { |
|
1996
|
const collection = await service.createPaymentCollections({ |
|
1997
|
amount: 500, |
|
1998
|
currency_code: "usd", |
|
1999
|
}) |
|
2000
|
|
|
2001
|
const session = await service.createPaymentSession(collection.id, { |
|
2002
|
provider_id: "pp_system_default", |
|
2003
|
amount: 500, |
|
2004
|
currency_code: "usd", |
|
2005
|
data: {}, |
|
2006
|
}) |
|
2007
|
|
|
2008
|
const payment = await service.authorizePaymentSession(session.id, {}) |
|
2009
|
|
|
2010
|
// Two concurrent captures that each pass the guard in isolation |
|
2011
|
// (500 - 0 = 500 remaining seen by both) but together capture 600. |
|
2012
|
const outcomes = await promiseAll([ |
|
2013
|
service |
|
2014
|
.capturePayment({ amount: 300, payment_id: payment.id }) |
|
2015
|
.then(() => "ok", () => "threw"), |
|
2016
|
service |
|
2017
|
.capturePayment({ amount: 300, payment_id: payment.id }) |
|
2018
|
.then(() => "ok", () => "threw"), |
|
2019
|
]) |
|
2020
|
|
|
2021
|
// Exactly one capture is admitted; the other is rejected by the guard. |
|
2022
|
expect(outcomes.filter((o) => o === "threw")).toHaveLength(1) |
|
2023
|
|
|
2024
|
const [finalCollection] = await service.listPaymentCollections({ |
|
2025
|
id: collection.id, |
|
2026
|
}) |
|
2027
|
|
|
2028
|
expect(finalCollection.captured_amount).toBe(300) |
|
2029
|
}) |
|
2030
|
|
|
2031
|
it("should not allow concurrent refunds to exceed the captured amount", async () => { |
|
2032
|
const collection = await service.createPaymentCollections({ |
|
2033
|
amount: 500, |
|
2034
|
currency_code: "usd", |
|
2035
|
}) |
|
2036
|
|
|
2037
|
const session = await service.createPaymentSession(collection.id, { |
|
2038
|
provider_id: "pp_system_default", |
|
2039
|
amount: 500, |
|
2040
|
currency_code: "usd", |
|
2041
|
data: {}, |
|
2042
|
}) |
|
2043
|
|
|
2044
|
const payment = await service.authorizePaymentSession(session.id, {}) |
|
2045
|
|
|
2046
|
await service.capturePayment({ amount: 500, payment_id: payment.id }) |
|
2047
|
|
|
2048
|
// Two concurrent refunds that each pass the guard in isolation |
|
2049
|
// (500 captured, 0 refunded seen by both) but together refund 600. |
|
2050
|
const outcomes = await promiseAll([ |
|
2051
|
service |
|
2052
|
.refundPayment({ amount: 300, payment_id: payment.id }) |
|
2053
|
.then(() => "ok", () => "threw"), |
|
2054
|
service |
|
2055
|
.refundPayment({ amount: 300, payment_id: payment.id }) |
|
2056
|
.then(() => "ok", () => "threw"), |
|
2057
|
]) |
|
2058
|
|
|
2059
|
// Exactly one refund is admitted; the other is rejected by the guard. |
|
2060
|
expect(outcomes.filter((o) => o === "threw")).toHaveLength(1) |
|
2061
|
|
|
2062
|
const [finalCollection] = await service.listPaymentCollections({ |
|
2063
|
id: collection.id, |
|
2064
|
}) |
|
2065
|
|
|
2066
|
expect(finalCollection.refunded_amount).toBe(300) |
|
2067
|
}) |
|
2068
|
}) |
|
2069
|
}) |
|
2070
|
|
|
2071
|
describe("getWebhookActionAndData", () => { |
|
2072
|
it("resolves the provider when given a bare provider identifier", async () => { |
|
2073
|
const result = await service.getWebhookActionAndData({ |
|
2074
|
provider: "system_default", |
|
2075
|
payload: { data: {}, rawData: Buffer.from(""), headers: {} }, |
|
2076
|
}) |
|
2077
|
|
|
2078
|
expect(result).toEqual({ action: PaymentActions.NOT_SUPPORTED }) |
|
2079
|
}) |
|
2080
|
|
|
2081
|
it("resolves the provider when already given a pp_-prefixed provider id", async () => { |
|
2082
|
const result = await service.getWebhookActionAndData({ |
|
2083
|
provider: "pp_system_default", |
|
2084
|
payload: { data: {}, rawData: Buffer.from(""), headers: {} }, |
|
2085
|
}) |
|
2086
|
|
|
2087
|
expect(result).toEqual({ action: PaymentActions.NOT_SUPPORTED }) |
|
2088
|
}) |
|
2089
|
}) |
|
2090
|
}) |
|
2091
|
}, |
|
2092
|
}) |