SubscriptionsSenior Backend Engineerteam: 6 engineersclient: Payment gateway, subscription billing

Subscription management system

Subscription renewals added to an existing payment gateway: priced first, then charged, retried and reported to the merchant through signed webhooks.

Jun 2025Nov 2025

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
  1. 01Context
  2. 02Problem & constraints
  3. 03Architecture
  4. 04Key decisions
  5. 05Results
  6. 06What I'd do differently
  7. 07Stack
01

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.

02

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.
03

Architecture

Scroll the diagram sideways

fig. 01: Renewal flow. A scheduler opens rule windows, workers claim events with row locks, each renewal is priced and then charged through the gateway, and every outcome goes to the merchant as a signed webhook.From Merchant backend to Subscription API: create, update, cancel. From Subscription API to Event queue: a cancellation creates a refund event. From Event scheduler to Event queue: pending events when a rule window opens. From Event queue to Renewal worker: claim with UPDLOCK and READPAST. From Renewal worker to Pricing: a pending payment with discounts and tax. From Renewal worker to Transaction API v2: charge or refund as a reference transaction. From Transaction API v2 to CyberSource. From Transaction API v2 to PayPal. From Renewal worker to Webhook outbox: every outcome. From Webhook outbox to Merchant backend, as signed webhooksMerchant backendAPI clientSubscription APIsubscriptions · rulesWebhook outboxHMAC-SHA256 · retriesEvent scheduleropens rule windowsEvent queueSQL table · row locksRenewal worker≤5 parallel · 60 s leasePricingdiscounts · sales taxTransaction API v2gateway coreCyberSourcePayPalsigned webhookspricecharge
fig. 01Renewal flow. A scheduler opens rule windows, workers claim events with row locks, each renewal is priced and then charged through the gateway, and every outcome goes to the merchant as a signed webhook.
04

Key decisions

DR-01

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.
DR-02

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.
DR-03

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.
05

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.
06

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.
07

Stack

  • C#
  • .NET 9
  • ASP.NET Core
  • SQL Server
  • T-SQL
  • Dapper
  • EF Core
  • Razor
  • TaxJar
  • CyberSource
  • PayPal
  • Webhooks