SatSale

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

commit 0ebedc9cd4eafbccb9f9aa81f2906b446b3e2d0e
parent f2d1c9e0b0e62f5c17edd63c55a2b05ba18385ca
Author: Nick <nicholas.w.farrow@gmail.com>
Date:   Mon, 12 Jul 2021 17:46:13 +1000

Fix woohook (#13)


Diffstat:
Mgateways/woo_satsale.php | 5+++--
Mgateways/woo_webhook.py | 7++++---
Msatsale.py | 11++++++++---
Mstatic/satsale.js | 20++++++++++++++------
4 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/gateways/woo_satsale.php b/gateways/woo_satsale.php @@ -126,7 +126,7 @@ function satsale_init_gateway_class() { global $woocommerce; - // we need it to get any order detailes + // we need it to get any order details $order = wc_get_order( $order_id ); // We need to store a signature of the data, and check it later during the webhook to confirm it is the same! @@ -135,7 +135,8 @@ function satsale_init_gateway_class() { */ $args = array( 'amount' => $order->get_total(), - 'w_url' => $this->callback_URL ); + 'w_url' => $this->callback_URL, + 'id' => $order_id) write_log($args); diff --git a/gateways/woo_webhook.py b/gateways/woo_webhook.py @@ -6,12 +6,12 @@ import time import requests -def hook(satsale_secret, invoice): +def hook(satsale_secret, invoice, order_id): key = codecs.decode(satsale_secret, "hex") # Calculate a secret that is required to send back to the # woocommerce gateway, proving we did not modify id nor amount. - secret_seed = str(int(100 * float(invoice["amount"]))).encode( + secret_seed = str(int(100 * float(invoice["dollar_value"]))).encode( "utf-8" ) print("Secret seed: {}".format(secret_seed)) @@ -23,6 +23,7 @@ def hook(satsale_secret, invoice): params = { "wc-api": "wc_satsale_gateway", "time": str(paid_time), + "id": order_id } message = (str(paid_time) + "." + json.dumps(params, separators=(",", ":"))).encode( "utf-8" @@ -37,6 +38,6 @@ def hook(satsale_secret, invoice): } # Send the webhook response, confirming the payment with woocommerce. - response = requests.get(invoice["w_url"], params=params, headers=headers) + response = requests.get(invoice["webhook"], params=params, headers=headers) return response diff --git a/satsale.py b/satsale.py @@ -122,7 +122,9 @@ class create_payment(Resource): if payment_method is None: payment_method = config.pay_method webhook = request.args.get("w_url") + print(webhook) if webhook is None: + print("NO WEBHOOK SUPPLIED") webhook = None # Create the payment using one of the connected nodes as a base @@ -212,16 +214,19 @@ class complete_payment(Resource): "Complete Payment" """Run post-payment processing such as any webhooks.""" uuid = request.args.get("uuid") + order_id = request.args.get("id") invoice = load_invoice_from_db(uuid) status = check_payment_status(uuid) if status["time_left"] < 0: - return {"expired"}, 400 + return {"message": "Expired."}, 400 + + print(invoice) if (invoice["webhook"] != None) and (invoice["webhook"] != ""): - # Call webhook - response = woo_webhook.hook(app.config["SECRET_KEY"], invoice) + print("Calling webhook {}".format(invoice["webhook"])) + response = woo_webhook.hook(app.config["SECRET_KEY"], invoice, order_id) if response.status_code != 200: err = "Failed to confirm order payment via webhook {}, please contact the store to ensure the order has been confirmed, error response is: {}".format( diff --git a/static/satsale.js b/static/satsale.js @@ -2,7 +2,14 @@ function payment(payment_data) { $('document').ready(function(){ var payment_uuid; - $.get("/api/createpayment", {amount: payment_data.amount, method: payment_data.method}).then(function(data) { + var invoiceData = {amount: payment_data.amount, method: payment_data.method}; + + // If a webhook URL is provided (woocommerce) + if (payment_data.w_url) { + invoiceData['w_url'] = payment_data.w_url + } + + $.get("/api/createpayment", invoiceData).then(function(data) { invoice = data.invoice; payment_uuid = invoice.uuid; @@ -23,8 +30,8 @@ function payment(payment_data) { } function check_payment(payment_uuid, checkinterval, payment_data) { - $.get("/api/checkpayment", {uuid: payment_uuid}).then(function(payment_data) { - payment_status = payment_data.status; + $.get("/api/checkpayment", {uuid: payment_uuid}).then(function(checkpayment_data) { + payment_status = checkpayment_data.status; console.log(payment_status); if (payment_status.expired == 1) { $('#status').text("Payment expired.").html(); @@ -36,8 +43,8 @@ function check_payment(payment_uuid, checkinterval, payment_data) { if (payment_status.payment_complete == 1) { $('#status').text("Payment confirmed.").html(); document.getElementById('timerContainer').style.visibility = "hidden"; - clearInterval(checkinterval); complete_payment(payment_uuid, payment_data); + // clearInterval(checkinterval); return 1; } else { @@ -54,11 +61,12 @@ function check_payment(payment_uuid, checkinterval, payment_data) { } function complete_payment(payment_uuid, payment_data) { - setTimeout(() => { window.location.replace(payment_data.redirect); }, 5000); - $.get("/api/completepayment", {uuid: payment_uuid}).then(function(payment_completion) { + var order_id = location.search.split('id=')[1]; + $.get("/api/completepayment", {uuid: payment_uuid, id: order_id}).then(function(payment_completion) { console.log(payment_completion); $('#status').text(payment_completion.message).html(); }); + setTimeout(() => { window.location.replace(payment_data.redirect); }, 5000); } function load_qr(payment_uuid) {