chore(core-flows): order payment status fix (#13321)

What:
 - Consider epsilon to check if an order is fully captured or refunded.
This commit is contained in:
Carlos R. L. Rodrigues
2025-08-28 09:59:24 -03:00
committed by GitHub
parent ff152e7ace
commit b111d01898
3 changed files with 53 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---
chore(core-flows): order payment status precision

View File

@@ -1,3 +1,4 @@
import { MathBN, MEDUSA_EPSILON } from "@medusajs/framework/utils"
import {
getLastFulfillmentStatus,
getLastPaymentStatus,
@@ -71,8 +72,18 @@ describe("Aggregate Order Status", () => {
expect(
getLastPaymentStatus({
payment_collections: [
{ status: "authorized", refunded_amount: 10, amount: 10 },
{ status: "authorized", refunded_amount: 5, amount: 10 },
{
status: "authorized",
refunded_amount: 10,
captured_amount: 10,
amount: 10,
},
{
status: "authorized",
refunded_amount: 5,
captured_amount: 10,
amount: 10,
},
{ status: "canceled" },
],
} as any)
@@ -90,6 +101,26 @@ describe("Aggregate Order Status", () => {
} as any)
).toEqual("partially_refunded")
expect(
getLastPaymentStatus({
payment_collections: [
{
status: "authorized",
captured_amount: 10,
refunded_amount: 10,
amount: 10,
},
{
status: "authorized",
captured_amount: 10,
refunded_amount: MathBN.sub(10, MEDUSA_EPSILON),
amount: 10,
},
{ status: "canceled" },
],
} as any)
).toEqual("refunded")
expect(
getLastPaymentStatus({
payment_collections: [

View File

@@ -1,5 +1,5 @@
import { OrderDetailDTO } from "@medusajs/framework/types"
import { isDefined, MathBN } from "@medusajs/framework/utils"
import { isDefined, MathBN, MEDUSA_EPSILON } from "@medusajs/framework/utils"
export const getLastPaymentStatus = (order: OrderDetailDTO) => {
const PaymentStatus = {
@@ -26,21 +26,25 @@ export const getLastPaymentStatus = (order: OrderDetailDTO) => {
(isDefined(paymentCollection.amount) &&
MathBN.eq(paymentCollection.amount, 0))
) {
paymentStatus[PaymentStatus.CAPTURED] += MathBN.gte(
paymentCollection.captured_amount as number,
paymentCollection.amount
const isGte = MathBN.lte(
MathBN.sub(
paymentCollection.amount,
paymentCollection.captured_amount as number
),
MEDUSA_EPSILON
)
? 1
: 0.5
paymentStatus[PaymentStatus.CAPTURED] += isGte ? 1 : 0.5
}
if (MathBN.gt(paymentCollection.refunded_amount ?? 0, 0)) {
paymentStatus[PaymentStatus.REFUNDED] += MathBN.gte(
paymentCollection.refunded_amount as number,
paymentCollection.amount
const isGte = MathBN.lte(
MathBN.sub(
paymentCollection.amount,
paymentCollection.refunded_amount as number
),
MEDUSA_EPSILON
)
? 1
: 0.5
paymentStatus[PaymentStatus.REFUNDED] += isGte ? 1 : 0.5
}
paymentStatus[paymentCollection.status] += 1