handling some errors

This commit is contained in:
Gene Takavic 2022-12-15 16:28:17 +01:00
parent 24c678a283
commit 90679f54ee
2 changed files with 21 additions and 6 deletions

View file

@ -171,6 +171,9 @@ async def get_hit(hit_id: str) -> Optional[Hit]:
async def get_hits(cards_ids: Union[str, List[str]]) -> List[Hit]: async def get_hits(cards_ids: Union[str, List[str]]) -> List[Hit]:
if len(cards_ids) == 0:
return []
q = ",".join(["?"] * len(cards_ids)) q = ",".join(["?"] * len(cards_ids))
rows = await db.fetchall( rows = await db.fetchall(
f"SELECT * FROM boltcards.hits WHERE card_id IN ({q})", (*cards_ids,) f"SELECT * FROM boltcards.hits WHERE card_id IN ({q})", (*cards_ids,)
@ -265,6 +268,9 @@ async def get_refund(refund_id: str) -> Optional[Refund]:
async def get_refunds(hits_ids: Union[str, List[str]]) -> List[Refund]: async def get_refunds(hits_ids: Union[str, List[str]]) -> List[Refund]:
if len(hits_ids) == 0:
return []
q = ",".join(["?"] * len(hits_ids)) q = ",".join(["?"] * len(hits_ids))
rows = await db.fetchall( rows = await db.fetchall(
f"SELECT * FROM boltcards.refunds WHERE hit_id IN ({q})", (*hits_ids,) f"SELECT * FROM boltcards.refunds WHERE hit_id IN ({q})", (*hits_ids,)

View file

@ -108,15 +108,24 @@ async def lnurl_callback(
pr: str = Query(None), pr: str = Query(None),
k1: str = Query(None), k1: str = Query(None),
): ):
if not k1:
return {"status": "ERROR", "reason": "Missing K1 token"}
hit = await get_hit(k1) hit = await get_hit(k1)
card = await get_card(hit.card_id)
if not hit: if not hit:
return {"status": "ERROR", "reason": f"LNURL-pay record not found."} return {"status": "ERROR", "reason": "Record not found for this charge (bad k1)"}
if hit.id != k1:
return {"status": "ERROR", "reason": "Bad K1"}
if hit.spent: if hit.spent:
return {"status": "ERROR", "reason": f"Payment already claimed"} return {"status": "ERROR", "reason": "Payment already claimed"}
invoice = bolt11.decode(pr) if not pr:
return {"status": "ERROR", "reason": "Missing payment request"}
try:
invoice = bolt11.decode(pr)
except:
return {"status": "ERROR", "reason": "Failed to decode payment request"}
card = await get_card(hit.card_id)
hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000)) hit = await spend_hit(id=hit.id, amount=int(invoice.amount_msat / 1000))
try: try:
await pay_invoice( await pay_invoice(