ZGo Payment Gateway v1.1

This commit is contained in:
Rene V. Vergara A. 2022-12-23 14:45:41 -05:00
commit 3dcc192621
8 changed files with 518 additions and 0 deletions

58
README.md Normal file
View File

@ -0,0 +1,58 @@
# ZGo Payment Gateway plugin for Woocommerce
Contributors:
Tags: WordPres, WooCommerce, payments, ecommerce, e-commerce, store, sales, sell, shop, shopping, cart, checkout
Requires at least : Wordpress 6, WooCommerce 7
Tested up to: WordPress 6.1.1, WooComerce 7.1,0
Requires PHP: 7.1
Stable tag: 1.0
ZGo's payment processing solution for WooCommerce. This plugin implements a payment gateway to let buyers pay with Zcash
## Description
To Do
## Installation
1. Get your ZGo's OwnerID and Token for Woocommerce from your ZGo Shop Account
2. Download the `zgopmtggwy` installation package from Gitlab??? `/wp-content/plugins/` directory.
3. In WooCommerce Settings page go to the Payment Gateways tab, then click on "ZGo Payment", you will be redirected to ZGo Payment Settings page
4. Fill the ZGo OwnerId and ZGo Token fields
5. Click "Save changes" and return to WooCommerce Payments Tab
6. Check "Enable" button. The credentials will be verified and the plugin will be activated if they are confirmed.
## Usage
To Do
## Support
To Do
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
To Do
## License
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
## Project status
Development

34
ZGoPmtGwy.md Normal file
View File

@ -0,0 +1,34 @@
# Table of Contents
#### What is ZGo Payment Gateway for *WooCommerce*
#### How it Works?
#### System Requirements
#### Installing ZGo Payment Gateway
## What is ZGo Payment Gateway for *WooCommerce*
ZGo Payment Gateway for Woocomerce is a Plugin that allows a WooCommerce based online store, to receive payments using Zcash.
The Plugin connects WooCommerce Online Store with ZGo Backend to provide customers with an easy way to pay using a phone wallet.
## How it Works
The payment flow is showed bellow:
![ZGo PmtService Flow](ZGo_PmtGwy_Flow.png "ZGo PmtService Flow")
Figure 1 ZGo Payment GateWay Flow
The payment flow showed in Figure 1, includes the transaction confirmation and also marking customer's order as paid in WooComerce store database. ZGo Backend monitors the payment transaction and once it detects at least 5 blockchain confirmations, will report this to ZGo Payment Plugin.
## System Requirements
ZGo Business Account
WooComerce Based Online Store
## ZGo Payment Gateway Installation
### Configuring WooCommerce Store

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1021 B

BIN
assets/zgopmtgwy_logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
assets/zgopmtsrv_50px.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

11
changelog.md Normal file
View File

@ -0,0 +1,11 @@
*** Changelog ***
# ZGo Payment Plugin for WooCommerce
## Version 0.2 - 2022-11-07
* Initial alpha release
Uses test authentication and payent REST API ()
## Version 0.4 - 2022-11-18
integration to ZGo's Authentication End Point in test mode

415
zgopmtgwy.php Normal file
View File

@ -0,0 +1,415 @@
<?php
/**
* Plugin Name: ZGo Payment Gateway
* Plugin URI: https://vergara.tech'
* Description: ZGo latest payment processing solution. Accept payments using Zcash.
* Text Domain: zgopmtgwy
* Version: 0.5
* Author: Vergara Tech
* Author URI: https://vergara.tech
*
**/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! in_array( 'woocommerce/woocommerce.php',
apply_filters('active_plugins',
get_option('active_plugins') ) ) ) {
return;
}
add_action( 'plugins_loaded', 'zgopmt_init' );
function zgopmt_init() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
class WC_ZGopmt_Gateway extends WC_Payment_Gateway {
public $domain;
public $zgoownerid;
public $zgotoken;
public $siteURL;
public $zpmtdb;
/**
* Constructor for the gateway.
*/
public function __construct() {
global $wpdb;
$this->console_log("Gateway constructor accesed.");
$this->console_log('Create zgo_payments table if not exists...');
//
// Create payments table in WordPress database
//
$sql = 'create table if not exists zgo_payments (' .
'pmt_orderid varchar(64),' .
'pmt_wc_order varchar(20),' .
'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) )';
$this->console_log('Create table Query -> ' . $sql);
$wpdb->query($sql);
$this->console_log('zgopayments Table created in MySQL ...');
$iconurl = plugin_dir_url( __FILE__ ) .
'assets/zgo-icon-full_6pct.png';
$this->siteURL = get_site_url();
$this->console_log("Site URL: " . $this->siteURL);
$this->domain = 'zgopmt';
$this->id = "zgo_payment";
$this->icon = $iconurl;
$this->has_fields = false;
$this->method_title = __('ZGo Payment',
$this->domain);
$this->method_description = __('ZGo Payment - Accept payments using Zcash.', $this->domain);
// Load the settings.
$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_' .
$this->id,
array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
if ( ! $this->is_valid_for_use() )
$this->enabled = false;
/**
* Add the webhook for payment confirmation from ZGo
*/
add_action( 'woocommerce_api_zpmtcallback', array($this,'zconfirm'));
}
public function init_form_fields() {
$this->form_fields = apply_filters(
'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' => __( ' ',
$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' => __( ' ',
$this->domain ),
'desc_tip' => true,
'description' => __( 'Type or paste your ZGo Token (Found in your ZGo Shop Settings)',
$this->domain ),
),
)
);
}
/*
* Check if configuration is valid
*/
public function is_valid_for_use() {
$isvalid = false;
if ( isset($this->zgoownerid) &&
($this->zgoownerid !== '') ) {
$url = 'https://test.zgo.cash/auth?ownerid=' .
$this->zgoownerid . '&token=' .
$this->zgotoken . '&siteurl=' .
$this->base64url_encode($this->siteURL);
$this->console_log('URL -> ' . $url );
$response = wp_remote_get($url);
$this->console_log(json_encode($response));
$httpcode = wp_remote_retrieve_response_code( $response );
$this->console_log('Status Code -> ' . $httpcode );
switch ( $httpcode ) {
case 200:
$body = wp_remote_retrieve_body( $response );
$oid = json_decode($body);
$this->console_log('Body -> ' . $body);
$isvalid = $oid->{'authorized'};
break;
case 202:
$body = wp_remote_retrieve_body($response );
$oid = json_decode($body);
$this->console_log('message -> ' . $oid->{'message'});
break;
default:
$this->console_log('Conection error..');
break;
}
}
return $isvalid;
}
/*
* Process Payment
*/
public function process_payment( $order_id ) {
global $wpdb;
$this->console_log('Site URL ->' . $this->siteURL);
$this->console_log('Processing payment for order ' . $order_id);
$order = wc_get_order( $order_id );
// $wc_order = wc_get_product($order_id);
$wc_order_key = $order->get_order_key();
$this->console_log('Order ' . $order_id . ' key = ' . $wc_order_key);
$url = 'https://test.zgo.cash/woopayment' .
'?ownerid=' . $this->zgoownerid .
'&token=' . $this->zgotoken .
'&order_id=' . $order_id .
'&currency=' . strtolower($order->get_currency()) .
'&amount=' . $order->get_total() .
'&date=' . date_format($order->get_date_created(),'Y-m-d') .
'&siteurl=' . $this->base64url_encode($this->siteURL) .
'&orderkey=' . $wc_order_key;
//'&orderkey=' . ;
$this->console_log('ZGoPayment URL -> ' . $url);
$this->console_log('Calling wp_remote_get()');
$response = wp_remote_get($url);
$httpcode = wp_remote_retrieve_response_code( $response );
$this->console_log('Status Code ->' . $httpcode );
switch ( $httpcode ) {
case 200:
wc_add_notice( 'Order on hold, please wait for confirmation');
$order->update_status('on_hold',__('Awaiting payment confirmation','woocommerce'));
$this->console_log('Order ' . $order_id . ' status set to ON_HOLD');
$body = wp_remote_retrieve_body( $response );
$this->console_log('res. body = ' . $body);
$oid = json_decode($body);
$zgoOrderid = $oid->{'order'};
$this->console_log('ZGo order = ' . $zgoOrderid);
//
// Save ZGo Order ID and Cart order
//
$this->console_log("Preparing SQL insert statement");
$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)";
$this->console_log($sql);
$wpdb->query($sql);
// Remove cart.
WC()->cart->empty_cart();
$this->console_log($this->get_return_url( $order ));
return array(
'result' => 'success',
'redirect' => 'https://dev.zgo.cash/invoice/' . $zgoOrderid,
);
break;
case 202:
$body = wp_remote_retrieve_body( $response );
$this->console_log('res. body = ' . $body);
$msg = json_decode($body);
$this->console_log('Order ' . $order_id . ' -> ZGo Order Generation Error : ' . $msg->{'message'});
$order->update_status('failed',__('Order ' . $order_id . ' -> ZGo Order Generation Error : ' . $msg->{'message'},'woocommerce'));
break;
default:
return;
}
}
/**
* Confirm payment and complete order
*/
public function zconfirm() {
global $wpdb;
$this->console_log('zconfirm called ');
$token = $_GET['token'];
$zgoOrderid = $_GET['orderid'];
$orderid = $_GET['wc_orderid'];
$totalzec = $_GET['totalzec'];
$rate = $_GET['rate'];
$order = wc_get_order( $orderid );
$sql = "select * from zgo_payments where pmt_wc_order = '" . $orderid . "';";
$this->console_log('SQL -> ' . $sql);
$result = $wpdb->get_row($sql,OBJECT);
if ( ! is_null($result) ) {
$this->console_log('Query successfull...');
$this->console_log('totalzec -> ', $totalzec);
$this->console_log('rate -> ', $rate);
$this->console_log('pmt_wc_paid=' . $result->pmt_wc_paid);
$this->console_log('local token -> ' . $this->zgotoken);
$this->console_log('received token -> ' . $token);
$this->console_log('zgoOrderid -> ' . $zgoOrderid);
$this->console_log('pmt_orderid -> ' . $result->pmt_orderid);
if ( ( $token == $this->zgotoken )
&& ( $result->pmt_orderid == $zgoOrderid )
&& ( $result->pmt_wc_paid == '0' ) ) {
$this->console_log('Test successfull...');
$this->console_log('Order status -> ' . $order->get_status());
switch ( $order->get_status() ) {
case 'pending':
case 'failed':
$this->console_log('Confirming payment for order ' . $orderid);
$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 . "';";
/*
$this->zpmtdb->exec($sql);
*/
$this->console_log($sql);
$wpdb->query($sql);
$this->console_log('Order marked as paid in zgo_payments table...');
update_option('webhook_debug', $_GET);
break;
default:
$this->console_log('Order ' . $orderid . ' already paid or cancelled...');
break;
}
} else {
$this->console_log('Invalid parameters...');
}
} else {
$this->console_log('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/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), '+/', '-_'));
// $this->console_log('data -> ' . $data);
// $this->console_log('edata -> ' . $edata);
return $edata;
}
}
add_filter( 'woocommerce_payment_gateways',
'add_custom_gateway_class' );
function add_custom_gateway_class( $methods ) {
if ( ! in_array('WC_ZGopmt_Gateway', $methods) ) {
$methods[] = 'WC_ZGopmt_Gateway';
}
return $methods;
}
}