specify webhooks from invoice creation and call them.

This commit is contained in:
fiatjaf 2020-12-18 16:21:48 -03:00 committed by fiatjaf
parent 503c981bc9
commit 4623220316
2 changed files with 21 additions and 23 deletions

View File

@ -1,4 +1,5 @@
import trio # type: ignore import trio # type: ignore
import httpx
from typing import List from typing import List
from lnbits.tasks import register_invoice_listener 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 def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel):
async for payment in invoice_paid_chan: async for payment in invoice_paid_chan:
# send information to sse channel
for send_channel in sse_listeners: for send_channel in sse_listeners:
try: try:
send_channel.send_nowait(payment) send_channel.send_nowait(payment)
except trio.WouldBlock: except trio.WouldBlock:
print("removing sse listener", send_channel) print("removing sse listener", send_channel)
sse_listeners.remove(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

View File

@ -21,13 +21,7 @@ from ..tasks import sse_listeners
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_wallet(): async def api_wallet():
return ( return (
jsonify( jsonify({"id": g.wallet.id, "name": g.wallet.name, "balance": g.wallet.balance_msat,}),
{
"id": g.wallet.id,
"name": g.wallet.name,
"balance": g.wallet.balance_msat,
}
),
HTTPStatus.OK, HTTPStatus.OK,
) )
@ -51,6 +45,7 @@ async def api_payments():
"memo": {"type": "string", "empty": False, "required": True, "excludes": "description_hash"}, "memo": {"type": "string", "empty": False, "required": True, "excludes": "description_hash"},
"description_hash": {"type": "string", "empty": False, "required": True, "excludes": "memo"}, "description_hash": {"type": "string", "empty": False, "required": True, "excludes": "memo"},
"lnurl_callback": {"type": "string", "nullable": True, "required": False}, "lnurl_callback": {"type": "string", "nullable": True, "required": False},
"extra": {"type": "dict", "nullable": True, "required": False},
} }
) )
async def api_payments_create_invoice(): async def api_payments_create_invoice():
@ -63,7 +58,11 @@ async def api_payments_create_invoice():
try: try:
payment_hash, payment_request = await create_invoice( 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: except Exception as exc:
await db.rollback() await db.rollback()
@ -77,11 +76,7 @@ async def api_payments_create_invoice():
if g.data.get("lnurl_callback"): if g.data.get("lnurl_callback"):
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
r = await client.get( r = await client.get(g.data["lnurl_callback"], params={"pr": payment_request}, timeout=10,)
g.data["lnurl_callback"],
params={"pr": payment_request},
timeout=10,
)
if r.is_error: if r.is_error:
lnurl_response = r.text lnurl_response = r.text
else: else:
@ -157,9 +152,7 @@ async def api_payments_pay_lnurl():
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
try: try:
r = await client.get( r = await client.get(
g.data["callback"], g.data["callback"], params={"amount": g.data["amount"], "comment": g.data["comment"]}, timeout=40,
params={"amount": g.data["amount"], "comment": g.data["comment"]},
timeout=40,
) )
if r.is_error: if r.is_error:
return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST
@ -199,10 +192,7 @@ async def api_payments_pay_lnurl():
extra["comment"] = g.data["comment"] extra["comment"] = g.data["comment"]
payment_hash = await pay_invoice( payment_hash = await pay_invoice(
wallet_id=g.wallet.id, wallet_id=g.wallet.id, payment_request=params["pr"], description=g.data.get("description", ""), extra=extra,
payment_request=params["pr"],
description=g.data.get("description", ""),
extra=extra,
) )
except Exception as exc: except Exception as exc:
await db.rollback() await db.rollback()
@ -362,9 +352,7 @@ async def api_lnurlscan(code: str):
@core_app.route("/api/v1/lnurlauth", methods=["POST"]) @core_app.route("/api/v1/lnurlauth", methods=["POST"])
@api_check_wallet_key("admin") @api_check_wallet_key("admin")
@api_validate_post_request( @api_validate_post_request(
schema={ schema={"callback": {"type": "string", "required": True},}
"callback": {"type": "string", "required": True},
}
) )
async def api_perform_lnurlauth(): async def api_perform_lnurlauth():
err = await perform_lnurlauth(g.data["callback"]) err = await perform_lnurlauth(g.data["callback"])