lnbits-legend/lnbits/extensions/lnurlp/tasks.py

80 lines
2.5 KiB
Python
Raw Normal View History

2021-10-05 09:19:21 +01:00
import asyncio
import json
2022-07-15 18:11:11 +01:00
import httpx
2022-12-19 19:41:10 +02:00
from loguru import logger
2022-12-21 15:04:49 +02:00
from lnbits.core.crud import update_payment_extra
from lnbits.core.models import Payment
from lnbits.helpers import get_current_extension_name
from lnbits.tasks import register_invoice_listener
from .crud import get_pay_link
2021-10-05 09:19:21 +01:00
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, get_current_extension_name())
2021-10-05 09:19:21 +01:00
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
2021-10-17 18:33:29 +01:00
2023-01-04 13:24:45 +01:00
async def on_invoice_paid(payment: Payment):
if payment.extra.get("tag") != "lnurlp":
return
if payment.extra.get("wh_status"):
# this webhook has already been sent
return
pay_link = await get_pay_link(payment.extra.get("link", -1))
if pay_link and pay_link.webhook_url:
async with httpx.AsyncClient() as client:
try:
2023-01-04 13:24:45 +01:00
r: httpx.Response = 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,
2023-01-04 13:24:45 +01:00
"body": json.loads(pay_link.webhook_body)
if pay_link.webhook_body
else "",
},
2023-01-04 13:24:45 +01:00
headers=json.loads(pay_link.webhook_headers)
if pay_link.webhook_headers
else None,
timeout=40,
)
await mark_webhook_sent(
payment.payment_hash,
r.status_code,
r.is_success,
r.reason_phrase,
r.text,
)
2022-12-19 19:41:10 +02:00
except Exception as ex:
logger.error(ex)
await mark_webhook_sent(
payment.payment_hash, -1, False, "Unexpected Error", str(ex)
)
async def mark_webhook_sent(
payment_hash: str, status: int, is_success: bool, reason_phrase="", text=""
) -> None:
await update_payment_extra(
payment_hash,
{
"wh_status": status, # keep for backwards compability
"wh_success": is_success,
"wh_message": reason_phrase,
"wh_response": text,
},
)