2022-07-02 21:16:27 -06:00
|
|
|
import asyncio
|
|
|
|
|
2022-10-21 18:15:27 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
2022-07-02 21:16:27 -06:00
|
|
|
from lnbits.core.models import Payment
|
2022-12-07 10:21:45 +00:00
|
|
|
from lnbits.core.services import create_invoice, pay_invoice, websocketUpdater
|
2022-10-21 18:15:27 +02:00
|
|
|
from lnbits.helpers import get_current_extension_name
|
|
|
|
from lnbits.tasks import register_invoice_listener
|
2022-07-02 21:16:27 -06:00
|
|
|
|
|
|
|
from .crud import get_tpos
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
2022-07-02 21:16:27 -06:00
|
|
|
|
|
|
|
while True:
|
|
|
|
payment = await invoice_queue.get()
|
|
|
|
await on_invoice_paid(payment)
|
|
|
|
|
|
|
|
|
|
|
|
async def on_invoice_paid(payment: Payment) -> None:
|
2022-10-21 18:15:27 +02:00
|
|
|
if payment.extra.get("tag") != "tpos":
|
2022-07-02 21:16:27 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
tpos = await get_tpos(payment.extra.get("tposId"))
|
|
|
|
tipAmount = payment.extra.get("tipAmount")
|
|
|
|
|
2022-12-07 10:21:45 +00:00
|
|
|
strippedPayment = {
|
|
|
|
"amount":payment.amount,
|
|
|
|
"fee":payment.fee,
|
|
|
|
"checking_id":payment.checking_id,
|
|
|
|
"payment_hash":payment.payment_hash,
|
|
|
|
"bolt11":payment.bolt11,
|
|
|
|
}
|
|
|
|
|
|
|
|
await websocketUpdater(payment.extra.get("tposId"), str(strippedPayment))
|
|
|
|
|
2022-07-02 21:16:27 -06:00
|
|
|
if tipAmount is None:
|
2022-07-05 17:18:22 +02:00
|
|
|
# no tip amount
|
2022-07-02 21:16:27 -06:00
|
|
|
return
|
|
|
|
|
2022-10-21 18:15:27 +02:00
|
|
|
payment_hash, payment_request = await create_invoice(
|
2022-07-02 21:16:27 -06:00
|
|
|
wallet_id=tpos.tip_wallet,
|
2022-10-21 18:15:27 +02:00
|
|
|
amount=int(tipAmount), # sats
|
|
|
|
internal=True,
|
|
|
|
memo=f"tpos tip",
|
2022-07-02 21:16:27 -06:00
|
|
|
)
|
2022-10-21 18:15:27 +02:00
|
|
|
logger.debug(f"tpos: tip invoice created: {payment_hash}")
|
2022-07-02 21:16:27 -06:00
|
|
|
|
2022-10-21 18:15:27 +02:00
|
|
|
checking_id = await pay_invoice(
|
|
|
|
payment_request=payment_request,
|
|
|
|
wallet_id=payment.wallet_id,
|
|
|
|
extra={"tag": "tpos"},
|
|
|
|
)
|
|
|
|
logger.debug(f"tpos: tip invoice paid: {checking_id}")
|