forked from lnbits/lnurldevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
50 lines (41 loc) · 1.65 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.helpers import get_current_extension_name
from lnbits.tasks import register_invoice_listener
from .crud import get_lnurldevicepayment, update_lnurldevicepayment
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:
# (avoid loops)
if payment.extra.get("tag") == "Switch":
lnurldevicepayment = await get_lnurldevicepayment(payment.extra["id"])
if not lnurldevicepayment:
return
if lnurldevicepayment.payhash == "used":
return
lnurldevicepayment.payhash = lnurldevicepayment.payload
lnurldevicepayment = await update_lnurldevicepayment(lnurldevicepayment)
comment = payment.extra["comment"]
variable = False
if payment.extra["variable"] == "True":
variable = True
payload = lnurldevicepayment.payload
if variable:
payload = int(
(int(payload) / int(lnurldevicepayment.sats))
* int(payment.extra["amount"])
)
if comment:
return await websocket_updater(
lnurldevicepayment.deviceid,
f"{lnurldevicepayment.pin}-{payload}-{comment}",
)
return await websocket_updater(
lnurldevicepayment.deviceid,
f"{lnurldevicepayment.pin}-{payload}",
)