mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2024-11-20 10:39:59 +01:00
Should work, listener logic in
This commit is contained in:
parent
0ab158992e
commit
7e6b2ba029
@ -25,6 +25,10 @@ async def get_lnurlpayout(lnurlpayout_id: str) -> Optional[lnurlpayout]:
|
||||
row = await db.fetchone("SELECT * FROM lnurlpayout.lnurlpayouts WHERE id = ?", (lnurlpayout_id,))
|
||||
return lnurlpayout(**row) if row else None
|
||||
|
||||
async def get_lnurlpayout_from_wallet(wallet_id: str) -> Optional[lnurlpayout]:
|
||||
row = await db.fetchone("SELECT * FROM lnurlpayout.lnurlpayouts WHERE wallet = ?", (wallet_id,))
|
||||
return lnurlpayout(**row) if row else None
|
||||
|
||||
async def get_lnurlpayouts(wallet_ids: Union[str, List[str]]) -> List[lnurlpayout]:
|
||||
if isinstance(wallet_ids, str):
|
||||
wallet_ids = [wallet_ids]
|
||||
|
@ -5,9 +5,9 @@ import httpx
|
||||
from lnbits.core import db as core_db
|
||||
from lnbits.core.models import Payment
|
||||
from lnbits.tasks import register_invoice_listener
|
||||
from lnbits.core.api import api_wallet
|
||||
from lnbits.core.views.api import api_wallet
|
||||
|
||||
from .crud import get_lnurlpayout
|
||||
from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet
|
||||
|
||||
|
||||
async def wait_for_paid_invoices():
|
||||
@ -22,12 +22,13 @@ async def wait_for_paid_invoices():
|
||||
async def on_invoice_paid(payment: Payment) -> None:
|
||||
try:
|
||||
# Check its got a payout associated with it
|
||||
lnurlpayout_link = await get_lnurlpayout(payment.extra.get("link", -1))
|
||||
lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id)
|
||||
print(lnurlpayout_link)
|
||||
|
||||
if lnurlpayout_link:
|
||||
# Check the wallet balance is more than the threshold
|
||||
wallet = api_wallet(payment.wallet_id)
|
||||
if wallet.balance < lnurlpayout_link.threshold:
|
||||
wallet = await api_wallet(payment.wallet_id)
|
||||
if wallet.balance + (wallet.balance/100*2) < lnurlpayout_link.threshold:
|
||||
return
|
||||
# Get the invoice from the LNURL to pay
|
||||
async with httpx.AsyncClient() as client:
|
||||
@ -36,20 +37,24 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||
if str(url["domain"])[0:4] != "http":
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken")
|
||||
try:
|
||||
r = await client.post(
|
||||
r = await client.get(
|
||||
str(url["domain"]),
|
||||
json={
|
||||
"payment_hash": payment.payment_hash,
|
||||
"payment_request": payment.bolt11,
|
||||
"amount": payment.amount,
|
||||
"comment": payment.extra.get("comment"),
|
||||
"lnurlp": pay_link.id,
|
||||
},
|
||||
timeout=40,
|
||||
)
|
||||
await mark_webhook_sent(payment, r.status_code)
|
||||
res = r.json()
|
||||
print(res)
|
||||
try:
|
||||
r = await client.get(
|
||||
res.callback + "?amount=" + (lnurlpayout_link.threshold * 1000),
|
||||
timeout=40,
|
||||
)
|
||||
res = r.json()
|
||||
print(res)
|
||||
await api_payments_pay_invoice(res.pr, payment.wallet_id)
|
||||
except:
|
||||
return
|
||||
except (httpx.ConnectError, httpx.RequestError):
|
||||
await mark_webhook_sent(payment, -1)
|
||||
return
|
||||
|
||||
except Exception:
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
|
||||
|
@ -10,7 +10,7 @@ from lnbits.core.views.api import api_payment, api_payments_decode
|
||||
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
||||
|
||||
from . import lnurlpayout_ext
|
||||
from .crud import create_lnurlpayout, delete_lnurlpayout, get_lnurlpayout, get_lnurlpayouts
|
||||
from .crud import create_lnurlpayout, delete_lnurlpayout, get_lnurlpayout, get_lnurlpayouts, get_lnurlpayout_from_wallet
|
||||
from .models import lnurlpayout, CreateLnurlPayoutData
|
||||
|
||||
|
||||
@ -29,17 +29,22 @@ async def api_lnurlpayouts(
|
||||
async def api_lnurlpayout_create(
|
||||
data: CreateLnurlPayoutData, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
try:
|
||||
url = await api_payments_decode({"data": data.lnurlpay})
|
||||
|
||||
if str(url["domain"])[0:4] != "http":
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
|
||||
lnurlpayout = await create_lnurlpayout(wallet_id=wallet.wallet.id, data=data)
|
||||
return lnurlpayout.dict()
|
||||
except Exception:
|
||||
print(await get_lnurlpayout_from_wallet(wallet.wallet.id))
|
||||
if await get_lnurlpayout_from_wallet(wallet.wallet.id):
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Wallet already has lnurlpayout set")
|
||||
return
|
||||
url = await api_payments_decode({"data": data.lnurlpay})
|
||||
if "domain" not in url:
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="LNURL could not be decoded")
|
||||
return
|
||||
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, data=data)
|
||||
if not lnurlpayout:
|
||||
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Failed to save LNURLPayout")
|
||||
|
||||
|
||||
return
|
||||
return lnurlpayout.dict()
|
||||
|
||||
@lnurlpayout_ext.delete("/api/v1/lnurlpayouts/{lnurlpayout_id}")
|
||||
async def api_lnurlpayout_delete(
|
||||
|
Loading…
Reference in New Issue
Block a user