Skip to content

Commit

Permalink
fix failing payment check
Browse files Browse the repository at this point in the history
  • Loading branch information
talvasconcelos committed Mar 22, 2024
1 parent d57407b commit e97c121
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
36 changes: 36 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import asyncio

from loguru import logger

from lnbits.core.models import Payment
from lnbits.helpers import get_current_extension_name
from lnbits.tasks import register_invoice_listener

from .crud import get_appointment, set_appointment_paid, get_schedule


async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, get_current_extension_name())

while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)


async def on_invoice_paid(payment: Payment) -> None:
if payment.extra.get("tag") != "lncalendar":
# not a lncalendar invoice
return

appointment = await get_appointment(payment.checking_id)
if not appointment:
logger.error("this should never happen", payment)
return

schedule = await get_schedule(appointment.schedule)
assert schedule

if payment.amount == schedule.amount * 1000:
await payment.set_pending(False)
await set_appointment_paid(payment.payment_hash)
24 changes: 13 additions & 11 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from fastapi import Depends, Query
from fastapi.exceptions import HTTPException

from lnbits.core.crud import get_user, get_wallet, get_standalone_payment
from lnbits.core.services import check_transaction_status, create_invoice
from loguru import logger

from lnbits.core.crud import get_user
from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment
from lnbits.decorators import (
WalletTypeInfo,
require_admin_key,
Expand Down Expand Up @@ -134,21 +137,20 @@ async def api_purge_appointments(schedule_id: str):

@lncalendar_ext.get("/api/v1/appointment/{schedule_id}/{payment_hash}")
async def api_appointment_check_invoice(schedule_id: str, payment_hash: str):
payment = await get_standalone_payment(payment_hash)
if not payment:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Payment does not exist."
)

schedule = await get_schedule(schedule_id)
if not schedule:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Schedule does not exist."
)
if not payment.pending and payment.amount == schedule.amount * 1000:
await set_appointment_paid(payment_hash)
return {"paid": True}

try:
status = await api_payment(payment_hash)
if status["paid"]:
await set_appointment_paid(payment_hash)
return {"paid": True}

except Exception:
return {"paid": False}
return {"paid": False}


Expand Down

0 comments on commit e97c121

Please sign in to comment.