diff --git a/.changeset/ninety-tips-brush.md b/.changeset/ninety-tips-brush.md new file mode 100644 index 0000000000..df70f63c1d --- /dev/null +++ b/.changeset/ninety-tips-brush.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +chore(core-flows): order payment status precision diff --git a/packages/core/core-flows/src/order/utils/__tests__/aggregate-status.spec.ts b/packages/core/core-flows/src/order/utils/__tests__/aggregate-status.spec.ts index 111675ac8b..b6b9d29e20 100644 --- a/packages/core/core-flows/src/order/utils/__tests__/aggregate-status.spec.ts +++ b/packages/core/core-flows/src/order/utils/__tests__/aggregate-status.spec.ts @@ -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: [ diff --git a/packages/core/core-flows/src/order/utils/aggregate-status.ts b/packages/core/core-flows/src/order/utils/aggregate-status.ts index d9afd14be0..786b236370 100644 --- a/packages/core/core-flows/src/order/utils/aggregate-status.ts +++ b/packages/core/core-flows/src/order/utils/aggregate-status.ts @@ -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