Case study: E-commerce and payments
Full-Stack E-Commerce Platform, Checkout to Delivery
A payment can't be applied twice, and missed confirmations reconcile on their own.
Storefront, two payment gateways, subscriptions, referrals, four shipping carriers, an ops dashboard and a Claude-powered WhatsApp support agent. Exactly-once webhooks, Redis locks and queues on AWS.
- Where
- Independent build
- Role
- Architect and full-stack engineer
- When
- 2025 to 2026
- Team
- Solo, end to end
- 2 + 4
- payment gateways and shipping carriers behind one order flow
- 1x
- per webhook: signature check, then the event ID claimed in Redis
- 400+
- automated tests, including Playwright end-to-end with axe checks
- TypeScript
- Express
- tRPC
- Next.js
- Expo
- PostgreSQL
- Redis
- AWS SQS
- Terraform
- Claude + MCP
TL;DR: I designed and built a direct-to-consumer e-commerce platform end to end: storefront, cart and checkout on two payment gateways, subscriptions and renewals, a referral program, shipping through four carriers, an operations dashboard, and a Claude-powered WhatsApp support agent. 8 apps in one TypeScript monorepo, run on AWS with 400+ automated tests. Every webhook is processed exactly once, and money-moving actions take a lock.
What problem did this system need to solve?
A small team selling physical products online has to run the same machinery as a large store: take payments reliably, ship through more than one carrier, handle returns and renewals, answer customers quickly, and keep the books straight for tax. Most of the bugs that cost money live in the seams between those systems: a payment webhook that arrives twice, a refund clicked twice, an order whose payment confirmation never arrived.
The goal was one platform where each of those seams is handled by design, not by someone noticing later.
What did I build?
I owned the architecture and wrote the full stack, solo:
- Storefront in Next.js: product pages, cart, checkout, order tracking, a wholesale page, a blog, and account pages for orders, subscriptions and referrals.
- Checkout and payments on two payment gateways, chosen from config, plus subscriptions with renewals and shareable payment links.
- Fulfilment: shipment creation and tracking sync across four carriers, a packing screen, return management and stock levels.
- Growth: a referral program that tracks clicks and add-to-carts, confirms rewards on a schedule and pays out withdrawals after admin review. Products sync to the Facebook catalog.
- Operations dashboard in Next.js: orders, manual orders, packing, returns, stock, support tickets, WhatsApp chats, growth stats and GST sales reports.
- WhatsApp AI support agent built on Claude with MCP tools, so it answers order and product questions from live data instead of guessing. It also transcribes voice notes and can send the catalog in chat.
- API: Express 5 with tRPC, typed end to end with Zod. 22 routers and about 155 procedures, plus webhook routes for payments, shipping and messaging.
- Mobile app in Expo / React Native, sharing types and config with the web through a Turborepo monorepo.
- Infrastructure as code: Terraform for AWS (EC2, SQS, EventBridge Scheduler, Lambda, KMS, CloudWatch), Docker Compose behind Caddy, and Supabase Postgres, Auth and Storage.
Which system design principles hold it together?
Every webhook is processed exactly once
Payment and shipping providers retry webhooks, sometimes several times. Each handler first verifies the signature with a constant-time compare, then claims the event ID in Redis with SET NX and a 24-hour expiry. A retry finds the key already taken and returns early, so a payment is never applied twice.
// Simplified: verify, then claim the event before doing any work.
if (!timingSafeEqual(expectedSignature, receivedSignature)) return res.status(401).end();
const claimed = await redis.set(`webhook:${provider}:${event.id}`, "1", "EX", 86_400, "NX");
if (!claimed) return res.status(200).end(); // already handled: acknowledge and stop
await applyPaymentEvent(event);
Money-moving actions take a lock
Cancelling an order, issuing a refund, withdrawing referral earnings and the daily cron each take a short Redis lock. The lock holds a random token and is released with a compare-and-delete Lua script, so one request can never free a lock that another request now owns.
The server decides the price, and payments get reconciled
Checkout recomputes every price on the server instead of trusting the cart. A delayed job and the daily cron check any order whose payment confirmation never arrived and reconcile it with the gateway, so a dropped webhook does not leave a paid order stuck as unpaid.
Slow work leaves the request path
- Delayed tasks go to an SQS queue drained by a long-polling worker. A message that fails three times moves to a dead-letter queue to be inspected, not retried forever.
- Scheduled work (daily, weekly, monthly and reminder jobs) is triggered by EventBridge Scheduler through a small Lambda, not by an in-process timer that dies on deploy.
- Incoming WhatsApp messages are debounced in Redis, so three quick messages get one considered reply. A version counter cancels a reply that a newer message has made stale.
Limits and graceful degradation
- Rate limiting uses an atomic sliding window in a Redis Lua script, with eight limiters tuned per action. If Redis is down it falls back to in-memory limits.
- Caching is read-through with a TTL. A cache outage slows reads down but never breaks them.
- Outbound calls to the WhatsApp API have per-attempt timeouts and retry with 1 s and 3 s backoff.
Secure by default
API inputs and the environment are validated with Zod. Secrets are encrypted with SOPS and KMS, and gitleaks guards against committing new ones. Procedures are tiered as public, signed-in, employee and admin. One-time links store only a hash of their secret and are consumed with a conditional update.
Ship small, roll back fast
GitHub Actions builds the images, pushes them to a registry and deploys over SSH, one deploy at a time. A separate workflow rolls back to any earlier commit. Sentry, PostHog, CloudWatch alarms and rate-limited Slack alerts cover errors, product analytics and on-call.
What were the results?
| Area | Outcome |
|---|---|
| Payments | Retries and duplicate webhooks cannot double-apply a payment; missed confirmations are reconciled automatically |
| Fulfilment | Four carriers and returns run from one dashboard, with tracking synced back to the customer |
| Support | An AI agent answers from live order and product data on WhatsApp, with a human inbox alongside |
| Scope | 8 apps and 2 shared packages: API, 3 web apps, mobile, internal tools |
| Quality | 400+ automated tests: Playwright end-to-end with axe accessibility checks, API tests, mobile tests |
| Operations | Terraform-defined AWS, one-command rollback, alerts on errors and failed jobs |
What would I do the same way next time?
Treat every external callback as untrusted and repeatable: verify it, claim it, then act. And move anything slow or scheduled onto a queue or a scheduler from day one. Those two habits removed the classes of bugs that hurt payments and fulfilment most.
What would I change?
Add versioned database migrations instead of managing the schema directly in the database console, so every environment can be rebuilt from the repository.