2020-09-02 12:44:54 -03:00
|
|
|
import re
|
2020-05-08 21:03:18 +01:00
|
|
|
from flask import g, jsonify, request
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
from lnbits.core.crud import get_user, get_wallet
|
2020-08-31 22:12:46 -03:00
|
|
|
from lnbits.core.services import create_invoice, check_invoice_status
|
2020-05-08 21:03:18 +01:00
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
|
|
|
|
|
|
|
from lnbits.extensions.lnticket import lnticket_ext
|
2020-08-30 23:19:43 -03:00
|
|
|
from .crud import (
|
|
|
|
create_ticket,
|
|
|
|
update_ticket,
|
|
|
|
get_ticket,
|
|
|
|
get_tickets,
|
|
|
|
delete_ticket,
|
|
|
|
create_form,
|
|
|
|
update_form,
|
|
|
|
get_form,
|
|
|
|
get_forms,
|
|
|
|
delete_form,
|
|
|
|
)
|
2020-05-08 21:03:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
#########FORMS##########
|
|
|
|
|
2020-08-30 23:19:43 -03:00
|
|
|
|
2020-05-08 21:03:18 +01:00
|
|
|
@lnticket_ext.route("/api/v1/forms", methods=["GET"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
def api_forms():
|
|
|
|
wallet_ids = [g.wallet.id]
|
|
|
|
|
|
|
|
if "all_wallets" in request.args:
|
|
|
|
wallet_ids = get_user(g.wallet.user).wallet_ids
|
|
|
|
|
|
|
|
return jsonify([form._asdict() for form in get_forms(wallet_ids)]), HTTPStatus.OK
|
|
|
|
|
|
|
|
|
|
|
|
@lnticket_ext.route("/api/v1/forms", methods=["POST"])
|
|
|
|
@lnticket_ext.route("/api/v1/forms/<form_id>", methods=["PUT"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"wallet": {"type": "string", "empty": False, "required": True},
|
|
|
|
"name": {"type": "string", "empty": False, "required": True},
|
|
|
|
"description": {"type": "string", "min": 0, "required": True},
|
2020-08-30 23:19:43 -03:00
|
|
|
"costpword": {"type": "integer", "min": 0, "required": True},
|
2020-05-08 21:03:18 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
def api_form_create(form_id=None):
|
|
|
|
if form_id:
|
|
|
|
form = get_form(form_id)
|
|
|
|
|
|
|
|
if not form:
|
|
|
|
return jsonify({"message": "Form does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if form.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your form."}), HTTPStatus.FORBIDDEN
|
|
|
|
|
|
|
|
form = update_form(form_id, **g.data)
|
|
|
|
else:
|
|
|
|
form = create_form(**g.data)
|
|
|
|
return jsonify(form._asdict()), HTTPStatus.CREATED
|
|
|
|
|
|
|
|
|
|
|
|
@lnticket_ext.route("/api/v1/forms/<form_id>", methods=["DELETE"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
def api_form_delete(form_id):
|
|
|
|
form = get_form(form_id)
|
|
|
|
|
|
|
|
if not form:
|
|
|
|
return jsonify({"message": "Form does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if form.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your form."}), HTTPStatus.FORBIDDEN
|
|
|
|
|
|
|
|
delete_form(form_id)
|
|
|
|
|
|
|
|
return "", HTTPStatus.NO_CONTENT
|
|
|
|
|
2020-08-30 23:19:43 -03:00
|
|
|
|
2020-05-08 21:03:18 +01:00
|
|
|
#########tickets##########
|
|
|
|
|
2020-08-30 23:19:43 -03:00
|
|
|
|
2020-05-08 21:03:18 +01:00
|
|
|
@lnticket_ext.route("/api/v1/tickets", methods=["GET"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
def api_tickets():
|
|
|
|
wallet_ids = [g.wallet.id]
|
|
|
|
|
|
|
|
if "all_wallets" in request.args:
|
|
|
|
wallet_ids = get_user(g.wallet.user).wallet_ids
|
|
|
|
|
|
|
|
return jsonify([form._asdict() for form in get_tickets(wallet_ids)]), HTTPStatus.OK
|
|
|
|
|
|
|
|
|
2020-09-02 12:44:54 -03:00
|
|
|
@lnticket_ext.route("/api/v1/tickets/<form_id>", methods=["POST"])
|
2020-08-10 19:06:56 +01:00
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"form": {"type": "string", "empty": False, "required": True},
|
|
|
|
"name": {"type": "string", "empty": False, "required": True},
|
2020-09-02 12:44:54 -03:00
|
|
|
"email": {"type": "string", "empty": True, "required": True},
|
2020-08-10 19:06:56 +01:00
|
|
|
"ltext": {"type": "string", "empty": False, "required": True},
|
2020-08-30 23:19:43 -03:00
|
|
|
}
|
|
|
|
)
|
2020-09-02 12:44:54 -03:00
|
|
|
def api_ticket_make_ticket(form_id):
|
|
|
|
form = get_form(form_id)
|
|
|
|
if not form:
|
2020-08-10 19:06:56 +01:00
|
|
|
return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND
|
2020-05-08 21:03:18 +01:00
|
|
|
try:
|
2020-09-02 12:44:54 -03:00
|
|
|
nwords = len(re.split(r"\s+", g.data["ltext"]))
|
|
|
|
sats = nwords * form.costpword
|
2020-08-31 22:12:46 -03:00
|
|
|
payment_hash, payment_request = create_invoice(
|
2020-09-02 12:44:54 -03:00
|
|
|
wallet_id=form.wallet,
|
|
|
|
amount=sats,
|
|
|
|
memo=f"ticket with {nwords} words on {form_id}",
|
|
|
|
extra={"tag": "lnticket"},
|
2020-05-08 21:03:18 +01:00
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
|
|
|
|
2020-09-02 12:44:54 -03:00
|
|
|
ticket = create_ticket(payment_hash=payment_hash, wallet=form.wallet, sats=sats, **g.data)
|
2020-05-08 21:03:18 +01:00
|
|
|
|
2020-08-10 19:06:56 +01:00
|
|
|
if not ticket:
|
|
|
|
return jsonify({"message": "LNTicket could not be fetched."}), HTTPStatus.NOT_FOUND
|
2020-05-08 21:03:18 +01:00
|
|
|
|
2020-08-31 22:12:46 -03:00
|
|
|
return jsonify({"payment_hash": payment_hash, "payment_request": payment_request}), HTTPStatus.OK
|
2020-05-08 21:03:18 +01:00
|
|
|
|
|
|
|
|
2020-08-31 22:12:46 -03:00
|
|
|
@lnticket_ext.route("/api/v1/tickets/<payment_hash>", methods=["GET"])
|
|
|
|
def api_ticket_send_ticket(payment_hash):
|
|
|
|
ticket = get_ticket(payment_hash)
|
2020-05-08 21:03:18 +01:00
|
|
|
try:
|
2020-08-31 22:12:46 -03:00
|
|
|
is_paid = not check_invoice_status(ticket.wallet, payment_hash).pending
|
2020-05-08 21:03:18 +01:00
|
|
|
except Exception:
|
|
|
|
return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if is_paid:
|
2020-08-31 22:12:46 -03:00
|
|
|
wallet = get_wallet(ticket.wallet)
|
|
|
|
payment = wallet.get_payment(payment_hash)
|
2020-05-08 21:03:18 +01:00
|
|
|
payment.set_pending(False)
|
2020-08-31 22:12:46 -03:00
|
|
|
ticket = update_ticket(paid=True, payment_hash=payment_hash)
|
2020-08-10 19:06:56 +01:00
|
|
|
return jsonify({"paid": True, "ticket_id": ticket.id}), HTTPStatus.OK
|
2020-05-08 21:03:18 +01:00
|
|
|
|
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
|
|
|
|
2020-08-30 23:19:43 -03:00
|
|
|
|
2020-05-08 21:03:18 +01:00
|
|
|
@lnticket_ext.route("/api/v1/tickets/<ticket_id>", methods=["DELETE"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
def api_ticket_delete(ticket_id):
|
|
|
|
ticket = get_ticket(ticket_id)
|
|
|
|
|
|
|
|
if not ticket:
|
|
|
|
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if ticket.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your ticket."}), HTTPStatus.FORBIDDEN
|
|
|
|
|
|
|
|
delete_ticket(ticket_id)
|
|
|
|
|
2020-05-09 18:07:31 +01:00
|
|
|
return "", HTTPStatus.NO_CONTENT
|