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

65 lines
2 KiB
Python
Raw Normal View History

2021-10-29 16:43:26 +01:00
import asyncio
import httpx
2023-01-05 12:02:23 +01:00
from loguru import logger
2021-10-29 16:43:26 +01:00
from lnbits.core.models import Payment
from lnbits.helpers import get_current_extension_name
2021-10-29 16:43:26 +01:00
from lnbits.tasks import register_invoice_listener
from .crud import get_address, get_domain, set_address_paid, set_address_renewed
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, get_current_extension_name())
2021-10-29 16:43:26 +01:00
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def call_webhook_on_paid(payment_hash):
### Use webhook to notify about cloudflare registration
address = await get_address(payment_hash)
2023-01-05 12:02:23 +01:00
assert address
2021-10-29 16:43:26 +01:00
domain = await get_domain(address.domain)
2023-01-05 12:02:23 +01:00
assert domain
2021-10-29 16:43:26 +01:00
if not domain.webhook:
return
async with httpx.AsyncClient() as client:
try:
r = await client.post(
domain.webhook,
json={
"domain": domain.domain,
"address": address.username,
"email": address.email,
"cost": str(address.sats) + " sats",
"duration": str(address.duration) + " days",
},
timeout=40,
)
2023-01-05 12:02:23 +01:00
r.raise_for_status()
except Exception as e:
logger.error(f"lnaddress: error calling webhook on paid: {str(e)}")
2021-10-29 16:43:26 +01:00
async def on_invoice_paid(payment: Payment) -> None:
2023-01-05 13:08:39 +01:00
2022-07-15 18:11:11 +01:00
if payment.extra.get("tag") == "lnaddress":
2021-10-29 16:43:26 +01:00
await payment.set_pending(False)
await set_address_paid(payment_hash=payment.payment_hash)
await call_webhook_on_paid(payment_hash=payment.payment_hash)
2021-10-29 16:43:26 +01:00
2022-07-15 18:11:11 +01:00
elif payment.extra.get("tag") == "renew lnaddress":
2021-10-29 16:43:26 +01:00
await payment.set_pending(False)
2021-11-26 05:58:20 +00:00
await set_address_renewed(
address_id=payment.extra["id"], duration=payment.extra["duration"]
)
await call_webhook_on_paid(payment_hash=payment.payment_hash)
2021-10-29 16:43:26 +01:00
else:
return