SatSale

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

commit 18a96c4366609863c2f01b768e2c8e7ea7ed9b5c
parent 84132138539e7a77c717f7a5e231626ced02273b
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Thu, 14 Jan 2021 23:37:02 +1100

Implement get api for payment requests, and initiate payment websocket

Diffstat:
Mgateways/btcpyment.php | 66++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mserver.py | 16++++++++++------
Mstatic/server_connection.js | 16+++++++---------
Mtemplates/index.html | 17++++++++++-------
4 files changed, 77 insertions(+), 38 deletions(-)

diff --git a/gateways/btcpyment.php b/gateways/btcpyment.php @@ -11,6 +11,21 @@ /* * This action hook registers our PHP class as a WooCommerce payment gateway */ + + if (!function_exists('write_log')) { + + function write_log($log) { + if (true === WP_DEBUG) { + if (is_array($log) || is_object($log)) { + error_log(print_r($log, true)); + } else { + error_log($log); + } + } + } + + } + add_filter( 'woocommerce_payment_gateways', 'btcpyment_add_gateway_class' ); function btcpyment_add_gateway_class( $gateways ) { $gateways[] = 'WC_btcpyment_Gateway'; // your class name is here @@ -61,7 +76,7 @@ function btcpyment_init_gateway_class() { add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); // You can also register a webhook here - // add_action( 'woocommerce_api_BTCPyment', array( $this, 'webhook' ) ); + add_action( 'woocommerce_api_btcpyment', array( $this, 'webhook' ) ); } /** @@ -142,17 +157,17 @@ function btcpyment_init_gateway_class() { if ( ! $this->testmode && ! is_ssl() ) { return; } - - // let's suppose it is our payment processor JavaScript that allows to obtain a token - wp_enqueue_script( 'btcpyment_js', 'https://btcpyment.nickfarrow.com/pay.js' ); - - // and this is our custom JS in your plugin directory that works with token.js - wp_register_script( 'woocommerce_btcpyment', plugins_url( 'btcpyment.js', __FILE__ ), array( 'jquery', 'btcpyment_js' ) ); - - // in most payment processors you have to use PUBLIC KEY to obtain a token - wp_localize_script( 'woocommerce_btcpyment', 'btcpyment_params', array( - 'publishableKey' => $this->publishable_key - ) ); + // + // // let's suppose it is our payment processor JavaScript that allows to obtain a token + // wp_enqueue_script( 'btcpyment_js', 'https://btcpyment.nickfarrow.com' ); + // + // // and this is our custom JS in your plugin directory that works with token.js + // wp_register_script( 'woocommerce_btcpyment', plugins_url( 'btcpyment.js', __FILE__ ), array( 'jquery', 'btcpyment_js' ) ); + // + // // in most payment processors you have to use PUBLIC KEY to obtain a token + // wp_localize_script( 'woocommerce_btcpyment', 'btcpyment_params', array( + // 'publishableKey' => $this->publishable_key + // ) ); wp_enqueue_script( 'woocommerce_btcpyment' ); @@ -174,16 +189,35 @@ function btcpyment_init_gateway_class() { * Array with parameters for API interaction */ $args = array( - bogus => 2 + 'amount' => 100 ); /* * Your API interaction could be built with wp_remote_post() */ - $response = wp_remote_post( '{payment processor endpoint}', $args ); + $redir_url = add_query_arg( + $args, + 'https://btcpyment.nickfarrow.com/payment' + ); + + write_log($redir_url); + + // $response = wp_remote_post( 'https://btcpyment.nickfarrow.com/', $args ); + $response = wp_remote_post($redir_url); + + write_log($response); + // + // $querystring = http_build_query( $payload ); + + // echo '<script>console.log("PHP error: ' . implode(", ", $redir_url) . '")</script>'; + // echo '<script>console.log("PHP error: ' . implode(", ", $response) . '")</script>'; + return [ + 'result' => 'success', + 'redirect' => $redir_url + ]; - if( !is_wp_error( $response ) ) { + if( !is_wp_error( $response ) ) { $body = json_decode( $response['body'], true ); @@ -212,7 +246,7 @@ function btcpyment_init_gateway_class() { } } else { - wc_add_notice( 'Connection error.', 'error' ); + wc_add_notice( 'Connection error.', 'error' ); return; } diff --git a/server.py b/server.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template, session +from flask import Flask, render_template, session, request from flask_socketio import SocketIO, emit, disconnect from markupsafe import escape import time @@ -18,20 +18,24 @@ socket_ = SocketIO(app, async_mode=async_mode, cors_allowed_origins="*") # Render html @app.route('/') def index(): + # TODO + # DONATION STYLE FORM PAGE return render_template('index.html', async_mode=socket_.async_mode) -@app.route('/payment', methods=['GET', 'POST']) -def payment(): - return render_template('index.html', async_mode=socket_.async_mode) +@app.route('/pay') +def payment_page(): + amount = request.args.get('amount') + payload={'amount' : amount} + return render_template('index.html', amount=amount, async_mode=socket_.async_mode) # Basic return on initialisation -@socket_.on('initialise', namespace='/pay') +@socket_.on('initialise') def test_message(message): emit('payresponse', {'time_left': -1, 'response': message['data']}) # Main payment method for websocket # Recieves form amount and initiates invoice and payment processing. -@socket_.on('payment', namespace='/pay') +@socket_.on('make_payment') def make_payment(payload): print("Requesting payment for {}".format(payload['amount'])) diff --git a/static/server_connection.js b/static/server_connection.js @@ -1,6 +1,6 @@ // Websocket logic, talks to server.py pay -$(document).ready(function() { - namespace = '/pay'; +function initiate(amount) { + namespace = '/'; var socket = io(namespace); socket.on('connect', function() { @@ -21,19 +21,17 @@ $(document).ready(function() { cb(); }); - $('form#pay').submit(function(event) { - socket.emit('payment', {'amount': $('#pay_data').val(), 'label' : null}); - return false; - }); -}); + socket.emit('make_payment', {'amount': amount, 'label' : null}); + return false +} // Additional steps to take when giving a response to the webpage // Update qr code, and hide timer function conditionalPageLogic(msg) { if (msg.address != null) { - document.getElementById('logo').classList.add("qr"); + // document.getElementById('logo').classList.add("qr"); // document.getElementById('logo').src = "static/qr_codes/" + msg.uuid + ".png"; - document.getElementById('logo').style.display = "none"; + // document.getElementById('logo').style.display = "none"; document.getElementById('qrImage').style.display = "block"; document.getElementById('qrClick').href = "/static/qr_codes/" + msg.uuid + ".png"; document.getElementById('qrImage').src = "/static/qr_codes/" + msg.uuid + ".png"; diff --git a/templates/index.html b/templates/index.html @@ -3,10 +3,13 @@ <head> <title>BTCPyment</title> <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}"> - + <script src="//code.jquery.com/jquery-1.12.4.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.4/socket.io.js"></script> <script src="{{ url_for('static', filename='server_connection.js') }}"></script> + <script type="text/javascript"> + initiate({{ amount }}); + </script> <style> @@ -80,15 +83,15 @@ <div id="paybox"> <div id="row" height="50px"> <div id="left" style="display:inline-block;" height="75px"> - <h1>Donate Bitcoin</h> + <h1>Pay Bitcoin</h> </div> <div id="right"> - <a id="logo" target="_blank"><img id="logo" style="padding-top:25px;padding-left:30px;" width="65px" src="https://i.pinimg.com/originals/3a/dd/40/3add4023fa9b435e7da3c09156b88015.png"></a> - <a id="qrClick" target="_blank"><img class="qr" id="qrImage" style="padding-top:25px;display:none" width="65px" src="https://i.pinimg.com/originals/3a/dd/40/3add4023fa9b435e7da3c09156b88015.png"></a> + <!-- <a id="logo" target="_blank"><img id="logo" style="padding-top:25px;padding-left:30px;" width="65px" src="https://i.pinimg.com/originals/3a/dd/40/3add4023fa9b435e7da3c09156b88015.png"></a> --> + <a id="qrClick" target="_blank"><img class="qr" id="qrImage" style="padding-top:25px;display:block" width="65px" src="https://i.pinimg.com/originals/3a/dd/40/3add4023fa9b435e7da3c09156b88015.png"></a> </div> </div> - <div id="paymentForm"> + <!-- <div id="paymentForm"> <center> <form id="pay" method="POST" action='#' style="margin:0;padding0;"> <h2 style="margin:0;padding0;">Amount: @@ -98,9 +101,9 @@ <input style="width:100%" type="submit" value="Pay" onclick="hideAmountShowPayment()"> </form> </center> - </div> + </div> --> - <div id="paymentDetails" style="display:none; padding: 0;"> + <div id="paymentDetails" style="display:block; padding: 0;"> <p style="padding:0;">Send: <b><span id="amount"></span></b> BTC</p> <p style="padding:0;">To: </p><b><p id="address" onclick="copyTextFromElement('address')"></p></b> <p style="padding:0;"><span id="status"></span></p>