Skip to content

[Bug]: net negative credit line totals are zeroed by the sub-cent clamp in calculateCreditLinesTotal #16302

Description

@shuvamk

Package.json file

{
  "dependencies": {
    "@medusajs/framework": "2.18.0",
    "@medusajs/medusa": "2.18.0"
  }
}

Node.js version

v26.4.0

Database and its version

N/A — pure totals helper

Operating system name and version

macOS 26.5

Browser name

No response

What happended?

With a negative credit line, order.credit_line_total and summary.credit_line_total return
different numbers for the same order, and pending_difference never settles back to 0, so
the same overpayment can be settled to store credit again.

calculateCreditLinesTotal (packages/core/utils/src/totals/credit-lines/index.ts) drops
sub-cent residue with MathBN.lte(creditLinesTotal, currencyEpsilon). The comparison is
one-sided, so every net negative total also satisfies it and is zeroed;
calculate-order-change.ts, which produces summary, has no such clamp.

Negative credit lines are supported: validateOrderCreditLinesStep allows them when
pending_difference < 0, and the admin balance settlement form sends amount: ... * -1 to
settle an overpayment to store credit.

Expected behavior

decorateCartTotals({
  currency_code: "usd",
  items: [{ unit_price: 50, quantity: 2, tax_lines: [] }],
  credit_lines: [{ amount: -106 }],
})
// credit_line_total: -106, total: 206

Actual behavior

credit_line_total: 0 while summary.credit_line_total still reports -106; total: 100.

totals/cart/index.ts already compares MathBN.abs(...) for the sibling
pending_difference clamp. I have a fix ready and can open a PR.

Link to reproduction repo

Not needed — see the snippet above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions