Writing
Commerce

How to Prevent Duplicate Subscription Orders in Medusa v2

Use a renewal record per billing cycle to handle duplicate webhooks, reconcile captured payments, and reuse the same Medusa order on retries.

Give every subscription billing cycle one renewal record in Medusa v2. When a webhook reports a capture you've already recorded, reuse the order linked to that renewal. Record the provider's payment ID separately so a second capture for the same cycle can go to review.

I used this design for a custom subscription integration where a payment provider handles recurring billing and Medusa owns orders and inventory. I wanted the handler to cope with late callbacks, a capture reported before stock was reserved, and a worker stopping partway through writing an order.

Use the webhook journal to track delivery

I kept the webhook journal and the renewal records separate. The journal records whether a provider event was processed. The renewal table has a unique index on the subscription contract and billing cycle. It also stores the payment and Medusa order IDs once they're known. The handler takes a lock per contract so callbacks for the same subscription don't attempt to update its renewal at the same time.

Say a September renewal is linked to payment A. The provider sends a second event with a new event ID, also for payment A. The journal sees a new event, but the renewal handler finds the same payment and order. If that renewal is already complete, the handler can return without creating another order. If the provider confirms payment B for September, the newer flow records a separate case for review and leaves payment A linked to the order.

Medusa can reuse a finished workflow execution when you pass the same transaction ID, enable idempotent, and keep the execution within its retentionTime. Keep the billing-cycle identity in the database so the webhook handler and recovery tools can still find the renewal after that window closes. Medusa documentation on storing workflow executions

Reserve inventory before billing

When the provider reports an upcoming renewal, the workflow prepares a Medusa order and reserves stock. The renewal stores the reservation time. A later success callback triggers verification of the provider's capture and its ownership, amount and currency. The workflow imports the provider's existing capture into the prepared Medusa order.

If a worker stops after the payment import, a retry reads the existing payment collection and order transactions. When the capture matches and its order transaction is missing, the workflow can recreate the transaction from the existing capture. If the amount differs, the cycle can't be verified, or the stock reservation is missing, the newer flow records the payment for review before completing the order or changing the provider's billing state. Medusa's payment flow describes how payment collections relate to orders and captures. Medusa Payment Module guide

Retry an interrupted order event

The local tests covered an upcoming notification arriving after a renewal was complete, a second captured payment for the same cycle, and a missing order transaction after a capture. In another test, the queue accepts the order event and the database write that records dispatch fails. The retry uses the same job ID, and the simulated queue delivers a single event.

When investigating a duplicate renewal, start by comparing the billing cycle, the provider payment IDs, and the linked Medusa order. Then trace the order event. A second order, an extra provider capture, and a repeated queue delivery leave different records and need different fixes. The newer review handling was implemented and tested locally. The rollout notes recorded provider sandbox acceptance as pending at that stage.

The subscription renewal case study covers the wider project. I work on Medusa v2 integrations and payment reconciliation for teams investigating gaps between what the provider captured and what Medusa recorded.