Merge pull request #1229 from motorina0/fix_extra_column_select

fix: handle `extra` updates for internal payments
This commit is contained in:
calle 2022-12-26 11:56:30 +01:00 committed by GitHub
commit e3c14876a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 17 deletions

View file

@ -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),
)

View file

@ -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,
},
)

View file

@ -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"}