Subscription management system
Subscription renewals added to an existing payment gateway: priced first, then charged, retried and reported to the merchant through signed webhooks.
Millions
active subscriptions
How it was measured
Active subscriptions handled by the billing engine at peak, from the subscription database.
99.9%
uptime
How it was measured
Monthly availability of the billing services from production monitoring during the project.
47
API endpoints for subscriptions, discounts, rules and webhooks
How it was measured
HTTP endpoints counted in the subsystem's merchant and admin controllers. The team built them together; the renewal worker and the discount module are mine.
On this page
Context
The payment gateway from the payment platform case processed card and PayPal payments for subscription e-commerce merchants. This project added renewal billing to the gateway itself.
Merchants register subscriptions, products and renewal rules through the API. The gateway prices each renewal, charges it, retries failed charges and reports every outcome back. I worked on it from the first prototype, as a contractor through Valletta Software.
Problem & constraints
- A renewal must never be priced or charged twice, even with several workers running.
- A worker can stop in the middle of a renewal, and another one has to pick the event up.
- Card and PayPal renewals work differently: PayPal needs a billing agreement, and refunds use different calls.
- Each merchant sets its own timing: when to price, when to charge, how many retries and how far apart.
- Webhooks go to URLs the merchant controls, so the gateway must not be tricked into calling its own network.
Architecture
Scroll the diagram sideways
Key decisions
A renewal queue in the database, with row locks and a lease
- Context
- Renewals must run in parallel across API instances without being processed twice.
- Options considered
- A: a message broker or a job library. B: an events table in SQL Server, claimed with UPDLOCK, ROWLOCK and READPAST, plus a heartbeat lease. The gateway already ran its background jobs this way.
- Trade-offs
- B means polling every 30 s and extra load on the database. In return there is no new infrastructure, and the event state is saved together with the subscription.
- Outcome
- Chose B. Each instance runs up to 5 renewals at a time, and a stalled event is picked up again after 2 minutes.
Price first, then charge
- Context
- The renewal price depends on the current offer, discounts and sales tax, and failed charges are retried.
- Options considered
- A: calculate the price at charge time. B: two phases, each with its own rule window: create a pending payment with discounts and tax, then charge it.
- Trade-offs
- B adds states, tables and a second window. In return the amount is fixed and visible before the charge, the merchant gets a webhook when it is created, and retries reuse the same price.
- Outcome
- Chose B. Applied discounts are stored per item, so every renewal price can be explained later.
Renewals go through the gateway's own transaction API
- Context
- A renewal is a merchant-initiated charge on stored payment details.
- Options considered
- A: call the processor SDKs directly from the billing code. B: send the renewal to the gateway's v2 transaction API as a reference transaction on the last accepted one.
- Trade-offs
- B ties the worker to the transaction manager. In return renewals get the same routing, records and processor handling as any other charge, and cards and PayPal share one code path.
- Outcome
- Chose B. Refunds made directly in the gateway also find their way back to the subscription.
Results
- A renewal worker for three event types: create a pending payment, charge it, refund it.
- Dunning by payment rules set per company or project: attempt limits and days between attempts. After the last failure, auto-renew is turned off and the merchant is notified.
- A discount module I built end to end: fixed and percent discounts with criteria on the renewal count, applied per item and managed on six admin pages.
- US sales tax at renewal through TaxJar, split per item on the charge.
- Cancellation with a full or partial refund, capped at what was charged and processed as retried events.
- Signed webhooks (HMAC-SHA256) with an outbox and retries. I added test webhooks and a guard that blocks private and internal addresses.
What I'd do differently
- I would write tests for the renewal worker from the first prototype. The subsystem had none, and a column added later was missing from the retry path.
- I would make the charge idempotent end to end: an idempotency key on the transaction call, and the payment status saved in the same step as the charge.
- I would merge into the main branch in small slices behind a flag instead of keeping a long subscriptions branch. Many payment fixes had to be made twice.