|
1
|
import { |
|
2
|
addToCartWorkflow, |
|
3
|
beginOrderEditOrderWorkflow, |
|
4
|
compensatePaymentIfNeededStep, |
|
5
|
completeCartWorkflow, |
|
6
|
confirmOrderEditRequestWorkflow, |
|
7
|
createCartWorkflow, |
|
8
|
createPaymentCollectionForCartWorkflow, |
|
9
|
createPaymentSessionsWorkflow, |
|
10
|
getOrderDetailWorkflow, |
|
11
|
listShippingOptionsForCartWorkflow, |
|
12
|
orderEditAddNewItemWorkflow, |
|
13
|
processPaymentWorkflow, |
|
14
|
} from "@medusajs/core-flows" |
|
15
|
import { |
|
16
|
createStep, |
|
17
|
createWorkflow, |
|
18
|
WorkflowData, |
|
19
|
} from "@medusajs/framework/workflows-sdk" |
|
20
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
21
|
import { |
|
22
|
ICartModuleService, |
|
23
|
ICustomerModuleService, |
|
24
|
IEventBusModuleService, |
|
25
|
IFulfillmentModuleService, |
|
26
|
IInventoryService, |
|
27
|
IPaymentModuleService, |
|
28
|
IPricingModuleService, |
|
29
|
IProductModuleService, |
|
30
|
IRegionModuleService, |
|
31
|
ISalesChannelModuleService, |
|
32
|
IStockLocationService, |
|
33
|
Message, |
|
34
|
} from "@medusajs/types" |
|
35
|
import { |
|
36
|
ContainerRegistrationKeys, |
|
37
|
Modules, |
|
38
|
PaymentCollectionStatus, |
|
39
|
ProductStatus, |
|
40
|
remoteQueryObjectFromString, |
|
41
|
} from "@medusajs/utils" |
|
42
|
import { |
|
43
|
adminHeaders, |
|
44
|
createAdminUser, |
|
45
|
generatePublishableKey, |
|
46
|
generateStoreHeaders, |
|
47
|
} from "../../../../helpers/create-admin-user" |
|
48
|
import { linkRegionPaymentProviders } from "../../../../helpers/link-region-payment-providers" |
|
49
|
import { seedStorefrontDefaults } from "../../../../helpers/seed-storefront-defaults" |
|
50
|
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer" |
|
51
|
|
|
52
|
jest.setTimeout(200000) |
|
53
|
|
|
54
|
const env = {} |
|
55
|
|
|
56
|
medusaIntegrationTestRunner({ |
|
57
|
env, |
|
58
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
59
|
describe("Carts workflows", () => { |
|
60
|
let appContainer |
|
61
|
let cartModuleService: ICartModuleService |
|
62
|
let regionModuleService: IRegionModuleService |
|
63
|
let scModuleService: ISalesChannelModuleService |
|
64
|
let customerModule: ICustomerModuleService |
|
65
|
let productModule: IProductModuleService |
|
66
|
let pricingModule: IPricingModuleService |
|
67
|
let paymentModule: IPaymentModuleService |
|
68
|
let stockLocationModule: IStockLocationService |
|
69
|
let inventoryModule: IInventoryService |
|
70
|
let fulfillmentModule: IFulfillmentModuleService |
|
71
|
let remoteLink, remoteQuery, query |
|
72
|
let storeHeaders |
|
73
|
let salesChannel |
|
74
|
let defaultRegion |
|
75
|
let customer, storeHeadersWithCustomer |
|
76
|
let setPricingContextHook: any |
|
77
|
let eventBus: IEventBusModuleService |
|
78
|
|
|
79
|
beforeAll(async () => { |
|
80
|
appContainer = getContainer() |
|
81
|
cartModuleService = appContainer.resolve(Modules.CART) |
|
82
|
regionModuleService = appContainer.resolve(Modules.REGION) |
|
83
|
scModuleService = appContainer.resolve(Modules.SALES_CHANNEL) |
|
84
|
customerModule = appContainer.resolve(Modules.CUSTOMER) |
|
85
|
productModule = appContainer.resolve(Modules.PRODUCT) |
|
86
|
pricingModule = appContainer.resolve(Modules.PRICING) |
|
87
|
paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
88
|
eventBus = appContainer.resolve(Modules.EVENT_BUS) |
|
89
|
fulfillmentModule = appContainer.resolve(Modules.FULFILLMENT) |
|
90
|
inventoryModule = appContainer.resolve(Modules.INVENTORY) |
|
91
|
stockLocationModule = appContainer.resolve(Modules.STOCK_LOCATION) |
|
92
|
remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK) |
|
93
|
remoteQuery = appContainer.resolve( |
|
94
|
ContainerRegistrationKeys.REMOTE_QUERY |
|
95
|
) |
|
96
|
query = appContainer.resolve(ContainerRegistrationKeys.QUERY) |
|
97
|
|
|
98
|
createCartWorkflow.hooks.setPricingContext( |
|
99
|
(input) => { |
|
100
|
if (setPricingContextHook) { |
|
101
|
return setPricingContextHook(input) |
|
102
|
} |
|
103
|
}, |
|
104
|
() => {} |
|
105
|
) |
|
106
|
addToCartWorkflow.hooks.setPricingContext( |
|
107
|
(input) => { |
|
108
|
if (setPricingContextHook) { |
|
109
|
return setPricingContextHook(input) |
|
110
|
} |
|
111
|
}, |
|
112
|
() => {} |
|
113
|
) |
|
114
|
listShippingOptionsForCartWorkflow.hooks.setPricingContext( |
|
115
|
(input) => { |
|
116
|
if (setPricingContextHook) { |
|
117
|
return setPricingContextHook(input) |
|
118
|
} |
|
119
|
}, |
|
120
|
() => {} |
|
121
|
) |
|
122
|
}) |
|
123
|
|
|
124
|
beforeAll(async () => { |
|
125
|
const publishableKey = await generatePublishableKey(appContainer) |
|
126
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
127
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
128
|
|
|
129
|
const result = await createAuthenticatedCustomer(api, storeHeaders, { |
|
130
|
first_name: "tony", |
|
131
|
last_name: "stark", |
|
132
|
email: "tony@test-industries.com", |
|
133
|
}) |
|
134
|
|
|
135
|
customer = result.customer |
|
136
|
storeHeadersWithCustomer = { |
|
137
|
headers: { |
|
138
|
...storeHeaders.headers, |
|
139
|
authorization: `Bearer ${result.jwt}`, |
|
140
|
}, |
|
141
|
} |
|
142
|
|
|
143
|
const { region } = await seedStorefrontDefaults(appContainer, "dkk") |
|
144
|
|
|
145
|
defaultRegion = region |
|
146
|
|
|
147
|
salesChannel = ( |
|
148
|
await api.post( |
|
149
|
"/admin/sales-channels", |
|
150
|
{ name: "test sales channel", description: "channel" }, |
|
151
|
adminHeaders |
|
152
|
) |
|
153
|
).data.sales_channel |
|
154
|
|
|
155
|
await dbUtils.snapshot() |
|
156
|
}) |
|
157
|
|
|
158
|
describe("CompleteCartWorkflow", () => { |
|
159
|
it("should complete cart with custom item", async () => { |
|
160
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
161
|
name: "Webshop", |
|
162
|
}) |
|
163
|
|
|
164
|
const location = await stockLocationModule.createStockLocations({ |
|
165
|
name: "Warehouse", |
|
166
|
}) |
|
167
|
|
|
168
|
const region = await regionModuleService.createRegions({ |
|
169
|
name: "US", |
|
170
|
currency_code: "usd", |
|
171
|
}) |
|
172
|
|
|
173
|
await linkRegionPaymentProviders(appContainer, region.id) |
|
174
|
|
|
175
|
let cart = await cartModuleService.createCarts({ |
|
176
|
currency_code: "usd", |
|
177
|
sales_channel_id: salesChannel.id, |
|
178
|
region_id: region.id, |
|
179
|
}) |
|
180
|
|
|
181
|
await remoteLink.create([ |
|
182
|
{ |
|
183
|
[Modules.SALES_CHANNEL]: { |
|
184
|
sales_channel_id: salesChannel.id, |
|
185
|
}, |
|
186
|
[Modules.STOCK_LOCATION]: { |
|
187
|
stock_location_id: location.id, |
|
188
|
}, |
|
189
|
}, |
|
190
|
]) |
|
191
|
|
|
192
|
cart = await cartModuleService.retrieveCart(cart.id, { |
|
193
|
select: ["id", "region_id", "currency_code", "sales_channel_id"], |
|
194
|
}) |
|
195
|
|
|
196
|
await addToCartWorkflow(appContainer).run({ |
|
197
|
input: { |
|
198
|
items: [ |
|
199
|
{ |
|
200
|
title: "Test item", |
|
201
|
subtitle: "Test subtitle", |
|
202
|
thumbnail: "some-url", |
|
203
|
requires_shipping: false, |
|
204
|
is_discountable: false, |
|
205
|
is_tax_inclusive: false, |
|
206
|
unit_price: 3000, |
|
207
|
metadata: { |
|
208
|
foo: "bar", |
|
209
|
}, |
|
210
|
quantity: 1, |
|
211
|
}, |
|
212
|
{ |
|
213
|
title: "zero price item", |
|
214
|
subtitle: "zero price item", |
|
215
|
thumbnail: "some-url", |
|
216
|
requires_shipping: false, |
|
217
|
is_discountable: false, |
|
218
|
is_tax_inclusive: false, |
|
219
|
unit_price: 0, |
|
220
|
quantity: 1, |
|
221
|
}, |
|
222
|
], |
|
223
|
cart_id: cart.id, |
|
224
|
}, |
|
225
|
}) |
|
226
|
|
|
227
|
cart = await cartModuleService.retrieveCart(cart.id, { |
|
228
|
relations: ["items"], |
|
229
|
}) |
|
230
|
|
|
231
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
232
|
input: { |
|
233
|
cart_id: cart.id, |
|
234
|
}, |
|
235
|
}) |
|
236
|
|
|
237
|
const [paymentCollection] = |
|
238
|
await paymentModule.listPaymentCollections({}) |
|
239
|
|
|
240
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
241
|
input: { |
|
242
|
payment_collection_id: paymentCollection.id, |
|
243
|
provider_id: "pp_system_default", |
|
244
|
context: {}, |
|
245
|
data: {}, |
|
246
|
}, |
|
247
|
}) |
|
248
|
|
|
249
|
await completeCartWorkflow(appContainer).run({ |
|
250
|
input: { |
|
251
|
id: cart.id, |
|
252
|
}, |
|
253
|
}) |
|
254
|
|
|
255
|
const { data } = await query.graph({ |
|
256
|
entity: "cart", |
|
257
|
filters: { |
|
258
|
id: cart.id, |
|
259
|
}, |
|
260
|
fields: ["id", "currency_code", "completed_at", "items.*"], |
|
261
|
}) |
|
262
|
|
|
263
|
expect(data[0]).toEqual( |
|
264
|
expect.objectContaining({ |
|
265
|
id: cart.id, |
|
266
|
currency_code: "usd", |
|
267
|
completed_at: expect.any(Date), |
|
268
|
items: [ |
|
269
|
{ |
|
270
|
cart_id: cart.id, |
|
271
|
compare_at_unit_price: null, |
|
272
|
created_at: expect.any(Date), |
|
273
|
deleted_at: null, |
|
274
|
id: expect.any(String), |
|
275
|
is_discountable: false, |
|
276
|
is_giftcard: false, |
|
277
|
is_tax_inclusive: false, |
|
278
|
is_custom_price: true, |
|
279
|
metadata: { |
|
280
|
foo: "bar", |
|
281
|
}, |
|
282
|
product_collection: null, |
|
283
|
product_description: null, |
|
284
|
product_handle: null, |
|
285
|
product_id: null, |
|
286
|
product_subtitle: null, |
|
287
|
product_title: null, |
|
288
|
product_type: null, |
|
289
|
product_type_id: null, |
|
290
|
quantity: 1, |
|
291
|
raw_compare_at_unit_price: null, |
|
292
|
raw_unit_price: { |
|
293
|
precision: 20, |
|
294
|
value: "3000", |
|
295
|
}, |
|
296
|
requires_shipping: false, |
|
297
|
subtitle: "Test subtitle", |
|
298
|
thumbnail: "some-url", |
|
299
|
title: "Test item", |
|
300
|
unit_price: 3000, |
|
301
|
updated_at: expect.any(Date), |
|
302
|
variant_barcode: null, |
|
303
|
variant_id: null, |
|
304
|
variant_option_values: null, |
|
305
|
variant_sku: null, |
|
306
|
variant_title: null, |
|
307
|
}, |
|
308
|
expect.objectContaining({ |
|
309
|
title: "zero price item", |
|
310
|
subtitle: "zero price item", |
|
311
|
is_custom_price: true, |
|
312
|
unit_price: 0, |
|
313
|
}), |
|
314
|
], |
|
315
|
}) |
|
316
|
) |
|
317
|
}) |
|
318
|
|
|
319
|
it("should complete cart reserving inventory from available locations", async () => { |
|
320
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
321
|
name: "Webshop", |
|
322
|
}) |
|
323
|
|
|
324
|
const location = await stockLocationModule.createStockLocations({ |
|
325
|
name: "Warehouse", |
|
326
|
}) |
|
327
|
|
|
328
|
const location2 = await stockLocationModule.createStockLocations({ |
|
329
|
name: "Side Warehouse", |
|
330
|
}) |
|
331
|
|
|
332
|
const [product] = await productModule.createProducts([ |
|
333
|
{ |
|
334
|
title: "Test product", |
|
335
|
status: ProductStatus.PUBLISHED, |
|
336
|
variants: [ |
|
337
|
{ |
|
338
|
title: "Test variant", |
|
339
|
}, |
|
340
|
], |
|
341
|
}, |
|
342
|
]) |
|
343
|
|
|
344
|
const inventoryItem = await inventoryModule.createInventoryItems({ |
|
345
|
sku: "inv-1234", |
|
346
|
}) |
|
347
|
|
|
348
|
await inventoryModule.createInventoryLevels([ |
|
349
|
{ |
|
350
|
inventory_item_id: inventoryItem.id, |
|
351
|
location_id: location.id, |
|
352
|
stocked_quantity: 1, |
|
353
|
reserved_quantity: 0, |
|
354
|
}, |
|
355
|
]) |
|
356
|
|
|
357
|
await inventoryModule.createInventoryLevels([ |
|
358
|
{ |
|
359
|
inventory_item_id: inventoryItem.id, |
|
360
|
location_id: location2.id, |
|
361
|
stocked_quantity: 1, |
|
362
|
reserved_quantity: 0, |
|
363
|
}, |
|
364
|
]) |
|
365
|
|
|
366
|
const priceSet = await pricingModule.createPriceSets({ |
|
367
|
prices: [ |
|
368
|
{ |
|
369
|
amount: 3000, |
|
370
|
currency_code: "usd", |
|
371
|
}, |
|
372
|
], |
|
373
|
}) |
|
374
|
|
|
375
|
await pricingModule.createPricePreferences({ |
|
376
|
attribute: "currency_code", |
|
377
|
value: "usd", |
|
378
|
is_tax_inclusive: true, |
|
379
|
}) |
|
380
|
|
|
381
|
await remoteLink.create([ |
|
382
|
{ |
|
383
|
[Modules.PRODUCT]: { |
|
384
|
variant_id: product.variants[0].id, |
|
385
|
}, |
|
386
|
[Modules.PRICING]: { |
|
387
|
price_set_id: priceSet.id, |
|
388
|
}, |
|
389
|
}, |
|
390
|
{ |
|
391
|
[Modules.SALES_CHANNEL]: { |
|
392
|
sales_channel_id: salesChannel.id, |
|
393
|
}, |
|
394
|
[Modules.STOCK_LOCATION]: { |
|
395
|
stock_location_id: location.id, |
|
396
|
}, |
|
397
|
}, |
|
398
|
{ |
|
399
|
[Modules.SALES_CHANNEL]: { |
|
400
|
sales_channel_id: salesChannel.id, |
|
401
|
}, |
|
402
|
[Modules.STOCK_LOCATION]: { |
|
403
|
stock_location_id: location2.id, |
|
404
|
}, |
|
405
|
}, |
|
406
|
{ |
|
407
|
[Modules.PRODUCT]: { |
|
408
|
variant_id: product.variants[0].id, |
|
409
|
}, |
|
410
|
[Modules.INVENTORY]: { |
|
411
|
inventory_item_id: inventoryItem.id, |
|
412
|
}, |
|
413
|
}, |
|
414
|
]) |
|
415
|
|
|
416
|
// complete 2 carts |
|
417
|
for (let i = 1; i <= 2; i++) { |
|
418
|
const cart = await cartModuleService.createCarts({ |
|
419
|
currency_code: "usd", |
|
420
|
sales_channel_id: salesChannel.id, |
|
421
|
}) |
|
422
|
|
|
423
|
await addToCartWorkflow(appContainer).run({ |
|
424
|
input: { |
|
425
|
items: [ |
|
426
|
{ |
|
427
|
variant_id: product.variants[0].id, |
|
428
|
quantity: 1, |
|
429
|
requires_shipping: false, |
|
430
|
}, |
|
431
|
], |
|
432
|
cart_id: cart.id, |
|
433
|
}, |
|
434
|
}) |
|
435
|
|
|
436
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
437
|
input: { |
|
438
|
cart_id: cart.id, |
|
439
|
}, |
|
440
|
}) |
|
441
|
|
|
442
|
const [payCol] = await remoteQuery( |
|
443
|
remoteQueryObjectFromString({ |
|
444
|
entryPoint: "cart_payment_collection", |
|
445
|
variables: { filters: { cart_id: cart.id } }, |
|
446
|
fields: ["payment_collection_id"], |
|
447
|
}) |
|
448
|
) |
|
449
|
|
|
450
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
451
|
input: { |
|
452
|
payment_collection_id: payCol.payment_collection_id, |
|
453
|
provider_id: "pp_system_default", |
|
454
|
context: {}, |
|
455
|
data: {}, |
|
456
|
}, |
|
457
|
}) |
|
458
|
|
|
459
|
await completeCartWorkflow(appContainer).run({ |
|
460
|
input: { |
|
461
|
id: cart.id, |
|
462
|
}, |
|
463
|
}) |
|
464
|
} |
|
465
|
|
|
466
|
const reservations = await api.get( |
|
467
|
`/admin/reservations`, |
|
468
|
adminHeaders |
|
469
|
) |
|
470
|
|
|
471
|
const locations = reservations.data.reservations.map( |
|
472
|
(r) => r.location_id |
|
473
|
) |
|
474
|
|
|
475
|
expect(locations).toEqual( |
|
476
|
expect.arrayContaining([location.id, location2.id]) |
|
477
|
) |
|
478
|
}) |
|
479
|
|
|
480
|
it("should complete cart when payment webhook is called first and payment has auto-capture on", async () => { |
|
481
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
482
|
name: "Webshop", |
|
483
|
}) |
|
484
|
|
|
485
|
const location = await stockLocationModule.createStockLocations({ |
|
486
|
name: "Warehouse", |
|
487
|
}) |
|
488
|
|
|
489
|
const [product] = await productModule.createProducts([ |
|
490
|
{ |
|
491
|
title: "Test product", |
|
492
|
status: ProductStatus.PUBLISHED, |
|
493
|
variants: [ |
|
494
|
{ |
|
495
|
title: "Test variant", |
|
496
|
manage_inventory: false, |
|
497
|
}, |
|
498
|
], |
|
499
|
}, |
|
500
|
]) |
|
501
|
|
|
502
|
const priceSet = await pricingModule.createPriceSets({ |
|
503
|
prices: [ |
|
504
|
{ |
|
505
|
amount: 3000, |
|
506
|
currency_code: "usd", |
|
507
|
}, |
|
508
|
], |
|
509
|
}) |
|
510
|
|
|
511
|
await pricingModule.createPricePreferences({ |
|
512
|
attribute: "currency_code", |
|
513
|
value: "usd", |
|
514
|
has_tax_inclusive: true, |
|
515
|
}) |
|
516
|
|
|
517
|
await remoteLink.create([ |
|
518
|
{ |
|
519
|
[Modules.PRODUCT]: { |
|
520
|
variant_id: product.variants[0].id, |
|
521
|
}, |
|
522
|
[Modules.PRICING]: { |
|
523
|
price_set_id: priceSet.id, |
|
524
|
}, |
|
525
|
}, |
|
526
|
{ |
|
527
|
[Modules.SALES_CHANNEL]: { |
|
528
|
sales_channel_id: salesChannel.id, |
|
529
|
}, |
|
530
|
[Modules.STOCK_LOCATION]: { |
|
531
|
stock_location_id: location.id, |
|
532
|
}, |
|
533
|
}, |
|
534
|
]) |
|
535
|
|
|
536
|
// create cart |
|
537
|
const cart = await cartModuleService.createCarts({ |
|
538
|
currency_code: "usd", |
|
539
|
sales_channel_id: salesChannel.id, |
|
540
|
}) |
|
541
|
|
|
542
|
await addToCartWorkflow(appContainer).run({ |
|
543
|
input: { |
|
544
|
items: [ |
|
545
|
{ |
|
546
|
variant_id: product.variants[0].id, |
|
547
|
quantity: 1, |
|
548
|
requires_shipping: false, |
|
549
|
}, |
|
550
|
], |
|
551
|
cart_id: cart.id, |
|
552
|
}, |
|
553
|
}) |
|
554
|
|
|
555
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
556
|
input: { |
|
557
|
cart_id: cart.id, |
|
558
|
}, |
|
559
|
}) |
|
560
|
|
|
561
|
const [payCol] = await remoteQuery( |
|
562
|
remoteQueryObjectFromString({ |
|
563
|
entryPoint: "cart_payment_collection", |
|
564
|
variables: { filters: { cart_id: cart.id } }, |
|
565
|
fields: ["payment_collection_id"], |
|
566
|
}) |
|
567
|
) |
|
568
|
|
|
569
|
const { result: paymentSession } = |
|
570
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
571
|
input: { |
|
572
|
payment_collection_id: payCol.payment_collection_id, |
|
573
|
provider_id: "pp_system_default", |
|
574
|
context: {}, |
|
575
|
data: {}, |
|
576
|
}, |
|
577
|
}) |
|
578
|
|
|
579
|
// payment webhook is triggered before complete cart workflow |
|
580
|
await processPaymentWorkflow(appContainer).run({ |
|
581
|
input: { |
|
582
|
action: "captured", |
|
583
|
data: { |
|
584
|
session_id: paymentSession.id, |
|
585
|
amount: 3000, |
|
586
|
}, |
|
587
|
}, |
|
588
|
}) |
|
589
|
|
|
590
|
// call complete cart workflow after |
|
591
|
const { result: order } = await completeCartWorkflow( |
|
592
|
appContainer |
|
593
|
).run({ |
|
594
|
input: { |
|
595
|
id: cart.id, |
|
596
|
}, |
|
597
|
}) |
|
598
|
|
|
599
|
const { result: fullOrder } = await getOrderDetailWorkflow( |
|
600
|
appContainer |
|
601
|
).run({ |
|
602
|
input: { |
|
603
|
fields: ["*"], |
|
604
|
order_id: order.id, |
|
605
|
}, |
|
606
|
}) |
|
607
|
|
|
608
|
expect(fullOrder.payment_status).toBe("captured") |
|
609
|
expect(fullOrder.payment_collections[0].authorized_amount).toBe(3000) |
|
610
|
expect(fullOrder.payment_collections[0].captured_amount).toBe(3000) |
|
611
|
expect(fullOrder.payment_collections[0].status).toBe("completed") |
|
612
|
}) |
|
613
|
|
|
614
|
it("should refund payment when payment webhook is called first and payment has auto-capture on but the completion fails", async () => { |
|
615
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
616
|
name: "Webshop", |
|
617
|
}) |
|
618
|
|
|
619
|
const location = await stockLocationModule.createStockLocations({ |
|
620
|
name: "Warehouse", |
|
621
|
}) |
|
622
|
|
|
623
|
const [product] = await productModule.createProducts([ |
|
624
|
{ |
|
625
|
title: "Test product", |
|
626
|
status: ProductStatus.PUBLISHED, |
|
627
|
variants: [ |
|
628
|
{ |
|
629
|
title: "Test variant", |
|
630
|
manage_inventory: false, |
|
631
|
}, |
|
632
|
], |
|
633
|
}, |
|
634
|
]) |
|
635
|
|
|
636
|
const priceSet = await pricingModule.createPriceSets({ |
|
637
|
prices: [ |
|
638
|
{ |
|
639
|
amount: 3000, |
|
640
|
currency_code: "usd", |
|
641
|
}, |
|
642
|
], |
|
643
|
}) |
|
644
|
|
|
645
|
await pricingModule.createPricePreferences({ |
|
646
|
attribute: "currency_code", |
|
647
|
value: "usd", |
|
648
|
is_tax_inclusive: true, |
|
649
|
}) |
|
650
|
|
|
651
|
await remoteLink.create([ |
|
652
|
{ |
|
653
|
[Modules.PRODUCT]: { |
|
654
|
variant_id: product.variants[0].id, |
|
655
|
}, |
|
656
|
[Modules.PRICING]: { |
|
657
|
price_set_id: priceSet.id, |
|
658
|
}, |
|
659
|
}, |
|
660
|
{ |
|
661
|
[Modules.SALES_CHANNEL]: { |
|
662
|
sales_channel_id: salesChannel.id, |
|
663
|
}, |
|
664
|
[Modules.STOCK_LOCATION]: { |
|
665
|
stock_location_id: location.id, |
|
666
|
}, |
|
667
|
}, |
|
668
|
]) |
|
669
|
|
|
670
|
// create cart |
|
671
|
const cart = await cartModuleService.createCarts({ |
|
672
|
currency_code: "usd", |
|
673
|
sales_channel_id: salesChannel.id, |
|
674
|
}) |
|
675
|
|
|
676
|
await addToCartWorkflow(appContainer).run({ |
|
677
|
input: { |
|
678
|
items: [ |
|
679
|
{ |
|
680
|
variant_id: product.variants[0].id, |
|
681
|
quantity: 1, |
|
682
|
requires_shipping: false, |
|
683
|
}, |
|
684
|
], |
|
685
|
cart_id: cart.id, |
|
686
|
}, |
|
687
|
}) |
|
688
|
|
|
689
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
690
|
input: { |
|
691
|
cart_id: cart.id, |
|
692
|
}, |
|
693
|
}) |
|
694
|
|
|
695
|
const [payCol] = await remoteQuery( |
|
696
|
remoteQueryObjectFromString({ |
|
697
|
entryPoint: "cart_payment_collection", |
|
698
|
variables: { filters: { cart_id: cart.id } }, |
|
699
|
fields: ["payment_collection_id"], |
|
700
|
}) |
|
701
|
) |
|
702
|
|
|
703
|
const { result: paymentSession } = |
|
704
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
705
|
input: { |
|
706
|
payment_collection_id: payCol.payment_collection_id, |
|
707
|
provider_id: "pp_system_default", |
|
708
|
context: {}, |
|
709
|
data: {}, |
|
710
|
}, |
|
711
|
}) |
|
712
|
|
|
713
|
let validateHook: Function | undefined = () => { |
|
714
|
throw new Error("cart complete failed") |
|
715
|
} |
|
716
|
completeCartWorkflow.hooks.validate(() => { |
|
717
|
if (validateHook) { |
|
718
|
validateHook() |
|
719
|
} |
|
720
|
}) |
|
721
|
|
|
722
|
// payment webhook is triggered before complete cart workflow |
|
723
|
await processPaymentWorkflow(appContainer).run({ |
|
724
|
input: { |
|
725
|
action: "captured", |
|
726
|
data: { |
|
727
|
session_id: paymentSession.id, |
|
728
|
amount: 3000, |
|
729
|
}, |
|
730
|
}, |
|
731
|
}) |
|
732
|
|
|
733
|
validateHook = undefined |
|
734
|
|
|
735
|
const paymentCollectionQuery = await query.graph({ |
|
736
|
entity: "payment_collection", |
|
737
|
filters: { |
|
738
|
id: paymentSession.payment_collection_id, |
|
739
|
}, |
|
740
|
fields: [ |
|
741
|
"*", |
|
742
|
"payment_sessions.*", |
|
743
|
"payments.*", |
|
744
|
"payments.captures.*", |
|
745
|
"payments.refunds.*", |
|
746
|
], |
|
747
|
}) |
|
748
|
|
|
749
|
// expects the payment to be refunded and a new payment session to be created |
|
750
|
expect(paymentCollectionQuery.data[0].payments[0]).toEqual( |
|
751
|
expect.objectContaining({ |
|
752
|
amount: 3000, |
|
753
|
payment_session_id: paymentSession.id, |
|
754
|
refunds: [ |
|
755
|
expect.objectContaining({ |
|
756
|
note: "Refunded due to cart completion failure", |
|
757
|
amount: 3000, |
|
758
|
}), |
|
759
|
], |
|
760
|
captures: [ |
|
761
|
expect.objectContaining({ |
|
762
|
amount: 3000, |
|
763
|
}), |
|
764
|
], |
|
765
|
}) |
|
766
|
) |
|
767
|
|
|
768
|
const sessions = paymentCollectionQuery.data[0].payment_sessions |
|
769
|
expect(sessions).toHaveLength(1) |
|
770
|
|
|
771
|
expect(sessions[0].id).toBeDefined() |
|
772
|
expect(sessions[0].id).not.toBe(paymentSession.id) |
|
773
|
expect(sessions[0].status).toBe("pending") |
|
774
|
}) |
|
775
|
|
|
776
|
it("should NOT refund payment when a stale compensation runs against a payment session whose cart already has a placed order", async () => { |
|
777
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
778
|
name: "Webshop", |
|
779
|
}) |
|
780
|
|
|
781
|
const location = await stockLocationModule.createStockLocations({ |
|
782
|
name: "Warehouse", |
|
783
|
}) |
|
784
|
|
|
785
|
const [product] = await productModule.createProducts([ |
|
786
|
{ |
|
787
|
title: "Test product", |
|
788
|
status: ProductStatus.PUBLISHED, |
|
789
|
variants: [ |
|
790
|
{ |
|
791
|
title: "Test variant", |
|
792
|
manage_inventory: false, |
|
793
|
}, |
|
794
|
], |
|
795
|
}, |
|
796
|
]) |
|
797
|
|
|
798
|
const priceSet = await pricingModule.createPriceSets({ |
|
799
|
prices: [ |
|
800
|
{ |
|
801
|
amount: 3000, |
|
802
|
currency_code: "usd", |
|
803
|
}, |
|
804
|
], |
|
805
|
}) |
|
806
|
|
|
807
|
await pricingModule.createPricePreferences({ |
|
808
|
attribute: "currency_code", |
|
809
|
value: "usd", |
|
810
|
is_tax_inclusive: true, |
|
811
|
}) |
|
812
|
|
|
813
|
await remoteLink.create([ |
|
814
|
{ |
|
815
|
[Modules.PRODUCT]: { |
|
816
|
variant_id: product.variants[0].id, |
|
817
|
}, |
|
818
|
[Modules.PRICING]: { |
|
819
|
price_set_id: priceSet.id, |
|
820
|
}, |
|
821
|
}, |
|
822
|
{ |
|
823
|
[Modules.SALES_CHANNEL]: { |
|
824
|
sales_channel_id: salesChannel.id, |
|
825
|
}, |
|
826
|
[Modules.STOCK_LOCATION]: { |
|
827
|
stock_location_id: location.id, |
|
828
|
}, |
|
829
|
}, |
|
830
|
]) |
|
831
|
|
|
832
|
const cart = await cartModuleService.createCarts({ |
|
833
|
currency_code: "usd", |
|
834
|
sales_channel_id: salesChannel.id, |
|
835
|
}) |
|
836
|
|
|
837
|
await addToCartWorkflow(appContainer).run({ |
|
838
|
input: { |
|
839
|
items: [ |
|
840
|
{ |
|
841
|
variant_id: product.variants[0].id, |
|
842
|
quantity: 1, |
|
843
|
requires_shipping: false, |
|
844
|
}, |
|
845
|
], |
|
846
|
cart_id: cart.id, |
|
847
|
}, |
|
848
|
}) |
|
849
|
|
|
850
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
851
|
input: { cart_id: cart.id }, |
|
852
|
}) |
|
853
|
|
|
854
|
const [payCol] = await remoteQuery( |
|
855
|
remoteQueryObjectFromString({ |
|
856
|
entryPoint: "cart_payment_collection", |
|
857
|
variables: { filters: { cart_id: cart.id } }, |
|
858
|
fields: ["payment_collection_id"], |
|
859
|
}) |
|
860
|
) |
|
861
|
|
|
862
|
const { result: paymentSession } = |
|
863
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
864
|
input: { |
|
865
|
payment_collection_id: payCol.payment_collection_id, |
|
866
|
provider_id: "pp_system_default", |
|
867
|
context: {}, |
|
868
|
data: {}, |
|
869
|
}, |
|
870
|
}) |
|
871
|
|
|
872
|
// Webhook-captures payment. processPaymentWorkflow internally |
|
873
|
// triggers completeCartWorkflow on a successful capture, so this |
|
874
|
// single call both captures AND places the order. This is the |
|
875
|
// "later attempt succeeded" leg of the race. |
|
876
|
await processPaymentWorkflow(appContainer).run({ |
|
877
|
input: { |
|
878
|
action: "captured", |
|
879
|
data: { |
|
880
|
session_id: paymentSession.id, |
|
881
|
amount: 3000, |
|
882
|
}, |
|
883
|
}, |
|
884
|
}) |
|
885
|
|
|
886
|
const { data: orderCartLinks } = await query.graph({ |
|
887
|
entity: "order_cart", |
|
888
|
fields: ["order_id"], |
|
889
|
filters: { cart_id: cart.id }, |
|
890
|
}) |
|
891
|
expect(orderCartLinks[0]?.order_id).toBeDefined() |
|
892
|
|
|
893
|
// Now simulate a late-arriving compensation from a prior FAILED |
|
894
|
// completeCartWorkflow that captured the same payment_session_id |
|
895
|
// before failing. We build a minimal workflow that registers the |
|
896
|
// step and then forces a failure so its compensation closure runs. |
|
897
|
const forceFailStep = createStep("force-fail-step", async () => { |
|
898
|
throw new Error("force compensation") |
|
899
|
}) |
|
900
|
|
|
901
|
const staleCompensationWorkflow = createWorkflow( |
|
902
|
"test-stale-compensate-payment-if-needed", |
|
903
|
(input: WorkflowData<{ payment_session_id: string }>) => { |
|
904
|
compensatePaymentIfNeededStep({ |
|
905
|
payment_session_id: input.payment_session_id, |
|
906
|
}) |
|
907
|
forceFailStep() |
|
908
|
} |
|
909
|
) |
|
910
|
|
|
911
|
const staleRun = await staleCompensationWorkflow(appContainer).run({ |
|
912
|
input: { payment_session_id: paymentSession.id }, |
|
913
|
throwOnError: false, |
|
914
|
}) |
|
915
|
|
|
916
|
// The workflow must have failed (so its compensation chain ran) |
|
917
|
expect(staleRun.errors?.length ?? 0).toBeGreaterThan(0) |
|
918
|
|
|
919
|
const paymentCollectionQuery = await query.graph({ |
|
920
|
entity: "payment_collection", |
|
921
|
filters: { id: paymentSession.payment_collection_id }, |
|
922
|
fields: [ |
|
923
|
"*", |
|
924
|
"payment_sessions.*", |
|
925
|
"payments.*", |
|
926
|
"payments.captures.*", |
|
927
|
"payments.refunds.*", |
|
928
|
], |
|
929
|
}) |
|
930
|
|
|
931
|
const paymentCollectionRow = paymentCollectionQuery.data[0] |
|
932
|
const payment = paymentCollectionRow.payments[0] |
|
933
|
|
|
934
|
// The payment must remain captured against the placed order; the |
|
935
|
// stale compensation must not have refunded it. |
|
936
|
expect(payment).toEqual( |
|
937
|
expect.objectContaining({ |
|
938
|
amount: 3000, |
|
939
|
captures: [expect.objectContaining({ amount: 3000 })], |
|
940
|
refunds: [], |
|
941
|
}) |
|
942
|
) |
|
943
|
|
|
944
|
// No new (pending) payment session should have been created either. |
|
945
|
const sessions = paymentCollectionRow.payment_sessions |
|
946
|
expect(sessions).toHaveLength(1) |
|
947
|
expect(sessions[0].id).toBe(paymentSession.id) |
|
948
|
}) |
|
949
|
|
|
950
|
it("should complete cart when payment webhook and storefront are called in simultaneously", async () => { |
|
951
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
952
|
name: "Webshop", |
|
953
|
}) |
|
954
|
|
|
955
|
const location = await stockLocationModule.createStockLocations({ |
|
956
|
name: "Warehouse", |
|
957
|
}) |
|
958
|
|
|
959
|
const [product] = await productModule.createProducts([ |
|
960
|
{ |
|
961
|
title: "Test product", |
|
962
|
status: ProductStatus.PUBLISHED, |
|
963
|
variants: [ |
|
964
|
{ |
|
965
|
title: "Test variant", |
|
966
|
manage_inventory: false, |
|
967
|
}, |
|
968
|
], |
|
969
|
}, |
|
970
|
]) |
|
971
|
|
|
972
|
const priceSet = await pricingModule.createPriceSets({ |
|
973
|
prices: [ |
|
974
|
{ |
|
975
|
amount: 3000, |
|
976
|
currency_code: "usd", |
|
977
|
}, |
|
978
|
], |
|
979
|
}) |
|
980
|
|
|
981
|
await pricingModule.createPricePreferences({ |
|
982
|
attribute: "currency_code", |
|
983
|
value: "usd", |
|
984
|
has_tax_inclusive: true, |
|
985
|
}) |
|
986
|
|
|
987
|
await remoteLink.create([ |
|
988
|
{ |
|
989
|
[Modules.PRODUCT]: { |
|
990
|
variant_id: product.variants[0].id, |
|
991
|
}, |
|
992
|
[Modules.PRICING]: { |
|
993
|
price_set_id: priceSet.id, |
|
994
|
}, |
|
995
|
}, |
|
996
|
{ |
|
997
|
[Modules.SALES_CHANNEL]: { |
|
998
|
sales_channel_id: salesChannel.id, |
|
999
|
}, |
|
1000
|
[Modules.STOCK_LOCATION]: { |
|
1001
|
stock_location_id: location.id, |
|
1002
|
}, |
|
1003
|
}, |
|
1004
|
]) |
|
1005
|
|
|
1006
|
// create cart |
|
1007
|
const cart = await cartModuleService.createCarts({ |
|
1008
|
currency_code: "usd", |
|
1009
|
sales_channel_id: salesChannel.id, |
|
1010
|
}) |
|
1011
|
|
|
1012
|
await addToCartWorkflow(appContainer).run({ |
|
1013
|
input: { |
|
1014
|
items: [ |
|
1015
|
{ |
|
1016
|
variant_id: product.variants[0].id, |
|
1017
|
quantity: 1, |
|
1018
|
requires_shipping: false, |
|
1019
|
}, |
|
1020
|
], |
|
1021
|
cart_id: cart.id, |
|
1022
|
}, |
|
1023
|
}) |
|
1024
|
|
|
1025
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
1026
|
input: { |
|
1027
|
cart_id: cart.id, |
|
1028
|
}, |
|
1029
|
}) |
|
1030
|
|
|
1031
|
const [payCol] = await remoteQuery( |
|
1032
|
remoteQueryObjectFromString({ |
|
1033
|
entryPoint: "cart_payment_collection", |
|
1034
|
variables: { filters: { cart_id: cart.id } }, |
|
1035
|
fields: ["payment_collection_id"], |
|
1036
|
}) |
|
1037
|
) |
|
1038
|
|
|
1039
|
const { result: paymentSession } = |
|
1040
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
1041
|
input: { |
|
1042
|
payment_collection_id: payCol.payment_collection_id, |
|
1043
|
provider_id: "pp_system_default", |
|
1044
|
context: {}, |
|
1045
|
data: {}, |
|
1046
|
}, |
|
1047
|
}) |
|
1048
|
|
|
1049
|
const [{ result: order }] = await Promise.all([ |
|
1050
|
completeCartWorkflow(appContainer).run({ |
|
1051
|
input: { |
|
1052
|
id: cart.id, |
|
1053
|
}, |
|
1054
|
}), |
|
1055
|
processPaymentWorkflow(appContainer).run({ |
|
1056
|
input: { |
|
1057
|
action: "captured", |
|
1058
|
data: { |
|
1059
|
session_id: paymentSession.id, |
|
1060
|
amount: 3000, |
|
1061
|
}, |
|
1062
|
}, |
|
1063
|
}), |
|
1064
|
]) |
|
1065
|
|
|
1066
|
await new Promise((resolve) => setTimeout(resolve, 100)) |
|
1067
|
|
|
1068
|
const { result: fullOrder } = await getOrderDetailWorkflow( |
|
1069
|
appContainer |
|
1070
|
).run({ |
|
1071
|
input: { |
|
1072
|
fields: ["*"], |
|
1073
|
order_id: order.id, |
|
1074
|
}, |
|
1075
|
}) |
|
1076
|
|
|
1077
|
expect(fullOrder.payment_status).toBe("captured") |
|
1078
|
expect(fullOrder.payment_collections[0].authorized_amount).toBe(3000) |
|
1079
|
expect(fullOrder.payment_collections[0].captured_amount).toBe(3000) |
|
1080
|
expect(fullOrder.payment_collections[0].status).toBe("completed") |
|
1081
|
}) |
|
1082
|
|
|
1083
|
it("should clear events when complete cart fails after emitting events", async () => { |
|
1084
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
1085
|
name: "Webshop", |
|
1086
|
}) |
|
1087
|
|
|
1088
|
const location = await stockLocationModule.createStockLocations({ |
|
1089
|
name: "Warehouse", |
|
1090
|
}) |
|
1091
|
|
|
1092
|
const region = await regionModuleService.createRegions({ |
|
1093
|
name: "US", |
|
1094
|
currency_code: "usd", |
|
1095
|
}) |
|
1096
|
|
|
1097
|
await linkRegionPaymentProviders(appContainer, region.id) |
|
1098
|
|
|
1099
|
let cart = await cartModuleService.createCarts({ |
|
1100
|
currency_code: "usd", |
|
1101
|
sales_channel_id: salesChannel.id, |
|
1102
|
region_id: region.id, |
|
1103
|
}) |
|
1104
|
|
|
1105
|
await remoteLink.create([ |
|
1106
|
{ |
|
1107
|
[Modules.SALES_CHANNEL]: { |
|
1108
|
sales_channel_id: salesChannel.id, |
|
1109
|
}, |
|
1110
|
[Modules.STOCK_LOCATION]: { |
|
1111
|
stock_location_id: location.id, |
|
1112
|
}, |
|
1113
|
}, |
|
1114
|
]) |
|
1115
|
|
|
1116
|
cart = await cartModuleService.retrieveCart(cart.id, { |
|
1117
|
select: ["id", "region_id", "currency_code", "sales_channel_id"], |
|
1118
|
}) |
|
1119
|
|
|
1120
|
await addToCartWorkflow(appContainer).run({ |
|
1121
|
input: { |
|
1122
|
items: [ |
|
1123
|
{ |
|
1124
|
title: "Test item", |
|
1125
|
subtitle: "Test subtitle", |
|
1126
|
thumbnail: "some-url", |
|
1127
|
requires_shipping: false, |
|
1128
|
is_discountable: false, |
|
1129
|
is_tax_inclusive: false, |
|
1130
|
unit_price: 3000, |
|
1131
|
metadata: { |
|
1132
|
foo: "bar", |
|
1133
|
}, |
|
1134
|
quantity: 1, |
|
1135
|
}, |
|
1136
|
{ |
|
1137
|
title: "zero price item", |
|
1138
|
subtitle: "zero price item", |
|
1139
|
thumbnail: "some-url", |
|
1140
|
requires_shipping: false, |
|
1141
|
is_discountable: false, |
|
1142
|
is_tax_inclusive: false, |
|
1143
|
unit_price: 0, |
|
1144
|
quantity: 1, |
|
1145
|
}, |
|
1146
|
], |
|
1147
|
cart_id: cart.id, |
|
1148
|
}, |
|
1149
|
}) |
|
1150
|
|
|
1151
|
cart = await cartModuleService.retrieveCart(cart.id, { |
|
1152
|
relations: ["items"], |
|
1153
|
}) |
|
1154
|
|
|
1155
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
1156
|
input: { |
|
1157
|
cart_id: cart.id, |
|
1158
|
}, |
|
1159
|
}) |
|
1160
|
|
|
1161
|
const [paymentCollection] = |
|
1162
|
await paymentModule.listPaymentCollections({}) |
|
1163
|
|
|
1164
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
1165
|
input: { |
|
1166
|
payment_collection_id: paymentCollection.id, |
|
1167
|
provider_id: "pp_system_default", |
|
1168
|
context: {}, |
|
1169
|
data: {}, |
|
1170
|
}, |
|
1171
|
}) |
|
1172
|
|
|
1173
|
let grouppedEventBefore: Message[] = [] |
|
1174
|
let eventGroupId!: string |
|
1175
|
|
|
1176
|
/** |
|
1177
|
* Register order.placed listener to trigger the event group |
|
1178
|
* registration and be able to check the event group during |
|
1179
|
* the workflow execution against it after compensation |
|
1180
|
*/ |
|
1181
|
|
|
1182
|
eventBus.subscribe("order.placed", async () => { |
|
1183
|
// noop |
|
1184
|
}) |
|
1185
|
|
|
1186
|
const workflow = completeCartWorkflow(appContainer) |
|
1187
|
|
|
1188
|
workflow.addAction("throw", { |
|
1189
|
invoke: async function failStep({ context }) { |
|
1190
|
eventGroupId = context!.eventGroupId! |
|
1191
|
grouppedEventBefore = ( |
|
1192
|
(eventBus as any).groupedEventsMap_ as Map<string, any> |
|
1193
|
).get(context!.eventGroupId!) |
|
1194
|
|
|
1195
|
throw new Error( |
|
1196
|
`Failed to do something before ending complete cart workflow` |
|
1197
|
) |
|
1198
|
}, |
|
1199
|
}) |
|
1200
|
|
|
1201
|
const { errors } = await workflow.run({ |
|
1202
|
input: { |
|
1203
|
id: cart.id, |
|
1204
|
}, |
|
1205
|
throwOnError: false, |
|
1206
|
}) |
|
1207
|
|
|
1208
|
const grouppedEventAfter = |
|
1209
|
((eventBus as any).groupedEventsMap_ as Map<string, any>).get( |
|
1210
|
eventGroupId |
|
1211
|
) ?? [] |
|
1212
|
|
|
1213
|
expect(grouppedEventBefore).toHaveLength(17) |
|
1214
|
expect(grouppedEventAfter).toHaveLength(0) // events have been compensated |
|
1215
|
|
|
1216
|
expect(errors[0].error.message).toBe( |
|
1217
|
"Failed to do something before ending complete cart workflow" |
|
1218
|
) |
|
1219
|
}) |
|
1220
|
|
|
1221
|
it("should avoid completing cart when order already exists", async () => { |
|
1222
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
1223
|
name: "Webshop", |
|
1224
|
}) |
|
1225
|
|
|
1226
|
const location = await stockLocationModule.createStockLocations({ |
|
1227
|
name: "Warehouse", |
|
1228
|
}) |
|
1229
|
|
|
1230
|
const [product] = await productModule.createProducts([ |
|
1231
|
{ |
|
1232
|
title: "Test product", |
|
1233
|
status: ProductStatus.PUBLISHED, |
|
1234
|
variants: [ |
|
1235
|
{ |
|
1236
|
title: "Test variant", |
|
1237
|
manage_inventory: false, |
|
1238
|
}, |
|
1239
|
], |
|
1240
|
}, |
|
1241
|
]) |
|
1242
|
|
|
1243
|
const priceSet = await pricingModule.createPriceSets({ |
|
1244
|
prices: [ |
|
1245
|
{ |
|
1246
|
amount: 3000, |
|
1247
|
currency_code: "usd", |
|
1248
|
}, |
|
1249
|
], |
|
1250
|
}) |
|
1251
|
|
|
1252
|
await pricingModule.createPricePreferences({ |
|
1253
|
attribute: "currency_code", |
|
1254
|
value: "usd", |
|
1255
|
has_tax_inclusive: true, |
|
1256
|
}) |
|
1257
|
|
|
1258
|
await remoteLink.create([ |
|
1259
|
{ |
|
1260
|
[Modules.PRODUCT]: { |
|
1261
|
variant_id: product.variants[0].id, |
|
1262
|
}, |
|
1263
|
[Modules.PRICING]: { |
|
1264
|
price_set_id: priceSet.id, |
|
1265
|
}, |
|
1266
|
}, |
|
1267
|
{ |
|
1268
|
[Modules.SALES_CHANNEL]: { |
|
1269
|
sales_channel_id: salesChannel.id, |
|
1270
|
}, |
|
1271
|
[Modules.STOCK_LOCATION]: { |
|
1272
|
stock_location_id: location.id, |
|
1273
|
}, |
|
1274
|
}, |
|
1275
|
]) |
|
1276
|
|
|
1277
|
// create cart |
|
1278
|
const cart = await cartModuleService.createCarts({ |
|
1279
|
currency_code: "usd", |
|
1280
|
sales_channel_id: salesChannel.id, |
|
1281
|
}) |
|
1282
|
|
|
1283
|
await addToCartWorkflow(appContainer).run({ |
|
1284
|
input: { |
|
1285
|
items: [ |
|
1286
|
{ |
|
1287
|
variant_id: product.variants[0].id, |
|
1288
|
quantity: 1, |
|
1289
|
requires_shipping: false, |
|
1290
|
}, |
|
1291
|
], |
|
1292
|
cart_id: cart.id, |
|
1293
|
}, |
|
1294
|
}) |
|
1295
|
|
|
1296
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
1297
|
input: { |
|
1298
|
cart_id: cart.id, |
|
1299
|
}, |
|
1300
|
}) |
|
1301
|
|
|
1302
|
const [payCol] = await remoteQuery( |
|
1303
|
remoteQueryObjectFromString({ |
|
1304
|
entryPoint: "cart_payment_collection", |
|
1305
|
variables: { filters: { cart_id: cart.id } }, |
|
1306
|
fields: ["payment_collection_id"], |
|
1307
|
}) |
|
1308
|
) |
|
1309
|
|
|
1310
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
1311
|
input: { |
|
1312
|
payment_collection_id: payCol.payment_collection_id, |
|
1313
|
provider_id: "pp_system_default", |
|
1314
|
context: {}, |
|
1315
|
data: {}, |
|
1316
|
}, |
|
1317
|
}) |
|
1318
|
|
|
1319
|
const { |
|
1320
|
result: { id: orderId }, |
|
1321
|
} = await completeCartWorkflow(appContainer).run({ |
|
1322
|
input: { |
|
1323
|
id: cart.id, |
|
1324
|
}, |
|
1325
|
}) |
|
1326
|
|
|
1327
|
await beginOrderEditOrderWorkflow(appContainer).run({ |
|
1328
|
input: { |
|
1329
|
order_id: orderId, |
|
1330
|
}, |
|
1331
|
}) |
|
1332
|
await orderEditAddNewItemWorkflow(appContainer).run({ |
|
1333
|
input: { |
|
1334
|
order_id: orderId, |
|
1335
|
items: [ |
|
1336
|
{ |
|
1337
|
variant_id: product.variants[0].id, |
|
1338
|
quantity: 1, |
|
1339
|
}, |
|
1340
|
], |
|
1341
|
}, |
|
1342
|
}) |
|
1343
|
await confirmOrderEditRequestWorkflow(appContainer).run({ |
|
1344
|
input: { order_id: orderId }, |
|
1345
|
}) |
|
1346
|
|
|
1347
|
const orderPaymentCollections = await remoteQuery( |
|
1348
|
remoteQueryObjectFromString({ |
|
1349
|
entryPoint: "order_payment_collection", |
|
1350
|
variables: { filters: { order_id: orderId } }, |
|
1351
|
fields: ["payment_collection_id"], |
|
1352
|
}) |
|
1353
|
) |
|
1354
|
|
|
1355
|
const [pendingPaymentCollection] = await remoteQuery( |
|
1356
|
remoteQueryObjectFromString({ |
|
1357
|
entryPoint: "payment_collection", |
|
1358
|
variables: { |
|
1359
|
filters: { |
|
1360
|
id: orderPaymentCollections.map( |
|
1361
|
(orderPayCol) => orderPayCol.payment_collection_id |
|
1362
|
), |
|
1363
|
status: PaymentCollectionStatus.NOT_PAID, |
|
1364
|
}, |
|
1365
|
}, |
|
1366
|
fields: ["id"], |
|
1367
|
}) |
|
1368
|
) |
|
1369
|
|
|
1370
|
const { result: paymentSession } = |
|
1371
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
1372
|
input: { |
|
1373
|
payment_collection_id: pendingPaymentCollection.id, |
|
1374
|
provider_id: "pp_system_default", |
|
1375
|
context: {}, |
|
1376
|
data: {}, |
|
1377
|
}, |
|
1378
|
}) |
|
1379
|
|
|
1380
|
let completeCartCalled = false |
|
1381
|
const workflow = processPaymentWorkflow(appContainer) |
|
1382
|
|
|
1383
|
workflow.addAction("track-complete-cart-step", { |
|
1384
|
invoke: async function trackStep({ invoke }) { |
|
1385
|
completeCartCalled = !!invoke["complete-cart-after-payment-step"] |
|
1386
|
}, |
|
1387
|
}) |
|
1388
|
|
|
1389
|
await workflow.run({ |
|
1390
|
input: { |
|
1391
|
action: "captured", |
|
1392
|
data: { |
|
1393
|
session_id: paymentSession.id, |
|
1394
|
amount: 3000, |
|
1395
|
}, |
|
1396
|
}, |
|
1397
|
}) |
|
1398
|
|
|
1399
|
expect(completeCartCalled).toBe(false) |
|
1400
|
}) |
|
1401
|
|
|
1402
|
it("should NOT complete a cart when the payment webhook carries no session_id", async () => { |
|
1403
|
const salesChannel = await scModuleService.createSalesChannels({ |
|
1404
|
name: "Webshop", |
|
1405
|
}) |
|
1406
|
|
|
1407
|
const location = await stockLocationModule.createStockLocations({ |
|
1408
|
name: "Warehouse", |
|
1409
|
}) |
|
1410
|
|
|
1411
|
const [product] = await productModule.createProducts([ |
|
1412
|
{ |
|
1413
|
title: "Test product", |
|
1414
|
status: ProductStatus.PUBLISHED, |
|
1415
|
variants: [ |
|
1416
|
{ |
|
1417
|
title: "Test variant", |
|
1418
|
manage_inventory: false, |
|
1419
|
}, |
|
1420
|
], |
|
1421
|
}, |
|
1422
|
]) |
|
1423
|
|
|
1424
|
const priceSet = await pricingModule.createPriceSets({ |
|
1425
|
prices: [ |
|
1426
|
{ |
|
1427
|
amount: 3000, |
|
1428
|
currency_code: "usd", |
|
1429
|
}, |
|
1430
|
], |
|
1431
|
}) |
|
1432
|
|
|
1433
|
await pricingModule.createPricePreferences({ |
|
1434
|
attribute: "currency_code", |
|
1435
|
value: "usd", |
|
1436
|
has_tax_inclusive: true, |
|
1437
|
}) |
|
1438
|
|
|
1439
|
await remoteLink.create([ |
|
1440
|
{ |
|
1441
|
[Modules.PRODUCT]: { |
|
1442
|
variant_id: product.variants[0].id, |
|
1443
|
}, |
|
1444
|
[Modules.PRICING]: { |
|
1445
|
price_set_id: priceSet.id, |
|
1446
|
}, |
|
1447
|
}, |
|
1448
|
{ |
|
1449
|
[Modules.SALES_CHANNEL]: { |
|
1450
|
sales_channel_id: salesChannel.id, |
|
1451
|
}, |
|
1452
|
[Modules.STOCK_LOCATION]: { |
|
1453
|
stock_location_id: location.id, |
|
1454
|
}, |
|
1455
|
}, |
|
1456
|
]) |
|
1457
|
|
|
1458
|
// A completable cart with a payment session, but no order yet. This |
|
1459
|
// mimics an unrelated, open cart that a foreign/sessionless webhook |
|
1460
|
// must never touch. |
|
1461
|
const cart = await cartModuleService.createCarts({ |
|
1462
|
currency_code: "usd", |
|
1463
|
sales_channel_id: salesChannel.id, |
|
1464
|
}) |
|
1465
|
|
|
1466
|
await addToCartWorkflow(appContainer).run({ |
|
1467
|
input: { |
|
1468
|
items: [ |
|
1469
|
{ |
|
1470
|
variant_id: product.variants[0].id, |
|
1471
|
quantity: 1, |
|
1472
|
requires_shipping: false, |
|
1473
|
}, |
|
1474
|
], |
|
1475
|
cart_id: cart.id, |
|
1476
|
}, |
|
1477
|
}) |
|
1478
|
|
|
1479
|
await createPaymentCollectionForCartWorkflow(appContainer).run({ |
|
1480
|
input: { |
|
1481
|
cart_id: cart.id, |
|
1482
|
}, |
|
1483
|
}) |
|
1484
|
|
|
1485
|
const [payCol] = await remoteQuery( |
|
1486
|
remoteQueryObjectFromString({ |
|
1487
|
entryPoint: "cart_payment_collection", |
|
1488
|
variables: { filters: { cart_id: cart.id } }, |
|
1489
|
fields: ["payment_collection_id"], |
|
1490
|
}) |
|
1491
|
) |
|
1492
|
|
|
1493
|
const { result: paymentSession } = |
|
1494
|
await createPaymentSessionsWorkflow(appContainer).run({ |
|
1495
|
input: { |
|
1496
|
payment_collection_id: payCol.payment_collection_id, |
|
1497
|
provider_id: "pp_system_default", |
|
1498
|
context: {}, |
|
1499
|
data: {}, |
|
1500
|
}, |
|
1501
|
}) |
|
1502
|
|
|
1503
|
// Authorize the session so a Payment exists for this cart. This makes |
|
1504
|
// the sessionless webhook resolve down the capture -> completion path |
|
1505
|
// (rather than the autocapture path), faithfully reproducing the |
|
1506
|
// cross-tenant incident where an unrelated cart was completed. |
|
1507
|
await paymentModule.authorizePaymentSession(paymentSession.id, {}) |
|
1508
|
|
|
1509
|
let completeCartCalled = false |
|
1510
|
const workflow = processPaymentWorkflow(appContainer) |
|
1511
|
|
|
1512
|
workflow.addAction("track-complete-cart-step", { |
|
1513
|
invoke: async function trackStep({ invoke }) { |
|
1514
|
completeCartCalled = !!invoke["complete-cart-after-payment-step"] |
|
1515
|
}, |
|
1516
|
}) |
|
1517
|
|
|
1518
|
// A webhook whose payment intent has no Medusa session_id (e.g. an |
|
1519
|
// event from a co-tenant store sharing the same provider account). |
|
1520
|
await workflow.run({ |
|
1521
|
input: { |
|
1522
|
action: "captured", |
|
1523
|
data: { |
|
1524
|
session_id: undefined, |
|
1525
|
amount: 3000, |
|
1526
|
}, |
|
1527
|
}, |
|
1528
|
}) |
|
1529
|
|
|
1530
|
expect(completeCartCalled).toBe(false) |
|
1531
|
|
|
1532
|
const orders = await query.graph({ |
|
1533
|
entity: "order_cart", |
|
1534
|
fields: ["id"], |
|
1535
|
filters: { |
|
1536
|
cart_id: cart.id, |
|
1537
|
}, |
|
1538
|
}) |
|
1539
|
|
|
1540
|
expect(orders.data).toHaveLength(0) |
|
1541
|
}) |
|
1542
|
}) |
|
1543
|
}) |
|
1544
|
}, |
|
1545
|
}) |