refactor: only use one (direction) flag for identifying payment

This commit is contained in:
Vlad Stan 2022-12-22 12:23:46 +02:00
parent a434731729
commit 98e0ea5ae2
2 changed files with 17 additions and 15 deletions

View File

@ -455,20 +455,14 @@ async def update_payment_extra(
payment_hash: str, payment_hash: str,
extra: dict, extra: dict,
outgoing: bool = False, 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: amount_clause = "AND amount < 0" if outgoing else "AND amount > 0"
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(
f"SELECT hash, extra from apipayments WHERE hash = ? {amount_clause}", f"SELECT hash, extra from apipayments WHERE hash = ? {amount_clause}",

View File

@ -52,21 +52,29 @@ async def on_invoice_paid(payment: Payment) -> None:
r: httpx.Response = await client.post(pay_link.webhook_url, **kwargs) r: httpx.Response = await client.post(pay_link.webhook_url, **kwargs)
await mark_webhook_sent( 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: except Exception as ex:
logger.error(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( 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: ) -> 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( await update_payment_extra(
payment_hash=payment.payment_hash, extra=payment.extra, incoming=True payment_hash,
{
"wh_status": status, # keep for backwards compability
"wh_success": is_success,
"wh_message": reason_phrase,
"wh_response": text,
},
) )