mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-20 13:04:23 +02:00
fix: call webhook in the background (from the task.py
)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
import httpx
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.models import Payment
|
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.helpers import get_current_extension_name
|
||||||
from lnbits.tasks import register_invoice_listener
|
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():
|
async def wait_for_paid_invoices():
|
||||||
@@ -30,4 +32,22 @@ async def on_invoice_paid(payment: Payment) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
await payment.set_pending(False)
|
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)
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
import httpx
|
|
||||||
from fastapi.params import Depends
|
from fastapi.params import Depends
|
||||||
from starlette.exceptions import HTTPException
|
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."
|
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 {
|
return {
|
||||||
**compact_charge(charge),
|
**compact_charge(charge),
|
||||||
**{"time_elapsed": charge.time_elapsed},
|
**{"time_elapsed": charge.time_elapsed},
|
||||||
|
Reference in New Issue
Block a user