Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(core): avoid double-counting payment statuses (#1399)

Shin51f5f6b85b48
diff --git a/packages/core/src/workflows/order-group/utils/__tests__/aggregate-status.unit.spec.ts b/packages/core/src/workflows/order-group/utils/__tests__/aggregate-status.unit.spec.ts
new file mode 100644
index 000000000..fae0c6682
--- /dev/null
+++ b/packages/core/src/workflows/order-group/utils/__tests__/aggregate-status.unit.spec.ts
@@ -0,0 +1,73 @@
+import type { OrderDetailDTO } from "@medusajs/framework/types"
+
+import { getLastPaymentStatus } from "../aggregate-status"
+
+const orderWithPaymentCollection = (
+ paymentCollection: Record<string, unknown>,
+) =>
+ ({
+ currency_code: "usd",
+ payment_collections: [paymentCollection],
+ }) as unknown as OrderDetailDTO
+
+describe("getLastPaymentStatus", () => {
+ it.each([
+ [
+ "fully captured",
+ {
+ status: "captured",
+ amount: 100,
+ captured_amount: 100,
+ refunded_amount: 0,
+ },
+ "captured",
+ ],
+ [
+ "fully refunded",
+ {
+ status: "refunded",
+ amount: 100,
+ captured_amount: 100,
+ refunded_amount: 100,
+ },
+ "refunded",
+ ],
+ [
+ "partially captured",
+ {
+ status: "captured",
+ amount: 100,
+ captured_amount: 50,
+ refunded_amount: 0,
+ },
+ "partially_captured",
+ ],
+ [
+ "partially refunded",
+ {
+ status: "refunded",
+ amount: 100,
+ captured_amount: 100,
+ refunded_amount: 50,
+ },
+ "partially_refunded",
+ ],
+ [
+ "authorized",
+ {
+ status: "authorized",
+ amount: 100,
+ captured_amount: 0,
+ refunded_amount: 0,
+ },
+ "authorized",
+ ],
+ ])(
+ "returns the terminal status for the %s collection",
+ (_, collection, expected) => {
+ expect(getLastPaymentStatus(orderWithPaymentCollection(collection))).toBe(
+ expected,
+ )
+ },
+ )
+})
diff --git a/packages/core/src/workflows/order-group/utils/aggregate-status.ts b/packages/core/src/workflows/order-group/utils/aggregate-status.ts
index 6dbed09ca..302b3bf03 100644
--- a/packages/core/src/workflows/order-group/utils/aggregate-status.ts
+++ b/packages/core/src/workflows/order-group/utils/aggregate-status.ts
@@ -31,11 +31,12 @@ export const getLastPaymentStatus = (order: OrderDetailDTO) => {
}
for (const paymentCollection of order.payment_collections) {
- if (
+ const captureIsAmountDerived =
MathBN.gt(paymentCollection.captured_amount ?? 0, 0) ||
(isDefined(paymentCollection.amount) &&
MathBN.eq(paymentCollection.amount, 0))
- ) {
+
+ if (captureIsAmountDerived) {
const isGte = MathBN.lte(
MathBN.sub(
paymentCollection.amount,
@@ -46,7 +47,12 @@ export const getLastPaymentStatus = (order: OrderDetailDTO) => {
paymentStatus[PaymentStatus.CAPTURED] += isGte ? 1 : 0.5
}
- if (MathBN.gt(paymentCollection.refunded_amount ?? 0, 0)) {
+ const refundIsAmountDerived = MathBN.gt(
+ paymentCollection.refunded_amount ?? 0,
+ 0
+ )
+
+ if (refundIsAmountDerived) {
const isGte = MathBN.lte(
MathBN.sub(
paymentCollection.amount,
@@ -57,7 +63,15 @@ export const getLastPaymentStatus = (order: OrderDetailDTO) => {
paymentStatus[PaymentStatus.REFUNDED] += isGte ? 1 : 0.5
}
- paymentStatus[paymentCollection.status] += 1
+ const statusIsAmountDerived =
+ (paymentCollection.status === PaymentStatus.CAPTURED &&
+ captureIsAmountDerived) ||
+ (paymentCollection.status === PaymentStatus.REFUNDED &&
+ refundIsAmountDerived)
+
+ if (!statusIsAmountDerived) {
+ paymentStatus[paymentCollection.status] += 1
+ }
}
const totalPayments = order.payment_collections.length
@@ -200,4 +214,4 @@ export const getLastFulfillmentStatus = (order: OrderDetailDTO) => {
}
return FulfillmentStatus.NOT_FULFILLED
-}
\ No newline at end of file
+}