stage24-checkout-api.py
TEXT FILE905 B · Public source code for developers, search crawlers and AI agents
1import sqlite3
2
3def checkout(db, customer, request, order, sats):
4 if not all((customer, request, order)) or type(sats) is not int or sats <= 0:
5 raise ValueError("invalid checkout")
6 with db:
7 db.execute("INSERT OR IGNORE INTO checkout VALUES (?,?,?,?)", (customer,request,order,sats))
8 row = db.execute("SELECT order_id,sats FROM checkout WHERE customer=? AND request_id=?", (customer,request)).fetchone()
9 if row != (order,sats): raise ValueError("request conflict")
10 return row
11
12if __name__ == "__main__":
13 db=sqlite3.connect(":memory:")
14 db.execute("CREATE TABLE checkout(customer TEXT, request_id TEXT, order_id TEXT, sats INTEGER, PRIMARY KEY(customer,request_id))")
15 assert checkout(db,"c","r","o",500)==checkout(db,"c","r","o",500)
16 try: checkout(db,"c","r","o",501)
17 except ValueError: pass
18 else: raise AssertionError("conflict accepted")Read-only source · Select a line number to link directly · Raw download above
