paying function works

This commit is contained in:
benarc 2021-12-19 22:28:19 +00:00
parent 331a383223
commit 6a4290c0e9
4 changed files with 20 additions and 16 deletions

View File

@ -11,7 +11,7 @@ async def create_lnurlpayout(wallet_id: str, admin_key: str, data: CreateLnurlPa
await db.execute(
"""
INSERT INTO lnurlpayout.lnurlpayouts (id, title, wallet, admin_key, lnurlpay, threshold)
VALUES (?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?)
""",
(lnurlpayout_id, data.title, wallet_id, admin_key, data.lnurlpay, data.threshold),
)

View File

@ -13,4 +13,4 @@ class lnurlpayout(BaseModel):
wallet: str
admin_key: str
lnurlpay: str
threshold: str
threshold: int

View File

@ -6,6 +6,8 @@ from lnbits.core import db as core_db
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
from lnbits.core.views.api import api_wallet
from lnbits.core.crud import get_wallet
from lnbits.core.views.api import api_payment, api_payments_decode, pay_invoice
from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet
@ -23,19 +25,18 @@ async def on_invoice_paid(payment: Payment) -> None:
try:
# Check its got a payout associated with it
lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id)
print(lnurlpayout_link)
if lnurlpayout_link:
print("poo11")
# Check the wallet balance is more than the threshold
wallet = await api_wallet(lnurlpayout_link.admin_key)
print("poo1")
if wallet.balance + (wallet.balance/100*2) < lnurlpayout_link.threshold:
wallet = await get_wallet(lnurlpayout_link.wallet)
if wallet.balance < lnurlpayout_link.threshold + (lnurlpayout_link.threshold*0.02):
return
print("poo2")
# Get the invoice from the LNURL to pay
async with httpx.AsyncClient() as client:
try:
url = await api_payments_decode({"data": data.lnurlpay})
url = await api_payments_decode({"data": lnurlpayout_link.lnurlpay})
if str(url["domain"])[0:4] != "http":
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken")
try:
@ -44,16 +45,18 @@ async def on_invoice_paid(payment: Payment) -> None:
timeout=40,
)
res = r.json()
print(res)
print(res["callback"])
try:
r = await client.get(
res["callback"] + "?amount=" + (lnurlpayout_link.threshold * 1000),
res["callback"] + "?amount=" + str(int((wallet.balance - wallet.balance*0.02) * 1000)),
timeout=40,
)
res = r.json()
print(res["pr"])
await api_payments_pay_invoice(res["pr"], payment.wallet_id)
await pay_invoice(
wallet_id=payment.wallet_id,
payment_request=res["pr"],
extra={"tag": "lnurlpayout"},
)
return
except:
return
except (httpx.ConnectError, httpx.RequestError):

View File

@ -18,7 +18,7 @@ from .tasks import on_invoice_paid
async def api_lnurlpayouts(
all_wallets: bool = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
):
wallet_ids = [wallet.wallet.id]
wallet_ids = wallet.wallet.id
if all_wallets:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
@ -39,7 +39,8 @@ async def api_lnurlpayout_create(
if str(url["domain"])[0:4] != "http":
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not valid LNURL")
return
lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, admin_key=wallet.admin_key, data=data)
print(wallet)
lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, admin_key=wallet.wallet.adminkey, data=data)
if not lnurlpayout:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
return