DEV Community

Cover image for How to deliver digital downloads automatically after BTCPay invoices with Netlify Functions
SecondWindMC
SecondWindMC

Posted on

How to deliver digital downloads automatically after BTCPay invoices with Netlify Functions

Selling a digital download through BTCPay is easy right up to the delivery step. BTCPay can create the invoice, but it does not inherently know how to release a private zip after payment.

I needed this for a small static store. The constraints were:

  • static product pages;
  • BTCPay Point of Sale invoices;
  • no customer accounts;
  • no email-required delivery;
  • no database;
  • no trusting a browser redirect as proof of payment;
  • automatic delivery after confirmation.

I packaged the result as an open-source Netlify template:

btcpay-digital-fulfillment

The architecture

static HTML form
  -> BTCPay POS invoice
  -> buyer pays
  -> BTCPay redirect template with {InvoiceId}
  -> Netlify Function checks Greenfield invoice status
  -> zip download only when status is Settled
Enter fullscreen mode Exit fullscreen mode

The important rule is that the redirect is only a hint. The function independently asks BTCPay for the invoice before releasing anything.

Invoice to product mapping

The POS item key / choiceKey becomes invoice.metadata.itemCode. The template maps that value to product metadata in products.config.json:

{
  "example": {
    "name": "Example Product",
    "zip": "example-product.zip",
    "filename": "example-product.zip"
  }
}
Enter fullscreen mode Exit fullscreen mode

A build script converts products/*.zip into base64 modules embedded in the function bundle. This is not a good design for huge video files, but it works well for small-to-medium digital downloads and keeps the system database-free.

Verification is the important part

The Netlify Function validates the invoice ID shape, then fetches the invoice from BTCPay with a least-privilege API key:

const r = await fetch(
  `${host}/api/v1/stores/${storeId}/invoices/${invoiceId}`,
  { headers: { authorization: `token ${apiKey}` } }
);
invoice = await r.json();
Enter fullscreen mode Exit fullscreen mode

Only invoice.status === 'Settled' releases the product. New, Processing, and Paid get an auto-refreshing waiting page. Expired, invalid, unknown-product, and incomplete invoices do not receive the zip.

That sounds obvious, but it is the difference between payment verification and simply hiding a download link.

The serverless size-limit trap

The first implementation returned the entire zip as a base64 function response. That worked for the small Minecraft plugin zips, but the sound-effect pack was slightly over the practical serverless response ceiling after base64 encoding.

The fix is not to expose a public zip URL. Instead, the function serves a small downloader page for large products. That page fetches several verified chunks and reassembles them locally in the browser:

for (let i = 0; i < parts; i++) {
  const r = await fetch(
    location.pathname +
      '?invoice=' + encodeURIComponent(invoice) +
      '&part=' + i
  );
  pieces.push(await r.text());
}
Enter fullscreen mode Exit fullscreen mode

Every chunk request repeats the same server-side settled-invoice verification. The buyer still only needs the receipt link, but a person who guesses a public file URL cannot bypass payment because there is no public file URL.

Why not just use a paid commerce platform?

For many sellers, a hosted commerce platform is the correct answer. This template is for a narrower case:

  • you already want BTCPay;
  • you want static storefront pages;
  • you do not want a database or shop backend;
  • you are comfortable deploying one Netlify Function;
  • you want simple automatic fulfillment for a small catalog.

It is not a replacement for tax handling, customer accounts, license keys, or a full commerce platform. It is just the missing payment-to-download pipe.

Repository

The MIT-licensed template, setup instructions, security notes, and example product are here:

SecondWindMC/btcpay-digital-fulfillment

I am using the same architecture live at secondwindmc.github.io for automatic Bitcoin checkout delivery. If you use it, test with one real small payment before relying on it: invoice creation, redirect, settlement, download bytes, expired-invoice refusal, and large-file chunking should all be verified against your own BTCPay instance.

Top comments (0)