2022-12-23 19:45:41 +00:00
< ? php
/**
2023-06-23 16:49:12 +00:00
* Plugin Name : ZGo Payment Gateway
* Plugin URI : https :// vergara . tech '
* Description : ZGo latest payment processing solution for Woocommerce . Accept payments using Zcash .
2023-06-26 14:14:32 +00:00
* Version : 1.0 . 0
2023-06-23 16:49:12 +00:00
* Requires at least : 5.2
* Requires PHP : 7.2
* Author : Vergara Tech LLC
2023-06-26 14:14:32 +00:00
* Author URI : https :// zgo . cash /
2023-06-23 16:49:12 +00:00
* License : GPL v2 or later
* License URI : https :// www . gnu . org / licenses / gpl - 2.0 . html
**/
2022-12-23 19:45:41 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2023-06-23 16:49:12 +00:00
exit ; // Exit if accessed directly
2022-12-23 19:45:41 +00:00
}
if ( ! in_array ( 'woocommerce/woocommerce.php' ,
2023-06-23 16:49:12 +00:00
apply_filters ( 'active_plugins' ,
get_option ( 'active_plugins' ) ) ) ) {
2022-12-23 19:45:41 +00:00
return ;
}
add_action ( 'plugins_loaded' , 'zgopmt_init' );
function zgopmt_init () {
2023-06-23 16:49:12 +00:00
if ( ! class_exists ( 'WC_Payment_Gateway' ) ) {
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
return ;
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
}
2022-12-23 19:45:41 +00:00
class WC_ZGopmt_Gateway extends WC_Payment_Gateway {
public $domain ;
public $zgoownerid ;
public $zgotoken ;
public $siteURL ;
2023-06-23 16:49:12 +00:00
public $zpmtdb ;
/**
* Constructor for the gateway .
*/
public function __construct () {
global $wpdb ;
//
// Create payments table in WordPress database
//
$sql = 'create table if not exists zgo_payments (' .
'pmt_orderid varchar(64),' .
'pmt_wc_order varchar(20),' .
2022-12-23 19:45:41 +00:00
'pmt_wc_custname varchar(100),' .
'pmt_accepted varchar(30),' .
'pmt_confirmed varchar(30),' .
'pmt_amount double (12,2) not null default 0.0,' .
'pmt_rate double (8,2) not null default 0.0,' .
'pmt_zec double (12,8) not null default 0.0,' .
'pmt_wc_paid int not null default 0,' .
'unique pmt_orderix (pmt_orderid, pmt_wc_order) )' ;
2023-06-23 16:49:12 +00:00
$wpdb -> query ( $sql );
2022-12-23 19:45:41 +00:00
$iconurl = plugin_dir_url ( __FILE__ ) .
2023-06-23 16:49:12 +00:00
'assets/img/zgo-icon-full_6pct.png' ;
2022-12-23 19:45:41 +00:00
$this -> siteURL = get_site_url ();
2022-12-26 22:10:06 +00:00
$this -> domain = 'zgopmtgwy' ;
2022-12-23 19:45:41 +00:00
$this -> id = " zgo_payment " ;
$this -> icon = $iconurl ;
$this -> has_fields = false ;
$this -> method_title = __ ( 'ZGo Payment' ,
2023-06-23 16:49:12 +00:00
$this -> domain );
2022-12-23 19:45:41 +00:00
$this -> method_description = __ ( 'ZGo Payment - Accept payments using Zcash.' , $this -> domain );
2023-06-23 16:49:12 +00:00
// Load the settings.
2022-12-23 19:45:41 +00:00
$this -> init_form_fields ();
$this -> init_settings ();
$this -> title = $this -> get_option ( 'title' );
$this -> description = $this -> get_option ( 'description' );
$this -> instructions = $this -> get_option ( 'instructions' , $this -> description );
$this -> zgoownerid = $this -> get_option ( 'zgoownerid' );
$this -> zgotoken = $this -> get_option ( 'zgotoken' );
// Actions
add_action ( 'woocommerce_update_options_payment_gateways_' .
2023-06-23 16:49:12 +00:00
$this -> id ,
array ( $this , 'process_admin_options' ) );
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
add_action ( 'woocommerce_thankyou_' . $this -> id , array ( $this , 'thankyou_page' ) );
2022-12-23 19:45:41 +00:00
if ( ! $this -> is_valid_for_use () )
$this -> enabled = false ;
2023-06-23 18:06:40 +00:00
2022-12-23 19:45:41 +00:00
/**
2023-06-23 16:49:12 +00:00
* Add the webhook for payment confirmation from ZGo
*/
2023-06-26 14:14:32 +00:00
//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' )));
2023-06-23 16:49:12 +00:00
}
2022-12-23 19:45:41 +00:00
public function init_form_fields () {
$this -> form_fields = apply_filters (
2023-06-23 16:49:12 +00:00
'woo_zgopmtsrv_fields' , array (
'enabled' => array (
'title' => __ ( 'Enable/Disable' ,
$this -> domain ),
'type' => 'checkbox' ,
'label' => __ ( 'Enable payments with Zcash' , $this -> domain ),
'default' => 'yes'
),
'title' => array (
'title' => __ ( 'ZGo Payment Service title' ,
$this -> domain ),
'type' => 'text' ,
'default' => __ ( 'ZGo Payment Gateway' ,
$this -> domain ),
'desc_tip' => true ,
'description' => __ ( 'Add a new title for the ZGo Payment Service that your customers will see when they are in the checkout page' ,
$this -> domain ),
),
'description' => array (
'title' => __ ( 'ZGo Payment Service Confirmation' ,
$this -> domain ),
'type' => 'textarea' ,
'default' => __ ( '<b>Pay with Zcash</b>, ZGo will report your payment as soon as it gets confirmed. Normally it takes about 5 minutes.<br> <a href="https://zgo.cash" target="_blank">Read more...</a>' ,
$this -> domain ),
'desc_tip' => true ,
'description' => __ ( 'Payment confirmation description that the customer will see on your checkout.' ,
$this -> domain ),
),
'instructions' => array (
'title' => __ ( 'Instructions' ,
$this -> domain ),
'type' => 'textarea' ,
'default' => __ ( 'Default instrctions' ,
$this -> domain ),
'desc_tip' => true ,
'description' => __ ( 'Instruction that will be added to the Thank You page and order email' ,
$this -> domain ),
),
'zgoownerid' => array (
'title' => __ ( 'ZGo OwnerId' ,
$this -> domain ),
'type' => 'text' ,
'default' => __ ( 'Replace this text with your ZGo Owner ID ' ,
$this -> domain ),
'desc_tip' => true ,
'description' => __ ( 'Type or paste your ZGo Account Owner Id (Found in your ZGo Shop Settings)' ,
$this -> domain ),
),
'zgotoken' => array (
'title' => __ ( 'ZGo Token' ,
$this -> domain ),
'type' => 'text' ,
'default' => __ ( 'Replace this text with your ZGo Token' ,
$this -> domain ),
'desc_tip' => true ,
'description' => __ ( 'Type or paste your ZGo Token (Found in your ZGo Shop Settings)' ,
$this -> domain ),
),
)
2022-12-23 19:45:41 +00:00
);
}
/*
2023-06-23 16:49:12 +00:00
* Check if configuration is valid
*/
public function is_valid_for_use () {
$isvalid = false ;
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
if ( isset ( $this -> zgoownerid ) &&
( $this -> zgoownerid !== '' ) ) {
2022-12-23 19:45:41 +00:00
2023-06-23 15:46:13 +00:00
$url = 'https://test.zgo.cash/auth?ownerid=' .
2023-06-23 16:49:12 +00:00
$this -> zgoownerid . '&token=' .
$this -> zgotoken . '&siteurl=' .
$this -> base64url_encode ( $this -> siteURL );
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
$response = wp_remote_get ( $url );
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
$httpcode = wp_remote_retrieve_response_code ( $response );
switch ( $httpcode ) {
2022-12-23 19:45:41 +00:00
case 200 :
$body = wp_remote_retrieve_body ( $response );
$oid = json_decode ( $body );
2023-06-23 16:49:12 +00:00
$isvalid = $oid -> { 'authorized' };
break ;
case 202 :
$body = wp_remote_retrieve_body ( $response );
2022-12-23 19:45:41 +00:00
$oid = json_decode ( $body );
2023-06-23 16:49:12 +00:00
break ;
2022-12-23 19:45:41 +00:00
default :
2023-06-23 16:49:12 +00:00
break ;
}
}
2022-12-23 19:45:41 +00:00
return $isvalid ;
}
/*
2023-06-23 16:49:12 +00:00
* Process Payment
*/
2022-12-23 19:45:41 +00:00
public function process_payment ( $order_id ) {
global $wpdb ;
$order = wc_get_order ( $order_id );
2023-06-23 16:49:12 +00:00
// $wc_order = wc_get_product($order_id);
2022-12-23 19:45:41 +00:00
$wc_order_key = $order -> get_order_key ();
2023-06-23 16:08:17 +00:00
$url = 'https://test.zgo.cash/woopayment' .
2022-12-23 19:45:41 +00:00
'?ownerid=' . $this -> zgoownerid .
'&token=' . $this -> zgotoken .
2023-06-23 16:49:12 +00:00
'&order_id=' . $order_id .
2022-12-23 19:45:41 +00:00
'¤cy=' . strtolower ( $order -> get_currency ()) .
'&amount=' . $order -> get_total () .
'&date=' . date_format ( $order -> get_date_created (), 'Y-m-d' ) .
2023-06-23 16:49:12 +00:00
'&siteurl=' . $this -> base64url_encode ( $this -> siteURL ) .
'&orderkey=' . $wc_order_key ;
2022-12-23 19:45:41 +00:00
2023-06-23 16:49:12 +00:00
//'&orderkey=' . ;
2022-12-23 19:45:41 +00:00
$response = wp_remote_get ( $url );
$httpcode = wp_remote_retrieve_response_code ( $response );
switch ( $httpcode ) {
2023-06-23 16:49:12 +00:00
case 200 :
wc_add_notice ( 'Order on hold, please wait for confirmation' );
$order -> update_status ( 'on_hold' , __ ( 'Awaiting payment confirmation' , 'woocommerce' ));
$body = wp_remote_retrieve_body ( $response );
$oid = json_decode ( $body );
$zgoOrderid = $oid -> { 'order' };
$zgoOrderToken = $oid -> { 'token' };
//
// Save ZGo Order ID and Cart order
//
$sql3 = $wpdb -> prepare ( 'replace into zgo_payments (pmt_orderid, pmt_wc_order, pmt_wc_custname, pmt_accepted, pmt_confirmed, pmt_amount, pmt_rate, pmt_zec, pmt_wc_paid) values (%s, %s, %s, %s, %s, %f, 0, 0, 0);' ,
$zgoOrderid , $order_id , $order -> get_billing_first_name () . ' ' . $order -> get_billing_last_name (), date ( 'Y-m-d H:i:s' ), '' , $order -> get_total ());
//$sql = "replace into zgo_payments (" .
//"pmt_orderid," .
//"pmt_wc_order," .
//"pmt_wc_custname," .
//"pmt_accepted," .
//"pmt_confirmed," .
//"pmt_amount," .
//"pmt_rate," .
//"pmt_zec," .
//"pmt_wc_paid) values ('" .
//$zgoOrderid . "','" .
//$order_id . "','" .
//$order->get_billing_first_name() . " " .
//$order->get_billing_last_name() . "','" .
//date('Y-m-d H:i:s') . "','',".
//$order->get_total() .
//",0,0,0)";
$wpdb -> query ( $sql3 );
// Remove cart.
WC () -> cart -> empty_cart ();
return array (
'result' => 'success' ,
'redirect' => 'https://dev.zgo.cash/invoice/' . $zgoOrderid . '?token=' . $zgoOrderToken ,
);
break ;
case 202 :
$body = wp_remote_retrieve_body ( $response );
$msg = json_decode ( $body );
$order -> update_status ( 'failed' , __ ( 'Order ' . $order_id . ' -> ZGo Order Generation Error : ' . $msg -> { 'message' }, 'woocommerce' ));
break ;
default :
return ;
2022-12-23 19:45:41 +00:00
}
}
/**
2023-06-23 16:49:12 +00:00
* Confirm payment and complete order
*/
2023-06-26 14:14:32 +00:00
//public function zconfirm() {
//global $wpdb;
//$token = $_GET['token'];
//$zgoOrderid = $_GET['orderid'];
//$orderid = $_GET['wc_orderid'];
//$totalzec = $_GET['totalzec'];
//$rate = $_GET['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 ( ($token == $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);
//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...');
//}
//}
public function thankyou_page () {
if ( $description = $this -> get_description () ) {
echo wpautop ( wptexturize ( $description ) );
}
}
public function console_log ( $data ) {
$file = plugin_dir_path ( __DIR__ ) . '/zgopmtgwy/assets/log/console.log' ;
file_put_contents ( $file , $data . chr ( 0x0D ) . chr ( 0x0A ), FILE_TEXT | FILE_APPEND | LOCK_EX );
}
public function base64url_encode ( $data ) {
$edata = str_replace ( '=' , '' , strtr ( base64_encode ( $data ), '+/' , '-_' ));
return $edata ;
}
2022-12-23 19:45:41 +00:00
2023-06-26 14:14:32 +00:00
public function zgoapicallback ( $request ) {
2023-06-23 16:49:12 +00:00
global $wpdb ;
2023-06-26 14:14:32 +00:00
//$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' );
2022-12-23 19:45:41 +00:00
$order = wc_get_order ( $orderid );
2023-06-23 18:06:40 +00:00
$sql = $wpdb -> prepare ( 'select * from zgo_payments where pmt_wc_order = %s ;' , $orderid );
2023-06-23 14:09:25 +00:00
//$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';";
2022-12-23 19:45:41 +00:00
$result = $wpdb -> get_row ( $sql , OBJECT );
if ( ! is_null ( $result ) ) {
2023-06-26 14:14:32 +00:00
if ( ( hash ( 'sha256' , $token ) == hash ( 'sha256' , $this -> zgotoken ) )
2023-06-23 14:09:25 +00:00
&& ( $result -> pmt_orderid == $zgoOrderid )
&& ( $result -> pmt_wc_paid == '0' ) ) {
2022-12-23 19:45:41 +00:00
switch ( $order -> get_status () ) {
2023-06-23 14:09:25 +00:00
case 'pending' :
case 'failed' :
$order -> payment_complete ();
$order -> reduce_order_stock ();
//
// Mark order as completed in ZGo DB
//
//$sql = "update zgo_payments set " .
2023-06-23 16:49:12 +00:00
//"pmt_confirmed='" . date('Y-m-d H:i:s') .
//"', pmt_rate=" . $rate .
//", pmt_zec=" . $totalzec .
//", pmt_wc_paid=1 " .
//" where pmt_wc_order='" . $orderid . "';";
2023-06-23 14:09:25 +00:00
$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 );
2023-06-26 14:14:32 +00:00
//update_option('webhook_debug', $_GET);
return array ( 'message' => 'Order ' . $orderid . ' mark as paid correctly.' );
2023-06-23 14:09:25 +00:00
break ;
default :
2023-06-23 18:06:40 +00:00
//$this->console_log('Order ' . $orderid . ' already paid or cancelled...');
return array ( " message " => 'Order ' . $orderid . ' already paid or cancelled...' );
2023-06-23 14:09:25 +00:00
break ;
2022-12-23 19:45:41 +00:00
}
} else {
2023-06-23 18:06:40 +00:00
//$this->console_log('Invalid parameters...');
return array ( " message " => 'Invalid parameters...' );
2022-12-23 19:45:41 +00:00
}
} else {
2023-06-23 18:06:40 +00:00
//$this->console_log('Database error...');
return array ( " message " => 'Database error...' );
2022-12-23 19:45:41 +00:00
}
2023-06-26 14:14:32 +00:00
//return array( 'custom' => 'Data', "request" => $data->get_params() );
2022-12-23 19:45:41 +00:00
}
2023-06-23 18:06:40 +00:00
2023-06-23 16:49:12 +00:00
}
2022-12-23 19:45:41 +00:00
add_filter ( 'woocommerce_payment_gateways' ,
2023-06-23 16:49:12 +00:00
'add_custom_gateway_class' );
2022-12-23 19:45:41 +00:00
function add_custom_gateway_class ( $methods ) {
if ( ! in_array ( 'WC_ZGopmt_Gateway' , $methods ) ) {
2023-06-23 16:49:12 +00:00
$methods [] = 'WC_ZGopmt_Gateway' ;
}
return $methods ;
2022-12-23 19:45:41 +00:00
}
2022-12-30 20:54:06 +00:00
}
// Include the dependencies needed to instantiate the plugin.
foreach ( glob ( plugin_dir_path ( __FILE__ ) . 'assets/php/*.php' ) as $file ) {
include_once $file ;
}
add_action ( 'plugins_loaded' , 'zgopmtlist_plugin' );
function zgopmtlist_plugin () {
2023-06-23 16:49:12 +00:00
$path = plugin_dir_path ( __FILE__ );
$plugin = new zpmt_stats ( new zpmt_stats_page ( $path ) );
$plugin -> init ();
2022-12-23 19:45:41 +00:00
}
2023-06-23 18:06:40 +00:00
function get_custom ( $data ) {
global $wpdb ;
//$token = $_GET['token'];
//$zgoOrderid = $_GET['orderid'];
//$orderid = $_GET['wc_orderid'];
//$totalzec = $_GET['totalzec'];
//$rate = $_GET['rate'];
2023-06-26 14:14:32 +00:00
$token = $data -> get_param ( 'token' );
$zgoOrderid = $data -> get_param ( 'orderid' );
$orderid = $data -> get_param ( 'wc_orderid' );
$totalzec = $data -> get_param ( 'totalzec' );
$rate = $data -> get_param ( 'rate' );
2023-06-23 18:06:40 +00:00
$order = wc_get_order ( $orderid );
2023-06-26 14:14:32 +00:00
$sql = $wpdb -> prepare ( 'select * from zgo_payments where pmt_wc_order = %s ;' , $orderid );
//$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';";
2023-06-23 18:06:40 +00:00
$result = $wpdb -> get_row ( $sql , OBJECT );
if ( ! is_null ( $result ) ) {
if ( ( $token == $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);
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() );
}
2023-06-26 14:14:32 +00:00
function plugin_register_rest_apis () {
$zgoPlugin = new WC_ZGopmt_Gateway ();
$zgoPlugin -> register_routes ();
}
add_action ( 'rest_api_init' , 'plugin_register_rest_apis' );