|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { ModuleRegistrationName, Modules } from "@medusajs/utils" |
|
3
|
import { |
|
4
|
adminHeaders, |
|
5
|
createAdminUser, |
|
6
|
} from "../../../../helpers/create-admin-user" |
|
7
|
import { setupTaxStructure } from "../../../../modules/__tests__/fixtures" |
|
8
|
import { createOrderSeeder } from "../../fixtures/order" |
|
9
|
|
|
10
|
jest.setTimeout(300000) |
|
11
|
|
|
12
|
medusaIntegrationTestRunner({ |
|
13
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
14
|
let container |
|
15
|
|
|
16
|
beforeAll(async () => { |
|
17
|
container = getContainer() |
|
18
|
await setupTaxStructure(container.resolve(ModuleRegistrationName.TAX)) |
|
19
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
20
|
|
|
21
|
await dbUtils.snapshot() |
|
22
|
}) |
|
23
|
|
|
24
|
describe("POST /admin/orders/:id/cancel - Credit Line Calculation with Multiple Payments", () => { |
|
25
|
describe("when order has multiple payments with different statuses", () => { |
|
26
|
it("should only include captured payment amounts in credit line, not other status payments", async () => { |
|
27
|
// Create order with initial payment |
|
28
|
const seeder = await createOrderSeeder({ |
|
29
|
api, |
|
30
|
container, |
|
31
|
}) |
|
32
|
const order = seeder.order |
|
33
|
const paymentModule = container.resolve(Modules.PAYMENT) |
|
34
|
|
|
35
|
// Get the original payment collection and payment |
|
36
|
const originalPayment = order.payment_collections[0].payments[0] |
|
37
|
const paymentCollectionId = order.payment_collections[0].id |
|
38
|
|
|
39
|
// Use the authorized payment amount |
|
40
|
// Note: Amounts in Medusa are already in the correct format (e.g., 1272 for $1272 USD) |
|
41
|
const paymentAmount = originalPayment.amount |
|
42
|
const partialCaptureAmount = 50 // Capture a fixed partial amount for testing |
|
43
|
|
|
44
|
// Capture the original payment for partial amount |
|
45
|
await api.post( |
|
46
|
`/admin/payments/${originalPayment.id}/capture`, |
|
47
|
{ amount: partialCaptureAmount }, |
|
48
|
adminHeaders |
|
49
|
) |
|
50
|
|
|
51
|
// Create additional payment sessions to simulate failed payment attempts |
|
52
|
// Payment session 1 - will be authorized then canceled (failed intent) |
|
53
|
const failedSession1 = await paymentModule.createPaymentSession( |
|
54
|
paymentCollectionId, |
|
55
|
{ |
|
56
|
provider_id: "pp_system_default", |
|
57
|
amount: paymentAmount, |
|
58
|
currency_code: "usd", |
|
59
|
data: {}, |
|
60
|
} |
|
61
|
) |
|
62
|
const failedPayment1 = await paymentModule.authorizePaymentSession( |
|
63
|
failedSession1.id, |
|
64
|
{} |
|
65
|
) |
|
66
|
await paymentModule.cancelPayment(failedPayment1.id) |
|
67
|
|
|
68
|
// Payment session 2 - will be authorized then canceled (another failed intent) |
|
69
|
const failedSession2 = await paymentModule.createPaymentSession( |
|
70
|
paymentCollectionId, |
|
71
|
{ |
|
72
|
provider_id: "pp_system_default", |
|
73
|
amount: paymentAmount, |
|
74
|
currency_code: "usd", |
|
75
|
data: {}, |
|
76
|
} |
|
77
|
) |
|
78
|
const failedPayment2 = await paymentModule.authorizePaymentSession( |
|
79
|
failedSession2.id, |
|
80
|
{} |
|
81
|
) |
|
82
|
await paymentModule.cancelPayment(failedPayment2.id) |
|
83
|
|
|
84
|
// Payment session 3 - authorized but not captured (pending) |
|
85
|
const pendingSession = await paymentModule.createPaymentSession( |
|
86
|
paymentCollectionId, |
|
87
|
{ |
|
88
|
provider_id: "pp_system_default", |
|
89
|
amount: 75, // Fixed amount for pending payment |
|
90
|
currency_code: "usd", |
|
91
|
data: {}, |
|
92
|
} |
|
93
|
) |
|
94
|
const pendingPayment = await paymentModule.authorizePaymentSession( |
|
95
|
pendingSession.id, |
|
96
|
{} |
|
97
|
) |
|
98
|
|
|
99
|
// Get updated order with all payments |
|
100
|
const orderBeforeCancel = ( |
|
101
|
await api.get( |
|
102
|
`/admin/orders/${order.id}?fields=*payment_collections.payments.amount,*payment_collections.payments.captured_at,*payment_collections.payments.canceled_at,*payment_collections.payments.captures.amount,*credit_lines.amount`, |
|
103
|
adminHeaders |
|
104
|
) |
|
105
|
).data.order |
|
106
|
|
|
107
|
// Verify we have the expected payment states |
|
108
|
expect( |
|
109
|
orderBeforeCancel.payment_collections[0].payments |
|
110
|
).toHaveLength(4) |
|
111
|
|
|
112
|
const payments = orderBeforeCancel.payment_collections[0].payments |
|
113
|
|
|
114
|
// Find payments by their characteristics |
|
115
|
const capturedPayment = payments.find( |
|
116
|
(p) => p.id === originalPayment.id |
|
117
|
) |
|
118
|
const canceledPayments = payments.filter( |
|
119
|
(p) => p.canceled_at !== null |
|
120
|
) |
|
121
|
const authorizedOnlyPayments = payments.filter( |
|
122
|
(p) => |
|
123
|
p.captured_at === null && |
|
124
|
p.canceled_at === null && |
|
125
|
p.captures.length === 0 |
|
126
|
) |
|
127
|
|
|
128
|
expect(capturedPayment).toBeDefined() |
|
129
|
expect(capturedPayment.captures).toHaveLength(1) |
|
130
|
expect(capturedPayment.captures[0].amount).toBe(partialCaptureAmount) |
|
131
|
expect(canceledPayments).toHaveLength(2) |
|
132
|
expect(authorizedOnlyPayments).toHaveLength(1) |
|
133
|
|
|
134
|
// Cancel the order |
|
135
|
const cancelResponse = await api.post( |
|
136
|
`/admin/orders/${order.id}/cancel`, |
|
137
|
{}, |
|
138
|
adminHeaders |
|
139
|
) |
|
140
|
|
|
141
|
expect(cancelResponse.status).toBe(200) |
|
142
|
|
|
143
|
// Get order with credit lines |
|
144
|
const canceledOrder = ( |
|
145
|
await api.get( |
|
146
|
`/admin/orders/${order.id}?fields=*credit_lines.amount,*credit_lines.reference,*credit_lines.reference_id,*payment_collections.payments.amount,*payment_collections.payments.captured_at,*payment_collections.payments.canceled_at,*payment_collections.payments.captures.amount`, |
|
147
|
adminHeaders |
|
148
|
) |
|
149
|
).data.order |
|
150
|
|
|
151
|
expect(canceledOrder.status).toBe("canceled") |
|
152
|
|
|
153
|
// The critical assertion: credit lines should only account for captured amounts |
|
154
|
// Expected: Only 50 (the partialCaptureAmount) should be credited |
|
155
|
// Bug behavior: Would credit all payment amounts (50 + paymentAmount + paymentAmount + 75) |
|
156
|
const totalCreditLineAmount = canceledOrder.credit_lines.reduce( |
|
157
|
(sum, cl) => sum + cl.amount, |
|
158
|
0 |
|
159
|
) |
|
160
|
|
|
161
|
// This test expects ONLY the captured amount (50) to be credited |
|
162
|
// If the bug exists, this will fail because it would credit all payment amounts |
|
163
|
expect(totalCreditLineAmount).toBe(50) |
|
164
|
|
|
165
|
// Verify that credit line total matches only captured amounts, not all payment amounts |
|
166
|
expect(canceledOrder.summary.credit_line_total).toBe(50) |
|
167
|
|
|
168
|
// The order total should be properly balanced (1272 - 50 = 1222) |
|
169
|
expect(canceledOrder.summary.current_order_total).toBe(1222) |
|
170
|
expect(canceledOrder.summary.accounting_total).toBe(1222) |
|
171
|
}) |
|
172
|
|
|
173
|
it("should not double-count pre-existing refunds when cancelling an order", async () => { |
|
174
|
const seeder = await createOrderSeeder({ |
|
175
|
api, |
|
176
|
container, |
|
177
|
}) |
|
178
|
const order = seeder.order |
|
179
|
|
|
180
|
const originalPayment = order.payment_collections[0].payments[0] |
|
181
|
const paymentAmount = originalPayment.amount |
|
182
|
const partialRefundAmount = 500 |
|
183
|
|
|
184
|
await api.post( |
|
185
|
`/admin/payments/${originalPayment.id}/capture`, |
|
186
|
{ amount: paymentAmount }, |
|
187
|
adminHeaders |
|
188
|
) |
|
189
|
|
|
190
|
await api.post( |
|
191
|
`/admin/payments/${originalPayment.id}/refund`, |
|
192
|
{ amount: partialRefundAmount }, |
|
193
|
adminHeaders |
|
194
|
) |
|
195
|
|
|
196
|
const orderBeforeCancel = ( |
|
197
|
await api.get( |
|
198
|
`/admin/orders/${order.id}?fields=*credit_lines.amount,*payment_collections.payments.captures.amount,*payment_collections.payments.refunds.amount`, |
|
199
|
adminHeaders |
|
200
|
) |
|
201
|
).data.order |
|
202
|
|
|
203
|
const beforePayment = |
|
204
|
orderBeforeCancel.payment_collections[0].payments[0] |
|
205
|
expect(beforePayment.captures).toHaveLength(1) |
|
206
|
expect(beforePayment.captures[0].amount).toBe(paymentAmount) |
|
207
|
expect(beforePayment.refunds).toHaveLength(1) |
|
208
|
expect(beforePayment.refunds[0].amount).toBe(partialRefundAmount) |
|
209
|
|
|
210
|
const creditLineAmountFromRefund = |
|
211
|
orderBeforeCancel.credit_lines.reduce( |
|
212
|
(sum, cl) => sum + cl.amount, |
|
213
|
0 |
|
214
|
) |
|
215
|
|
|
216
|
const cancelResponse = await api.post( |
|
217
|
`/admin/orders/${order.id}/cancel`, |
|
218
|
{}, |
|
219
|
adminHeaders |
|
220
|
) |
|
221
|
|
|
222
|
expect(cancelResponse.status).toBe(200) |
|
223
|
|
|
224
|
const canceledOrder = ( |
|
225
|
await api.get( |
|
226
|
`/admin/orders/${order.id}?fields=*credit_lines.amount,*payment_collections.payments.captures.amount,*payment_collections.payments.refunds.amount`, |
|
227
|
adminHeaders |
|
228
|
) |
|
229
|
).data.order |
|
230
|
|
|
231
|
expect(canceledOrder.status).toBe("canceled") |
|
232
|
|
|
233
|
const totalCreditLineAmount = canceledOrder.credit_lines.reduce( |
|
234
|
(sum, cl) => sum + cl.amount, |
|
235
|
0 |
|
236
|
) |
|
237
|
|
|
238
|
// Cancel should only credit the remaining (non-refunded) captured |
|
239
|
// amount, not the full captured amount. Total credit lines across |
|
240
|
// the partial refund + the cancel should equal the captured amount. |
|
241
|
expect(totalCreditLineAmount).toBe(paymentAmount) |
|
242
|
|
|
243
|
const creditLineFromCancel = |
|
244
|
totalCreditLineAmount - creditLineAmountFromRefund |
|
245
|
expect(creditLineFromCancel).toBe(paymentAmount - partialRefundAmount) |
|
246
|
}) |
|
247
|
|
|
248
|
it("should handle order with all payments canceled - zero credit line", async () => { |
|
249
|
// Create order with initial payment |
|
250
|
const seeder = await createOrderSeeder({ |
|
251
|
api, |
|
252
|
container, |
|
253
|
}) |
|
254
|
const order = seeder.order |
|
255
|
const paymentModule = container.resolve(Modules.PAYMENT) |
|
256
|
|
|
257
|
// Cancel the original payment (customer never successfully paid) |
|
258
|
const originalPayment = order.payment_collections[0].payments[0] |
|
259
|
const paymentAmount = originalPayment.amount |
|
260
|
await paymentModule.cancelPayment(originalPayment.id) |
|
261
|
|
|
262
|
// Create additional canceled payment sessions (all failed) |
|
263
|
const paymentCollectionId = order.payment_collections[0].id |
|
264
|
|
|
265
|
const failedSession1 = await paymentModule.createPaymentSession( |
|
266
|
paymentCollectionId, |
|
267
|
{ |
|
268
|
provider_id: "pp_system_default", |
|
269
|
amount: paymentAmount, |
|
270
|
currency_code: "usd", |
|
271
|
data: {}, |
|
272
|
} |
|
273
|
) |
|
274
|
const failedPayment1 = await paymentModule.authorizePaymentSession( |
|
275
|
failedSession1.id, |
|
276
|
{} |
|
277
|
) |
|
278
|
await paymentModule.cancelPayment(failedPayment1.id) |
|
279
|
|
|
280
|
const failedSession2 = await paymentModule.createPaymentSession( |
|
281
|
paymentCollectionId, |
|
282
|
{ |
|
283
|
provider_id: "pp_system_default", |
|
284
|
amount: paymentAmount, |
|
285
|
currency_code: "usd", |
|
286
|
data: {}, |
|
287
|
} |
|
288
|
) |
|
289
|
const failedPayment2 = await paymentModule.authorizePaymentSession( |
|
290
|
failedSession2.id, |
|
291
|
{} |
|
292
|
) |
|
293
|
await paymentModule.cancelPayment(failedPayment2.id) |
|
294
|
|
|
295
|
// Get order before cancel |
|
296
|
const orderBeforeCancel = ( |
|
297
|
await api.get( |
|
298
|
`/admin/orders/${order.id}?fields=*payment_collections.payments.canceled_at`, |
|
299
|
adminHeaders |
|
300
|
) |
|
301
|
).data.order |
|
302
|
|
|
303
|
// Verify all payments are canceled |
|
304
|
const allPaymentsCanceled = |
|
305
|
orderBeforeCancel.payment_collections[0].payments.every( |
|
306
|
(p) => p.canceled_at !== null |
|
307
|
) |
|
308
|
expect(allPaymentsCanceled).toBe(true) |
|
309
|
|
|
310
|
// Try to cancel the order |
|
311
|
const cancelResponse = await api |
|
312
|
.post(`/admin/orders/${order.id}/cancel`, {}, adminHeaders) |
|
313
|
.catch((e) => e.response) |
|
314
|
|
|
315
|
// The order should cancel successfully |
|
316
|
expect(cancelResponse.status).toBe(200) |
|
317
|
|
|
318
|
// Get order with credit lines |
|
319
|
const canceledOrder = ( |
|
320
|
await api.get( |
|
321
|
`/admin/orders/${order.id}?fields=*credit_lines.amount`, |
|
322
|
adminHeaders |
|
323
|
) |
|
324
|
).data.order |
|
325
|
|
|
326
|
// No captured payments = no credit line amount |
|
327
|
// Bug behavior: Would credit sum of all canceled payment amounts |
|
328
|
const totalCreditLineAmount = canceledOrder.credit_lines.reduce( |
|
329
|
(sum, cl) => sum + cl.amount, |
|
330
|
0 |
|
331
|
) |
|
332
|
|
|
333
|
expect(totalCreditLineAmount).toBe(0) |
|
334
|
expect(canceledOrder.summary.credit_line_total).toBe(0) |
|
335
|
}) |
|
336
|
}) |
|
337
|
}) |
|
338
|
}, |
|
339
|
}) |