chore: Ensure refund doesn't exceed captured amount (#13744)

* wip

* chore: prepare for PR

* move to end

* Change order of operations in refundPaymentWorkflow

Updated the order of operations in the refundPaymentWorkflow.

* chore: Add validation
This commit is contained in:
Oli Juhl
2025-10-13 22:09:46 +02:00
committed by GitHub
parent 723dc082f0
commit 1d2b4566fd
5 changed files with 195 additions and 38 deletions

View File

@@ -254,7 +254,7 @@ medusaIntegrationTestRunner({
refund_reason_id: refundReason.id,
refund_reason: expect.objectContaining({
label: "test",
code: "test"
code: "test",
}),
}),
],
@@ -369,6 +369,73 @@ medusaIntegrationTestRunner({
}),
])
})
it("should throw if the refund amount exceeds the payment amount", async () => {
const payment = order.payment_collections[0].payments[0]
const refundReason = (
await api.post(
`/admin/refund-reasons`,
{ label: "Test", code: "test" },
adminHeaders
)
).data.refund_reason
await api.post(
`/admin/payments/${payment.id}/capture`,
undefined,
adminHeaders
)
await api.post(
`/admin/payments/${payment.id}/refund`,
{
amount: 50,
refund_reason_id: refundReason.id,
},
adminHeaders
)
const updatedOrder = (
await api.get(`/admin/orders/${order.id}`, adminHeaders)
).data.order
expect(updatedOrder.credit_line_total).toEqual(50)
expect(updatedOrder.credit_lines).toEqual([
expect.objectContaining({
reference: "Test",
reference_id: "test",
}),
])
try {
await api.post(
`/admin/payments/${payment.id}/refund`,
{
amount: 5000,
refund_reason_id: refundReason.id,
},
adminHeaders
)
} catch (error) {
expect(error.response.status).toBe(400)
expect(error.response.data.message).toBe(
"You are not allowed to refund more than the captured amount"
)
}
const updatedUpdatedOrder = (
await api.get(`/admin/orders/${order.id}`, adminHeaders)
).data.order
expect(updatedUpdatedOrder.credit_line_total).toEqual(50)
expect(updatedUpdatedOrder.credit_lines).toEqual([
expect.objectContaining({
reference: "Test",
reference_id: "test",
}),
])
})
})
},
})