How to Generate Bitcoin Invoices for Online Store Orders
Bind an order to a unique invoice and a wallet-issued destination. A watch identifier is an observation handle, not proof of payment. Persist the expected amount, network and expiry. The example constructs the local record after address allocation; it does not generate Bitcoin addresses.
Source code
1from dataclasses import dataclass
2import secrets
3
4@dataclass(frozen=True)
5class Invoice:
6 invoice_id: str
7 order_id: str
8 network: str
9 address: str
10 expected_sat: int
11 expires_at: int
12
13def issue(order, net, address, sats, now, ttl):
14 if net not in ("main","test") or not order or not address or type(sats) is not int or sats <= 0 or type(ttl) is not int or ttl <= 0:
15 raise ValueError("invalid invoice")
16 return Invoice(secrets.token_hex(16),order,net,address,sats,now+ttl)
17
18if __name__ == "__main__":
19 a=issue("order-1","test","fixture-only",2000,100,60)
20 assert a.expires_at==160 and a.expected_sat==2000
21 assert a.invoice_id != issue("order-1","test","fixture-only",2000,100,60).invoice_idLocal validation
Run stage24-order-invoices.py with Python 3. The example uses synthetic data and does not execute live wallet operations. Production deployment requires authenticated access, durable storage, trusted chain evidence and integration testing.
Related documentation
https://thamer.org/api
