From 59b3941843563af4243fc65792dbb240877fb597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Wed, 16 Oct 2024 11:09:13 +0200 Subject: [PATCH] rc5 --- views_lnurl.py | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/views_lnurl.py b/views_lnurl.py index f86d83f..88b8934 100644 --- a/views_lnurl.py +++ b/views_lnurl.py @@ -9,7 +9,8 @@ import shortuuid from fastapi import APIRouter, HTTPException, Request, Response from fastapi.responses import JSONResponse from fastapi.routing import APIRoute -from lnbits.core.crud import update_payment_extra +from lnbits.core.models import Payment +from lnbits.core.crud import update_payment from lnbits.core.services import pay_invoice from loguru import logger @@ -162,7 +163,7 @@ async def api_lnurl_callback( ) from exc try: - payment_hash = await pay_invoice( + payment = await pay_invoice( wallet_id=link.wallet, payment_request=pr, max_sat=link.max_withdrawable, @@ -175,7 +176,7 @@ async def api_lnurl_callback( await delete_hash_check(id_unique_hash or unique_hash) if link.webhook_url: - await dispatch_webhook(link, payment_hash, pr) + await dispatch_webhook(link, payment, pr) return {"status": "OK"} except Exception as exc: # If payment fails, delete the hash stored so another attempt can be made. @@ -193,14 +194,14 @@ def check_unique_link(link: WithdrawLink, unique_hash: str) -> bool: async def dispatch_webhook( - link: WithdrawLink, payment_hash: str, payment_request: str + link: WithdrawLink, payment: Payment, payment_request: str ) -> None: async with httpx.AsyncClient() as client: try: r: httpx.Response = await client.post( link.webhook_url, json={ - "payment_hash": payment_hash, + "payment_hash": payment.payment_hash, "payment_request": payment_request, "lnurlw": link.id, "body": json.loads(link.webhook_body) if link.webhook_body else "", @@ -210,24 +211,17 @@ async def dispatch_webhook( ), timeout=40, ) - await update_payment_extra( - payment_hash=payment_hash, - extra={ - "wh_success": r.is_success, - "wh_message": r.reason_phrase, - "wh_response": r.text, - }, - outgoing=True, - ) + payment.extra["wh_success"] = r.is_success + payment.extra["wh_message"] = r.reason_phrase + payment.extra["wh_response"] = r.text + await update_payment(payment) except Exception as exc: # webhook fails shouldn't cause the lnurlw to fail # since invoice is already paid logger.error(f"Caught exception when dispatching webhook url: {exc!s}") - await update_payment_extra( - payment_hash=payment_hash, - extra={"wh_success": False, "wh_message": str(exc)}, - outgoing=True, - ) + payment.extra["wh_success"] = False + payment.extra["wh_message"] = str(exc) + await update_payment(payment) # FOR LNURLs WHICH ARE UNIQUE