2020-03-07 22:27:00 +01:00
|
|
|
from flask import g, jsonify, request
|
2020-05-03 15:57:05 +02:00
|
|
|
from http import HTTPStatus
|
2020-06-08 00:46:16 +02:00
|
|
|
from binascii import unhexlify
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
from lnbits import bolt11
|
2020-03-04 23:11:15 +01:00
|
|
|
from lnbits.core import core_app
|
2020-09-01 03:12:46 +02:00
|
|
|
from lnbits.core.services import create_invoice, pay_invoice
|
2020-09-02 05:58:21 +02:00
|
|
|
from lnbits.core.crud import delete_expired_invoices
|
2020-04-16 17:27:36 +02:00
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2020-04-11 22:18:17 +02:00
|
|
|
from lnbits.settings import WALLET
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
@core_app.route("/api/v1/payments", methods=["GET"])
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-03-07 22:27:00 +01:00
|
|
|
def api_payments():
|
|
|
|
if "check_pending" in request.args:
|
2020-09-02 05:58:21 +02:00
|
|
|
delete_expired_invoices()
|
2020-04-17 21:13:57 +02:00
|
|
|
|
2020-09-01 03:55:19 +02:00
|
|
|
for payment in g.wallet.get_payments(complete=False, pending=True):
|
2020-09-02 05:32:52 +02:00
|
|
|
if payment.is_uncheckable:
|
|
|
|
pass
|
|
|
|
elif payment.is_out:
|
2020-03-31 19:05:25 +02:00
|
|
|
payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
|
2020-04-21 23:47:16 +02:00
|
|
|
else:
|
2020-03-31 19:05:25 +02:00
|
|
|
payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify(g.wallet.get_payments()), HTTPStatus.OK
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"amount": {"type": "integer", "min": 1, "required": True},
|
2020-06-13 03:46:40 +02:00
|
|
|
"memo": {"type": "string", "empty": False, "required": True, "excludes": "description_hash"},
|
|
|
|
"description_hash": {"type": "string", "empty": False, "required": True, "excludes": "memo"},
|
2020-04-11 19:47:25 +02:00
|
|
|
}
|
|
|
|
)
|
2020-03-07 22:27:00 +01:00
|
|
|
def api_payments_create_invoice():
|
2020-06-08 00:46:16 +02:00
|
|
|
if "description_hash" in g.data:
|
|
|
|
description_hash = unhexlify(g.data["description_hash"])
|
|
|
|
memo = ""
|
|
|
|
else:
|
|
|
|
description_hash = b""
|
|
|
|
memo = g.data["memo"]
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
try:
|
2020-09-01 03:12:46 +02:00
|
|
|
payment_hash, payment_request = create_invoice(
|
2020-06-08 00:46:16 +02:00
|
|
|
wallet_id=g.wallet.id, amount=g.data["amount"], memo=memo, description_hash=description_hash
|
2020-04-11 22:18:17 +02:00
|
|
|
)
|
2020-03-31 19:05:25 +02:00
|
|
|
except Exception as e:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
invoice = bolt11.decode(payment_request)
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"payment_hash": invoice.payment_hash,
|
|
|
|
"payment_request": payment_request,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": invoice.payment_hash,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("admin")
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(schema={"bolt11": {"type": "string", "empty": False, "required": True}})
|
2020-03-07 22:27:00 +01:00
|
|
|
def api_payments_pay_invoice():
|
|
|
|
try:
|
2020-09-01 03:12:46 +02:00
|
|
|
payment_hash = pay_invoice(wallet_id=g.wallet.id, payment_request=g.data["bolt11"])
|
2020-04-16 17:10:53 +02:00
|
|
|
except ValueError as e:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.BAD_REQUEST
|
2020-04-16 17:10:53 +02:00
|
|
|
except PermissionError as e:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.FORBIDDEN
|
2020-03-07 22:27:00 +01:00
|
|
|
except Exception as e:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"payment_hash": payment_hash,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": payment_hash,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
@core_app.route("/api/v1/payments", methods=["POST"])
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(schema={"out": {"type": "boolean", "required": True}})
|
2020-03-07 22:27:00 +01:00
|
|
|
def api_payments_create():
|
|
|
|
if g.data["out"] is True:
|
|
|
|
return api_payments_pay_invoice()
|
|
|
|
return api_payments_create_invoice()
|
|
|
|
|
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
@core_app.route("/api/v1/payments/<payment_hash>", methods=["GET"])
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-09-01 03:12:46 +02:00
|
|
|
def api_payment(payment_hash):
|
|
|
|
payment = g.wallet.get_payment(payment_hash)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
if not payment:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": "Payment does not exist."}), HTTPStatus.NOT_FOUND
|
2020-03-07 22:27:00 +01:00
|
|
|
elif not payment.pending:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"paid": True}), HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
try:
|
2020-09-02 05:32:52 +02:00
|
|
|
if payment.is_uncheckable:
|
|
|
|
pass
|
|
|
|
elif payment.is_out:
|
2020-09-01 03:12:46 +02:00
|
|
|
is_paid = not WALLET.get_payment_status(payment.checking_id).pending
|
2020-03-07 22:27:00 +01:00
|
|
|
elif payment.is_in:
|
2020-09-01 03:12:46 +02:00
|
|
|
is_paid = not WALLET.get_invoice_status(payment.checking_id).pending
|
2020-03-04 23:11:15 +01:00
|
|
|
except Exception:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-04-17 21:13:57 +02:00
|
|
|
if is_paid:
|
2020-03-07 22:27:00 +01:00
|
|
|
payment.set_pending(False)
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"paid": True}), HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|