From 462322031669191f786c91c9b71689e54d64cbb5 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 18 Dec 2020 16:21:48 -0300 Subject: [PATCH] specify webhooks from invoice creation and call them. --- lnbits/core/tasks.py | 10 ++++++++++ lnbits/core/views/api.py | 34 +++++++++++----------------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lnbits/core/tasks.py b/lnbits/core/tasks.py index e0e2839..bf4150e 100644 --- a/lnbits/core/tasks.py +++ b/lnbits/core/tasks.py @@ -1,4 +1,5 @@ import trio # type: ignore +import httpx from typing import List from lnbits.tasks import register_invoice_listener @@ -14,9 +15,18 @@ async def register_listeners(): async def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel): async for payment in invoice_paid_chan: + # send information to sse channel for send_channel in sse_listeners: try: send_channel.send_nowait(payment) except trio.WouldBlock: print("removing sse listener", send_channel) sse_listeners.remove(send_channel) + + # dispatch webhook + if payment.extra and "webhook" in payment.extra: + async with httpx.AsyncClient() as client: + try: + await client.post(payment.extra["webhook"], json=payment._asdict(), timeout=40) + except (httpx.ConnectError, httpx.RequestError): + pass diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 4e69275..a293f34 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -21,13 +21,7 @@ from ..tasks import sse_listeners @api_check_wallet_key("invoice") async def api_wallet(): return ( - jsonify( - { - "id": g.wallet.id, - "name": g.wallet.name, - "balance": g.wallet.balance_msat, - } - ), + jsonify({"id": g.wallet.id, "name": g.wallet.name, "balance": g.wallet.balance_msat,}), HTTPStatus.OK, ) @@ -51,6 +45,7 @@ async def api_payments(): "memo": {"type": "string", "empty": False, "required": True, "excludes": "description_hash"}, "description_hash": {"type": "string", "empty": False, "required": True, "excludes": "memo"}, "lnurl_callback": {"type": "string", "nullable": True, "required": False}, + "extra": {"type": "dict", "nullable": True, "required": False}, } ) async def api_payments_create_invoice(): @@ -63,7 +58,11 @@ async def api_payments_create_invoice(): try: payment_hash, payment_request = await create_invoice( - wallet_id=g.wallet.id, amount=g.data["amount"], memo=memo, description_hash=description_hash + wallet_id=g.wallet.id, + amount=g.data["amount"], + memo=memo, + description_hash=description_hash, + extra=g.data["extra"], ) except Exception as exc: await db.rollback() @@ -77,11 +76,7 @@ async def api_payments_create_invoice(): if g.data.get("lnurl_callback"): async with httpx.AsyncClient() as client: try: - r = await client.get( - g.data["lnurl_callback"], - params={"pr": payment_request}, - timeout=10, - ) + r = await client.get(g.data["lnurl_callback"], params={"pr": payment_request}, timeout=10,) if r.is_error: lnurl_response = r.text else: @@ -157,9 +152,7 @@ async def api_payments_pay_lnurl(): async with httpx.AsyncClient() as client: try: r = await client.get( - g.data["callback"], - params={"amount": g.data["amount"], "comment": g.data["comment"]}, - timeout=40, + g.data["callback"], params={"amount": g.data["amount"], "comment": g.data["comment"]}, timeout=40, ) if r.is_error: return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST @@ -199,10 +192,7 @@ async def api_payments_pay_lnurl(): extra["comment"] = g.data["comment"] payment_hash = await pay_invoice( - wallet_id=g.wallet.id, - payment_request=params["pr"], - description=g.data.get("description", ""), - extra=extra, + wallet_id=g.wallet.id, payment_request=params["pr"], description=g.data.get("description", ""), extra=extra, ) except Exception as exc: await db.rollback() @@ -362,9 +352,7 @@ async def api_lnurlscan(code: str): @core_app.route("/api/v1/lnurlauth", methods=["POST"]) @api_check_wallet_key("admin") @api_validate_post_request( - schema={ - "callback": {"type": "string", "required": True}, - } + schema={"callback": {"type": "string", "required": True},} ) async def api_perform_lnurlauth(): err = await perform_lnurlauth(g.data["callback"])