Side project
Shared Billing & Subscription Server
One recurring-billing server shared by several products. Cards become billing keys in the browser; the server keeps only an encrypted key and moves subscription state from signed, idempotent webhooks.
Process
- Plan
Scoped one shared billing server so no product rebuilds payments; chose not to own a central user table.
- Design
Billing keys, service keys, webhook idempotency, retry and refund policy fixed on paper first.
- Build
API-only Next.js server, three client packages, a key-issuing CLI, tests.
- Ship
Policy rules are covered by node --test; a merge to main deploys to production automatically, with certificates decoded at build and runtime.
- Improve
Fixed a bug where redelivered webhooks extended a period twice, and settled retry and refund rules.
Overview
An API-only server so each product does not rebuild payment-gateway integration, recurring billing, refunds, and webhooks. Products authenticate with a service id and key; subscriptions are keyed by (service, external user id). There is no central user table—that would be SSO under another name.
Why I built it
Integrating payments product by product made it easy for raw card fields to pass through an app server. Products also assembled URLs by hand, called paths that did not exist, and got 404s for months; payment-method lookups returned 401 from an empty token.
Client packages (api, ui, utils) now ship from the same repository as the server, so the contract lives in one place.
System
Next.js API routes, Prisma and MySQL. The schema is the master copy for a database the ops console also reads. A daily scheduled job expires lapsed subscriptions. The database is reached with a client certificate.
Service keys are stored as SHA-256 hashes and compared in constant time. An unknown service and a wrong key return the same 401. A CLI issues, rotates, and disables keys; the plaintext prints once.
See this in the system diagram
Payment flow
The browser turns a card into a billing key with the gateway SDK. The server looks the key up at the gateway and stores it encrypted with AES-256-GCM, keeping only issuer and last four digits for display.
Webhooks are verified against the raw body before parsing; failures get a bare 400. Idempotency has two layers: a unique (provider, event id) row with received and processed tracked separately, so an event that died mid-processing reruns on redelivery; and a conditional update that only moves PENDING or FAILED to PAID, so only the request that changed one row extends the period.
Policy
Failed payments retry every 3 days, up to 3 times, with a 7-day grace period. A full refund marks the payment refunded, schedules cancellation, and revokes pending scheduled payments.
Partial refunds are not applied automatically—there is no amount column, and REFUNDED would read as full. A payment that arrives after cancellation is recorded but does not revive the subscription; it raises a warning for a person. Existing subscribers keep their price when a plan changes.
Stack
Next.js 16, React 19, @portone/server-sdk, Prisma 6, MySQL, TypeScript, node --test, npm workspaces.
Notes
Hard parts: the first payment’s webhook added a free extra billing period because the payment path had already written PAID. Prisma 6’s MySQL connector only takes certificate paths, so certificates are decoded from environment variables to files—at both build and runtime, because the build machine’s /tmp is not the function’s /tmp. Middleware had to be split edge-safe after the bundle passed 1MB.
What held: card data never crosses this server, and redeliveries or concurrent requests extend a period exactly once.