2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2020-12-18 20:21:48 +01:00
|
|
|
import httpx
|
2020-10-06 05:39:54 +02:00
|
|
|
from typing import List
|
2020-09-28 04:12:55 +02:00
|
|
|
|
2020-10-06 06:50:55 +02:00
|
|
|
from lnbits.tasks import register_invoice_listener
|
2021-04-17 23:27:15 +02:00
|
|
|
|
2020-12-24 13:38:35 +01:00
|
|
|
from . import db
|
2021-04-17 23:27:15 +02:00
|
|
|
from .crud import get_balance_notify
|
2020-12-24 13:38:35 +01:00
|
|
|
from .models import Payment
|
2020-09-28 04:12:55 +02:00
|
|
|
|
2021-08-30 19:55:02 +02:00
|
|
|
api_invoice_listeners: List[asyncio.Queue] = []
|
2020-09-28 04:12:55 +02:00
|
|
|
|
|
|
|
|
2021-08-30 19:55:02 +02:00
|
|
|
async def register_task_listeners():
|
|
|
|
invoice_paid_queue = asyncio.Queue(5)
|
|
|
|
register_invoice_listener(invoice_paid_queue)
|
|
|
|
asyncio.create_task(wait_for_paid_invoices(invoice_paid_queue))
|
2020-10-06 06:50:55 +02:00
|
|
|
|
|
|
|
|
2021-08-30 19:55:02 +02:00
|
|
|
async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue):
|
|
|
|
while True:
|
|
|
|
payment = await invoice_paid_queue.get()
|
|
|
|
|
2020-12-18 20:21:48 +01:00
|
|
|
# send information to sse channel
|
2021-06-02 22:04:56 +02:00
|
|
|
await dispatch_invoice_listener(payment)
|
2020-12-18 20:21:48 +01:00
|
|
|
|
|
|
|
# dispatch webhook
|
2020-12-24 13:38:35 +01:00
|
|
|
if payment.webhook and not payment.webhook_status:
|
|
|
|
await dispatch_webhook(payment)
|
|
|
|
|
2021-04-17 23:27:15 +02:00
|
|
|
# dispatch balance_notify
|
|
|
|
url = await get_balance_notify(payment.wallet_id)
|
|
|
|
if url:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
2021-10-17 19:33:29 +02:00
|
|
|
r = await client.post(url, timeout=4)
|
2021-04-17 23:27:15 +02:00
|
|
|
await mark_webhook_sent(payment, r.status_code)
|
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
|
|
|
pass
|
|
|
|
|
2020-12-24 13:38:35 +01:00
|
|
|
|
2021-06-02 22:04:56 +02:00
|
|
|
async def dispatch_invoice_listener(payment: Payment):
|
|
|
|
for send_channel in api_invoice_listeners:
|
2020-12-24 13:38:35 +01:00
|
|
|
try:
|
2021-08-30 19:55:02 +02:00
|
|
|
send_channel.put_nowait(payment)
|
|
|
|
except asyncio.QueueFull:
|
2020-12-24 13:38:35 +01:00
|
|
|
print("removing sse listener", send_channel)
|
2021-06-02 22:04:56 +02:00
|
|
|
api_invoice_listeners.remove(send_channel)
|
2020-12-24 13:38:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def dispatch_webhook(payment: Payment):
|
|
|
|
async with httpx.AsyncClient() as client:
|
2022-01-05 10:48:26 +01:00
|
|
|
data = payment.dict()
|
2020-12-24 13:38:35 +01:00
|
|
|
try:
|
2021-10-17 19:33:29 +02:00
|
|
|
r = await client.post(payment.webhook, json=data, timeout=40)
|
2020-12-24 13:38:35 +01:00
|
|
|
await mark_webhook_sent(payment, r.status_code)
|
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
|
|
|
await mark_webhook_sent(payment, -1)
|
|
|
|
|
|
|
|
|
|
|
|
async def mark_webhook_sent(payment: Payment, status: int) -> None:
|
|
|
|
await db.execute(
|
|
|
|
"""
|
2021-07-03 20:39:58 +02:00
|
|
|
UPDATE apipayments SET webhook_status = ?
|
2020-12-24 13:38:35 +01:00
|
|
|
WHERE hash = ?
|
|
|
|
""",
|
|
|
|
(status, payment.payment_hash),
|
|
|
|
)
|