fix: handle extra updates for internal payments

This commit is contained in:
Vlad Stan 2022-12-22 11:12:19 +02:00
parent 4e8014a177
commit b56d08a70e
2 changed files with 17 additions and 10 deletions

View file

@ -452,18 +452,27 @@ async def update_payment_details(
async def update_payment_extra( async def update_payment_extra(
payment_hash: str, hash: str,
extra: dict, extra: dict,
outgoing: bool = False,
incoming: bool = False,
conn: Optional[Connection] = None, conn: Optional[Connection] = None,
) -> None: ) -> None:
""" """
Only update the `extra` field for the payment. Only update the `extra` field for the payment.
Old values in the `extra` JSON object will be kept unless the new `extra` overwrites them. 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( 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,), (hash,),
) )
if not row: if not row:
return return
@ -471,11 +480,8 @@ async def update_payment_extra(
db_extra.update(extra) db_extra.update(extra)
await (conn or db).execute( await (conn or db).execute(
""" f"UPDATE apipayments SET extra = ? WHERE hash = ? {amount_clause} ",
UPDATE apipayments SET extra = ? (json.dumps(db_extra), hash),
WHERE hash = ?
""",
(json.dumps(db_extra), payment_hash),
) )

View file

@ -163,12 +163,13 @@ async def api_lnurl_callback(
r: httpx.Response = await client.post(link.webhook_url, **kwargs) r: httpx.Response = await client.post(link.webhook_url, **kwargs)
await update_payment_extra( await update_payment_extra(
payment_hash, hash=payment_hash,
{ extra={
"wh_success": r.is_success, "wh_success": r.is_success,
"wh_message": r.reason_phrase, "wh_message": r.reason_phrase,
"wh_response": r.text, "wh_response": r.text,
}, },
outgoing=True,
) )
except Exception as exc: except Exception as exc:
# webhook fails shouldn't cause the lnurlw to fail since invoice is already paid # webhook fails shouldn't cause the lnurlw to fail since invoice is already paid