|
1
|
import { processPaymentWorkflow } from "@medusajs/core-flows" |
|
2
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
3
|
import { Modules, PaymentActions, ProductStatus } from "@medusajs/utils" |
|
4
|
import { setTimeout } from "timers/promises" |
|
5
|
import { |
|
6
|
createAdminUser, |
|
7
|
generatePublishableKey, |
|
8
|
generateStoreHeaders, |
|
9
|
} from "../../../../helpers/create-admin-user" |
|
10
|
import { createAuthenticatedCustomer } from "../../../../modules/helpers/create-authenticated-customer" |
|
11
|
|
|
12
|
jest.setTimeout(100000) |
|
13
|
|
|
14
|
const env = {} |
|
15
|
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } } |
|
16
|
|
|
17
|
const pendingAuthProviderId = "pp_pending-auth_pending-auth" |
|
18
|
|
|
19
|
const shippingAddressData = { |
|
20
|
address_1: "test address 1", |
|
21
|
address_2: "test address 2", |
|
22
|
city: "SF", |
|
23
|
country_code: "US", |
|
24
|
province: "CA", |
|
25
|
postal_code: "94016", |
|
26
|
} |
|
27
|
|
|
28
|
medusaIntegrationTestRunner({ |
|
29
|
env, |
|
30
|
testSuite: ({ dbConnection, getContainer, api }) => { |
|
31
|
let appContainer |
|
32
|
let storeHeaders |
|
33
|
let storeHeadersWithCustomer |
|
34
|
let region |
|
35
|
let salesChannel |
|
36
|
let product |
|
37
|
let shippingProfile |
|
38
|
let cart |
|
39
|
|
|
40
|
beforeAll(async () => { |
|
41
|
appContainer = getContainer() |
|
42
|
}) |
|
43
|
|
|
44
|
beforeEach(async () => { |
|
45
|
await createAdminUser(dbConnection, adminHeaders, appContainer) |
|
46
|
|
|
47
|
const publishableKey = await generatePublishableKey(appContainer) |
|
48
|
storeHeaders = generateStoreHeaders({ publishableKey }) |
|
49
|
|
|
50
|
const { customer, jwt } = await createAuthenticatedCustomer( |
|
51
|
api, |
|
52
|
storeHeaders |
|
53
|
) |
|
54
|
storeHeadersWithCustomer = { |
|
55
|
headers: { |
|
56
|
...storeHeaders.headers, |
|
57
|
authorization: `Bearer ${jwt}`, |
|
58
|
}, |
|
59
|
} |
|
60
|
|
|
61
|
region = ( |
|
62
|
await api.post( |
|
63
|
"/admin/regions", |
|
64
|
{ |
|
65
|
payment_providers: [ |
|
66
|
"pp_system_default", |
|
67
|
"pp_pending-auth_pending-auth", |
|
68
|
], |
|
69
|
name: "Test Region", |
|
70
|
currency_code: "usd", |
|
71
|
countries: ["US"], |
|
72
|
automatic_taxes: false, |
|
73
|
}, |
|
74
|
adminHeaders |
|
75
|
) |
|
76
|
).data.region |
|
77
|
|
|
78
|
salesChannel = ( |
|
79
|
await api.post( |
|
80
|
"/admin/sales-channels", |
|
81
|
{ name: "Test SC", is_disabled: false }, |
|
82
|
adminHeaders |
|
83
|
) |
|
84
|
).data.sales_channel |
|
85
|
|
|
86
|
shippingProfile = ( |
|
87
|
await api.post( |
|
88
|
"/admin/shipping-profiles", |
|
89
|
{ name: "Test Profile", type: "default" }, |
|
90
|
adminHeaders |
|
91
|
) |
|
92
|
).data.shipping_profile |
|
93
|
|
|
94
|
product = ( |
|
95
|
await api.post( |
|
96
|
"/admin/products", |
|
97
|
{ |
|
98
|
title: "Test Product", |
|
99
|
status: ProductStatus.PUBLISHED, |
|
100
|
options: [{ title: "Size", values: ["S"] }], |
|
101
|
variants: [ |
|
102
|
{ |
|
103
|
title: "Test Variant", |
|
104
|
sku: "TEST-VARIANT", |
|
105
|
prices: [{ currency_code: "usd", amount: 1500 }], |
|
106
|
options: { Size: "S" }, |
|
107
|
manage_inventory: false, |
|
108
|
}, |
|
109
|
], |
|
110
|
sales_channels: [{ id: salesChannel.id }], |
|
111
|
shipping_profile_id: shippingProfile.id, |
|
112
|
}, |
|
113
|
adminHeaders |
|
114
|
) |
|
115
|
).data.product |
|
116
|
|
|
117
|
// Setup fulfillment infrastructure |
|
118
|
const stockLocation = ( |
|
119
|
await api.post( |
|
120
|
"/admin/stock-locations", |
|
121
|
{ name: "test location" }, |
|
122
|
adminHeaders |
|
123
|
) |
|
124
|
).data.stock_location |
|
125
|
|
|
126
|
await api.post( |
|
127
|
`/admin/stock-locations/${stockLocation.id}/sales-channels`, |
|
128
|
{ add: [salesChannel.id] }, |
|
129
|
adminHeaders |
|
130
|
) |
|
131
|
|
|
132
|
const fulfillmentSets = ( |
|
133
|
await api.post( |
|
134
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
|
135
|
{ name: "Test Set", type: "test-type" }, |
|
136
|
adminHeaders |
|
137
|
) |
|
138
|
).data.stock_location.fulfillment_sets |
|
139
|
|
|
140
|
const fulfillmentSet = ( |
|
141
|
await api.post( |
|
142
|
`/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
|
143
|
{ |
|
144
|
name: "Test Zone", |
|
145
|
geo_zones: [{ type: "country", country_code: "US" }], |
|
146
|
}, |
|
147
|
adminHeaders |
|
148
|
) |
|
149
|
).data.fulfillment_set |
|
150
|
|
|
151
|
await api.post( |
|
152
|
`/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
|
153
|
{ add: ["manual_test-provider"] }, |
|
154
|
adminHeaders |
|
155
|
) |
|
156
|
|
|
157
|
const shippingOption = ( |
|
158
|
await api.post( |
|
159
|
"/admin/shipping-options", |
|
160
|
{ |
|
161
|
name: "Test Shipping", |
|
162
|
service_zone_id: fulfillmentSet.service_zones[0].id, |
|
163
|
shipping_profile_id: shippingProfile.id, |
|
164
|
provider_id: "manual_test-provider", |
|
165
|
price_type: "flat", |
|
166
|
type: { |
|
167
|
label: "Test", |
|
168
|
description: "Test", |
|
169
|
code: "test-code", |
|
170
|
}, |
|
171
|
prices: [{ currency_code: "usd", amount: 1000 }], |
|
172
|
rules: [], |
|
173
|
}, |
|
174
|
adminHeaders |
|
175
|
) |
|
176
|
).data.shipping_option |
|
177
|
|
|
178
|
cart = ( |
|
179
|
await api.post( |
|
180
|
"/store/carts", |
|
181
|
{ |
|
182
|
currency_code: "usd", |
|
183
|
sales_channel_id: salesChannel.id, |
|
184
|
region_id: region.id, |
|
185
|
shipping_address: shippingAddressData, |
|
186
|
items: [{ variant_id: product.variants[0].id, quantity: 1 }], |
|
187
|
}, |
|
188
|
storeHeadersWithCustomer |
|
189
|
) |
|
190
|
).data.cart |
|
191
|
|
|
192
|
await api.post( |
|
193
|
`/store/carts/${cart.id}/shipping-methods`, |
|
194
|
{ option_id: shippingOption.id }, |
|
195
|
storeHeaders |
|
196
|
) |
|
197
|
}) |
|
198
|
|
|
199
|
describe("POST /store/carts/:id/complete with pending_authorization", () => { |
|
200
|
it("should create an order when payment returns pending_authorization", async () => { |
|
201
|
const paymentCollection = ( |
|
202
|
await api.post( |
|
203
|
"/store/payment-collections", |
|
204
|
{ cart_id: cart.id }, |
|
205
|
storeHeaders |
|
206
|
) |
|
207
|
).data.payment_collection |
|
208
|
|
|
209
|
await api.post( |
|
210
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
211
|
{ provider_id: pendingAuthProviderId }, |
|
212
|
storeHeaders |
|
213
|
) |
|
214
|
|
|
215
|
const completedCart = ( |
|
216
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
217
|
).data |
|
218
|
|
|
219
|
// Order should be created despite pending payment |
|
220
|
expect(completedCart.type).toBe("order") |
|
221
|
expect(completedCart.order).toEqual( |
|
222
|
expect.objectContaining({ |
|
223
|
id: expect.any(String), |
|
224
|
currency_code: "usd", |
|
225
|
status: "pending", |
|
226
|
}) |
|
227
|
) |
|
228
|
|
|
229
|
const orderId = completedCart.order.id |
|
230
|
|
|
231
|
// Verify order payment status is "awaiting" |
|
232
|
const order = ( |
|
233
|
await api.get( |
|
234
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections,*payment_collections.payment_sessions,*payment_collections.payments`, |
|
235
|
adminHeaders |
|
236
|
) |
|
237
|
).data.order |
|
238
|
|
|
239
|
expect(order.payment_status).toBe("awaiting") |
|
240
|
|
|
241
|
// Verify no payment record exists |
|
242
|
const payments = order.payment_collections?.[0]?.payments ?? [] |
|
243
|
expect(payments).toHaveLength(0) |
|
244
|
|
|
245
|
// Verify payment session is in pending_authorization |
|
246
|
const sessions = order.payment_collections?.[0]?.payment_sessions ?? [] |
|
247
|
expect(sessions).toHaveLength(1) |
|
248
|
expect(sessions[0].status).toBe("pending_authorization") |
|
249
|
}) |
|
250
|
|
|
251
|
it("should return the same order on idempotent retry", async () => { |
|
252
|
const paymentCollection = ( |
|
253
|
await api.post( |
|
254
|
"/store/payment-collections", |
|
255
|
{ cart_id: cart.id }, |
|
256
|
storeHeaders |
|
257
|
) |
|
258
|
).data.payment_collection |
|
259
|
|
|
260
|
await api.post( |
|
261
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
262
|
{ provider_id: pendingAuthProviderId }, |
|
263
|
storeHeaders |
|
264
|
) |
|
265
|
|
|
266
|
const firstComplete = ( |
|
267
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
268
|
).data |
|
269
|
|
|
270
|
expect(firstComplete.type).toBe("order") |
|
271
|
|
|
272
|
// Second completion should return same order |
|
273
|
const secondComplete = ( |
|
274
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
275
|
).data |
|
276
|
|
|
277
|
expect(secondComplete.type).toBe("order") |
|
278
|
expect(secondComplete.order.id).toBe(firstComplete.order.id) |
|
279
|
}) |
|
280
|
}) |
|
281
|
|
|
282
|
describe("POST /admin/orders/:id/payment-sessions/authorize", () => { |
|
283
|
it("should authorize a pending payment session when provider confirms payment", async () => { |
|
284
|
// Complete cart with pending_authorization |
|
285
|
const paymentCollection = ( |
|
286
|
await api.post( |
|
287
|
"/store/payment-collections", |
|
288
|
{ cart_id: cart.id }, |
|
289
|
storeHeaders |
|
290
|
) |
|
291
|
).data.payment_collection |
|
292
|
|
|
293
|
await api.post( |
|
294
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
295
|
{ provider_id: pendingAuthProviderId }, |
|
296
|
storeHeaders |
|
297
|
) |
|
298
|
|
|
299
|
const completedCart = ( |
|
300
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
301
|
).data |
|
302
|
|
|
303
|
expect(completedCart.type).toBe("order") |
|
304
|
const orderId = completedCart.order.id |
|
305
|
|
|
306
|
// Get the payment session ID |
|
307
|
const orderBefore = ( |
|
308
|
await api.get( |
|
309
|
`/admin/orders/${orderId}?fields=*payment_collections,*payment_collections.payment_sessions`, |
|
310
|
adminHeaders |
|
311
|
) |
|
312
|
).data.order |
|
313
|
|
|
314
|
const sessionId = |
|
315
|
orderBefore.payment_collections[0].payment_sessions[0].id |
|
316
|
|
|
317
|
// Simulate payment arriving by updating session data |
|
318
|
const paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
319
|
await paymentModule.updatePaymentSession({ |
|
320
|
id: sessionId, |
|
321
|
currency_code: "usd", |
|
322
|
amount: orderBefore.payment_collections[0].amount, |
|
323
|
data: { payment_received: true }, |
|
324
|
}) |
|
325
|
|
|
326
|
// Admin triggers authorization |
|
327
|
const authResult = ( |
|
328
|
await api.post( |
|
329
|
`/admin/orders/${orderId}/payment-sessions/authorize?fields=+payment_status`, |
|
330
|
{ payment_session_id: sessionId }, |
|
331
|
adminHeaders |
|
332
|
) |
|
333
|
).data |
|
334
|
|
|
335
|
expect(authResult.is_authorized).toBe(true) |
|
336
|
expect(authResult.order).toEqual( |
|
337
|
expect.objectContaining({ |
|
338
|
id: orderId, |
|
339
|
payment_status: "authorized", |
|
340
|
}) |
|
341
|
) |
|
342
|
|
|
343
|
// Verify order now has authorized payment status |
|
344
|
const orderAfter = ( |
|
345
|
await api.get( |
|
346
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections.payments`, |
|
347
|
adminHeaders |
|
348
|
) |
|
349
|
).data.order |
|
350
|
|
|
351
|
expect(orderAfter.payment_status).toBe("authorized") |
|
352
|
expect(orderAfter.payment_collections[0].payments).toHaveLength(1) |
|
353
|
}) |
|
354
|
|
|
355
|
it("should return still-pending status when provider has not received payment", async () => { |
|
356
|
const paymentCollection = ( |
|
357
|
await api.post( |
|
358
|
"/store/payment-collections", |
|
359
|
{ cart_id: cart.id }, |
|
360
|
storeHeaders |
|
361
|
) |
|
362
|
).data.payment_collection |
|
363
|
|
|
364
|
await api.post( |
|
365
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
366
|
{ provider_id: pendingAuthProviderId }, |
|
367
|
storeHeaders |
|
368
|
) |
|
369
|
|
|
370
|
const completedCart = ( |
|
371
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
372
|
).data |
|
373
|
|
|
374
|
expect(completedCart.type).toBe("order") |
|
375
|
const orderId = completedCart.order.id |
|
376
|
|
|
377
|
// Get session and collection IDs |
|
378
|
const order = ( |
|
379
|
await api.get( |
|
380
|
`/admin/orders/${orderId}?fields=*payment_collections,*payment_collections.payment_sessions`, |
|
381
|
adminHeaders |
|
382
|
) |
|
383
|
).data.order |
|
384
|
|
|
385
|
const sessionId = order.payment_collections[0].payment_sessions[0].id |
|
386
|
|
|
387
|
// Admin triggers authorization — payment NOT received yet |
|
388
|
// The provider will return pending_authorization again |
|
389
|
const authResult = ( |
|
390
|
await api.post( |
|
391
|
`/admin/orders/${orderId}/payment-sessions/authorize?fields=+payment_status`, |
|
392
|
{ payment_session_id: sessionId }, |
|
393
|
adminHeaders |
|
394
|
) |
|
395
|
).data |
|
396
|
|
|
397
|
// Order should still be awaiting |
|
398
|
expect(authResult.is_authorized).toBe(false) |
|
399
|
expect(authResult.order).toEqual( |
|
400
|
expect.objectContaining({ |
|
401
|
id: orderId, |
|
402
|
payment_status: "awaiting", |
|
403
|
}) |
|
404
|
) |
|
405
|
|
|
406
|
// Order should still be awaiting |
|
407
|
const orderAfter = ( |
|
408
|
await api.get( |
|
409
|
`/admin/orders/${orderId}?fields=+payment_status`, |
|
410
|
adminHeaders |
|
411
|
) |
|
412
|
).data.order |
|
413
|
|
|
414
|
expect(orderAfter.payment_status).toBe("awaiting") |
|
415
|
}) |
|
416
|
}) |
|
417
|
|
|
418
|
describe("POST /hooks/payment/:provider — webhook authorize for existing order", () => { |
|
419
|
const webhookProviderPath = "pending-auth_pending-auth" |
|
420
|
|
|
421
|
async function waitForPaymentStatus( |
|
422
|
orderId: string, |
|
423
|
expectedStatus: string, |
|
424
|
maxWaitMs = 10000 |
|
425
|
) { |
|
426
|
const start = Date.now() |
|
427
|
while (Date.now() - start < maxWaitMs) { |
|
428
|
const order = ( |
|
429
|
await api.get( |
|
430
|
`/admin/orders/${orderId}?fields=+payment_status`, |
|
431
|
adminHeaders |
|
432
|
) |
|
433
|
).data.order |
|
434
|
|
|
435
|
if (order.payment_status === expectedStatus) { |
|
436
|
return order |
|
437
|
} |
|
438
|
|
|
439
|
await setTimeout(500) |
|
440
|
} |
|
441
|
|
|
442
|
throw new Error( |
|
443
|
`Timed out waiting for payment_status to become "${expectedStatus}"` |
|
444
|
) |
|
445
|
} |
|
446
|
|
|
447
|
it("should authorize payment via webhook when order exists with pending_authorization", async () => { |
|
448
|
// 1. Complete cart with pending_authorization provider |
|
449
|
const paymentCollection = ( |
|
450
|
await api.post( |
|
451
|
"/store/payment-collections", |
|
452
|
{ cart_id: cart.id }, |
|
453
|
storeHeaders |
|
454
|
) |
|
455
|
).data.payment_collection |
|
456
|
|
|
457
|
await api.post( |
|
458
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
459
|
{ provider_id: pendingAuthProviderId }, |
|
460
|
storeHeaders |
|
461
|
) |
|
462
|
|
|
463
|
const completedCart = ( |
|
464
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
465
|
).data |
|
466
|
|
|
467
|
expect(completedCart.type).toBe("order") |
|
468
|
const orderId = completedCart.order.id |
|
469
|
|
|
470
|
// 2. Get the session ID and verify initial state |
|
471
|
const orderBefore = ( |
|
472
|
await api.get( |
|
473
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections,*payment_collections.payment_sessions,*payment_collections.payments`, |
|
474
|
adminHeaders |
|
475
|
) |
|
476
|
).data.order |
|
477
|
|
|
478
|
expect(orderBefore.payment_status).toBe("awaiting") |
|
479
|
const sessionId = |
|
480
|
orderBefore.payment_collections[0].payment_sessions[0].id |
|
481
|
expect(orderBefore.payment_collections[0].payments).toHaveLength(0) |
|
482
|
|
|
483
|
// 3. Update session data so provider returns AUTHORIZED on next authorize call |
|
484
|
const paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
485
|
await paymentModule.updatePaymentSession({ |
|
486
|
id: sessionId, |
|
487
|
currency_code: "usd", |
|
488
|
amount: orderBefore.payment_collections[0].amount, |
|
489
|
data: { payment_received: true }, |
|
490
|
}) |
|
491
|
|
|
492
|
// 4. Send webhook — the provider's getWebhookActionAndData reads the body |
|
493
|
// and returns the action. The subscriber + processPaymentWorkflow handle the rest. |
|
494
|
const webhookRes = await api.post( |
|
495
|
`/hooks/payment/${webhookProviderPath}`, |
|
496
|
{ |
|
497
|
action: PaymentActions.AUTHORIZED, |
|
498
|
session_id: sessionId, |
|
499
|
amount: orderBefore.payment_collections[0].amount, |
|
500
|
} |
|
501
|
) |
|
502
|
|
|
503
|
expect(webhookRes.status).toBe(200) |
|
504
|
|
|
505
|
// 5. Wait for async webhook processing and verify payment is authorized |
|
506
|
await waitForPaymentStatus(orderId, "authorized") |
|
507
|
|
|
508
|
const orderAfter = ( |
|
509
|
await api.get( |
|
510
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections,*payment_collections.payments`, |
|
511
|
adminHeaders |
|
512
|
) |
|
513
|
).data.order |
|
514
|
|
|
515
|
expect(orderAfter.payment_status).toBe("authorized") |
|
516
|
expect(orderAfter.payment_collections[0].payments).toHaveLength(1) |
|
517
|
expect(orderAfter.payment_collections[0].status).toBe("authorized") |
|
518
|
}) |
|
519
|
|
|
520
|
it("should capture payment via webhook when order exists with pending_authorization and action is SUCCESSFUL", async () => { |
|
521
|
// 1. Complete cart with pending_authorization provider |
|
522
|
const paymentCollection = ( |
|
523
|
await api.post( |
|
524
|
"/store/payment-collections", |
|
525
|
{ cart_id: cart.id }, |
|
526
|
storeHeaders |
|
527
|
) |
|
528
|
).data.payment_collection |
|
529
|
|
|
530
|
await api.post( |
|
531
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
532
|
{ provider_id: pendingAuthProviderId }, |
|
533
|
storeHeaders |
|
534
|
) |
|
535
|
|
|
536
|
const completedCart = ( |
|
537
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
538
|
).data |
|
539
|
|
|
540
|
expect(completedCart.type).toBe("order") |
|
541
|
const orderId = completedCart.order.id |
|
542
|
|
|
543
|
// 2. Get session ID |
|
544
|
const orderBefore = ( |
|
545
|
await api.get( |
|
546
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections,*payment_collections.payment_sessions`, |
|
547
|
adminHeaders |
|
548
|
) |
|
549
|
).data.order |
|
550
|
|
|
551
|
const sessionId = |
|
552
|
orderBefore.payment_collections[0].payment_sessions[0].id |
|
553
|
|
|
554
|
// 3. Update session data so provider returns AUTHORIZED |
|
555
|
const paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
556
|
await paymentModule.updatePaymentSession({ |
|
557
|
id: sessionId, |
|
558
|
currency_code: "usd", |
|
559
|
amount: orderBefore.payment_collections[0].amount, |
|
560
|
data: { payment_received: true }, |
|
561
|
}) |
|
562
|
|
|
563
|
// 4. Send webhook with SUCCESSFUL (autocapture) action |
|
564
|
const webhookRes = await api.post( |
|
565
|
`/hooks/payment/${webhookProviderPath}`, |
|
566
|
{ |
|
567
|
action: PaymentActions.SUCCESSFUL, |
|
568
|
session_id: sessionId, |
|
569
|
amount: orderBefore.payment_collections[0].amount, |
|
570
|
} |
|
571
|
) |
|
572
|
|
|
573
|
expect(webhookRes.status).toBe(200) |
|
574
|
|
|
575
|
// 5. Wait for async webhook processing and verify payment is captured |
|
576
|
await waitForPaymentStatus(orderId, "captured") |
|
577
|
|
|
578
|
const orderAfter = ( |
|
579
|
await api.get( |
|
580
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections,*payment_collections.payments,*payment_collections.payments.captures`, |
|
581
|
adminHeaders |
|
582
|
) |
|
583
|
).data.order |
|
584
|
|
|
585
|
expect(orderAfter.payment_status).toBe("captured") |
|
586
|
expect(orderAfter.payment_collections[0].payments).toHaveLength(1) |
|
587
|
expect( |
|
588
|
orderAfter.payment_collections[0].payments[0].captures |
|
589
|
).toHaveLength(1) |
|
590
|
expect(orderAfter.payment_collections[0].status).toBe("completed") |
|
591
|
}) |
|
592
|
|
|
593
|
it("should not throw and should leave the order awaiting when a SUCCESSFUL webhook arrives while the provider still returns pending_authorization (autocapture, no payment yet)", async () => { |
|
594
|
// 1. Complete cart with pending_authorization provider |
|
595
|
const paymentCollection = ( |
|
596
|
await api.post( |
|
597
|
"/store/payment-collections", |
|
598
|
{ cart_id: cart.id }, |
|
599
|
storeHeaders |
|
600
|
) |
|
601
|
).data.payment_collection |
|
602
|
|
|
603
|
await api.post( |
|
604
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
605
|
{ provider_id: pendingAuthProviderId }, |
|
606
|
storeHeaders |
|
607
|
) |
|
608
|
|
|
609
|
const completedCart = ( |
|
610
|
await api.post(`/store/carts/${cart.id}/complete`, {}, storeHeaders) |
|
611
|
).data |
|
612
|
|
|
613
|
expect(completedCart.type).toBe("order") |
|
614
|
const orderId = completedCart.order.id |
|
615
|
|
|
616
|
const orderBefore = ( |
|
617
|
await api.get( |
|
618
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections,*payment_collections.payment_sessions,*payment_collections.payments`, |
|
619
|
adminHeaders |
|
620
|
) |
|
621
|
).data.order |
|
622
|
|
|
623
|
expect(orderBefore.payment_status).toBe("awaiting") |
|
624
|
const sessionId = |
|
625
|
orderBefore.payment_collections[0].payment_sessions[0].id |
|
626
|
const amount = orderBefore.payment_collections[0].amount |
|
627
|
|
|
628
|
// 2. Run processPaymentWorkflow directly (bypassing the async event |
|
629
|
// bus, whose subscriber swallows workflow errors) with a |
|
630
|
// SUCCESSFUL action and no payment yet, and no payment_received |
|
631
|
// flag set on the session — the provider's authorizePayment call |
|
632
|
// triggered by the autocapture branch returns |
|
633
|
// pending_authorization again, so there is no payment to capture. |
|
634
|
const { errors } = await processPaymentWorkflow(appContainer).run({ |
|
635
|
input: { |
|
636
|
action: PaymentActions.SUCCESSFUL, |
|
637
|
data: { session_id: sessionId, amount }, |
|
638
|
}, |
|
639
|
throwOnError: false, |
|
640
|
}) |
|
641
|
|
|
642
|
expect(errors ?? []).toEqual([]) |
|
643
|
|
|
644
|
const orderAfter = ( |
|
645
|
await api.get( |
|
646
|
`/admin/orders/${orderId}?fields=+payment_status,*payment_collections.payments`, |
|
647
|
adminHeaders |
|
648
|
) |
|
649
|
).data.order |
|
650
|
|
|
651
|
expect(orderAfter.payment_status).toBe("awaiting") |
|
652
|
expect(orderAfter.payment_collections[0].payments).toHaveLength(0) |
|
653
|
|
|
654
|
// 3. The session should still be authorizable/capturable once the |
|
655
|
// payment actually arrives, proving the workflow didn't get stuck. |
|
656
|
const paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
657
|
await paymentModule.updatePaymentSession({ |
|
658
|
id: sessionId, |
|
659
|
currency_code: "usd", |
|
660
|
amount: orderBefore.payment_collections[0].amount, |
|
661
|
data: { payment_received: true }, |
|
662
|
}) |
|
663
|
|
|
664
|
const retryWebhookRes = await api.post( |
|
665
|
`/hooks/payment/${webhookProviderPath}`, |
|
666
|
{ |
|
667
|
action: PaymentActions.SUCCESSFUL, |
|
668
|
session_id: sessionId, |
|
669
|
amount: orderBefore.payment_collections[0].amount, |
|
670
|
} |
|
671
|
) |
|
672
|
expect(retryWebhookRes.status).toBe(200) |
|
673
|
|
|
674
|
await waitForPaymentStatus(orderId, "captured") |
|
675
|
}) |
|
676
|
|
|
677
|
it("should NOT complete the cart on an intermediate (PENDING) webhook action when no order exists yet", async () => { |
|
678
|
const paymentCollection = ( |
|
679
|
await api.post( |
|
680
|
"/store/payment-collections", |
|
681
|
{ cart_id: cart.id }, |
|
682
|
storeHeaders |
|
683
|
) |
|
684
|
).data.payment_collection |
|
685
|
|
|
686
|
const sessionId = ( |
|
687
|
await api.post( |
|
688
|
`/store/payment-collections/${paymentCollection.id}/payment-sessions`, |
|
689
|
{ provider_id: pendingAuthProviderId }, |
|
690
|
storeHeaders |
|
691
|
) |
|
692
|
).data.payment_collection.payment_sessions[0].id |
|
693
|
|
|
694
|
// Make the session authorizable so that, without the action guard, |
|
695
|
// the webhook-driven completion would succeed and complete the cart. |
|
696
|
const paymentModule = appContainer.resolve(Modules.PAYMENT) |
|
697
|
await paymentModule.updatePaymentSession({ |
|
698
|
id: sessionId, |
|
699
|
currency_code: "usd", |
|
700
|
amount: paymentCollection.amount, |
|
701
|
data: { payment_received: true }, |
|
702
|
}) |
|
703
|
|
|
704
|
const cartBefore = ( |
|
705
|
await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
706
|
).data.cart |
|
707
|
expect(cartBefore.completed_at).toBeNull() |
|
708
|
|
|
709
|
const webhookRes = await api.post( |
|
710
|
`/hooks/payment/${webhookProviderPath}`, |
|
711
|
{ |
|
712
|
action: PaymentActions.PENDING, |
|
713
|
session_id: sessionId, |
|
714
|
amount: paymentCollection.amount, |
|
715
|
} |
|
716
|
) |
|
717
|
expect(webhookRes.status).toBe(200) |
|
718
|
|
|
719
|
// The webhook is processed asynchronously. Keep checking for long |
|
720
|
// enough that a (wrongly) triggered completion would have landed — |
|
721
|
// the sibling webhook tests take ~6s for their effects to appear. |
|
722
|
const deadline = Date.now() + 8000 |
|
723
|
while (Date.now() < deadline) { |
|
724
|
const cartAfter = ( |
|
725
|
await api.get(`/store/carts/${cart.id}`, storeHeaders) |
|
726
|
).data.cart |
|
727
|
expect(cartAfter.completed_at).toBeNull() |
|
728
|
await setTimeout(500) |
|
729
|
} |
|
730
|
}) |
|
731
|
}) |
|
732
|
}, |
|
733
|
}) |