CashU in woocommerce

i have follow Problem:
i did created a cash u U integration to woo-commerce, work wonderful. but now the call back notification from cash u doesn’t work.
after payment i want the thank you message to be appeared and the product as complete.

i did make the follow code but dont know whats next. i am stuck. please help 🙁

Read More
<?php

add_action('plugins_loaded', 'woocommerce_promex_cashu_init', 0);
function woocommerce_promex_cashu_init(){
 if(!class_exists('WC_Payment_Gateway')) return;

 class WC_Promex_Cashu extends WC_Payment_Gateway{
 public function __construct(){
  $this -> id = 'cashu';
  $this -> medthod_title = 'Cash U Payment';
  $this -> has_fields = false;
  $this -> init_form_fields();
  $this -> init_settings();
  $this -> title = $this -> settings['title'];
  $this -> description = $this -> settings['description'];
  $this -> merchant_id = $this -> settings['merchant_id'];
  $this -> service_name = $this -> settings['service_name'];
  $this -> display_text = $this -> settings['display_text'];
  $this -> test_mode = $this -> settings['test_mode'];
  $this -> security_code = $this -> settings['security_code'];

  $this -> redirect_page_id = $this -> settings['redirect_page_id'];

  $this -> msg['message'] = "";
  $this -> msg['class'] = "";

  add_action('init', array(&$this, 'check_cashu_response'));
  if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
            add_action( 'woocommerce_update_options_payment_gateways_' .$this->id, array( &$this, 'process_admin_options' ) );
         } else {
            add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
        }
  add_action('woocommerce_receipt_cashu', array(&$this, 'receipt_page'));
  }
    function init_form_fields(){

   $this->form_fields = array(
        'enabled' => array(
            'title' => __( 'Enable/Disable', 'promex' ),
            'type' => 'checkbox',
            'label' => __( 'Enable CashU payment Gateways', 'promex' ),
            'default' => 'no'
        ),
        'title' => array(
            'title' => __( 'Title', 'promex' ),
            'type' => 'text',
            'description' => __( 'This controls the title which the user sees during checkout.', 'promex' ),
            'desc_tip' => true,
            'default' => __( 'CashU payment Gateways', 'promex' )
        ),
        'description' => array(
            'title' => __( 'Description', 'promex' ),
            'type' => 'textarea',
            'description' => __( 'This controls the description which the user sees during checkout.', 'promex' ),
            'default' => __( 'Desctiptions for CashU payment Gateways.', 'promex' )
        ),
        'merchant_id' => array(
            'title' => __( 'Merchant ID', 'promex' ),
            'type' => 'text',
            'description' => __( 'The Merchant ID as entered during registration on CashU .', 'promex' ),
            'default' => __( 'The Merchant ID for CashU payment Gateways.', 'promex' )
        ),
        'service_name' => array(
            'title' => __( 'Service Name', 'promex' ),
            'type' => 'text',
            'description' => __( 'Name of the service or domain entered by the Merchant that has multiple checkout pages.', 'promex' ),
            'default' => __( 'Service Name for CashU payment Gateways.', 'promex' )
        ),
        'test_mode' => array(
            'title' => __( 'Test Mode', 'promex' ),
            'type' => 'text',
            'description' => __( 'Encription Key from Cash U', 'promex' ),
            'default' => __( 'Instructions for CashU payment Gateways.', 'promex' )
        ),
        'security_code' => array(
            'title' => __( 'Security Code', 'promex' ),
            'type' => 'text',
            'description' => __( 'Encription Key from Cash U', 'promex' ),
            'default' => __( 'Instructions for CashU payment Gateways.', 'promex' )
        ),
        'enable_for_methods' => array(
            'title'         => __( 'Enable for shipping methods', 'promex' ),
            'type'          => 'multiselect',
            'class'         => 'chosen_select',
            'css'           => 'width: 450px;',
            'default'       => '',
            'description'   => __( 'If Cash U is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'promex' ),
            'options'       => $shipping_methods,
            'desc_tip'      => true,
        )
    );
}

   public function admin_options(){
    echo '<h3>'.__('CashU Payment Gateway', 'promex').'</h3>';
    echo '<p>'.__('CashU is most popular payment gateway for online shopping in MENA').'</p>';
    echo '<table class="form-table">';
    // Generate the HTML For the settings form.
    $this -> generate_settings_html();
    echo '</table>';

}

/**
 *  There are no payment fields for CashU, but we want to show the description if set.
 **/
function payment_fields(){
    if($this -> description) echo wpautop(wptexturize($this -> description));
}
/**
 * Receipt Page
 **/
function receipt_page($order){

    echo '<p>'.__('Thank you for your order, please click the button below to pay with CashU.', 'promex').'</p>';
    echo $this -> generate_cashu_form($order);

}

/**
 * Generate cashu button link
 **/
public function generate_cashu_form($order_id){

   global $woocommerce;
    $order = new WC_Order( $order_id );
    $txnid = $order_id.'_'.date("ymds");
    $productinfo = "Order $order_id";
    $security = $this ->security_code;
    $redirect_url = ($this -> redirect_page_id=="" || $this -> redirect_page_id==0)?get_site_url() . "/":get_permalink($this -> redirect_page_id);
    $environment_url = 'https://www.cashu.com/cgi-bin/pcashu.cgi';
    $hash = md5(strtolower($this -> merchant_id).':'.$order -> order_total.':usd:'.$security);

    $cashu_args = array(
      'merchant_id' => $this -> merchant_id,
      'token' => $hash,
      'display_text' => $order_id,
      'currency' => "USD",
      'amount' => $order -> order_total,
      'language' => "EN",
      'session_id' => $txnid,
      'txt1' => $productinfo,
      'test_mode' => $this -> test_mode,
      'service_name' => $this -> service_name,

      );

    $cashu_args_array = array();
    foreach($cashu_args as $key => $value){
      $cashu_args_array[] = "<input type='hidden' name='$key' value='$value'/>";
    }
    return '<form action="https://www.cashu.com/cgi-bin/pcashu.cgi" method="post" id="cashu_payment_form">
        ' . implode('', $cashu_args_array) . '

        <input type="submit" class="button-alt" id="submit_cashu_payment_form" value="'.__('Pay via CashU', 'promex').'" /> <a class="button cancel" href="'.$order->get_cancel_order_url().'">'.__('Cancel order &amp; restore cart', 'promex').'</a>

        </form>';
}

/**
*Cash U Notification after Success payment
**/

function cashu_response(){
    //!!!


}


/**
 * Process the payment and return the result
**/
function process_payment($order_id){
    global $woocommerce;
    $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(get_option('woocommerce_pay_page_id')))),
    );
    $order->update_status( 'processing', 'promex');
      WC()->cart->empty_cart();

}

}
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_promex_cashu_gateway($methods) {
$methods[] = ‘WC_Promex_Cashu’;
return $methods;
}

add_filter('woocommerce_payment_gateways', 'woocommerce_add_promex_cashu_gateway' );
}

and the XML notification from Cashu itself is like:

<?php 

$encryptionKey='encryption_key_value'; 

$sRequest=$_POST["sRequest"]; 

$successTransaction = new SimpleXMLElement($sRequest); 

$merchant_id=$successTransaction->cashUTransaction[0]->merchant_id; 

$amount= $successTransaction->cashUTransaction[0]->amount; 

$currency=$successTransaction->cashUTransaction[0]->currency; 

$language= $successTransaction->cashUTransaction[0]->language; 

$txt1= $successTransaction->cashUTransaction[0]->txt1; 

$token= $successTransaction->cashUTransaction[0]->token; 

$cashU_trnID= $successTransaction->cashUTransaction[0]->cashU_trnID; 

$session_id= $successTransaction->cashUTransaction[0]->session_id; 

$cashUToken= $successTransaction->cashUTransaction[0]->cashUToken; 

$display_text=$successTransaction->cashUTransaction[0]->display_text; 

$responseCode=$successTransaction->cashUTransaction[0]->responseCode; 

$servicesName=$successTransaction->cashUTransaction[0]->servicesName; 

$trnDate=$successTransaction->cashUTransaction[0]->trnDate; 

$originaAmount= "Original_amount";

$originaCurrency= "Original_currency";

       $calculatedCashuToken=MD5(strtolower($merchant_id).':'.$trn_id.':'.strtolower($encryptionKey)); 

if($calculatedCashuToken!=$cashUToken) {
echo’error_test’;

} else {

$calculatedtoken = md5(strtolower ($merchant_id).':'.$originaAmount.':'.strtolower($originaCurrency).':'.$encryptionKey); 
 if($calculatedtoken!=$token) { 

 echo'error_test_2';

 } else { 

echo 'test_ok_thank_you';
 } 

 if($responseCode=='OK') 
 { 

         $sRequest= "sRequest=<cashUTransaction><merchant_id>".$merchant_id."</merchant_id><cashU_trnID>".$cashU_trnID."</cashU_trnID><cashUToken>".$cashUToken."</cashUToken><responseCode>".$responseCode."</responseCode><responseDate>".date("Y-m-d H:i:s")."</responseDate></cashUTransaction>"; 

    $ch = curl_init();

    $useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"; 

    curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

    curl_setopt($ch, CURLOPT_URL, 'https://www.cashu.com/cgi-bin/notification/MerchantFeedBack.cgi'); 

    curl_setopt($ch, CURLOPT_VERBOSE, 0); 

    curl_setopt($ch, CURLOPT_HEADER, true);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    curl_setopt($ch, CURLOPT_POST, 1); 

    curl_setopt($ch, CURLOPT_TIMEOUT, 0); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $sRequest); 

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close')); 

    $result = curl_exec($ch); curl_close($ch); 
} 

}

Related posts