-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
50 lines (37 loc) · 1.42 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import asyncio
from lnbits.core.models import Payment
from lnbits.core.services import websocket_updater
from lnbits.tasks import register_invoice_listener
from .crud import (
get_bitcoinswitch_payment,
update_bitcoinswitch_payment,
)
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue, "ext_bitcoinswitch")
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") != "Switch":
return
bitcoinswitch_payment = await get_bitcoinswitch_payment(payment.extra["id"])
if not bitcoinswitch_payment or bitcoinswitch_payment.payment_hash == "paid":
return
bitcoinswitch_payment.payment_hash = bitcoinswitch_payment.payload
bitcoinswitch_payment = await update_bitcoinswitch_payment(bitcoinswitch_payment)
payload = bitcoinswitch_payment.payload
variable = payment.extra.get("variable", False)
if variable:
payload = str(
(int(payload) / int(bitcoinswitch_payment.sats))
* int(payment.extra["amount"])
)
payload = f"{bitcoinswitch_payment.pin}-{payload}"
comment = payment.extra.get("comment")
if comment:
payload = f"{payload}-{comment}"
return await websocket_updater(
bitcoinswitch_payment.bitcoinswitch_id,
payload,
)