fix(payment): validate total amount when refunding payment (#8437)
Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
@@ -750,7 +750,7 @@ export default class PaymentModuleService
|
||||
"amount",
|
||||
"raw_amount",
|
||||
],
|
||||
relations: ["captures.raw_amount"],
|
||||
relations: ["captures.raw_amount", "refunds.raw_amount"],
|
||||
},
|
||||
sharedContext
|
||||
)
|
||||
@@ -763,9 +763,16 @@ export default class PaymentModuleService
|
||||
const amountAsBigNumber = new BigNumber(next.raw_amount)
|
||||
return MathBN.add(captureAmount, amountAsBigNumber)
|
||||
}, MathBN.convert(0))
|
||||
const refundAmount = new BigNumber(data.amount)
|
||||
const refundedAmount = payment.refunds.reduce((refundedAmount, next) => {
|
||||
return MathBN.add(refundedAmount, next.raw_amount)
|
||||
}, MathBN.convert(0))
|
||||
|
||||
if (MathBN.lt(capturedAmount, refundAmount)) {
|
||||
const totalRefundedAmount = MathBN.add(
|
||||
refundedAmount,
|
||||
data.amount
|
||||
)
|
||||
|
||||
if (MathBN.lt(capturedAmount, totalRefundedAmount)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`You cannot refund more than what is captured on the payment.`
|
||||
|
||||
Reference in New Issue
Block a user