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 authored and GitHub committed 2025-08-28 12:59:24 +00:00
1 parent ff152e7ace
commit b111d01898
3 files changed
+53 -13

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---
chore(core-flows): order payment status precision
@@ -1,3 +1,4 @@
import { MathBN, MEDUSA_EPSILON } from "@medusajs/framework/utils"
import { import {
getLastFulfillmentStatus, getLastFulfillmentStatus,
getLastPaymentStatus, getLastPaymentStatus,
@@ -71,8 +72,18 @@ describe("Aggregate Order Status", () => {
expect( expect(
getLastPaymentStatus({ getLastPaymentStatus({
payment_collections: [ 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" }, { status: "canceled" },
], ],
} as any) } as any)
@@ -90,6 +101,26 @@ describe("Aggregate Order Status", () => {
} as any) } as any)
).toEqual("partially_refunded") ).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( expect(
getLastPaymentStatus({ getLastPaymentStatus({
payment_collections: [ payment_collections: [
@@ -1,5 +1,5 @@
import { OrderDetailDTO } from "@medusajs/framework/types" 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) => { export const getLastPaymentStatus = (order: OrderDetailDTO) => {
const PaymentStatus = { const PaymentStatus = {
@@ -26,21 +26,25 @@ export const getLastPaymentStatus = (order: OrderDetailDTO) => {
(isDefined(paymentCollection.amount) && (isDefined(paymentCollection.amount) &&
MathBN.eq(paymentCollection.amount, 0)) MathBN.eq(paymentCollection.amount, 0))
) { ) {
paymentStatus[PaymentStatus.CAPTURED] += MathBN.gte( const isGte = MathBN.lte(
paymentCollection.captured_amount as number, MathBN.sub(
paymentCollection.amount paymentCollection.amount,
paymentCollection.captured_amount as number
),
MEDUSA_EPSILON
) )
? 1 paymentStatus[PaymentStatus.CAPTURED] += isGte ? 1 : 0.5
: 0.5
} }
if (MathBN.gt(paymentCollection.refunded_amount ?? 0, 0)) { if (MathBN.gt(paymentCollection.refunded_amount ?? 0, 0)) {
paymentStatus[PaymentStatus.REFUNDED] += MathBN.gte( const isGte = MathBN.lte(
paymentCollection.refunded_amount as number, MathBN.sub(
paymentCollection.amount paymentCollection.amount,
paymentCollection.refunded_amount as number
),
MEDUSA_EPSILON
) )
? 1 paymentStatus[PaymentStatus.REFUNDED] += isGte ? 1 : 0.5
: 0.5
} }
paymentStatus[paymentCollection.status] += 1 paymentStatus[paymentCollection.status] += 1