Introduction
The friction in digital-product sales today is usually the payments layer: Stripe charges 2.9% + 30¢, Paddle takes 5%, Gumroad withholds payouts for days, and regional restrictions block buyers in many countries. Switching to Bitcoin via Lightning reduces fees to fractions of a cent, settles in seconds, and is accessible everywhere with internet access. Cloudflare Workers provide the execution model: single-tenant JavaScript at every POP, outbound HTTPS only required for invoice creation, inbound HTTPS for the landing page. No database cluster, no queue, no warm-up spells.
This stack assumes you already control a domain and can point DNS to Cloudflare. You will generate a Lightning invoice dynamically per purchase, wait for WebSocket confirmation from a watchtower, and then redirect the buyer to a pre-signed Cloudflare R2 URL. The entire flow is stateless: the Worker keeps purchase data in KV, not Postgres.
Lightning Address (LNURL-Pay) Setup
LNURL-Pay is the pragmatic standard for browser-to-Lightning payments. It encodes a user-facing Lightning Address (e.g., sales@yourdomain.com) into a QR code on a product page. The browser scans the QR, hits an HTTPS endpoint that returns a JSON invoice request, the wallet creates an invoice, and the Worker receives a WebSocket push from a watchtower when the invoice is paid.
Create a Worker route at /lnurlp/.well-known/lnurlp/sales. The endpoint must:
- Respond with Content-Type application/json - Include { tag: lnurlp, callback: https://sales.yourdomain.com/lnurlp-callback?p={amount}, minSendable: 1000, maxSendable: 500000000, metadata: [[text/plain, Your Ebook],[text/identifier, ebook_1]]
Set Cloudflare KV namespace LIGHTNING with key invoice:{invoice_id} = {product_sku, buyer_email, status: pending}. The Worker uses the Lightning backend of your choice—CLN, LND, or a custodial provider like Alby—via REST. Pick the backend based on decentralization preference: CLN is self-hosted, LND supports AMP and keysend, Alby gives instant invoice generation but custody of funds.
Worker Code Skeleton
The Worker consists of three handlers: GET / (landing), GET /lnurlp-callback (invoice creation), and POST /webhook (invoice paid). The landing page is static HTML served from Workers Sites or R2; the critical path is the LNURL callback.
import { LRUCache } from 'lru-cache'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
const s3 = new S3Client({ region: 'auto', endpoint: `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com` }); const cache = new LRUCache({ max: 1000 });
export default { async fetch(req, env) { const url = new URL(req.url); if (url.pathname === '/') return new Response(landingHtml(env.BUYER_EMAIL));
if (url.pathname.startsWith('/lnurlp-callback')) { const amount = parseInt(url.searchParams.get('p')); const { pr } = await createInvoice(amount); return new Response(JSON.stringify({ pr, routes: [] }), { headers: { 'Content-Type': 'application/json' } }); }
if (url.pathname === '/webhook') { const body = await req.json(); if (body.payment_hash && body.settled) { const key = `invoice:${body.payment_hash}`; const pending = await env.LIGHTNING.get(key); if (pending) { const { product_sku, buyer_email } = JSON.parse(pending); const url = await getSignedUrl( s3, new GetObjectCommand({ Bucket: 'products', Key: product_sku }), { expiresIn: 86400 } ); await env.QUEUE.send({ email: buyer_email, url }); await env.LIGHTNING.put(key, JSON.stringify({ status: 'delivered' })); } return new Response('OK'); } return new Response('Not settled', { status: 400 }); } } };
Configure the worker with these secrets: LN_BACKEND_URL, LN_API_KEY, R2_ACCOUNT_ID, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY. The webhook endpoint should be restricted to POST only and rate-limited; Cloudflares Durable Objects or a secret header can enforce this.
Product Delivery via R2 Pre-signed URLs
Once an invoice is marked settled by the watchtower, the Worker generates a time-limited S3 pre-signed URL (R2) and enqueues an email.