Fixed LNticket extension

This commit is contained in:
arcbtc 2020-08-10 19:06:56 +01:00
parent 410e96771e
commit 0603e525ff
6 changed files with 66 additions and 51 deletions

View File

@ -10,28 +10,43 @@ from .models import Tickets, Forms
#######TICKETS########
def create_ticket(wallet: str, form: str, name: str, email: str, ltext: str, sats: int) -> Tickets:
formdata = get_form(form)
amount = formdata.amountmade + sats
def create_ticket(checking_id: str, wallet: str, form: str, name: str, email: str, ltext: str, sats: int) -> Tickets:
with open_ext_db("lnticket") as db:
ticket_id = urlsafe_short_hash()
db.execute(
"""
INSERT INTO tickets (id, form, email, ltext, name, wallet, sats)
VALUES (?, ?, ?, ?, ?, ?, ?)
INSERT INTO tickets (id, paid, form, email, ltext, name, wallet, sats)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(ticket_id, form, email, ltext, name, wallet, sats),
(checking_id, False, form, email, ltext, name, wallet, sats),
)
return get_ticket(checking_id)
def update_ticket(paid: bool, checking_id: str) -> Tickets:
with open_ext_db("lnticket") as db:
row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (checking_id,))
if row[1] == True:
return get_ticket(checking_id)
db.execute(
"""
UPDATE tickets
SET paid = ?
WHERE id = ?
""",
(paid, checking_id),
)
formdata = get_form(row[2])
amount = formdata.amountmade + row[7]
db.execute(
"""
UPDATE forms
SET amountmade = ?
WHERE id = ?
""",
(amount, form),
(amount, row[2]),
)
return get_ticket(ticket_id)
return get_ticket(checking_id)
def get_ticket(ticket_id: str) -> Optional[Tickets]:

View File

@ -20,6 +20,7 @@ def m001_initial(db):
"""
CREATE TABLE IF NOT EXISTS tickets (
id TEXT PRIMARY KEY,
paid BOOLEAN NOT NULL,
form TEXT NOT NULL,
email TEXT NOT NULL,
ltext TEXT NOT NULL,

View File

@ -13,6 +13,7 @@ class Forms(NamedTuple):
class Tickets(NamedTuple):
id: str
paid: bool
form: str
email: str
ltext: str

View File

@ -138,14 +138,18 @@
Invoice: function () {
var self = this
axios
.get(
.post(
'/lnticket/api/v1/tickets/' +
'{{ form_id }}' +
'/' +
self.formDialog.data.sats
'{{ form_id }}/' +
self.formDialog.data.sats,
{
form: '{{ form_id }}',
name: self.formDialog.data.name,
email: self.formDialog.data.email,
ltext: self.formDialog.data.text,
sats: self.formDialog.data.sats
}
)
.then(function (response) {
self.paymentReq = response.data.payment_request
self.paymentCheck = response.data.checking_id
@ -160,21 +164,10 @@
status: 'pending',
paymentReq: self.paymentReq
}
if (self.formDialog.data.email == '') {
daemail = 'null'
} else {
daemail = self.formDialog.data.email
}
paymentChecker = setInterval(function () {
axios
.post('/lnticket/api/v1/tickets/' + self.paymentCheck, {
form: '{{ form_id }}',
name: self.formDialog.data.name,
email: daemail,
ltext: self.formDialog.data.text,
sats: self.formDialog.data.sats
})
.get('/lnticket/api/v1/tickets/' + self.paymentCheck)
.then(function (res) {
if (res.data.paid) {
clearInterval(paymentChecker)

View File

@ -7,7 +7,7 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.settings import WALLET
from lnbits.extensions.lnticket import lnticket_ext
from .crud import create_ticket, get_ticket, get_tickets, delete_ticket, create_form, update_form, get_form, get_forms, delete_form
from .crud import create_ticket, update_ticket, get_ticket, get_tickets, delete_ticket, create_form, update_form, get_form, get_forms, delete_form
#########FORMS##########
@ -82,22 +82,7 @@ def api_tickets():
return jsonify([form._asdict() for form in get_tickets(wallet_ids)]), HTTPStatus.OK
@lnticket_ext.route("/api/v1/tickets/<form_id>/<sats>", methods=["GET"])
def api_ticket_create(form_id, sats):
form = get_form(form_id)
try:
checking_id, payment_request = create_invoice(
wallet_id=form.wallet, amount=int(sats), memo=f"#lnticket {form_id}"
)
except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK
@lnticket_ext.route("/api/v1/tickets/<checking_id>", methods=["POST"])
@lnticket_ext.route("/api/v1/tickets/<form_id>/<sats>", methods=["POST"])
@api_validate_post_request(
schema={
"form": {"type": "string", "empty": False, "required": True},
@ -106,27 +91,47 @@ def api_ticket_create(form_id, sats):
"ltext": {"type": "string", "empty": False, "required": True},
"sats": {"type": "integer", "min": 0, "required": True}
})
def api_ticket_send_ticket(checking_id):
def api_ticket_make_ticket(form_id, sats):
form = get_form(g.data['form'])
if not form:
event = get_form(form_id)
if not event:
return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND
try:
checking_id, payment_request = create_invoice(
wallet_id=event.wallet, amount=int(sats), memo=f"#lnticket {form_id}"
)
except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
ticket = create_ticket(checking_id=checking_id, wallet=event.wallet, **g.data)
if not ticket:
return jsonify({"message": "LNTicket could not be fetched."}), HTTPStatus.NOT_FOUND
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK
@lnticket_ext.route("/api/v1/tickets/<checking_id>", methods=["GET"])
def api_ticket_send_ticket(checking_id):
theticket = get_ticket(checking_id)
try:
is_paid = not WALLET.get_invoice_status(checking_id).pending
except Exception:
return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND
if is_paid:
wallet = get_wallet(form.wallet)
wallet = get_wallet(theticket.wallet)
payment = wallet.get_payment(checking_id)
payment.set_pending(False)
create_ticket(wallet=form.wallet, **g.data)
ticket = update_ticket(paid=True, checking_id=checking_id)
return jsonify({"paid": True}), HTTPStatus.OK
return jsonify({"paid": True, "ticket_id": ticket.id}), HTTPStatus.OK
return jsonify({"paid": False}), HTTPStatus.OK
@lnticket_ext.route("/api/v1/tickets/<ticket_id>", methods=["DELETE"])
@api_check_wallet_key("invoice")
def api_ticket_delete(ticket_id):