commit 24a0ae9cae40d455a1785ead730af2b462fe9f1d
parent 31033e7c332085556154021bd6d47e10ae391795
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date: Thu, 4 Feb 2021 23:19:43 +1100
Implement toggle between lightning and on-chain
Diffstat:
4 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/config.py b/config.py
@@ -35,6 +35,7 @@ pay_method = "bitcoind"
# Switch payment_method to lnd if you want to use lightning payments instead. And uncomment lnd_dir.
#pay_method = "lnd"
#lnd_dir = "~/.lnd/"
+#lnd_rpcport = "10009"
# DO NOT CHANGE THIS TO TRUE UNLESS YOU WANT ALL PAYMENTS TO AUTOMATICALLY
# BE CONSIDERED AS PAID.
diff --git a/pay/lnd.py b/pay/lnd.py
@@ -20,7 +20,7 @@ class lnd(invoice):
self.copy_certs()
# Conect to lightning node
- connection_str = "{}:{}".format(config.host, config.rpcport)
+ connection_str = "{}:{}".format(config.host, config.lnd_rpcport)
print(
"Attempting to connect to lightning node {}. This may take a few minutes...".format(
connection_str
@@ -32,7 +32,7 @@ class lnd(invoice):
print("Attempting to initialise lnd rpc client...")
time.sleep(3)
self.lnd = LNDClient(
- "{}:{}".format(config.host, config.rpcport),
+ "{}:{}".format(config.host, config.lnd_rpcport),
macaroon_filepath="admin.macaroon",
cert_filepath="tls.cert",
)
diff --git a/server.py b/server.py
@@ -54,6 +54,7 @@ def index():
@app.route("/pay")
def payment_page():
params = dict(request.args)
+ params['lnd_enabled'] = (config.pay_method == "lnd")
# Render payment page with the request arguments (?amount= etc.)
return render_template("index.html", params=params, async_mode=socket_.async_mode)
@@ -77,8 +78,17 @@ def make_payment(payload):
else:
label = "donation"
+ # Get payment method, use one specified in query string if provided
+ if "method" in payload.keys():
+ if payload['method'] == 'lnd':
+ payment_method = payload['method']
+ else:
+ raise Exception("Invalid payment method supplied to query string: {}".format(payload['method']))
+ else:
+ payment_method = config.pay_method
+
# Initialise the payment invoice
- payment = create_invoice(amount, "USD", label)
+ payment = create_invoice(amount, "USD", label, payment_method)
# Wait for the amount to be sent to the address
process_payment(payment)
@@ -139,10 +149,10 @@ def update_status(payment, status, console_status=True):
# Initialise the payment via the payment method (bitcoind / lightningc / etc),
-def create_invoice(dollar_amount, currency, label):
- if config.pay_method == "bitcoind":
+def create_invoice(dollar_amount, currency, label, payment_method=config.pay_method):
+ if payment_method == "bitcoind":
payment = bitcoind.btcd(dollar_amount, currency, label)
- elif config.pay_method == "lnd":
+ elif payment_method == "lnd":
payment = lnd.lnd(dollar_amount, currency, label)
else:
print("Invalid payment method")
diff --git a/templates/index.html b/templates/index.html
@@ -110,11 +110,22 @@
<p id="timerContainer" style="padding:0;"><span id="timer"></span> seconds remaining.</p>
</div>
- <div id="information" style="text-align: right; padding: 0;">
- <small><a id="about" href="https://github.com/nickfarrow/BTCPyment" target="_blank">BTCPyment</a></small>
- </div>
+ <div id="row">
+ <div id="left" style="text-align: left; padding: 0;">
+ <!-- Alternate between offering lightning or on chain payment switch -->
+ {% if params.lnd_enabled %}
+ {% if params.method != "lnd"%}
+ <button onclick="replaceUrlParam(window.location, 'method', 'lnd');">Lightning Payment</button>
+ {% else %}
+ <button onclick="replaceUrlParam(window.location, 'method', 'btcd');">On-chain Payment</button>
+ {% endif %}
+ {% endif %}
+ </div>
- <div id="paybutton"></div>
+ <div id="right" style="text-align: right; padding: 0;">
+ <small><a id="about" href="https://github.com/nickfarrow/BTCPyment" target="_blank">BTCPyment</a></small>
+ </div>
+ </div>
</div>
</body>
</html>