mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-23 14:40:47 +01:00
Create lnbits.py
This commit is contained in:
parent
46c11f3946
commit
426981fe1c
1 changed files with 48 additions and 0 deletions
48
lnbits/wallets/lnbits.py
Normal file
48
lnbits/wallets/lnbits.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
from requests import get, post
|
||||||
|
from os import getenv
|
||||||
|
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
||||||
|
|
||||||
|
class LnbitsWallet(Wallet):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.endpoint = getenv("LNBITS_ENDPOINT")
|
||||||
|
self.auth_admin = getenv("LNBITS_ADMIN_MACAROON")
|
||||||
|
self.auth_invoice = getenv("LNBITS_INVOICE_MACAROON")
|
||||||
|
|
||||||
|
def create_invoice(self, amount: int, memo: str = "") -> InvoiceResponse:
|
||||||
|
r = post(
|
||||||
|
url=f"{self.endpoint}/api/v1/payments",
|
||||||
|
headers=self.auth_invoice,
|
||||||
|
json={"out": False, "amount": amount, "memo": memo}
|
||||||
|
)
|
||||||
|
ok, checking_id, payment_request, error_message = r.ok, None, None, None
|
||||||
|
|
||||||
|
if r.ok:
|
||||||
|
data = r.json()
|
||||||
|
checking_id, payment_request = data["checking_id"], data["payment_request"]
|
||||||
|
else:
|
||||||
|
error_message = r.json()["message"]
|
||||||
|
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
||||||
|
|
||||||
|
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
||||||
|
r = post(
|
||||||
|
url=f"{self.endpoint}/api/v1/payments",
|
||||||
|
headers=self.auth_admin,
|
||||||
|
json={"out": True, "bolt11": bolt11}
|
||||||
|
)
|
||||||
|
ok, checking_id, fee_msat, error_message = True, None, 0, None
|
||||||
|
|
||||||
|
if r.ok:
|
||||||
|
data = r.json()
|
||||||
|
checking_id = data["checking_id"]
|
||||||
|
else:
|
||||||
|
error_message = r.json()["message"]
|
||||||
|
return InvoiceResponse(ok, checking_id, fee_msat, error_message)
|
||||||
|
|
||||||
|
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
||||||
|
r = get(url=f"{self.endpoint}/v1/api/payments{checking_id}", headers=self.auth_invoice)
|
||||||
|
return PaymentStatus(r['paid'])
|
||||||
|
|
||||||
|
def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
||||||
|
r = get(url=f"{self.endpoint}/v1/api/payments{checking_id}", headers=self.auth_invoice)
|
||||||
|
return PaymentStatus(r['paid'])
|
Loading…
Add table
Reference in a new issue