Create, preview, download, and email professional invoices in seconds.
Cloudflare Worker (static UI + /api/* backend) + D1 (database), with
server-side email via Resend.
Status
| Phase | State |
|---|---|
| Core generator (form → live preview → PDF) | ✅ done |
| D1 schema + API (save/list/open/delete invoices) | ✅ done |
| Magic-link auth (passwordless, via Resend) | ✅ done |
| OAuth SSO (Google/GitHub/Microsoft, via auth broker) | ✅ done |
| Per-user business profile, defaults & logo | ✅ done |
| Email invoice to client (server-side Resend) | ✅ done |
Deploy to Cloudflare (invoicer.aswincloud.com) |
⛔ needs CF auth |
Run locally
The pure front-end generator needs no backend — any static server works:
python3 -m http.server 8099 --directory public
# open http://127.0.0.1:8099
For the full app (auth, save, email) run the Worker locally with Wrangler,
which serves ./public as static assets and handles /api/*:
npm install
npm run db:migrate:local # apply migrations to the local D1
npm run dev # wrangler dev (reads wrangler.toml)
Local secrets go in .dev.vars (git-ignored) — see Configuration below.
Features
- Form with live preview — every keystroke re-renders the invoice.
- Dynamic line items — add/remove rows, auto qty × rate.
- Tax modes — CGST+SGST split, single tax (IGST/VAT), or none.
- Discount, multi-currency (₹ uses Indian digit grouping), status badge.
- Download PDF via the browser print engine (client-side, offline-capable).
- Sign in passwordless (magic link) or via Google / GitHub / Microsoft — both resolve to one account per email.
- Business profile & invoice defaults persist in
localStorageand, once signed in, sync to your account (D1) so they follow you across devices. - Business logo — upload once, downscaled to a data-URL, shown atop the invoice (preview + PDF).
- My Invoices dashboard — save, reopen to edit, re-download, email, delete.
- Editing edits. Reopening an invoice and saving updates it rather than creating another; a paid invoice is locked, and invoice numbers are unique per account. Cancel one with the VOID status — the record and its number survive, and a replacement may reuse the number.
- Email invoice to client server-side via Resend.
- Shareable pay link — one URL the client opens to review the invoice and pay it with Razorpay; the invoice marks itself PAID when the money lands.
Architecture
scripts/ one-off maintenance (dedupe-invoices.mjs)
src/ Cloudflare Worker
index.js router: static assets via ASSETS binding + /api/* backend
lib.js JSON/cookie/HMAC helpers, Resend email
invoice-html.js server-side invoice HTML + totals (email, PDF and pay page)
oauth-routes.js OAuth SSO via the central broker (@aswincloud/auth)
razorpay.js Razorpay REST client + the two signature checks
pay.js /i/<token> public page, order/verify, order.paid webhook
public/ static site (served by the Worker's ASSETS binding)
index.html form + preview shell + modals
styles.css screen + @media print styles
js/ the client, ten classic scripts loaded in order (see js/core.js):
core, money, render, assets, profile, init, export, receipt, backend, modals
migrations/ D1 SQL migrations
wrangler.toml Worker config: main, [assets], D1 binding, vars
The Worker (main = "src/index.js") handles any /api/* path and delegates
everything else to the static [assets] binding.
Configuration
Non-secret vars live in wrangler.toml ([vars]): APP_NAME,
RESEND_FROM_EMAIL, APP_BASE_URL.
Secrets are never committed. Locally, put them in .dev.vars; in
production, set them with wrangler secret put:
| Secret | Purpose |
|---|---|
RESEND_API_KEY |
Send magic-link & invoice emails via Resend |
SESSION_SECRET |
HMAC key for the session cookie (falls back to AUTH_SIGNING_KEY) |
AUTH_BROKER_URL, RELAY_SECRET |
Enable OAuth SSO via the auth broker |
PRINT_RELAY_SECRET |
Signs print jobs sent to the thermal-printer relay |
RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET |
Razorpay API key pair, for pay links |
RAZORPAY_WEBHOOK_SECRET |
A different string — signs the webhook body |
WA_PHONE_NUMBER_ID, WA_ACCESS_TOKEN |
Meta Cloud API: the WhatsApp number messages go out from, and a permanent System User token. Use the number Chatwoot answers on, or replies land with the wrong bot |
WA_TEMPLATE_CONFIRMED, WA_TEMPLATE_SHIPPED, WA_TEMPLATE_DELIVERED |
Optional; default to the order_confirmed_new / order_shipped_link / order_delivered_new templates on the WABA |
WA_SHIPPED_BUTTON_BASE |
Optional; the fixed URL prefix of order_shipped_link's button, default https://shiptrack.aswincloud.com/track/. The message sends the rest of the tracking link (<carrier>/<awb>) as the button's dynamic suffix |
INVOICER_CHAT_SECRET |
Shared with the support bot; signs POST /api/chat/shipments |
Magic link works with just RESEND_API_KEY + a session key. SSO buttons only
appear when the broker trio (AUTH_BROKER_URL + RELAY_SECRET +
SESSION_SECRET) is configured.
Deploy
Requires Cloudflare auth (wrangler login or CLOUDFLARE_API_TOKEN):
wrangler d1 create invoicer-db # paste database_id into wrangler.toml
npm run db:migrate:remote
wrangler secret put RESEND_API_KEY
wrangler secret put SESSION_SECRET
# for SSO, also:
wrangler secret put AUTH_BROKER_URL
wrangler secret put RELAY_SECRET
# to print receipts on the thermal printer, also:
wrangler secret put PRINT_RELAY_SECRET
# for shareable pay links, also:
wrangler secret put RAZORPAY_KEY_ID
wrangler secret put RAZORPAY_KEY_SECRET
wrangler secret put RAZORPAY_WEBHOOK_SECRET
npm run deploy # the FIRST deploy only — see Deploying
Deploying
Pushing to master deploys: the repo is connected to Cloudflare Workers
Builds, which runs wrangler deploy on every push and shows up as the
"Workers Builds: invoicer" check on the commit. Do not deploy by hand from a
laptop — twice a deploy from a stale working tree briefly shipped the wrong
code. npm run deploy exists for the very first deploy of a fresh account and
for an emergency when Workers Builds is down, nothing else.
Two things Workers Builds does not do, so they stay manual and happen before
the PR merges: npm run db:migrate:remote for any new migration, and
wrangler secret put for any new secret.
Tests
npm test runs every file in test/ as its own process (test/run.mjs). The
same command runs in GitHub Actions on every pull request and in the merge
queue, and the "Tests" check is required, so a PR with a failing suite cannot
merge. Do not run the suites with node --test: they use their own assert
helper, and node --test reports them green even when assertions fail.
Shareable pay links
Copy link on a saved invoice gives you a URL like
https://invoicer.aswincloud.com/i/<32-hex>. The client opens it, reviews the
items, and pays with Razorpay Checkout without leaving the page or creating an
account. The Email button includes the same link as a Pay button.
Two rules the code is built around:
- The amount is computed server-side from
computeTotals()— the same call that renders the total the client is reading. Nothing the browser sends is used, so the charge cannot disagree with the page. - Only the webhook marks an invoice PAID. The checkout callback is
signature-verified and updates the UI, but Razorpay's server-to-server
order.paidis the sole authority on status.
The token is the credential — anyone holding the link can view and pay the invoice, which is the point. It is minted on first share (never at save time, so an invoice that is never shared has no link) and is unrelated to the invoice id.
After setting the secrets, add a webhook in the Razorpay dashboard pointing at
https://invoicer.aswincloud.com/api/webhook/razorpay for order.paid (and
payment.failed), using the value you set as RAZORPAY_WEBHOOK_SECRET. This is
a second webhook alongside the shop's; Razorpay supports several, each with
its own secret, and the shop's is unaffected. Invoicer receives the shop's
events too and ignores any order it does not own.
If the webhook never arrives, the money moved and nothing here knows. The
pay-me form (/pay) creates its invoice only from order.paid, so a webhook
that is not configured for this Worker, signed with a different secret, or given
up on by Razorpay leaves a customer who paid, saw "payment received", and got no
receipt, no email and no WhatsApp — with no row to show for it. That happened on
2026-09-26. Check missed payments in My Invoices (POST /api/paylink/reconcile, owner only) asks Razorpay for its recent orders and, for
every paid pay-link order with no invoice, does exactly what the webhook would
have: raises the PAID invoice, emails the receipt, sends the WhatsApp. Idempotent
on the Razorpay order id, so pressing it twice raises nothing twice.
Set PAY_ENABLED = "false" to stop taking payments — links still open and
invoices still render, only the Pay button goes away. Razorpay settles in INR,
so the button appears on ₹ invoices only.
Printing to the thermal printer
Two separate buttons, so neither can surprise you:
- POS receipt — always downloads the 57mm PDF, signed in or not.
- Print receipt — sends it to the thermal printer. Only shown when signed
in, and only works for an address in
PRINT_ALLOWED_EMAILS(defaults toINVOICE_OWNER_EMAIL). Signing in is not authorisation to print: anyone can create an account, so the allowlist is what gates the printer.
The button waits for the paper to actually come out, so "Printed ✓" means printed rather than queued. If it fails for any reason it downloads the PDF and says why — a printer that's off or out of paper costs a download, not the receipt.
The printer is Bluetooth, so the Worker cannot reach it. POST /api/print
HMAC-signs the PDF and forwards it to a relay running on the printer's LAN
(tejprint-relay.py, exposed at PRINT_RELAY_URL via a cloudflared tunnel),
which hands it to an ESP32-C3 BLE bridge. PRINT_RELAY_SECRET must match on
both ends. Set PRINT_ENABLED = "false" to turn the whole path off.
WhatsApp messages and shipments
Unpaid invoices go out as a payment request. Send invoice on an unpaid ₹
invoice sends invoice_pay_online (UTILITY, 2026-09-29): the invoice PDF and
one Pay online button (/i/<token>, Razorpay Checkout — card, UPI apps, net
banking), so every payment it leads to is confirmed by the webhook. Recorded in
wa_request_*, apart from wa_message_id, so the paid confirmation still goes
out automatically when the invoice is settled. It replaced
invoice_payment_request, whose second button, Pay by UPI, paid the owner's
own UPI ID directly: nothing can observe that transfer, so the customer could
never be told it succeeded. /u/<token> below still serves links already sent.
UPI QR on the invoice page. An unpaid invoice's page (/i/<token>) has
"Or pay by scanning a UPI QR" under the Pay button. Pressing it mints a Razorpay
single-use QR fixed to the exact amount (POST /api/pay/<token>/qr), shows
Razorpay's image of it, and polls GET /api/pay/<token>/status so the page turns
to Paid when the qr_code.credited webhook lands. Minted on request, never
on page load; reused while the amount matches and it has 15+ minutes left; a
changed total closes the old one first. Needs QR Codes enabled on the Razorpay
account and the webhook subscribed to qr_code.credited. Independent of
UPI_QR_ENABLED, which only governs /u/<token>.
/u/<token> hands the phone a upi://pay link — Android opens its UPI app
chooser at once, iOS gets Google Pay / PhonePe / Paytm buttons, and a QR is shown
for everything else. The link is, in order of preference:
- a Razorpay single-use UPI QR fixed to the invoice amount (
rzp_qr_*, migration 0021), minted on first open and reused while the amount matches and it has more than 30 minutes left. Payment arrives as theqr_code.creditedwebhook, which marks the invoice PAID (paid_via='upi_qr') and sends the receipts. Subscribe the Razorpay webhook toqr_code.creditedand have QR Codes enabled on the account, or this step is skipped; - the business's own UPI ID (
upi_vpa) with the amount and invoice number filled in. Nothing can observe a direct transfer, so these are marked paid by hand.UPI_QR_ENABLED = "false"forces this mode.
A card payment closes the invoice's QR, and a UPI credit on an already-paid invoice is not applied: the owner is emailed "Paid twice — refund one".
Shop orders are messaged automatically. Every paid order at
3d-prints.aswincloud.com already arrives here as an invoice (POST /api/ingest/order); the ingest now stores the customer's mobile and, when
WA_PHONE_NUMBER_ID/WA_ACCESS_TOKEN are set, sends order_confirmed_new with
the invoice PDF the moment the invoice is raised. Marking that order shipped or
delivered in the shop dashboard calls POST /api/ingest/shipment — the same
HMAC-over-raw-body + timestamp door as ingest (verifyShopRequest in
src/ingest.js) — which records the shipment and sends order_shipped_link or
order_delivered_new. Scoped to the owner's source='shop' invoice for that
receipt, so a guessed order number reaches nothing else; each kind is sent at
most once per invoice, so a tracking-number correction never re-notifies. The
shop's courier is free text: a ShipTrack carrier gets a live Track button,
anything else is named as typed and the button opens ShipTrack's unknown-carrier
page (other/<awb>) rather than a broken link — a Meta URL button's prefix is
fixed on the template, so it can only ever open ShipTrack.
Pay-link payments get a receipt, not an order confirmation. A /pay
payment is often not an order at all — consulting, a website, a repair — so
"your order has been confirmed, shipping news will follow" would be wrong. When
the webhook raises the PAID invoice, notifyPaid sends payment_received
(UTILITY, created 2026-09-26 on the WABA over the API): the receipt PDF as the
DOCUMENT header, and "Hi {{1}}, we've received your payment of {{2}} for
{{3}}. Your receipt {{4}} is attached. Thank you for choosing {{5}} — we
appreciate your business." Which template a PAID invoice gets is decided by
templateKindFor() in src/wa.js from where the invoice came from, and the
webhook, the shop ingest and the dashboard's WhatsApp button all go through it,
so they cannot disagree. WA_TEMPLATE_RECEIPT renames it. One shared
sendPaidConfirmation() (src/ingest.js) does the sending for both paths.
/pay bills as PAYLINK_BUSINESS (by business name; AswinCloud in
production) rather than the account's default business, so the receipt PDF's
header and the message's {{5}} say AswinCloud while the shop keeps billing as
Aswin3DPrints. The owner's "payment received" email carries everything needed
to act on it — name, mobile, email, what it was for, delivery address, amount,
receipt number, payment reference, and a link to the receipt.
For a customer who paid directly there is no shop order anywhere: the invoice
is the order record. It carries the customer's phone, and from 0019 the
courier and tracking number too.
The WhatsApp ▾ button on a paid invoice is a menu of three messages. Each opens a modal with the recipient and the exact text the customer will read, and nothing is sent until you confirm:
| Item | Template | Params | Also |
|---|---|---|---|
| Send invoice | order_confirmed_new |
customer, order no., business | DOCUMENT header: the invoice PDF, fetched by Meta from /i/<token>.pdf |
| Share tracking | order_shipped_link |
customer, order no., business, courier, tracking id + URL button → ShipTrack | records courier + tracking on the invoice before sending |
| Send delivered | order_delivered_new |
customer, order no., business | stamps delivered_at |
Preview and send build the same template params from the same code
(src/wa.js, src/shipment.js), so the two cannot disagree. The preview text
is a copy of each template's wording; if a template is edited in Meta Business
Manager, update TEMPLATE_TEXT in src/shipment.js too. All three are
business-initiated messages and must use Meta-approved templates; until a
template is approved a send fails with Meta's own message in the modal.
order_shipped_link's button is a dynamic URL button whose fixed prefix is
https://shiptrack.aswincloud.com/track/ (the message supplies <carrier>/<awb>).
If it is ever recreated with a different prefix, set WA_SHIPPED_BUTTON_BASE. Paid invoices
stay edit-locked; POST /api/invoices/:id/whatsapp is the one deliberate way
through that lock, for shipment fields only.
Delivered is detected automatically. A cron ([triggers] in
wrangler.toml, every 30 min) asks ShipTrack's public API about every parcel
shipped but not delivered, records what it said in track_status, and when one
has arrived stamps the invoice and sends order_delivered_new once (guarded by
wa_delivered_at). A courier site being down costs nothing but a retry.
POST /api/chat/shipments answers the support bot's "where is my parcel?"
on WhatsApp. HMAC-SHA256 over the raw body with INVOICER_CHAT_SECRET, a
timestamp within ±5 min, and the answer is scoped to the account in
INVOICE_OWNER_EMAIL — read from config, never the request — so however it is
called it cannot return another user's invoices. The customer is selected by the
phone Meta verified as the WhatsApp sender.
Migration note: d1 migrations apply --remote failed in Sept 2026 because
0015–0018 had been applied by hand and never recorded in d1_migrations.
They were recorded (metadata only, every column verified present first) and
0019 applied normally. If it happens again, that is the fix — not re-running
the SQL.
Notes
- Secrets live server-side only — never in the browser.
- Resend requires a
User-Agentheader on API calls, or it returns403 / 1010.