From 410689e04bf0b9e26d8b6c3b8039351825bfecb7 Mon Sep 17 00:00:00 2001 From: benarc Date: Mon, 6 Dec 2021 13:07:47 +0000 Subject: [PATCH] completing listener, need to fetch pay lnurlpay --- lnbits/extensions/lnurlpayout/tasks.py | 57 ++++++++++++-------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/lnbits/extensions/lnurlpayout/tasks.py b/lnbits/extensions/lnurlpayout/tasks.py index 2397addb..a97bbbb4 100644 --- a/lnbits/extensions/lnurlpayout/tasks.py +++ b/lnbits/extensions/lnurlpayout/tasks.py @@ -20,38 +20,33 @@ async def wait_for_paid_invoices(): async def on_invoice_paid(payment: Payment) -> None: + # Check its got a payout associated with it + lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1)) + + if lnurlpayout_link: + # Check the wallet balance is more than the threshold - if payment.extra.get("wh_status"): - # this webhook has already been sent - return - # check link - pay_link = await get_lnurlpayout(payment.extra.get("link", -1)) - if pay_link and pay_link.webhook_url: + # Getthe invoice from the LNURL to pay async with httpx.AsyncClient() as client: try: - r = await client.post( - pay_link.webhook_url, - json={ - "payment_hash": payment.payment_hash, - "payment_request": payment.bolt11, - "amount": payment.amount, - "comment": payment.extra.get("comment"), - "lnurlp": pay_link.id, - }, - timeout=40, - ) - await mark_webhook_sent(payment, r.status_code) - except (httpx.ConnectError, httpx.RequestError): - await mark_webhook_sent(payment, -1) + url = await api_payments_decode({"data": data.lnurlpay}) + if str(url["domain"])[0:4] != "http": + raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken") + try: + r = await client.post( + str(url["domain"]), + json={ + "payment_hash": payment.payment_hash, + "payment_request": payment.bolt11, + "amount": payment.amount, + "comment": payment.extra.get("comment"), + "lnurlp": pay_link.id, + }, + timeout=40, + ) + await mark_webhook_sent(payment, r.status_code) + except (httpx.ConnectError, httpx.RequestError): + await mark_webhook_sent(payment, -1) - -async def mark_webhook_sent(payment: Payment, status: int) -> None: - payment.extra["wh_status"] = status - - await core_db.execute( - """ - UPDATE apipayments SET extra = ? - WHERE hash = ? - """, - (json.dumps(payment.extra), payment.payment_hash), - ) \ No newline at end of file + except Exception: + raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout") \ No newline at end of file