The issue i'm facing is updating my order status from pending to completed or processing on successful payment. I'm suspecting that my gateway is not getting response. Here is my entire source code, please help me review
<?php
session_start();
/**
* Plugin Name: Woocommerce cashenvoy WebPay Module
* Description: A payment gateway plugin for Woocommerce for the cashenvoy WebPay system
* Author: Alex Onyia
* Email: winexviv@gmail.com
* Author URI: http://www.winexviv.com
* Version: 1.4
*/
function alex_webpay_init() {
if (!class_exists('WC_Payment_Gateway')) {
return;
}
class WC_WebPay extends WC_Payment_Gateway {
public function __construct() {
global $woocommerce;
$this->id = 'cashenvoy';
//$this->icon = apply_filters('woocommerce_cashenvoy_icon', 'http://www.cashenvoyng.com/images/logo.gif');
$this->has_fields = false;
$this->liveurl = 'https://www.cashenvoy.com/webservice/?cmd=cepay';
$this->testurl = 'https://www.cashenvoy.com/sandbox/?cmd=cepay';
$this->method_title = __( 'cashenvoy - WebPay', 'woocommerce' );
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
$this->product_id = $this->settings['product_id'];
$this->pay_item_id = $this->settings['pay_item_id'];
$this->testmode = $this->settings['testmode'];
$this->debug = $this->settings['debug'];
$this->debug_email = $this->settings['debug_email'];
$this->thanks_message = $this->settings['thanks_message'];
$this->error_message = $this->settings['error_message'];
$this->feedback_message = '';
$this->response_codes = $this->get_response_codes();
// Actions
add_action( 'init', array(&$this, 'check_ipn_response') );
add_action('valid-cashenvoy-ipn-request', array(&$this, 'successful_request') );
add_action('woocommerce_receipt_cashenvoy', array(&$this, 'receipt_page'));
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
add_action('woocommerce_get_transaction_status', array(&$this, 'get_transaction_status'));
add_action('woocommerce_thankyou_' . $this->id, array(&$this, 'thankyou_page'));
//Filters
add_filter('woocommerce_currencies', array($this, 'add_ngn_currency'));
add_filter('woocommerce_currency_symbol', array($this, 'add_ngn_currency_symbol'), 10, 2);
// Logs
if ($this->debug=='yes') $this->log = $woocommerce->logger();
if ( !$this->is_valid_for_use() ) $this->enabled = false;
}
function add_ngn_currency($currencies) {
$currencies['NGN'] = __( 'Nigerian Naira (NGN)', 'woocommerce' );
return $currencies;
}
function add_ngn_currency_symbol($currency_symbol, $currency) {
switch( $currency ) {
case 'NGN':
$currency_symbol = '?';
break;
}
return $currency_symbol;
}
function is_valid_for_use() {
$return = true;
if (!in_array(get_option('woocommerce_currency'), array('NGN'))) {
$return = false;
}
return $return;
}
function admin_options() {
echo '<h3>' . __('cashenvoy WebPay', 'woocommerce') . '</h3>';
echo '<p>' . __('cashenvoy WebPay works by sending the user to cashenvoy to enter their payment information.', 'woocommerce') . '</p>';
echo '<table class="form-table">';
if ( $this->is_valid_for_use() ) {
$this->generate_settings_html();
} else {
echo '<div class="inline error"><p><strong>' . __( 'Gateway Disabled', 'woocommerce' ) . '</strong>: ' . __( 'cashenvoy does not support your store currency.', 'woocommerce' ) . '</p></div>';
}
echo '</table>';
}
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable cashenvoy WebPay', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'cashenvoy WebPay', 'woocommerce' )
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce' ),
'default' => __("Pay via cashenvoy WebPay;", 'woocommerce')
),
'thanks_message' => array(
'title' => __( 'Thanks message', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'The message to show on a successful payment', 'woocommerce' ),
'default' => __("Thank you. Your order has been received", 'woocommerce')
),
'error_message' => array(
'title' => __( 'Failure message', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'The message to show when a payment has failed', 'woocommerce' ),
'default' => __("Sorry. There was a problem with your order", 'woocommerce')
),
'product_id' => array(
'title' => __( 'Product ID', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Product Identifier for PAYDirect. Provided to you by cashenvoy', 'woocommerce' ),
'default' => ''
),
'pay_item_id' => array(
'title' => __( 'Pay Item ID', 'woocommerce' ),
'type' => 'text',
'description' => __( 'PAYDirect Payment Item ID', 'woocommerce' ),
'default' => ''
),
'testmode' => array(
'title' => __( 'cashenvoy Test Mode', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable cashenvoy Test Mode', 'woocommerce' ),
'default' => 'yes'
),
'debug' => array(
'title' => __( 'Debug', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable logging (<code>woocommerce/logs/cashenvoy.txt</code>)', 'woocommerce' ),
'default' => 'no'
),
'debug_email' => array(
'title' => __( 'IPN Debug Email', 'woocommerce' ),
'type' => 'text',
'label' => __( 'Email address to send IPN info to. Used for debugging. Blank for no email', 'woocommerce' ),
'default' => ''
)
);
}
function payment_fields() {
if ($this->description) {
echo wpautop(wptexturize($this->description));
}
}
function get_cashenvoy_args( $order ) {
global $woocommerce;
$cetxref ="Nj".time().mt_rand(0,9999);
$order_id = $order->id . '_' . $this->product_id .'_'. $cetxref;
$redirect_url = $this->get_return_url($order);
$order_total = round(((number_format($order->get_order_total() + $order->get_order_discount(), 2, '.', ''))*100),0);
$cememo = 'New product(s) from Myneria Naturals';
if ($this->debug=='yes') {
$this->log->add( 'cashenvoy', 'Generating payment form for order #' . $order_id . '.');
}
$cashenvoy_args = array(
'ce_merchantid' => '629',
'ce_transref' => $cetxref,
'ce_amount' => $order_total,
'site_name' => site_url(),
'ce_memo' => $cememo,
'ce_notifyurl' => $redirect_url,
'ce_customerid' => trim($order->billing_first_name . ' ' . $order->billing_last_name),
);
if (isset($order->user_id)) {
$cashenvoy_args['cust_id'] = $order->user_id;
}
$cashenvoy_args = apply_filters('woocommerce_cashenvoy_args', $cashenvoy_args);
return $cashenvoy_args;
}
function generate_cashenvoy_form( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$cashenvoy_args = $this->get_cashenvoy_args( $order );
$cashenvoy_args_array = array();
$cashenvoy_adr = $this->liveurl;
if ( $this->testmode == 'yes' ) {
$cashenvoy_adr = $this->testurl;
}
foreach ($cashenvoy_args as $key => $value) {
$cashenvoy_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
}
$woocommerce->add_inline_js('
jQuery("body").block({
message: "<img src=\"'.esc_url( $woocommerce->plugin_url() ).'/assets/images/ajax-loader.gif\" alt=\"Redirecting...\" style=\"float:left; margin-right: 10px;\" />'.__('Thank you for your order. We are now redirecting you to cashenvoy to make payment.', 'woocommerce').'",
overlayCSS: {
background: "#fff",
opacity: 0.6
},
css: {
padding: 20,
textAlign: "center",
color: "#555",
border: "3px solid #aaa",
backgroundColor:"#fff",
cursor: "wait",
lineHeight: "32px"
}
});
jQuery("#submit_cashenvoy_payment_form").click();
');
$form = '<form action="'.esc_url( $cashenvoy_adr ).'" method="post" id="cashenvoy_payment_form">
' . implode('', $cashenvoy_args_array) . '
<input type="submit" class="button-alt" id="submit_cashenvoy_payment_form" value="'.__('Pay via cashenvoy', 'woocommerce').'" /> <a class="button cancel" href="'.esc_url( $order->get_cancel_order_url() ).'">'.__('Cancel order & restore cart', 'woocommerce').'</a>
</form>';
return $form;
}
function generate_cashenvoy_try_again_form( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$cashenvoy_args = $this->get_cashenvoy_args( $order );
$cashenvoy_args_array = array();
$cashenvoy_adr = $this->liveurl;
if ( $this->testmode == 'yes' ) {
$cashenvoy_adr = $this->testurl;
}
foreach ($cashenvoy_args as $key => $value) {
$cashenvoy_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
}
$form = '<form action="'.esc_url( $cashenvoy_adr ).'" method="post" id="cashenvoy_payment_form">
' . implode('', $cashenvoy_args_array) . '
<input type="submit" class="button-alt" id="submit_cashenvoy_payment_form" value="'.__('Try Again', 'woocommerce').'" />
</form>';
return $form;
}
function successful_request( $posted ) {
global $woocommerce;
$order_ref = $posted['ce_transref'];
$order = new WC_Order( (int) $order_ref );
//fool the thanks page into working?
$_GET['key'] = $order->order_key;
$_GET['order'] - $order->id;
if ($this->get_transaction_status($order_ref)) {
// Check order not already completed
if ($order->status == 'completed') :
if ($this->debug=='yes') {
$this->log->add( 'cashenvoy', 'Aborting, Order #' . $order_ref . ' is already complete.' );
}
return false;
endif;
// Payment completed
$order->add_order_note( __('IPN payment completed', 'woocommerce') );
$order->payment_complete();
$woocommerce->cart->empty_cart();
if ($this->debug=='yes') $this->log->add( 'cashenvoy', 'Payment complete.' );
foreach ($_GET as $k=>$v) {
update_post_meta((int)$order_ref, $k, $v);
}
update_post_meta( (int) $order_ref, 'Payment Method', 'cashenvoy');
$this->feedback_message = $this->thanks_message;
} else {
$error_code = '';
if (@$_GET['resp']) {
$error_code = $this->response_codes[$_GET['resp']];
}
$try_again = $this->generate_cashenvoy_try_again_form($this->parse_txn_ref_order_id($order_ref));
$order->add_order_note(__('Payment Failed - ' . $error_code, 'woocommerce'));
$order->update_status('failed');
$woocommerce->add_error('Transaction Failed: ' . $error_code . ' ' . $try_again); // Transaction Failed Notice on Checkout
$this->feedback_message = $this->failed_message . $error_code . ' ' . $try_again;
if ($this->debug=='yes') $this->log->add( 'cashenvoy', 'Payment error: ' . $error_code . ' raw data: ' . serialize($_GET));
}
}
function thankyou_page() {
echo wpautop($this->feedback_message);
}
public function get_response_codes() {
return array(
'C00'=>'CashEnvoy transaction successful. <br>Reference: $order_id'
,'C04'=>'CashEnvoy transaction denied. <br>Reason: Insufficient funds. <br>Reference: $order_id'
,'C05'=>'CashEnvoy transaction failed. <br>Reason: ".@$a."Error occurred. Contact support@cashenvoy.com <br>Reference: $order_id'
,'C03'=>'CashEnvoy transaction failed. <br>Reason: No transaction record. <br>Reference: $order_id'
);
}
function cashenvoy_thanks() {
echo $this->response_codes(urldecode($_GET['desc']));
if ($GET['desc'] != 'C00') {
//$this->generate_cashenvoy_form($_POST['ce_transref']);
}
}
function parse_txn_ref_order_id($txnref) {
$txnref = htmlentities($txnref);
$txn_details = explode('_', $txnref);
$product_id = $txn_details[1];
$order_id = $txn_details[0];
return $order_id;
}
function process_payment( $order_id ) {
$order = new WC_Order( $order_id );
return array(
'result' => 'success',
'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay'))))
);
}
function receipt_page( $order ) {
echo '<p>'.__('Thank you for your order, please click the button below to pay with cashenvoy.', 'woocommerce').'</p>';
echo $this->generate_cashenvoy_form( $order );
}
function check_ipn_response() {
if (isset($_POST['retRef'])) {
@ob_clean();
$_POST = stripslashes_deep($_POST);
header('HTTP/1.1 200 OK');
do_action("valid-cashenvoy-ipn-request", $_POST);
}
}
function get_transaction_status($transref,$mertid,$type='') {
global $woocommerce;
$cetxref ="Nj".time().mt_rand(0,9999);
$_SESSION['cetxref']=$cetxref;
$transref = $_SESSION['cetxref'];
$mertid = 629;
$type = '';
$endpoint = "https://www.cashenvoy.com/webservice/?cmd=requery";
if ($this->testmode) {
$endpoint = "https://www.cashenvoy.com/sandbox/?cmd=requery";
}
$request = 'mertid='.$mertid.'&transref='.$transref.'&respformat='.$type;
echo $request;
$ch = curl_init(); //initialize curl handle
curl_setopt($ch, CURLOPT_URL, $endpoint); //set the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //return as a variable
curl_setopt($ch, CURLOPT_POST, 1); //set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); //set the POST variables
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch); // grab URL and pass it to the browser. Run the whole process and return the response
curl_close($ch); //close the curl handle
return $response;
}
}
/**
* Add the gateway to WooCommerce
**/
function add_webpay_gateway( $methods ) {
$methods[] = 'WC_WebPay'; return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_webpay_gateway' );
}
add_filter('plugins_loaded', 'alex_webpay_init' );
?>