-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaskapp.py
77 lines (66 loc) · 2.5 KB
/
flaskapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask, flash, request, redirect, render_template, session
import datetime as dt
from lnbits import LNbitsCalls
#Flask
app = Flask(__name__)
app.secret_key = "test123" #CHANGE THIS
# CHANGE THESE
lnbits_kwargs = {
"host": "http://YOUR LN NODE TOR ADDRESS.onion",
"walletid": "YOUR LNBITS WALLET ID HERE",
"adminkey": "YOUR LNBITS ADMIN KEY HERE",
"invoicekey": "YOUR LNBITS INVOICE KEY HERE",
"tor": True
}
lnbits = LNbitsCalls(**lnbits_kwargs)
#Main UI
@app.route("/", methods=["GET"])
@app.route("/lightning", methods=["GET"])
def page_lightning():
return render_template("lightning.html", page="lightning")
#Payment QR Code
@app.route("/lightning/pay", methods=["GET"])
def page_pay():
return render_template("lightning.html", page="pay")
#LNBits actions
@app.route("/lnbits/<string:action>", methods=["GET"])
def api_lnbits(action=''):
data = {}
if action=="invoice": #create inbound invoice
session["invoice"] = lnbits.create_invoice(int(request.args["sats"]), request.args.get("memo"))
return redirect("/lightning/pay")
if action=="verify": #verify lnbits paid
if lnbits.verify_paid(session["invoice"]["payhash"]):
# =============================
#INSERT CODE: PAYMENT ACTION
# =============================
flash(str(session["invoice"]["sats"]) + ' sats received for ' + session["invoice"]["memo"], 'success')
session["invoice"] = {}
return redirect("/lightning")
else:
flash('payment not yet received', 'error')
return redirect("/lightning/pay")
if action=="withdraw": #pay outbound invoice
bolt11 = request.args.get("withdrawbolt11")
if bolt11:
amount = lnbits.bolt11amount(bolt11)
if 0 < amount:
# =============================
#INSERT CODE: WITHDRAWAL CONDITIONS
# =============================
res = lnbits.withdraw(bolt11)
if res.get("payment_hash"):
# =============================
#INSERT CODE: WITHDRAWAL ACTION
# =============================
flash('payment sent', 'success')
else:
flash(res.get("message"), 'error')
else:
flash('invoice not valid', 'error')
else:
flash('submit an invoice', 'error')
return redirect("/lightning")
if __name__ == "__main__":
debug = True
app.run(debug=debug)