How to Accept Bitcoin Payments in an Online Store
Keep prices in integer satoshis and calculate totals from a server-owned catalog. Never accept a browser-supplied price. The resulting order total is not payment evidence. Freeze the catalog version and order total before requesting an invoice.
Source code
1def total(catalog, cart):
2 if not cart: raise ValueError("empty cart")
3 result = 0
4 for sku, qty in cart.items():
5 price = catalog[sku]
6 if type(qty) is not int or type(price) is not int or not 1 <= qty <= 100 or price <= 0:
7 raise ValueError("invalid item")
8 result += price * qty
9 return result
10
11if __name__ == "__main__":
12 assert total({"book": 1200}, {"book": 2}) == 2400
13 try: total({"book": 1200}, {"book": -1})
14 except ValueError: pass
15 else: raise AssertionError("negative quantity accepted")Local validation
Run stage24-store-payments.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
