diff --git a/lnbits/extensions/satspay/tasks.py b/lnbits/extensions/satspay/tasks.py index 46c16bbc9..2cac64311 100644 --- a/lnbits/extensions/satspay/tasks.py +++ b/lnbits/extensions/satspay/tasks.py @@ -1,5 +1,6 @@ import asyncio +import httpx from loguru import logger from lnbits.core.models import Payment @@ -7,7 +8,8 @@ from lnbits.extensions.satspay.crud import check_address_balance, get_charge from lnbits.helpers import get_current_extension_name from lnbits.tasks import register_invoice_listener -# from .crud import get_ticket, set_ticket_paid +from .helpers import compact_charge +from .models import Charges async def wait_for_paid_invoices(): @@ -30,4 +32,22 @@ async def on_invoice_paid(payment: Payment) -> None: return await payment.set_pending(False) - await check_address_balance(charge_id=charge.id) + charge = await check_address_balance(charge_id=charge.id) + + if charge.paid and charge.webhook: + await call_webhook(charge) + + +async def call_webhook(charge: Charges): + async with httpx.AsyncClient() as client: + try: + r = await client.post( + charge.webhook, + json=compact_charge(charge), + timeout=40, + ) + except AssertionError: + charge.webhook = None + except Exception as e: + logger.warning(f"Failed to call webhook for charge {charge.id}") + logger.warning(e) diff --git a/lnbits/extensions/satspay/views_api.py b/lnbits/extensions/satspay/views_api.py index e1b87c41f..e516219e4 100644 --- a/lnbits/extensions/satspay/views_api.py +++ b/lnbits/extensions/satspay/views_api.py @@ -1,6 +1,5 @@ from http import HTTPStatus -import httpx from fastapi.params import Depends from starlette.exceptions import HTTPException @@ -119,16 +118,6 @@ async def api_charge_balance(charge_id): status_code=HTTPStatus.NOT_FOUND, detail="Charge does not exist." ) - if charge.paid and charge.webhook: - async with httpx.AsyncClient() as client: - try: - r = await client.post( - charge.webhook, - json=compact_charge(charge), - timeout=40, - ) - except AssertionError: - charge.webhook = None return { **compact_charge(charge), **{"time_elapsed": charge.time_elapsed},