refactor: a wallet is a wallet is a wallet

This commit is contained in:
Eneko Illarramendi
2020-03-31 19:05:25 +02:00
parent 75d97ddfc1
commit d03785558b
15 changed files with 207 additions and 167 deletions
+22 -17
View File
@@ -1,3 +1,4 @@
from os import getenv
from requests import get, post
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
@@ -6,27 +7,27 @@ from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
class LNPayWallet(Wallet):
"""https://docs.lnpay.co/"""
def __init__(self, *, endpoint: str, admin_key: str, invoice_key: str, api_key: str, read_key: str):
def __init__(self):
endpoint = getenv("LNPAY_API_ENDPOINT")
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.auth_admin = admin_key
self.auth_invoice = invoice_key
self.auth_read = read_key
self.auth_api = {"X-Api-Key": api_key}
self.auth_admin = getenv("LNPAY_ADMIN_KEY")
self.auth_invoice = getenv("LNPAY_INVOICE_KEY")
self.auth_read = getenv("LNPAY_READ_KEY")
self.auth_api = {"X-Api-Key": getenv("LNPAY_API_KEY")}
def create_invoice(self, amount: int, memo: str = "") -> InvoiceResponse:
payment_hash, payment_request = None, None
r = post(
url=f"{self.endpoint}/user/wallet/{self.auth_invoice}/invoice",
headers=self.auth_api,
json={"num_satoshis": f"{amount}", "memo": memo},
)
ok, checking_id, payment_request, error_message = r.status_code == 201, None, None, None
if r.ok:
if ok:
data = r.json()
payment_hash, payment_request = data["id"], data["payment_request"]
checking_id, payment_request = data["id"], data["payment_request"]
return InvoiceResponse(r, payment_hash, payment_request)
return InvoiceResponse(ok, checking_id, payment_request, error_message)
def pay_invoice(self, bolt11: str) -> PaymentResponse:
r = post(
@@ -34,17 +35,21 @@ class LNPayWallet(Wallet):
headers=self.auth_api,
json={"payment_request": bolt11},
)
ok, checking_id, fee_msat, error_message = r.status_code == 201, None, 0, None
return PaymentResponse(r, not r.ok)
if ok:
checking_id = r.json()["lnTx"]["id"]
def get_invoice_status(self, payment_hash: str) -> PaymentStatus:
return self.get_payment_status(payment_hash)
return PaymentResponse(ok, checking_id, fee_msat, error_message)
def get_payment_status(self, payment_hash: str) -> PaymentStatus:
r = get(url=f"{self.endpoint}/user/lntx/{payment_hash}", headers=self.auth_api)
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return self.get_payment_status(checking_id)
def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = get(url=f"{self.endpoint}/user/lntx/{checking_id}", headers=self.auth_api)
if not r.ok:
return PaymentStatus(r, None)
return PaymentStatus(None)
statuses = {0: None, 1: True, -1: False}
return PaymentStatus(r, statuses[r.json()["settled"]])
return PaymentStatus(statuses[r.json()["settled"]])