How to Create a Bitcoin Payment QR Payload for a Website
A Bitcoin payment QR should encode a Bitcoin URI rather than a secret or a private key. Keep amounts in integer satoshis until formatting the URI.
Implementation approach
Pass the resulting URI to a locally hosted QR encoder. This example produces the payload only, not a QR image. Supply a previously validated address from your backend. Avoid third-party QR services that can observe payment details.
Source code
1from urllib.parse import urlencode
2
3def payment_uri(validated_address, sats, label):
4 if type(sats) is not int or not 0<sats<=2100000000000000: raise ValueError('amount')
5 if not validated_address.isascii() or not validated_address.isalnum(): raise ValueError('address syntax')
6 whole, frac=divmod(sats,100000000)
7 return 'bitcoin:'+validated_address+'?'+urlencode({'amount':f'{whole}.{frac:08d}','label':label})
8
9if __name__=='__main__':
10 assert payment_uri('tb1qfixture',1234,'Order 7')=='bitcoin:tb1qfixture?amount=0.00001234&label=Order+7'Local validation
Run python3 stage22-payment-uri.py. The assertions use synthetic fixtures, not real Bitcoin addresses or payments. This is a reference example, not a complete production checkout. No SEND operation is included.
Related documentation
https://thamer.org/api
https://thamer.org/learn/bitcoin-ai-agents-advanced-research-guide
