The project
The goal: a creator portfolio, plus a way to sell digital products (files) directly, without a marketplace taking a cut or owning the customer relationship. I built both, as two separate applications in one repository.
- Frontend: a static Next.js creator portfolio and storefront, deployed to Cloudflare Workers with OpenNext, using TanStack Query for server data, Zustand for client state and React Hook Form with Zod for forms.
- Backend: a standalone Hono commerce API on Cloudflare Workers with Drizzle ORM on Neon Postgres, Razorpay for payments, R2 for private files, Cloudflare Queues for payment events and Resend for email.
The two are coupled only through runtime HTTP. There’s no shared package or build dependency, so either side can be redeployed, or replaced, independently.
The challenge
Selling files sounds simple until you list what has to be true:
- A buyer who pays must always receive their file, even if a webhook is delayed, duplicated or fails halfway.
- A buyer who didn’t pay must never receive it.
- Download links can’t be shared indefinitely.
- A buyer who loses their email needs a way back without contacting support.
The architecture
The API is organised in layers, so the payment and delivery rules can be tested without HTTP or a database:
presentation/ Hono routes (/v1/products, /v1/orders, /v1/webhooks/razorpay, /v1/download, /v1/admin/*),
admin auth, validation, error handler, dependency injection via context
application/ use cases: CreateOrder, EnqueuePaymentEvent, ConfirmPayment, DownloadAsset,
RequestFreshLink, VerifyFreshLink, SweepUnfulfilledOrders; ports for payment, queue, email, storage
domain/ Order, DownloadToken, Money and status value objects; domain errors
infrastructure/ Drizzle + Neon repositories, Razorpay adapter, R2 storage, Resend email, queue producer/consumer
The payment flow
- Create order. The API creates a pending order and a Razorpay order, with the amount computed server-side.
- Receive the webhook. Razorpay calls
POST /v1/webhooks/razorpay. The route reads the raw body and thex-razorpay-signatureheader and passes both to a use case. - Verify and enqueue. The use case checks the HMAC-SHA256 signature with a constant-time comparison, ignores events other than
payment.captured, looks up the order and enqueues a payment message. The webhook returns immediately. - Confirm idempotently. A queue consumer loads the order; if it’s already paid, it stops. Otherwise it marks it paid, revokes any existing tokens, issues a new download token and emails the link.
- Reconcile on a schedule. A cron trigger every six hours finds paid orders with no valid token and fulfils them, covering anything the queue path missed.
Download security
Files never leave private R2 storage directly. A download token is valid for 24 hours and five downloads; each download issues a short-lived signed URL. If a link expires, the buyer requests a fresh one, proves ownership with a verification code sent to their email (valid 15 minutes), and is limited to three requests per hour.
Engineering review: what I’d tighten
Writing this up meant re-reading the code critically. Two improvements I’d make before calling it finished:
- Retry failed queue messages explicitly. The consumer acknowledges messages that succeed and logs failures, but doesn’t call
retry()on them. Cloudflare Queues treats un-acked messages in a batch that completes successfully as delivered, so failed confirmations currently rely on the six-hourly sweep rather than the queue’s retries and dead-letter queue. - Make “mark paid” a conditional update. The idempotency check (
isPaid()) and the write are separate steps. Two concurrent deliveries of the same event could both pass the check. A singleUPDATE … WHERE status = 'pending' RETURNING …closes that gap.
Both are covered in the article linked below, along with the general pattern.