diff --git a/LNbits/__init__.py b/LNbits/__init__.py index 83f5e97..9de9a18 100644 --- a/LNbits/__init__.py +++ b/LNbits/__init__.py @@ -269,68 +269,48 @@ def wallet(): ) -# API requests @app.route("/v1/invoices", methods=["GET", "POST"]) def api_invoices(): - if request.headers["Content-Type"] == "application/json": - - postedjson = request.json - print(postedjson) + if request.headers["Content-Type"] != "application/json": + return jsonify({"ERROR": "MUST BE JSON"}), 200 - if "value" in postedjson: - if postedjson["value"].isdigit() == True: - if "memo" in postedjson: - con = db_connect() - cur = con.cursor() + postedjson = request.json - cur.execute( - "select * from wallets WHERE inkey = '" + request.headers["Grpc-Metadata-macaroon"] + "'" - ) - rows = cur.fetchall() + if "value" not in postedjson: + return jsonify({"ERROR": "NO VALUE"}), 200 - if len(rows) > 0: - cur.close() + if not postedjson["value"].isdigit(): + return jsonify({"ERROR": "VALUE MUST BE A NUMMBER"}), 200 - dataj = {"amt": postedjson["value"], "memo": postedjson["memo"]} - headers = {"Authorization": "Basic %s" % INVOICE_KEY} - r = requests.post(url=API_ENDPOINT + "/addinvoice", json=dataj, headers=headers) + if "memo" not in postedjson: + return jsonify({"ERROR": "NO MEMO"}), 200 - data = r.json() + with Database() as db: + rows = db.fetchall("SELECT * FROM wallets WHERE inkey = ?", (request.headers["Grpc-Metadata-macaroon"],)) - pay_req = data["pay_req"] - payment_hash = data["payment_hash"] + if len(rows) == 0: + return jsonify({"ERROR": "NO KEY"}), 200 - con = db_connect() - cur = con.cursor() + dataj = {"amt": postedjson["value"], "memo": postedjson["memo"]} + headers = {"Authorization": f"Basic {INVOICE_KEY}"} + r = requests.post(url=f"{API_ENDPOINT}/addinvoice", json=dataj, headers=headers) + data = r.json() - cur.execute( - "INSERT INTO apipayments (payhash, amount, wallet, paid, inkey, memo) VALUES ('" - + payment_hash - + "','" - + postedjson["value"] - + "','" - + rows[0][0] - + "','0','" - + request.headers["Grpc-Metadata-macaroon"] - + "','" - + postedjson["memo"] - + "')" - ) - con.commit() - cur.close() + pay_req = data["pay_req"] + payment_hash = data["payment_hash"] - return jsonify({"pay_req": pay_req, "payment_hash": payment_hash}), 200 + db.execute( + "INSERT INTO apipayments (payhash, amount, wallet, paid, inkey, memo) VALUES (?, ?, ?, 0, ?, ?)'", + ( + payment_hash, + postedjson["value"], + rows[0][0], + request.headers["Grpc-Metadata-macaroon"], + postedjson["memo"], + ), + ) - else: - return jsonify({"ERROR": "NO KEY"}), 200 - else: - return jsonify({"ERROR": "NO MEMO"}), 200 - else: - return jsonify({"ERROR": "VALUE MUST BE A NUMMBER"}), 200 - else: - return jsonify({"ERROR": "NO VALUE"}), 200 - else: - return jsonify({"ERROR": "MUST BE JSON"}), 200 + return jsonify({"pay_req": pay_req, "payment_hash": payment_hash}), 200 # API requests