Back in 2020 I wrote about blockchain and listed payments without intermediaries as one of the places where it could actually be useful. This year I stopped writing about it and built one.

Bitpaid is an invoicing app for vendors who want to get paid in Bitcoin. A vendor creates an invoice, sends the link to a customer, and watches the status change from “new” to “paid” without refreshing anything. That last part turned out to be the most interesting problem, so most of this post is about it.

Don’t build the payment processor

The first and best decision was not to write any payment logic myself. Watching the chain, generating addresses, tracking confirmations and handling underpaid or expired invoices is a lot of subtle code, and someone has already written it.

BTCPay Server is an open-source, self-hosted payment processor. It creates invoices, monitors payments and exposes a REST API, and no third party ever holds your money. I ran it on an EC2 instance and treated it as the source of truth for everything to do with money.

Bitpaid itself became a fairly thin layer on top:

  • a Next.js client app where vendors log in, create invoices and see their status
  • Amazon Cognito for vendor accounts, with each user linked to their BTCPay store through a custom storeId attribute
  • a Next.js rewrite that proxies requests to BTCPay, so the server address never appears in the browser
  • a small AWS Lambda for the one call that needed some server-side shaping
  • a Node.js notification service that turns BTCPay webhooks into live updates

If you are building anything that touches payments, start with this question: what is the smallest part of this I actually need to own?

Getting updates to the browser

BTCPay can call a webhook whenever an invoice changes. The browser, of course, cannot receive a webhook. Something has to sit in the middle, receive the HTTP call and pass it on to whichever vendor has the app open.

That middle piece is a tiny Express server with Socket.IO:

io.on("connection", (socket) => {
  const { storeid: storeId, accesstoken: accessToken } = socket.handshake.auth;

  verifyAccessToken(accessToken).then(() => {
    // one room per store, so a vendor with three tabs open gets all three updated
    socket.join(storeId);
  });
});

app.post("/notifications", (req, res) => {
  const { storeId } = req.body;
  if (storeId) io.to(storeId).emit("notification", req.body);
  res.status(200).end();
});

Each vendor’s browser connects with their Cognito access token and joins a room named after their store. When BTCPay reports a change, the service emits the event to that room and every open tab hears about it.

Treat the event as a signal, not as data

My first version tried to merge the webhook payload into the state on the client. It worked until it didn’t: events arrive out of order, payloads are partial, and you end up with two sources of truth that slowly drift apart.

The fix was simpler than the bug. The client now treats a notification purely as a hint that something changed and lets React Query fetch the real data again:

socket.on("notification", () => {
  queryClient.invalidateQueries({ queryKey: ["invoices"] });
});

The UI is always exactly what the API says, and the socket only decides when to ask. If a message gets lost, the worst case is a slightly stale list until the next refetch, not a wrong one. I now use this pattern for almost every real-time feature.

Small things that cost a day each

  • Duplicate connections. React’s strict mode mounts components twice in development, and a naive hook opens two sockets. Keep one connection per session and clean it up on unmount.
  • CORS and paths. Socket.IO runs its own handshake over HTTP first. Origin, path and credentials all have to match between client and server, and the errors you get when they don’t are not very helpful.
  • TLS in production. Browsers on an HTTPS page will not connect to a plain ws:// endpoint. Put the service behind a proper certificate before launch day, not on launch day.

What I would do differently

Shipping is a good teacher, and so is reading your own code six months later. Bitpaid is offline now, so these are lessons rather than open issues, but they are the most useful part of this post. Three things I would fix before handling anyone’s money again:

  1. Verify the webhook. BTCPay signs every webhook with an HMAC in the BTCPay-Sig header. My endpoint trusted any POST that contained a storeId. Anyone who found the URL could have sent fake “paid” notifications. They would only have triggered a refetch, thanks to the pattern above, but that is luck, not design. Check the signature and reject everything else.
  2. Check that the store belongs to the user. The socket checked that the token was valid, but not that the storeId in the handshake matched the one on that user’s account. A logged-in user who learned another store’s id could have listened to its updates. The server should read the store from the verified token, never from what the client sends.
  3. Keep secrets off the client. Each vendor’s BTCPay API key lived in a Cognito attribute that the client app could read. It was locked to that vendor’s store, which limits the damage, but a secret the browser can read is not a secret. It belongs behind a server-side call.

None of these are exotic. They are the standard rules: verify what comes in, derive identity on the server, and never send secrets to the browser. They are also exactly the rules that are easy to postpone when you are excited to see the status flip to “paid” for the first time.

Takeaways

  • Let a battle-tested tool own the money. Your code should orchestrate, not reimplement.
  • Webhooks plus WebSockets is a simple and reliable way to get server events into a browser.
  • Use real-time events to trigger refetches, and keep the API as the single source of truth.
  • Authenticate every hop: webhook to service, browser to service, and service to data.

Bitpaid was the first project where I owned everything, from the EC2 boxes and VPN to the animation that plays when a payment confirms. It taught me more about production systems than any course I took to get into this industry.