Most crypto checkout examples assume a cart. The customer clicks Pay, sends the money within a few minutes, and the order ships. Invoices behave differently. The link goes out in an email, sits in someone's inbox for a week, gets forwarded to a finance person, and is paid from an exchange account on the last day of the month. Sometimes two invoices for the same client are open at once.
I work on Coldtill, a self-hosted crypto payment gateway. Its payment API was built with shop checkouts in mind, but the same handful of fields cover invoices well once you know which defaults to change. This is how I'd wire it into a billing system that sends monthly invoices, in Python.
The fields that matter for an invoice
Creating a payment is one call, POST /api/v1/payment, with an API-Key header. Only two fields are required, customerID and amountInUSD. For invoices, most of the optional ones earn their place:
| Field | What it's for here |
|---|---|
customerID | Who is paying. More on this below, because it's the one that bites |
amountInUSD | The invoice total, sent as a decimal string |
invoiceID | Our invoice number. It's echoed back in every webhook |
customerEmail | The client's billing address, stored with the customer |
expire | The due date, as an RFC 3339 timestamp at least a minute ahead |
network, currency | Pin the payment to what the client agreed to pay with, e.g. BASE and USDC |
returnURL | Where the button on the paid checkout goes: back to the invoice page |
The response gives you a reference_id and a url. The URL is the hosted checkout, and that's the link that goes in the invoice email.
The one-open-payment rule
This is the part to design around before you write any code.
A customer in Coldtill has at most one open payment. Creating a new payment for the same customerID closes the previous one with the status REPLACED. For a shop that's exactly right: a customer who abandons a cart and starts again shouldn't have two live checkouts. If the previous payment has a deposit that's still confirming, the new request is refused with 409 instead, so money that's already on its way doesn't end up attached to a dead payment.
For invoices it means this: if you use the client's account ID as customerID, issuing invoice #142 silently closes the link for invoice #141. If #141 was still unpaid, its link stops working and the next webhook you get for it says REPLACED.
There are two ways round it:
- One open invoice per client. Roll everything outstanding into a single statement and create one payment for it. Fine if your billing already works that way, awkward if clients expect one invoice per job.
- One
customerIDper invoice. Use something likeacme-ltd:INV-2026-0142. Each invoice is then its own customer as far as the gateway is concerned, with its own deposit addresses, so issuing a new invoice never touches an old one.
I'd go with the second. The trade-off is that the same ID always gets the same deposit addresses, so a client paying several invoices gets a different address for each and can't save one to reuse. For invoices that turned out to be a feature: one address per invoice makes reconciliation trivial, and if a client pays an old invoice's address by mistake, you can tell exactly which invoice they were looking at.
Turning a due date into expire
By default a payment expires after the project's payment expiry, which is 24 hours unless you've changed it. That's sensible for a cart and far too short for net-30 terms. An unpaid payment that passes its expiry becomes CANCELLED.
So set expire from the invoice's due date, at the end of the day in the client's time zone:
from datetime import date, datetime, time from zoneinfo import ZoneInfo def expiry_for(due: date, tz: str) -> str: """End of the due date in the client's time zone, as RFC 3339.""" end_of_day = datetime.combine(due, time(23, 59), tzinfo=ZoneInfo(tz)) return end_of_day.isoformat() # e.g. 2026-12-31T23:59:00-05:00 The value has to be at least a minute in the future, which only matters if you generate links at the last moment.
Pinning the network and coin
Left alone, the hosted checkout lets the payer pick a network and a currency. That's friendly for shoppers. For a client whose finance team has agreed to pay "USDC on Base", it's one more chance to send the right coin on the wrong network.
Passing network and currency restricts the checkout to that one option. The network codes are ETH, BASE, POLYGON, BSC, TRON, SOL and BTC. On a testnet install the same codes map to the test networks, so BASE is Base Sepolia, which keeps staging and production code identical.
Before pinning, check that the network and token are actually enabled on your install. GET /api/v1/ticker needs no API key and returns an array with one entry per enabled token:
import os import requests BASE_URL = os.environ["COLDTILL_URL"] # e.g. https://pay.example.com API_KEY = os.environ["COLDTILL_API_KEY"] # a payments-only key is enough def is_enabled(network: str, currency: str) -> bool: r = requests.get(f"{BASE_URL}/api/v1/ticker", timeout=10) r.raise_for_status() return any(t["blockchainCode"] == network and t["currencyCode"] == currency for t in r.json()) Run it when someone saves a client's payment preference, not on every invoice.
Creating the payment
Putting it together:
class DepositStillConfirming(Exception): pass def create_invoice_payment(inv) -> str: body = { "customerID": f"{inv.client_slug}:{inv.number}", "customerEmail": inv.billing_email, "invoiceID": inv.number, "amountInUSD": f"{inv.total_usd:.2f}", "expire": expiry_for(inv.due_date, inv.client_tz), "returnURL": f"https://billing.example.com/i/{inv.public_token}", } if inv.pay_with: # e.g. ("BASE", "USDC") body["network"], body["currency"] = inv.pay_with r = requests.post(f"{BASE_URL}/api/v1/payment", json=body, headers={"API-Key": API_KEY}, timeout=20) if r.status_code == 409: raise DepositStillConfirming(inv.number) r.raise_for_status() p = r.json() db.invoices.update(inv.id, coldtill_ref=p["reference_id"], pay_url=p["url"]) return p["url"] A few notes:
- Payments-only keys. A key is either payments-only or full access, chosen when you create it. Billing code that only creates and reads payments doesn't need the ability to send payouts.
- Bulk runs. The API allows 600 requests a minute per client IP and answers
429beyond that. A month-end run of a few hundred invoices fits, but back off on429anyway. - The
400s are useful. AreturnURLthat isn'thttps://, or uses a host the project doesn't allow, is refused with400and an{"error": ...}body. Log that body. It tells you what's wrong.
The webhook side
Payments report back through signed webhooks. Verify the signature on the raw body exactly as the webhook docs show, then act on the signed status, never on the event header:
from decimal import Decimal from flask import Flask, request, abort app = Flask(__name__) SECRET = os.environ["COLDTILL_WEBHOOK_SECRET"].encode() @app.post("/hooks/coldtill") def coldtill_hook(): raw = request.get_data() # bytes, before any JSON parsing if not verify(request.headers, raw, SECRET): # HMAC check from the docs abort(401) ev = request.get_json() inv = db.invoices.by_number(ev["invoice_id"]) if inv is None or inv.coldtill_ref != ev["reference_id"]: return "", 200 # not ours, or an older payment status = ev["status"] filled = Decimal(ev["filled_amount_in_usd"]) if status in ("FILLED", "OVER_FILLED"): db.invoices.mark_paid(inv.id, txs=ev["payment_info"]) # idempotent elif status == "PARTIALLY_FILLED": db.invoices.note(inv.id, f"part-paid: ${filled}") elif status == "CANCELLED" and filled > 0: # closed with money on it: short, or arrived after the link expired db.invoices.flag(inv.id, f"closed with ${filled} received, check by hand") elif status == "CANCELLED": db.invoices.mark_link_expired(inv.id) return "", 200 Three statuses deserve a word in an invoicing context:
-
PARTIALLY_FILLEDmeans the confirmed amount is below 99% of what's owed. With invoices this is almost always an exchange withdrawal fee taken out of the amount the client typed in. The checkout tells them to send the exact amount, but it's worth saying the same on the invoice itself. The payment stays open, so they can send the difference to the same address. -
FILLEDcovers anything within 1% of the total, andOVER_FILLEDanything more than 1% above it. Both mean paid. If you want to return an overpayment, that's a payout from the hot wallet, which is a separate, approved flow. - Late money. If a client pays after the link has expired, the deposit still counts toward the filled amount, but the payment doesn't reopen; you get a progress event with the new
filled_amountwhile the status staysCANCELLED. That's why the handler flags a cancelled payment with money on it rather than marking anything paid. Someone should look at it, then either mark the invoice paid or send the rest of the balance as a new payment.
payment_info in each event carries the transaction hash, network and currency of every deposit. Store it with the invoice. When a client's accounts team asks "which transaction was that?", it's already there.
Re-issuing an expired link
When a client misses the due date and asks for a fresh link, create a new payment with the same customerID and invoiceID and a new expire. The old one is already CANCELLED, so there's no open payment to replace. Update the stored reference_id and link on the invoice, so the webhook check above follows the new payment.
The short version
- One
customerIDper invoice, so invoices never close each other. -
expirefrom the due date, not the 24-hour default. - Pin the network and coin whenever the client has agreed one.
- Treat payments that arrive after the link closed as a human decision.
None of this needed anything beyond the payment API and webhooks, but the defaults are cart defaults. Change them on purpose.

