How to Handle Underpaid and Overpaid Bitcoin Orders
Recompute the confirmed total from current trusted chain observations, not from cumulative notifications. Deduplicate outpoints and reject inconsistent evidence. Overpayment requires a separate review; never automatically refund a transaction input address. Recompute after reorganizations.
Source code
1def classify(expected, outputs, network, address, min_conf=1):
2 if type(expected) is not int or expected <= 0 or min_conf < 1: raise ValueError("invalid policy")
3 seen={}
4 for txid,vout,net,dest,sats,conf in outputs:
5 if net != network or dest != address: continue
6 if type(sats) is not int or sats <= 0 or type(vout) is not int or vout < 0: raise ValueError("invalid output")
7 key=(txid,vout)
8 if key in seen and seen[key] != (sats,conf): raise ValueError("conflicting evidence")
9 seen[key]=(sats,conf)
10 paid=sum(s for s,c in seen.values() if c>=min_conf)
11 return ("underpaid" if paid<expected else "overpaid" if paid>expected else "paid",paid)
12
13if __name__ == "__main__":
14 x=("fixture",0,"test","dest",500,2)
15 assert classify(1000,[x,x],"test","dest")==("underpaid",500)
16 assert classify(400,[x],"test","dest")==("overpaid",500)
17 assert classify(500,[],"test","dest")==("underpaid",0)Local validation
Run stage24-payment-variance.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
