SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit dac317dbc499388a09c059780a81663b47fe80dc
parent 737366dab02b9e03008edad687cbc86953c58914
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Tue, 22 Dec 2020 20:58:29 +1100

rename files, replace old server.py

Diffstat:
Ddemo.py | 121-------------------------------------------------------------------------------
Mserver.py | 152++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
2 files changed, 88 insertions(+), 185 deletions(-)

diff --git a/demo.py b/demo.py @@ -1,121 +0,0 @@ -from flask import Flask, render_template, session -from flask_socketio import SocketIO, emit, disconnect -from markupsafe import escape -import time - -import main -import config -import invoice -from pay import bitcoind - -async_mode = None -app = Flask(__name__) -app.config['SECRET_KEY'] = 'secret!' -socket_ = SocketIO(app, async_mode=async_mode) -# thread = None -# thread_lock = Lock() - -@app.route('/') -def index(): - return render_template('index.html', async_mode=socket_.async_mode) - -@socket_.on('initialise', namespace='/pay') -def test_message(message): - emit('payresponse', {'time_left': -1, 'response': message['data']}) - -@socket_.on('payment', namespace='/pay') -def make_payment(payload): - print("Requesting payment for {}".format(payload['amount'])) - - # Check the amount is a float - amount = payload['amount'] - try: - amount = float(amount) - except: - # Give response? - amount = None - return - - # Validate amount is a positive float - if not (isinstance(amount, float) and amount >= 0): - # Give response? - amount = None - return - - # Need to check this is safe! - label = payload['label'] - - # Initialise this payment - payment = create_invoice(amount, "USD", label) - - make_payment(payment) - - if payment.paid: - payment.status = 'Payment finalised.' - payment.response = 'Payment finalised.' - update_status(payment) - - ### DO SOMETHING - # Depends on config - # Get redirected? - # Nothing? - # Run custom script? - -def create_invoice(amount, currency, label): - payment_invoice = invoice.invoice(amount, currency, label) - payment = bitcoind.btcd(payment_invoice) - payment.get_address() - return payment - -def update_status(payment, console_status=True): - if console_status: - print(payment.status) - - emit('payresponse', { - 'status' : payment.status, - 'address' : payment.address, - 'amount' : payment.value, - 'time_left' : payment.time_left, - 'response': payment.response}) - return - -def make_payment(payment): - payment.status = 'Awaiting payment.' - payment.response = 'Awaiting payment.' - update_status(payment) - - # Track start_time for payment timeouts - payment.start_time = time.time() - while (time_left := config.payment_timeout - (time.time() - payment.start_time)) > 0: - payment.time_left = time_left - payment.confirmed_paid, payment.unconfirmed_paid = payment.check_payment() - - if payment.confirmed_paid > payment.value: - payment.paid = True - payment.status = "Payment successful! {} BTC".format(payment.confirmed_paid) - payment.response = "Payment successful! {} BTC".format(payment.confirmed_paid) - update_status(payment) - break - - elif payment.unconfirmed_paid > 0: - payment.status = "Discovered {} BTC payment. \ - Waiting for {} confirmations...".format(payment.unconfirmed_paid, config.required_confirmations) - payment.response = "Discovered {} BTC payment. \ - Waiting for {} confirmations...".format(payment.unconfirmed_paid, config.required_confirmations) - update_status(payment) - socket_.sleep(config.pollrate) - else: - payment.status = "Awaiting {} BTC payment...".format(payment.value) - payment.response = "Awaiting {} BTC payment...".format(payment.value) - update_status(payment) - socket_.sleep(config.pollrate) - else: - payment.status = "Payment expired." - payment.status = "Payment expired." - update_status(payment) - - return - - -if __name__ == '__main__': - socket_.run(app, debug=True) diff --git a/server.py b/server.py @@ -1,5 +1,5 @@ -from flask import Flask, request, url_for, jsonify, render_template -from flask_socketio import SocketIO +from flask import Flask, render_template, session +from flask_socketio import SocketIO, emit, disconnect from markupsafe import escape import time @@ -8,90 +8,114 @@ import config import invoice from pay import bitcoind +async_mode = None app = Flask(__name__) -socketio = SocketIO(app) +app.config['SECRET_KEY'] = 'secret!' +socket_ = SocketIO(app, async_mode=async_mode) +# thread = None +# thread_lock = Lock() -@app.route('/invoice', methods=['GET']) -def invoice(): - amount = request.values.get('amount') +@app.route('/') +def index(): + return render_template('index.html', async_mode=socket_.async_mode) +@socket_.on('initialise', namespace='/pay') +def test_message(message): + emit('payresponse', {'time_left': -1, 'response': message['data']}) + +@socket_.on('payment', namespace='/pay') +def make_payment(payload): + print("Requesting payment for {}".format(payload['amount'])) + + # Check the amount is a float + amount = payload['amount'] try: amount = float(amount) except: - print("Amount could not be interpreted as float {}".format(amount)) + # Give response? amount = None return - payment = create_invoice(amount, currency, label) - - - -@app.route('/pay', methods=['GET', 'POST']) -def pay(): - start_time = time.time() - - if True: #request.method == 'POST': - print("CALLING BITCOIND MAIN") - - amount = request.values.get('amount') - - try: - amount = float(amount) - except: - print("Amount could not be interpreted as float {}".format(amount)) - amount = None - return + # Validate amount is a positive float + if not (isinstance(amount, float) and amount >= 0): + # Give response? + amount = None + return - if (isinstance(amount, float) and amount >= 0): - print("Calling main payment function for {}".format(amount)) + # Need to check this is safe! + label = payload['label'] - if payment(amount, "USD", "wee"): - print("PAID") - return jsonify(paid=True) + # Initialise this payment + payment = create_invoice(amount, "USD", label) - else: - return jsonify(paid=False) + make_payment(payment) - else: - return jsonify(paid=False) + if payment.paid: + payment.status = 'Payment finalised.' + payment.response = 'Payment finalised.' + update_status(payment) + ### DO SOMETHING + # Depends on config + # Get redirected? + # Nothing? + # Run custom script? def create_invoice(amount, currency, label): - inv = invoice.invoice(amount, currency, label) - - payment = bitcoind.btcd() - payment.load_invoice(inv) + payment_invoice = invoice.invoice(amount, currency, label) + payment = bitcoind.btcd(payment_invoice) payment.get_address() return payment - -def payment(amount, currency, label): - payment = create_invoice(amount, currency, label) - - start_time = time.time() - while time.time() - start_time < config.payment_timeout: - conf_paid, unconf_paid = payment.check_payment() - print(conf_paid, unconf_paid) - - if conf_paid > payment.value: - print("Invoice {} paid! {} BTC.".format(payment.label, conf_paid)) +def update_status(payment, console_status=True): + if console_status: + print(payment.status) + + emit('payresponse', { + 'status' : payment.status, + 'address' : payment.address, + 'amount' : payment.value, + 'time_left' : payment.time_left, + 'response': payment.response}) + return + +def make_payment(payment): + payment.status = 'Awaiting payment.' + payment.response = 'Awaiting payment.' + update_status(payment) + + # Track start_time for payment timeouts + payment.start_time = time.time() + while (time_left := config.payment_timeout - (time.time() - payment.start_time)) > 0: + payment.time_left = time_left + payment.confirmed_paid, payment.unconfirmed_paid = payment.check_payment() + + if payment.confirmed_paid > payment.value: payment.paid = True - return True + payment.status = "Payment successful! {} BTC".format(payment.confirmed_paid) + payment.response = "Payment successful! {} BTC".format(payment.confirmed_paid) + update_status(payment) + break + + elif payment.unconfirmed_paid > 0: + payment.status = "Discovered {} BTC payment. \ + Waiting for {} confirmations...".format(payment.unconfirmed_paid, config.required_confirmations) + payment.response = "Discovered {} BTC payment. \ + Waiting for {} confirmations...".format(payment.unconfirmed_paid, config.required_confirmations) + update_status(payment) + socket_.sleep(config.pollrate) else: - time.sleep(15) - else: - print("Invoice {} expired.".format(payment.label)) - - if payment.paid: - print("Returning paid to site.") + payment.status = "Awaiting {} BTC payment...".format(payment.value) + payment.response = "Awaiting {} BTC payment...".format(payment.value) + update_status(payment) + socket_.sleep(config.pollrate) else: - print("Reload page, etc. need to create new invoice") + payment.status = "Payment expired." + payment.status = "Payment expired." + update_status(payment) - return False + return -if __name__ == "__main__": - socketio.run(app, debug=True) -#with app.test_client() as c: -# resp = c.post('/pay', data=dict(amount=69)) -# print(resp.data) +if __name__ == '__main__': + socket_.run(app, debug=True)