2020-04-10 22:04:29 +01:00
|
|
|
from flask import g, jsonify, request
|
2020-02-25 00:45:21 +00:00
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
from lnbits.core.crud import get_user, get_wallet
|
2020-04-16 17:10:53 +02:00
|
|
|
from lnbits.core.services import create_invoice
|
2020-04-16 17:27:36 +02:00
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2020-04-10 22:04:29 +01:00
|
|
|
from lnbits.helpers import Status
|
2020-04-16 17:10:53 +02:00
|
|
|
from lnbits.settings import WALLET
|
2020-04-10 22:04:29 +01:00
|
|
|
|
2020-04-16 17:10:53 +02:00
|
|
|
from lnbits.extensions.tpos import tpos_ext
|
2020-04-13 21:35:35 +01:00
|
|
|
from .crud import create_tpos, get_tpos, get_tposs, delete_tpos
|
2020-04-10 22:04:29 +01:00
|
|
|
|
2020-04-16 17:10:53 +02:00
|
|
|
|
2020-04-10 22:04:29 +01:00
|
|
|
@tpos_ext.route("/api/v1/tposs", methods=["GET"])
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-04-10 22:04:29 +01:00
|
|
|
def api_tposs():
|
|
|
|
wallet_ids = [g.wallet.id]
|
|
|
|
|
|
|
|
if "all_wallets" in request.args:
|
|
|
|
wallet_ids = get_user(g.wallet.user).wallet_ids
|
|
|
|
|
|
|
|
return jsonify([tpos._asdict() for tpos in get_tposs(wallet_ids)]), Status.OK
|
|
|
|
|
|
|
|
|
|
|
|
@tpos_ext.route("/api/v1/tposs", methods=["POST"])
|
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={
|
|
|
|
"name": {"type": "string", "empty": False, "required": True},
|
|
|
|
"currency": {"type": "string", "empty": False, "required": True},
|
|
|
|
}
|
|
|
|
)
|
2020-04-10 22:04:29 +01:00
|
|
|
def api_tpos_create():
|
2020-04-17 20:39:23 +02:00
|
|
|
tpos = create_tpos(wallet_id=g.wallet.id, **g.data)
|
2020-04-10 22:04:29 +01:00
|
|
|
|
|
|
|
return jsonify(tpos._asdict()), Status.CREATED
|
|
|
|
|
2020-04-11 22:18:17 +02:00
|
|
|
|
2020-04-10 22:04:29 +01:00
|
|
|
@tpos_ext.route("/api/v1/tposs/<tpos_id>", methods=["DELETE"])
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-04-10 22:04:29 +01:00
|
|
|
def api_tpos_delete(tpos_id):
|
|
|
|
tpos = get_tpos(tpos_id)
|
|
|
|
|
|
|
|
if not tpos:
|
|
|
|
return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND
|
|
|
|
|
|
|
|
if tpos.wallet != g.wallet.id:
|
2020-04-11 22:18:17 +02:00
|
|
|
return jsonify({"message": "Not your TPoS."}), Status.FORBIDDEN
|
2020-04-10 22:04:29 +01:00
|
|
|
|
|
|
|
delete_tpos(tpos_id)
|
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
return "", Status.NO_CONTENT
|
2020-04-10 22:04:29 +01:00
|
|
|
|
2020-04-11 22:18:17 +02:00
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
@tpos_ext.route("/api/v1/tposs/<tpos_id>/invoices/", methods=["POST"])
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(schema={"amount": {"type": "integer", "min": 1, "required": True}})
|
2020-04-10 22:04:29 +01:00
|
|
|
def api_tpos_create_invoice(tpos_id):
|
2020-04-13 21:35:35 +01:00
|
|
|
tpos = get_tpos(tpos_id)
|
2020-04-16 17:27:36 +02:00
|
|
|
|
2020-04-11 22:18:17 +02:00
|
|
|
if not tpos:
|
|
|
|
return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND
|
2020-04-16 17:27:36 +02:00
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
try:
|
|
|
|
checking_id, payment_request = create_invoice(
|
|
|
|
wallet_id=tpos.wallet, amount=g.data["amount"], memo=f"#tpos {tpos.name}"
|
|
|
|
)
|
2020-04-11 22:18:17 +02:00
|
|
|
except Exception as e:
|
|
|
|
return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR
|
2020-04-10 22:04:29 +01:00
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.CREATED
|
|
|
|
|
|
|
|
|
|
|
|
@tpos_ext.route("/api/v1/tposs/<tpos_id>/invoices/<checking_id>", methods=["GET"])
|
|
|
|
def api_tpos_check_invoice(tpos_id, checking_id):
|
|
|
|
tpos = get_tpos(tpos_id)
|
|
|
|
|
|
|
|
if not tpos:
|
|
|
|
return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND
|
|
|
|
|
|
|
|
try:
|
|
|
|
is_paid = not WALLET.get_invoice_status(checking_id).pending
|
|
|
|
except Exception:
|
|
|
|
return jsonify({"paid": False}), Status.OK
|
|
|
|
|
|
|
|
if is_paid:
|
|
|
|
wallet = get_wallet(tpos.wallet)
|
|
|
|
payment = wallet.get_payment(checking_id)
|
|
|
|
payment.set_pending(False)
|
2020-04-13 21:35:35 +01:00
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
return jsonify({"paid": True}), Status.OK
|
2020-04-16 17:27:36 +02:00
|
|
|
|
2020-04-17 20:39:23 +02:00
|
|
|
return jsonify({"paid": False}), Status.OK
|