diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 7d9f2a5b..a80fadf2 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -454,6 +454,7 @@ async def update_payment_details( async def update_payment_extra( payment_hash: str, extra: dict, + outgoing: bool = False, conn: Optional[Connection] = None, ) -> None: """ @@ -461,8 +462,10 @@ async def update_payment_extra( Old values in the `extra` JSON object will be kept unless the new `extra` overwrites them. """ + amount_clause = "AND amount < 0" if outgoing else "AND amount > 0" + row = await (conn or db).fetchone( - "SELECT hash, extra from apipayments WHERE hash = ?", + f"SELECT hash, extra from apipayments WHERE hash = ? {amount_clause}", (payment_hash,), ) if not row: @@ -471,10 +474,7 @@ async def update_payment_extra( db_extra.update(extra) await (conn or db).execute( - """ - UPDATE apipayments SET extra = ? - WHERE hash = ? - """, + f"UPDATE apipayments SET extra = ? WHERE hash = ? {amount_clause} ", (json.dumps(db_extra), payment_hash), ) diff --git a/lnbits/extensions/lnurlp/tasks.py b/lnbits/extensions/lnurlp/tasks.py index 72805603..b8da5e43 100644 --- a/lnbits/extensions/lnurlp/tasks.py +++ b/lnbits/extensions/lnurlp/tasks.py @@ -52,19 +52,29 @@ async def on_invoice_paid(payment: Payment) -> None: r: httpx.Response = await client.post(pay_link.webhook_url, **kwargs) await mark_webhook_sent( - payment, r.status_code, r.is_success, r.reason_phrase, r.text + payment.payment_hash, + r.status_code, + r.is_success, + r.reason_phrase, + r.text, ) except Exception as ex: logger.error(ex) - await mark_webhook_sent(payment, -1, False, "Unexpected Error", str(ex)) + await mark_webhook_sent( + payment.payment_hash, -1, False, "Unexpected Error", str(ex) + ) async def mark_webhook_sent( - payment: Payment, status: int, is_success: bool, reason_phrase="", text="" + payment_hash: str, status: int, is_success: bool, reason_phrase="", text="" ) -> None: - payment.extra["wh_status"] = status # keep for backwards compability - payment.extra["wh_success"] = is_success - payment.extra["wh_message"] = reason_phrase - payment.extra["wh_response"] = text - await update_payment_extra(payment.payment_hash, payment.extra) + await update_payment_extra( + payment_hash, + { + "wh_status": status, # keep for backwards compability + "wh_success": is_success, + "wh_message": reason_phrase, + "wh_response": text, + }, + ) diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index 7260df1e..86640443 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, - { + payment_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 @@ -176,8 +177,9 @@ async def api_lnurl_callback( "Caught exception when dispatching webhook url: " + str(exc) ) await update_payment_extra( - payment_hash, - {"wh_success": False, "wh_message": str(exc)}, + payment_hash=payment_hash, + extra={"wh_success": False, "wh_message": str(exc)}, + outgoing=True, ) return {"status": "OK"}