2021-10-27 22:09:55 +01:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
from lnbits.core.models import Payment
|
2022-10-04 09:51:47 +02:00
|
|
|
from lnbits.helpers import get_current_extension_name
|
2021-10-27 22:09:55 +01:00
|
|
|
from lnbits.tasks import register_invoice_listener
|
|
|
|
|
|
|
|
from .cloudflare import cloudflare_create_subdomain
|
|
|
|
from .crud import get_domain, set_subdomain_paid
|
|
|
|
|
|
|
|
|
|
|
|
async def wait_for_paid_invoices():
|
|
|
|
invoice_queue = asyncio.Queue()
|
2022-10-04 09:51:47 +02:00
|
|
|
register_invoice_listener(invoice_queue, get_current_extension_name())
|
2021-10-27 22:09:55 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
payment = await invoice_queue.get()
|
|
|
|
await on_invoice_paid(payment)
|
|
|
|
|
2021-11-12 04:14:55 +00:00
|
|
|
|
2021-10-27 22:09:55 +01:00
|
|
|
async def on_invoice_paid(payment: Payment) -> None:
|
2023-01-04 10:52:19 +01:00
|
|
|
if payment.extra.get("tag") != "lnsubdomain":
|
2021-10-27 22:09:55 +01:00
|
|
|
# not an lnurlp invoice
|
|
|
|
return
|
|
|
|
|
|
|
|
await payment.set_pending(False)
|
|
|
|
subdomain = await set_subdomain_paid(payment_hash=payment.payment_hash)
|
|
|
|
domain = await get_domain(subdomain.domain)
|
|
|
|
|
|
|
|
### Create subdomain
|
2021-11-08 12:28:11 +00:00
|
|
|
cf_response = await cloudflare_create_subdomain(
|
2023-01-09 12:14:44 +02:00
|
|
|
domain=domain, # type: ignore
|
2021-10-27 22:09:55 +01:00
|
|
|
subdomain=subdomain.subdomain,
|
|
|
|
record_type=subdomain.record_type,
|
|
|
|
ip=subdomain.ip,
|
|
|
|
)
|
|
|
|
|
|
|
|
### Use webhook to notify about cloudflare registration
|
2022-10-24 16:29:30 +02:00
|
|
|
if domain and domain.webhook:
|
2021-10-27 22:09:55 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.post(
|
|
|
|
domain.webhook,
|
|
|
|
json={
|
|
|
|
"domain": subdomain.domain_name,
|
|
|
|
"subdomain": subdomain.subdomain,
|
|
|
|
"record_type": subdomain.record_type,
|
|
|
|
"email": subdomain.email,
|
|
|
|
"ip": subdomain.ip,
|
|
|
|
"cost:": str(subdomain.sats) + " sats",
|
|
|
|
"duration": str(subdomain.duration) + " days",
|
|
|
|
"cf_response": cf_response,
|
|
|
|
},
|
|
|
|
timeout=40,
|
|
|
|
)
|
2023-01-21 15:35:53 +00:00
|
|
|
assert r
|
2021-10-27 22:09:55 +01:00
|
|
|
except AssertionError:
|
2023-01-20 10:44:07 +00:00
|
|
|
pass
|