|
1
|
import { |
|
2
|
createPaymentSessionsWorkflow, |
|
3
|
createPaymentSessionsWorkflowId, |
|
4
|
} from "@medusajs/core-flows" |
|
5
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
6
|
import { |
|
7
|
ICustomerModuleService, |
|
8
|
IPaymentModuleService, |
|
9
|
IRegionModuleService, |
|
10
|
} from "@medusajs/types" |
|
11
|
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils" |
|
12
|
|
|
13
|
jest.setTimeout(50000) |
|
14
|
|
|
15
|
const env = {} |
|
16
|
|
|
17
|
medusaIntegrationTestRunner({ |
|
18
|
env, |
|
19
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
20
|
describe("Carts workflows", () => { |
|
21
|
let appContainer |
|
22
|
let paymentModule: IPaymentModuleService |
|
23
|
let regionModule: IRegionModuleService |
|
24
|
let customerModule: ICustomerModuleService |
|
25
|
let query |
|
26
|
|
|
27
|
beforeAll(async () => { |
|
28
|
appContainer = getContainer() |
|
29
|
paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
30
|
regionModule = appContainer.resolve(Modules.REGION) |
|
31
|
customerModule = appContainer.resolve(Modules.CUSTOMER) |
|
32
|
query = appContainer.resolve(ContainerRegistrationKeys.QUERY) |
|
33
|
}) |
|
34
|
|
|
35
|
describe("createPaymentSessionWorkflow", () => { |
|
36
|
let region |
|
37
|
let paymentCollection |
|
38
|
let customer |
|
39
|
|
|
40
|
beforeEach(async () => { |
|
41
|
region = await regionModule.createRegions({ |
|
42
|
currency_code: "usd", |
|
43
|
name: "US", |
|
44
|
}) |
|
45
|
|
|
46
|
paymentCollection = await paymentModule.createPaymentCollections({ |
|
47
|
currency_code: "usd", |
|
48
|
amount: 1000, |
|
49
|
}) |
|
50
|
|
|
51
|
customer = await customerModule.createCustomers({ |
|
52
|
email: "test@test.com", |
|
53
|
first_name: "Test", |
|
54
|
last_name: "Test", |
|
55
|
}) |
|
56
|
}) |
|
57
|
|
|
58
|
it("should create payment sessions", async () => { |
|
59
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
60
|
input: { |
|
61
|
payment_collection_id: paymentCollection.id, |
|
62
|
provider_id: "pp_system_default", |
|
63
|
context: {}, |
|
64
|
data: {}, |
|
65
|
}, |
|
66
|
}) |
|
67
|
|
|
68
|
paymentCollection = await paymentModule.retrievePaymentCollection( |
|
69
|
paymentCollection.id, |
|
70
|
{ |
|
71
|
relations: ["payment_sessions"], |
|
72
|
} |
|
73
|
) |
|
74
|
|
|
75
|
expect(paymentCollection).toEqual( |
|
76
|
expect.objectContaining({ |
|
77
|
id: paymentCollection.id, |
|
78
|
currency_code: "usd", |
|
79
|
amount: 1000, |
|
80
|
payment_sessions: expect.arrayContaining([ |
|
81
|
expect.objectContaining({ |
|
82
|
amount: 1000, |
|
83
|
currency_code: "usd", |
|
84
|
provider_id: "pp_system_default", |
|
85
|
}), |
|
86
|
]), |
|
87
|
}) |
|
88
|
) |
|
89
|
}) |
|
90
|
|
|
91
|
it("should create payment sessions with customer", async () => { |
|
92
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
93
|
input: { |
|
94
|
payment_collection_id: paymentCollection.id, |
|
95
|
provider_id: "pp_system_default", |
|
96
|
customer_id: customer.id, |
|
97
|
}, |
|
98
|
}) |
|
99
|
|
|
100
|
const { |
|
101
|
data: [updatedPaymentCollection], |
|
102
|
} = await query.graph({ |
|
103
|
entity: "payment_collection", |
|
104
|
filters: { |
|
105
|
id: paymentCollection.id, |
|
106
|
}, |
|
107
|
fields: ["id", "currency_code", "amount", "payment_sessions.*"], |
|
108
|
}) |
|
109
|
|
|
110
|
expect(updatedPaymentCollection.payment_sessions).toHaveLength(1) |
|
111
|
expect(updatedPaymentCollection).toEqual( |
|
112
|
expect.objectContaining({ |
|
113
|
id: paymentCollection.id, |
|
114
|
currency_code: "usd", |
|
115
|
amount: 1000, |
|
116
|
payment_sessions: expect.arrayContaining([ |
|
117
|
expect.objectContaining({ |
|
118
|
context: expect.objectContaining({ |
|
119
|
customer: expect.objectContaining({ |
|
120
|
id: customer.id, |
|
121
|
}), |
|
122
|
account_holder: expect.objectContaining({ |
|
123
|
email: customer.email, |
|
124
|
}), |
|
125
|
}), |
|
126
|
}), |
|
127
|
]), |
|
128
|
}) |
|
129
|
) |
|
130
|
}) |
|
131
|
|
|
132
|
it("should create payment sessions when customer has no account_holder links)", async () => { |
|
133
|
const { |
|
134
|
data: [customerBefore], |
|
135
|
} = await query.graph({ |
|
136
|
entity: "customer", |
|
137
|
filters: { id: customer.id }, |
|
138
|
fields: ["id", "account_holders.*"], |
|
139
|
}) |
|
140
|
|
|
141
|
expect( |
|
142
|
customerBefore.account_holders === undefined || |
|
143
|
customerBefore.account_holders?.length === 0 |
|
144
|
).toBe(true) |
|
145
|
|
|
146
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
147
|
input: { |
|
148
|
payment_collection_id: paymentCollection.id, |
|
149
|
provider_id: "pp_system_default", |
|
150
|
customer_id: customer.id, |
|
151
|
}, |
|
152
|
}) |
|
153
|
|
|
154
|
const { |
|
155
|
data: [updatedPaymentCollection], |
|
156
|
} = await query.graph({ |
|
157
|
entity: "payment_collection", |
|
158
|
filters: { id: paymentCollection.id }, |
|
159
|
fields: ["id", "payment_sessions.*"], |
|
160
|
}) |
|
161
|
|
|
162
|
expect(updatedPaymentCollection.payment_sessions).toHaveLength(1) |
|
163
|
|
|
164
|
const { |
|
165
|
data: [customerAfter], |
|
166
|
} = await query.graph({ |
|
167
|
entity: "customer", |
|
168
|
filters: { id: customer.id }, |
|
169
|
fields: ["id", "account_holders.*"], |
|
170
|
}) |
|
171
|
|
|
172
|
expect(customerAfter.account_holders).toEqual( |
|
173
|
expect.arrayContaining([ |
|
174
|
expect.objectContaining({ |
|
175
|
email: customer.email, |
|
176
|
}), |
|
177
|
]) |
|
178
|
) |
|
179
|
}) |
|
180
|
|
|
181
|
it("should delete existing sessions when create payment sessions", async () => { |
|
182
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
183
|
input: { |
|
184
|
payment_collection_id: paymentCollection.id, |
|
185
|
provider_id: "pp_system_default", |
|
186
|
context: {}, |
|
187
|
data: {}, |
|
188
|
}, |
|
189
|
}) |
|
190
|
|
|
191
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
192
|
input: { |
|
193
|
payment_collection_id: paymentCollection.id, |
|
194
|
provider_id: "pp_system_default", |
|
195
|
context: {}, |
|
196
|
data: {}, |
|
197
|
}, |
|
198
|
}) |
|
199
|
|
|
200
|
paymentCollection = await paymentModule.retrievePaymentCollection( |
|
201
|
paymentCollection.id, |
|
202
|
{ relations: ["payment_sessions"] } |
|
203
|
) |
|
204
|
|
|
205
|
expect(paymentCollection).toEqual( |
|
206
|
expect.objectContaining({ |
|
207
|
id: paymentCollection.id, |
|
208
|
currency_code: "usd", |
|
209
|
amount: 1000, |
|
210
|
payment_sessions: [ |
|
211
|
expect.objectContaining({ |
|
212
|
amount: 1000, |
|
213
|
currency_code: "usd", |
|
214
|
provider_id: "pp_system_default", |
|
215
|
}), |
|
216
|
], |
|
217
|
}) |
|
218
|
) |
|
219
|
}) |
|
220
|
|
|
221
|
describe("compensation", () => { |
|
222
|
it("should delete created payment collection if a subsequent step fails", async () => { |
|
223
|
const workflow = createPaymentSessionsWorkflow(appContainer) |
|
224
|
|
|
225
|
workflow.appendAction("throw", createPaymentSessionsWorkflowId, { |
|
226
|
invoke: async function failStep() { |
|
227
|
throw new Error( |
|
228
|
`Failed to do something after creating payment sessions` |
|
229
|
) |
|
230
|
}, |
|
231
|
}) |
|
232
|
|
|
233
|
const region = await regionModule.createRegions({ |
|
234
|
currency_code: "usd", |
|
235
|
name: "US", |
|
236
|
}) |
|
237
|
|
|
238
|
let paymentCollection = |
|
239
|
await paymentModule.createPaymentCollections({ |
|
240
|
currency_code: "usd", |
|
241
|
amount: 1000, |
|
242
|
}) |
|
243
|
|
|
244
|
const { errors } = await workflow.run({ |
|
245
|
input: { |
|
246
|
payment_collection_id: paymentCollection.id, |
|
247
|
provider_id: "pp_system_default", |
|
248
|
context: {}, |
|
249
|
data: {}, |
|
250
|
}, |
|
251
|
throwOnError: false, |
|
252
|
}) |
|
253
|
|
|
254
|
expect(errors).toEqual([ |
|
255
|
{ |
|
256
|
action: "throw", |
|
257
|
handlerType: "invoke", |
|
258
|
error: expect.objectContaining({ |
|
259
|
message: `Failed to do something after creating payment sessions`, |
|
260
|
}), |
|
261
|
}, |
|
262
|
]) |
|
263
|
|
|
264
|
const sessions = await paymentModule.listPaymentSessions({ |
|
265
|
payment_collection_id: paymentCollection.id, |
|
266
|
}) |
|
267
|
|
|
268
|
expect(sessions).toHaveLength(0) |
|
269
|
}) |
|
270
|
|
|
271
|
it("should not delete account holder if it exists before creating payment sessions", async () => { |
|
272
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
273
|
input: { |
|
274
|
payment_collection_id: paymentCollection.id, |
|
275
|
provider_id: "pp_system_default", |
|
276
|
customer_id: customer.id, |
|
277
|
}, |
|
278
|
}) |
|
279
|
|
|
280
|
const { |
|
281
|
data: [updatedCustomer1], |
|
282
|
} = await query.graph({ |
|
283
|
entity: "customer", |
|
284
|
filters: { |
|
285
|
id: customer.id, |
|
286
|
}, |
|
287
|
fields: ["id", "account_holders.*"], |
|
288
|
}) |
|
289
|
|
|
290
|
expect(updatedCustomer1.account_holders).toEqual( |
|
291
|
expect.arrayContaining([ |
|
292
|
expect.objectContaining({ |
|
293
|
email: customer.email, |
|
294
|
}), |
|
295
|
]) |
|
296
|
) |
|
297
|
|
|
298
|
const newPaymentCollection = |
|
299
|
await paymentModule.createPaymentCollections({ |
|
300
|
currency_code: "usd", |
|
301
|
amount: 2000, |
|
302
|
}) |
|
303
|
|
|
304
|
const workflow = createPaymentSessionsWorkflow(appContainer) |
|
305
|
|
|
306
|
workflow.appendAction("throw", createPaymentSessionsWorkflowId, { |
|
307
|
invoke: async function failStep() { |
|
308
|
throw new Error( |
|
309
|
`Failed to do something after creating payment sessions` |
|
310
|
) |
|
311
|
}, |
|
312
|
}) |
|
313
|
|
|
314
|
const { errors } = await workflow.run({ |
|
315
|
input: { |
|
316
|
payment_collection_id: newPaymentCollection.id, |
|
317
|
provider_id: "pp_system_default", |
|
318
|
customer_id: customer.id, |
|
319
|
context: {}, |
|
320
|
data: {}, |
|
321
|
}, |
|
322
|
throwOnError: false, |
|
323
|
}) |
|
324
|
|
|
325
|
expect(errors).toEqual([ |
|
326
|
{ |
|
327
|
action: "throw", |
|
328
|
handlerType: "invoke", |
|
329
|
error: expect.objectContaining({ |
|
330
|
message: `Failed to do something after creating payment sessions`, |
|
331
|
}), |
|
332
|
}, |
|
333
|
]) |
|
334
|
|
|
335
|
const { |
|
336
|
data: [updatedCustomer2], |
|
337
|
} = await query.graph({ |
|
338
|
entity: "customer", |
|
339
|
filters: { |
|
340
|
id: customer.id, |
|
341
|
}, |
|
342
|
fields: ["id", "account_holders.*"], |
|
343
|
}) |
|
344
|
|
|
345
|
expect(updatedCustomer2.account_holders).toEqual( |
|
346
|
expect.arrayContaining([ |
|
347
|
expect.objectContaining({ |
|
348
|
email: customer.email, |
|
349
|
}), |
|
350
|
]) |
|
351
|
) |
|
352
|
}) |
|
353
|
|
|
354
|
it("should skip compensation for account holder step on failure", async () => { |
|
355
|
// Spy on deleteAccountHolder to verify it's NOT called during compensation |
|
356
|
const deleteAccountHolderSpy = jest.spyOn( |
|
357
|
paymentModule, |
|
358
|
"deleteAccountHolder" |
|
359
|
) |
|
360
|
|
|
361
|
const newCustomer = await customerModule.createCustomers({ |
|
362
|
email: "new-customer@test.com", |
|
363
|
first_name: "New", |
|
364
|
last_name: "Customer", |
|
365
|
}) |
|
366
|
|
|
367
|
const newPaymentCollection = |
|
368
|
await paymentModule.createPaymentCollections({ |
|
369
|
currency_code: "usd", |
|
370
|
amount: 3000, |
|
371
|
}) |
|
372
|
|
|
373
|
const workflow = createPaymentSessionsWorkflow(appContainer) |
|
374
|
|
|
375
|
workflow.appendAction("throw", createPaymentSessionsWorkflowId, { |
|
376
|
invoke: async function failStep() { |
|
377
|
throw new Error( |
|
378
|
`Failed to do something after creating payment sessions` |
|
379
|
) |
|
380
|
}, |
|
381
|
}) |
|
382
|
|
|
383
|
const { errors } = await workflow.run({ |
|
384
|
input: { |
|
385
|
payment_collection_id: newPaymentCollection.id, |
|
386
|
provider_id: "pp_system_default", |
|
387
|
customer_id: newCustomer.id, |
|
388
|
context: {}, |
|
389
|
data: {}, |
|
390
|
}, |
|
391
|
throwOnError: false, |
|
392
|
}) |
|
393
|
|
|
394
|
expect(errors).toEqual([ |
|
395
|
{ |
|
396
|
action: "throw", |
|
397
|
handlerType: "invoke", |
|
398
|
error: expect.objectContaining({ |
|
399
|
message: `Failed to do something after creating payment sessions`, |
|
400
|
}), |
|
401
|
}, |
|
402
|
]) |
|
403
|
|
|
404
|
// Verify deleteAccountHolder was NOT called because noCompensation: true |
|
405
|
// prevents the compensation function from running |
|
406
|
expect(deleteAccountHolderSpy).not.toHaveBeenCalled() |
|
407
|
|
|
408
|
deleteAccountHolderSpy.mockRestore() |
|
409
|
}) |
|
410
|
}) |
|
411
|
}) |
|
412
|
}) |
|
413
|
}, |
|
414
|
}) |