2021-12-29 12:57:43 +00:00
|
|
|
import asyncio
|
2022-11-25 14:12:13 +02:00
|
|
|
import json
|
2021-12-29 12:57:43 +00:00
|
|
|
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
2021-12-29 12:57:43 +00:00
|
|
|
from lnbits.core.models import Payment
|
|
|
|
from lnbits.extensions.satspay.crud import check_address_balance, get_charge
|
2022-10-04 09:51:47 +02:00
|
|
|
from lnbits.helpers import get_current_extension_name
|
2021-12-29 12:57:43 +00:00
|
|
|
from lnbits.tasks import register_invoice_listener
|
|
|
|
|
2022-11-25 14:12:13 +02:00
|
|
|
from .crud import update_charge
|
2022-11-25 11:25:40 +02:00
|
|
|
from .helpers import call_webhook
|
2021-12-29 12:57:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-12-29 12:57:43 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
payment = await invoice_queue.get()
|
|
|
|
await on_invoice_paid(payment)
|
|
|
|
|
|
|
|
|
|
|
|
async def on_invoice_paid(payment: Payment) -> None:
|
2023-01-05 12:47:53 +01:00
|
|
|
|
2022-07-15 18:11:11 +01:00
|
|
|
if payment.extra.get("tag") != "charge":
|
2021-12-29 12:57:43 +00:00
|
|
|
# not a charge invoice
|
|
|
|
return
|
|
|
|
|
2023-01-05 12:47:53 +01:00
|
|
|
assert payment.memo
|
2021-12-29 12:57:43 +00:00
|
|
|
charge = await get_charge(payment.memo)
|
|
|
|
if not charge:
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.error("this should never happen", payment)
|
2021-12-29 12:57:43 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
await payment.set_pending(False)
|
2022-11-24 16:57:59 +02:00
|
|
|
charge = await check_address_balance(charge_id=charge.id)
|
2023-01-05 12:47:53 +01:00
|
|
|
assert charge
|
2022-11-24 16:57:59 +02:00
|
|
|
|
2022-11-25 14:12:13 +02:00
|
|
|
if charge.must_call_webhook():
|
|
|
|
resp = await call_webhook(charge)
|
|
|
|
extra = {**charge.config.dict(), **resp}
|
|
|
|
await update_charge(charge_id=charge.id, extra=json.dumps(extra))
|