All posts
Forward Development

Medusa 2.22 refunds that return 400 after the money has moved

In Medusa 2.22.1 and 2.23.0, a refund on an order with an open order change hits the provider, then fails. How to find affected orders and guard against it.

  • Medusa.js
  • Migrations
  • Operations

A support agent opens an order in the Medusa admin and issues a partial refund. Earlier, someone started a return on the same order and never confirmed it. The admin shows a 400: Order (order_...) already has an existing active order change. The agent reasonably assumes nothing happened. They click again, or go to the Stripe dashboard and refund there. A week later finance finds the customer was refunded twice, and the order in Medusa shows a refunded total of zero.

This is medusajs/medusa#17947. It was reported against @medusajs/medusa, @medusajs/core-flows, @medusajs/order and @medusajs/payment 2.22.1. The reporter confirmed it is still present in 2.23.0 and on develop as of 2026-11-21. No released version fixes it yet, so upgrading won't help. Until a fix ships, you need guardrails in your own code.

The provider refund runs before anything that can fail

In 2.22.1, refundPaymentWorkflow (packages/core/core-flows/src/payment/workflows/refund-payment.ts) runs its steps in this order:

  1. refundPaymentStep calls the payment provider's refundPayment. Money leaves the merchant's account here.
  2. addOrderTransactionStep records the transaction.
  3. createOrderRefundCreditLinesWorkflow.runAsStep(...) tries to create an order change for the credit line.
  4. emitEventStep emits payment.refunded.

Step 3 fails if the order already has an order change with status pending or requested. That covers an unconfirmed return request, a draft claim or exchange, and a started order edit. OrderModuleService.createOrderChange rejects the second active change, and the workflow rolls back. Step 1 has no compensation, though, because a provider refund cannot be undone. The money stays refunded, a refund row stays on the payment, and the API returns a 400.

Workflows can only roll back cleanly when the steps that can't be undone run last. Here, the one step that can never be undone runs first, ahead of a check that is easy to fail.

Canceled orders hit the same problem. validateOrderRefundCreditLinesStep calls throwIfOrderIsCancelled only after refundPaymentStep has already run. Refunding a payment on a canceled order moves money and then errors in the same way.

What an affected order looks like

After a failed refund, the records disagree with each other:

  • payment.refunds contains the refund.
  • order.summary.refunded_total is still 0, and the order has no credit line.
  • The payment.refunded event was never emitted. Any subscriber listening for it did not run. That includes refund notifications, commission reversals, quota or inventory release, and ERP or accounting sync.
  • The provider shows a completed refund.

The error is also misleading. Retrying while the order change is still open refunds at the provider again and fails again. Each click moves more money. Once the order change is resolved, a later refund for the remaining amount succeeds. But the first refund never appears on the order, so the order's numbers stay wrong.

Finding orders that are already affected

You can't find these orders from the order side alone, because the order looks as if nothing happened. Compare the three sources of truth instead.

First, compare payment refunds with order totals. For each order, sum the refunds on every payment in its payment collections (linked through order_payment_collection). Compare that with order.summary.refunded_total. Any order where the payment-side sum is higher needs a look. The Query API is the easiest way to pull this, since you can fetch orders with their payment collections, payments and refunds in one graph. Run it against every order created since you went live on 2.22.x.

Second, compare the provider with Medusa. Export refunds from Stripe or whichever provider you use for the same period, and match each one to a refund row by provider reference and amount. A provider refund with no matching row usually means someone refunded outside Medusa after seeing the 400. That's the double refund this bug causes.

Third, list the side effects that didn't happen. For every order the first two checks flag, write down which payment.refunded subscribers should have run. Then re-run them by hand or with a one-off script. Do this before you fix the totals, so that nothing downstream acts on a stale state.

Fixing the order record itself takes judgment. Usually that means adding the credit line after the blocking order change is confirmed or canceled. Don't script it blindly. Review each order.

Block refunds while an order change is open

The reporter's workaround is the right first guardrail. Add a route middleware on POST /admin/payments/:id/refund that:

  1. loads the payment and its payment_collection_id;
  2. finds the order through order_payment_collection;
  3. rejects the request with a not_allowed error if that order has an order_change with status pending or requested, or if the order is canceled.

The check runs before the workflow, so a request it rejects never reaches the provider. Make the error message say what to do, for example: confirm or cancel the open return or edit first. Staff ignore generic errors. They act on ones that tell them the next step.

If you call refundPaymentWorkflow anywhere else, such as a custom route, a subscriber or a support tool, add the same check there. The middleware only protects the admin route.

Make refunds safe to retry

The middleware closes the known path. Idempotency limits the damage from paths nobody has found yet. Before the workflow runs, record a refund request keyed on payment ID, amount and a client-generated request ID. Reject a second request with the same key while the first is still running or has failed in an unclear way. In the admin, disable the refund button after the first click, and show the payment's existing refunds next to it, not just the order totals. Here, the payment's refund list is the only place that tells the truth.

If your provider integration supports idempotency keys on refund calls, pass one through. Check whether your provider module actually forwards it rather than assuming it does.

Reconcile against the provider on a schedule

Turn the one-off audit into a nightly job. Compare provider refunds with Medusa refund rows, and payment.refunds totals with order.summary.refunded_total. Alert on any difference. That job is useful long after this bug is fixed. Any refund flow can drift from the provider, and a mismatch costs less to catch the next morning than at month-end.

This matters most during a migration. Teams moving from Magento 2 to Medusa often carry over refund habits from the old admin, where a failed credit memo usually meant no money had moved. That assumption doesn't hold on 2.22.x or 2.23.0. We plan for this kind of difference in our Magento 2 and Adobe Commerce to Medusa or Vendure migrations.

What to do this week

  • Run the three-way comparison across all orders refunded since you deployed 2.22.x. Resolve each mismatch by hand, and re-run the missed payment.refunded side effects.
  • Ship the middleware that rejects refunds on orders with a pending or requested order change, or on canceled orders.
  • Tell support staff that a 400 on a refund doesn't mean no money moved. They should check the payment's refunds before trying again.
  • Schedule the nightly provider reconciliation.
  • Watch issue #17947. When a fix lands, read the changed step order in refund-payment.ts before you remove your middleware.

If you need help auditing refunds on a live Medusa store or adding these guardrails, get in touch.

Need this done on a real stack?

Magento 2, Adobe Commerce, migrations to Medusa.js or Vendure, enterprise Next.js, WordPress, and AI automation.

Contact us