diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 7d9f2a5bc..bed281110 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -452,18 +452,27 @@ async def update_payment_details( async def update_payment_extra( - payment_hash: str, + hash: str, extra: dict, + outgoing: bool = False, + incoming: bool = False, conn: Optional[Connection] = None, ) -> None: """ Only update the `extra` field for the payment. Old values in the `extra` JSON object will be kept unless the new `extra` overwrites them. """ + amount_clause = "" + + if outgoing != incoming: + if outgoing: + amount_clause = "AND amount < 0" + else: + amount_clause = "AND amount > 0" row = await (conn or db).fetchone( - "SELECT hash, extra from apipayments WHERE hash = ?", - (payment_hash,), + f"SELECT hash, extra from apipayments WHERE hash = ? {amount_clause}", + (hash,), ) if not row: return @@ -471,11 +480,8 @@ async def update_payment_extra( db_extra.update(extra) await (conn or db).execute( - """ - UPDATE apipayments SET extra = ? - WHERE hash = ? - """, - (json.dumps(db_extra), payment_hash), + f"UPDATE apipayments SET extra = ? WHERE hash = ? {amount_clause} ", + (json.dumps(db_extra), hash), ) diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index 7260df1e2..48ccaf4a9 100644 --- a/lnbits/extensions/withdraw/lnurl.py +++ b/lnbits/extensions/withdraw/lnurl.py @@ -163,12 +163,13 @@ async def api_lnurl_callback( r: httpx.Response = await client.post(link.webhook_url, **kwargs) await update_payment_extra( - payment_hash, - { + hash=payment_hash, + extra={ "wh_success": r.is_success, "wh_message": r.reason_phrase, "wh_response": r.text, }, + outgoing=True, ) except Exception as exc: # webhook fails shouldn't cause the lnurlw to fail since invoice is already paid