feat: partial work
This commit is contained in:
@@ -129,6 +129,15 @@ async def create_fiat_invoice(
|
|||||||
if not fiat_provider_name:
|
if not fiat_provider_name:
|
||||||
raise ValueError("Fiat provider is required for fiat invoices.")
|
raise ValueError("Fiat provider is required for fiat invoices.")
|
||||||
if not settings.is_fiat_provider_enabled(fiat_provider_name):
|
if not settings.is_fiat_provider_enabled(fiat_provider_name):
|
||||||
|
funding_source = get_funding_source()
|
||||||
|
if funding_source.__class__.__name__ == "LNbitsWallet":
|
||||||
|
fiat_invoice = await create_wallet_invoice(wallet_id, invoice_data)
|
||||||
|
fiat_invoice.fiat_provider = fiat_provider_name
|
||||||
|
fiat_invoice.extra["fiat_checking_id"] = fiat_invoice.checking_id
|
||||||
|
# TODO: move to payment
|
||||||
|
fiat_invoice.extra["fiat_payment_request"] = fiat_invoice.payment_request
|
||||||
|
|
||||||
|
return fiat_invoice
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Fiat provider '{fiat_provider_name}' is not enabled.",
|
f"Fiat provider '{fiat_provider_name}' is not enabled.",
|
||||||
)
|
)
|
||||||
@@ -219,6 +228,7 @@ async def create_wallet_invoice(wallet_id: str, data: CreateInvoice) -> Payment:
|
|||||||
payment_hash=data.payment_hash,
|
payment_hash=data.payment_hash,
|
||||||
labels=data.labels,
|
labels=data.labels,
|
||||||
external_id=data.external_id,
|
external_id=data.external_id,
|
||||||
|
fiat_provider=data.fiat_provider,
|
||||||
conn=conn,
|
conn=conn,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -261,6 +271,7 @@ async def create_invoice(
|
|||||||
payment_hash: str | None = None,
|
payment_hash: str | None = None,
|
||||||
labels: list[str] | None = None,
|
labels: list[str] | None = None,
|
||||||
external_id: str | None = None,
|
external_id: str | None = None,
|
||||||
|
fiat_provider: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
) -> Payment:
|
) -> Payment:
|
||||||
if not amount > 0:
|
if not amount > 0:
|
||||||
@@ -321,6 +332,9 @@ async def create_invoice(
|
|||||||
description_hash=description_hash,
|
description_hash=description_hash,
|
||||||
unhashed_description=unhashed_description,
|
unhashed_description=unhashed_description,
|
||||||
expiry=expiry or settings.lightning_invoice_expiry,
|
expiry=expiry or settings.lightning_invoice_expiry,
|
||||||
|
fiat_provider=fiat_provider,
|
||||||
|
currency=currency if currency != "sat" else None,
|
||||||
|
fiat_amount=amount if currency != "sat" else None,
|
||||||
)
|
)
|
||||||
if (
|
if (
|
||||||
not invoice_response.ok
|
not invoice_response.ok
|
||||||
@@ -332,6 +346,8 @@ async def create_invoice(
|
|||||||
status="pending",
|
status="pending",
|
||||||
)
|
)
|
||||||
invoice = bolt11_decode(invoice_response.payment_request)
|
invoice = bolt11_decode(invoice_response.payment_request)
|
||||||
|
extra["fiat_provider"] = fiat_provider
|
||||||
|
extra["fiat_payment_request"] = invoice_response.fiat_payment_request
|
||||||
|
|
||||||
create_payment_model = CreatePayment(
|
create_payment_model = CreatePayment(
|
||||||
wallet_id=user_wallet.source_wallet_id,
|
wallet_id=user_wallet.source_wallet_id,
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ class InvoiceResponse(NamedTuple):
|
|||||||
ok: bool
|
ok: bool
|
||||||
checking_id: str | None = None # payment_hash, rpc_id
|
checking_id: str | None = None # payment_hash, rpc_id
|
||||||
payment_request: str | None = None
|
payment_request: str | None = None
|
||||||
|
fiat_payment_request: str | None = None
|
||||||
error_message: str | None = None
|
error_message: str | None = None
|
||||||
preimage: str | None = None
|
preimage: str | None = None
|
||||||
fee_msat: int | None = None
|
fee_msat: int | None = None
|
||||||
|
|||||||
@@ -78,17 +78,28 @@ class LNbitsWallet(Wallet):
|
|||||||
data: dict = {"out": False, "amount": amount, "memo": memo or ""}
|
data: dict = {"out": False, "amount": amount, "memo": memo or ""}
|
||||||
if kwargs.get("expiry"):
|
if kwargs.get("expiry"):
|
||||||
data["expiry"] = kwargs["expiry"]
|
data["expiry"] = kwargs["expiry"]
|
||||||
|
if kwargs.get("fiat_provider"):
|
||||||
|
if "currency" not in kwargs or "fiat_amount" not in kwargs:
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=False,
|
||||||
|
error_message="Fiat provider requires "
|
||||||
|
"'currency' and 'fiat_amount' parameters.",
|
||||||
|
)
|
||||||
|
data["amount"] = kwargs["fiat_amount"]
|
||||||
|
data["unit"] = kwargs["currency"]
|
||||||
|
data["fiat_provider"] = kwargs["fiat_provider"]
|
||||||
|
|
||||||
if description_hash:
|
if description_hash:
|
||||||
data["description_hash"] = description_hash.hex()
|
data["description_hash"] = description_hash.hex()
|
||||||
if unhashed_description:
|
if unhashed_description:
|
||||||
data["unhashed_description"] = unhashed_description.hex()
|
data["unhashed_description"] = unhashed_description.hex()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
r = await self.client.post(url="/api/v1/payments", json=data)
|
print(f"Creating invoice with data: {data}")
|
||||||
|
r = await self.client.post(url="/api/v1/payments", json=data, timeout=30)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
|
||||||
# Backwards compatibility for pre-v1 which used the key "payment_request"
|
|
||||||
payment_str = data.get("bolt11") or data.get("payment_request")
|
payment_str = data.get("bolt11") or data.get("payment_request")
|
||||||
if r.is_error or not payment_str:
|
if r.is_error or not payment_str:
|
||||||
error_message = data["detail"] if "detail" in data else r.text
|
error_message = data["detail"] if "detail" in data else r.text
|
||||||
@@ -100,6 +111,7 @@ class LNbitsWallet(Wallet):
|
|||||||
ok=True,
|
ok=True,
|
||||||
checking_id=data["checking_id"],
|
checking_id=data["checking_id"],
|
||||||
payment_request=payment_str,
|
payment_request=payment_str,
|
||||||
|
fiat_payment_request=data.get("payment_request"),
|
||||||
preimage=data.get("preimage"),
|
preimage=data.get("preimage"),
|
||||||
)
|
)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
@@ -111,6 +123,11 @@ class LNbitsWallet(Wallet):
|
|||||||
return InvoiceResponse(
|
return InvoiceResponse(
|
||||||
ok=False, error_message="Server error: 'missing required fields'"
|
ok=False, error_message="Server error: 'missing required fields'"
|
||||||
)
|
)
|
||||||
|
except httpx.HTTPStatusError as exc:
|
||||||
|
logger.warning(exc.response.text)
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=False, error_message=f"Unable to connect to {self.endpoint}."
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning(exc)
|
logger.warning(exc)
|
||||||
return InvoiceResponse(
|
return InvoiceResponse(
|
||||||
@@ -213,6 +230,7 @@ class LNbitsWallet(Wallet):
|
|||||||
logger.info("connected to LNbits fundingsource websocket.")
|
logger.info("connected to LNbits fundingsource websocket.")
|
||||||
while settings.lnbits_running:
|
while settings.lnbits_running:
|
||||||
message = await ws.recv()
|
message = await ws.recv()
|
||||||
|
print(f"### Received message from websocket: {message}")
|
||||||
message_dict = json.loads(message)
|
message_dict = json.loads(message)
|
||||||
if (
|
if (
|
||||||
message_dict
|
message_dict
|
||||||
|
|||||||
Reference in New Issue
Block a user