Implement new API callback

This commit is contained in:
Rene Vergara 2023-06-26 09:14:32 -05:00
parent 53466a6788
commit 3dd59c757f
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
2 changed files with 132 additions and 62 deletions

View file

@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- Security hardening changes to plugin queries.
- Re-design of ZGo API callback to support WooCommerce 7.8.0
## [1.0.0-beta.1] - 2023-01-11 ## [1.0.0-beta.1] - 2023-01-11
- Adjust production server integration - Adjust production server integration

View file

@ -3,11 +3,11 @@
* Plugin Name: ZGo Payment Gateway * Plugin Name: ZGo Payment Gateway
* Plugin URI: https://vergara.tech' * Plugin URI: https://vergara.tech'
* Description: ZGo latest payment processing solution for Woocommerce. Accept payments using Zcash. * Description: ZGo latest payment processing solution for Woocommerce. Accept payments using Zcash.
* Version: 1.0.0 beta * Version: 1.0.0
* Requires at least: 5.2 * Requires at least: 5.2
* Requires PHP: 7.2 * Requires PHP: 7.2
* Author: Vergara Tech LLC * Author: Vergara Tech LLC
* Author URI: https://vergara.tech * Author URI: https://zgo.cash/
* License: GPL v2 or later * License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html * License URI: https://www.gnu.org/licenses/gpl-2.0.html
**/ **/
@ -97,7 +97,13 @@ function zgopmt_init() {
/** /**
* Add the webhook for payment confirmation from ZGo * Add the webhook for payment confirmation from ZGo
*/ */
add_action( 'woocommerce_api_zpmtcallback', array($this,'zconfirm')); //add_action( 'woocommerce_api_zpmtcallback', array($this,'zconfirm'));
}
public function register_routes() {
register_rest_route( 'wc/v3', 'zgocallback', array(
'methods' => 'GET',
'callback' => array($this,'zgoapicallback')));
} }
public function init_form_fields() { public function init_form_fields() {
@ -283,58 +289,58 @@ function zgopmt_init() {
/** /**
* Confirm payment and complete order * Confirm payment and complete order
*/ */
public function zconfirm() { //public function zconfirm() {
global $wpdb; //global $wpdb;
$token = $_GET['token']; //$token = $_GET['token'];
$zgoOrderid = $_GET['orderid']; //$zgoOrderid = $_GET['orderid'];
$orderid = $_GET['wc_orderid']; //$orderid = $_GET['wc_orderid'];
$totalzec = $_GET['totalzec']; //$totalzec = $_GET['totalzec'];
$rate = $_GET['rate']; //$rate = $_GET['rate'];
$order = wc_get_order( $orderid ); //$order = wc_get_order( $orderid );
$sql = $wpdb->prepare('select * from zgo_payments where pmt_wc_order = %s ;', $orderid); //$sql = $wpdb->prepare('select * from zgo_payments where pmt_wc_order = %s ;', $orderid);
//$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';"; ////$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';";
$result = $wpdb->get_row($sql,OBJECT); //$result = $wpdb->get_row($sql,OBJECT);
if ( ! is_null($result) ) { //if ( ! is_null($result) ) {
if ( ($token == $this->zgotoken ) //if ( ($token == $this->zgotoken )
&& ( $result->pmt_orderid == $zgoOrderid ) //&& ( $result->pmt_orderid == $zgoOrderid )
&& ( $result->pmt_wc_paid == '0' ) ) { //&& ( $result->pmt_wc_paid == '0' ) ) {
switch ( $order->get_status() ) { //switch ( $order->get_status() ) {
case 'pending': //case 'pending':
case 'failed': //case 'failed':
$order->payment_complete(); //$order->payment_complete();
$order->reduce_order_stock(); //$order->reduce_order_stock();
// ////
// Mark order as completed in ZGo DB //// Mark order as completed in ZGo DB
// ////
//$sql = "update zgo_payments set " . ////$sql = "update zgo_payments set " .
//"pmt_confirmed='" . date('Y-m-d H:i:s') . ////"pmt_confirmed='" . date('Y-m-d H:i:s') .
//"', pmt_rate=" . $rate . ////"', pmt_rate=" . $rate .
//", pmt_zec=" . $totalzec . ////", pmt_zec=" . $totalzec .
//", pmt_wc_paid=1 " . ////", pmt_wc_paid=1 " .
//" where pmt_wc_order='" . $orderid . "';"; ////" where pmt_wc_order='" . $orderid . "';";
$sql2 = $wpdb->prepare('update zgo_payments set pmt_confirmed = %s, pmt_rate = %f, pmt_zec = %f, pmt_wc_paid = 1 where pmt_wc_order = %s;', date('Y-m-d H:i:s'), $rate, $totalzec, $orderid ); //$sql2 = $wpdb->prepare('update zgo_payments set pmt_confirmed = %s, pmt_rate = %f, pmt_zec = %f, pmt_wc_paid = 1 where pmt_wc_order = %s;', date('Y-m-d H:i:s'), $rate, $totalzec, $orderid );
$wpdb->query($sql2); //$wpdb->query($sql2);
update_option('webhook_debug', $_GET); //update_option('webhook_debug', $_GET);
break; //break;
default: //default:
//$this->console_log('Order ' . $orderid . ' already paid or cancelled...'); ////$this->console_log('Order ' . $orderid . ' already paid or cancelled...');
return array("message" => 'Order ' . $orderid . ' already paid or cancelled...'); //return array("message" => 'Order ' . $orderid . ' already paid or cancelled...');
break; //break;
} //}
} else { //} else {
//$this->console_log('Invalid parameters...'); ////$this->console_log('Invalid parameters...');
return array("message" => 'Invalid parameters...'); //return array("message" => 'Invalid parameters...');
} //}
} else { //} else {
//$this->console_log('Database error...'); ////$this->console_log('Database error...');
return array("message" => 'Database error...'); //return array("message" => 'Database error...');
} //}
} //}
public function thankyou_page () { public function thankyou_page () {
if ( $description = $this->get_description() ) { if ( $description = $this->get_description() ) {
@ -355,6 +361,64 @@ function zgopmt_init() {
return $edata; return $edata;
} }
public function zgoapicallback($request) {
global $wpdb;
//$token = $_GET['token'];
//$zgoOrderid = $_GET['orderid'];
//$orderid = $_GET['wc_orderid'];
//$totalzec = $_GET['totalzec'];
//$rate = $_GET['rate'];
$token = $request->get_param('token');
$zgoOrderid = $request->get_param('orderid');
$orderid = $request->get_param('wc_orderid');
$totalzec = $request->get_param('totalzec');
$rate = $request->get_param('rate');
$order = wc_get_order( $orderid );
$sql = $wpdb->prepare('select * from zgo_payments where pmt_wc_order = %s ;', $orderid);
//$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';";
$result = $wpdb->get_row($sql,OBJECT);
if ( ! is_null($result) ) {
if ( (hash('sha256',$token) == hash('sha256',$this->zgotoken) )
&& ( $result->pmt_orderid == $zgoOrderid )
&& ( $result->pmt_wc_paid == '0' ) ) {
switch ( $order->get_status() ) {
case 'pending':
case 'failed':
$order->payment_complete();
$order->reduce_order_stock();
//
// Mark order as completed in ZGo DB
//
//$sql = "update zgo_payments set " .
//"pmt_confirmed='" . date('Y-m-d H:i:s') .
//"', pmt_rate=" . $rate .
//", pmt_zec=" . $totalzec .
//", pmt_wc_paid=1 " .
//" where pmt_wc_order='" . $orderid . "';";
$sql2 = $wpdb->prepare('update zgo_payments set pmt_confirmed = %s, pmt_rate = %f, pmt_zec = %f, pmt_wc_paid = 1 where pmt_wc_order = %s;', date('Y-m-d H:i:s'), $rate, $totalzec, $orderid );
$wpdb->query($sql2);
//update_option('webhook_debug', $_GET);
return array('message' => 'Order '. $orderid . ' mark as paid correctly.');
break;
default:
//$this->console_log('Order ' . $orderid . ' already paid or cancelled...');
return array("message" => 'Order ' . $orderid . ' already paid or cancelled...');
break;
}
} else {
//$this->console_log('Invalid parameters...');
return array("message" => 'Invalid parameters...');
}
} else {
//$this->console_log('Database error...');
return array("message" => 'Database error...');
}
//return array( 'custom' => 'Data', "request" => $data->get_params() );
}
} }
@ -391,15 +455,15 @@ function get_custom($data) {
//$orderid = $_GET['wc_orderid']; //$orderid = $_GET['wc_orderid'];
//$totalzec = $_GET['totalzec']; //$totalzec = $_GET['totalzec'];
//$rate = $_GET['rate']; //$rate = $_GET['rate'];
$token = $data->token; $token = $data->get_param('token');
$zgoOrderid = $data->orderid; $zgoOrderid = $data->get_param('orderid');
$orderid = $data->wc_orderid; $orderid = $data->get_param('wc_orderid');
$totalzec = $data->totalzec; $totalzec = $data->get_param('totalzec');
$rate = $data->rate; $rate = $data->get_param('rate');
$order = wc_get_order( $orderid ); $order = wc_get_order( $orderid );
//$sql = $wpdb->prepare('select * from zgo_payments where pmt_wc_order = %s ;', $orderid); $sql = $wpdb->prepare('select * from zgo_payments where pmt_wc_order = %s ;', $orderid);
$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';"; //$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';";
$result = $wpdb->get_row($sql,OBJECT); $result = $wpdb->get_row($sql,OBJECT);
if ( ! is_null($result) ) { if ( ! is_null($result) ) {
@ -441,8 +505,8 @@ function get_custom($data) {
//return array( 'custom' => 'Data', "request" => $data->get_params() ); //return array( 'custom' => 'Data', "request" => $data->get_params() );
} }
add_action( 'rest_api_init', function () { function plugin_register_rest_apis() {
register_rest_route( 'wc/v3', 'custom', array( $zgoPlugin = new WC_ZGopmt_Gateway();
'methods' => 'GET', $zgoPlugin->register_routes();
'callback' => 'get_custom')); }
}); add_action( 'rest_api_init', 'plugin_register_rest_apis');