scrubbing works, duplicated code warning
This commit is contained in:
parent
10e1bbcb66
commit
6a4170f4cc
2 changed files with 55 additions and 27 deletions
|
|
@ -1,9 +1,5 @@
|
||||||
import json
|
|
||||||
from sqlite3 import Row
|
from sqlite3 import Row
|
||||||
from typing import Dict, Optional
|
|
||||||
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
|
|
||||||
|
|
||||||
from fastapi.param_functions import Query
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
from http import HTTPStatus
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
from lnbits.core import db as core_db
|
from lnbits import bolt11
|
||||||
from lnbits.core.crud import get_wallet
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
|
from lnbits.core.services import pay_invoice
|
||||||
from lnbits.tasks import register_invoice_listener
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
from .crud import get_scrub_by_wallet, get_scrub_link
|
from .crud import get_scrub_by_wallet
|
||||||
from .models import ScrubLink
|
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_paid_invoices():
|
async def wait_for_paid_invoices():
|
||||||
|
|
@ -23,7 +25,7 @@ async def wait_for_paid_invoices():
|
||||||
|
|
||||||
async def on_invoice_paid(payment: Payment) -> None:
|
async def on_invoice_paid(payment: Payment) -> None:
|
||||||
# (avoid loops)
|
# (avoid loops)
|
||||||
if "scrub" == payment.extra.get("tag"):
|
if "scrubed" == payment.extra.get("tag"):
|
||||||
# already scrubbed
|
# already scrubbed
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -32,23 +34,53 @@ async def on_invoice_paid(payment: Payment) -> None:
|
||||||
if not scrub_link:
|
if not scrub_link:
|
||||||
return
|
return
|
||||||
|
|
||||||
from lnbits.core.views.api import (
|
from lnbits.core.views.api import api_lnurlscan
|
||||||
CreateLNURLData,
|
|
||||||
api_lnurlscan,
|
# DECODE LNURLP OR LNADDRESS
|
||||||
api_payments_pay_lnurl,
|
data = await api_lnurlscan(scrub_link.payoraddress)
|
||||||
|
|
||||||
|
|
||||||
|
# I REALLY HATE THIS DUPLICATION OF CODE!! CORE/VIEWS/API.PY, LINE 267
|
||||||
|
domain = urlparse(data["callback"]).netloc
|
||||||
|
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
try:
|
||||||
|
r = await client.get(
|
||||||
|
data["callback"],
|
||||||
|
params={"amount": payment.amount},
|
||||||
|
timeout=40,
|
||||||
|
)
|
||||||
|
if r.is_error:
|
||||||
|
raise httpx.ConnectError
|
||||||
|
except (httpx.ConnectError, httpx.RequestError):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail=f"Failed to connect to {domain}.",
|
||||||
|
)
|
||||||
|
|
||||||
|
params = json.loads(r.text)
|
||||||
|
if params.get("status") == "ERROR":
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail=f"{domain} said: '{params.get('reason', '')}'",
|
||||||
|
)
|
||||||
|
|
||||||
|
invoice = bolt11.decode(params["pr"])
|
||||||
|
if invoice.amount_msat != payment.amount:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
|
detail=f"{domain} returned an invalid invoice. Expected {payment.amount} msat, got {invoice.amount_msat}.",
|
||||||
|
)
|
||||||
|
|
||||||
|
payment_hash = await pay_invoice(
|
||||||
|
wallet_id=payment.wallet_id,
|
||||||
|
payment_request=params["pr"],
|
||||||
|
description=data["description"],
|
||||||
|
extra={"tag": "scrubed"},
|
||||||
)
|
)
|
||||||
|
|
||||||
wallet = await get_wallet(wallet_id=payment.wallet_id)
|
return {
|
||||||
assert wallet
|
"payment_hash": payment_hash,
|
||||||
|
# maintain backwards compatibility with API clients:
|
||||||
# PAY LNURLP AND LNADDRESS
|
"checking_id": payment_hash,
|
||||||
invoice = await api_lnurlscan(scrub_link.payoraddress)
|
}
|
||||||
invoice_data = CreateLNURLData(
|
|
||||||
callback = invoice["callback"],
|
|
||||||
description_hash = invoice["description_hash"],
|
|
||||||
amount = payment.amount
|
|
||||||
)
|
|
||||||
print("INV", invoice_data, wallet)
|
|
||||||
|
|
||||||
invoice_paid = await api_payments_pay_lnurl(data=invoice_data, wallet=wallet.adminkey)
|
|
||||||
print("PAID", invoice_paid)
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue