lnbits-legend/lnbits/wallets/lnbits.py

61 lines
2.0 KiB
Python
Raw Normal View History

2020-04-03 14:46:14 +02:00
from os import getenv
2020-04-25 20:13:49 +02:00
from requests import get, post
2020-04-03 14:46:14 +02:00
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
2020-04-25 20:13:49 +02:00
2020-04-03 14:46:14 +02:00
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"]
2020-04-25 20:13:49 +02:00
2020-04-03 14:46:14 +02:00
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"]
2020-04-25 20:13:49 +02:00
return PaymentResponse(ok, checking_id, fee_msat, error_message)
2020-04-03 14:46:14 +02:00
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
2020-04-03 14:50:54 +02:00
r = get(url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.auth_invoice)
2020-04-25 20:13:49 +02:00
if not r.ok:
return PaymentStatus(None)
return PaymentStatus(r.json()['paid'])
2020-04-03 14:46:14 +02:00
def get_payment_status(self, checking_id: str) -> PaymentStatus:
2020-04-03 14:50:54 +02:00
r = get(url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.auth_invoice)
2020-04-25 20:13:49 +02:00
if not r.ok:
return PaymentStatus(None)
return PaymentStatus(r.json()['paid'])