• Resolved bennac40

    (@bennac40)


    Hi,

    I have a custom payment gateway but I am not able to send cart total. It sends cart total as zero, but when I checkout with store currency, it sends out the price so I am guessing I should be adding a specific line or something. I hope you can help me. Below is the code of the payment gateway:

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    
    /**
     * Cheque Payment Gateway.
     *
     * Provides a Cheque Payment Gateway, mainly for testing purposes.
     *
     * @class 		WC_Gateway_Cheque
     * @extends		WC_Payment_Gateway
     * @version		2.1.0
     * @package		WooCommerce/Classes/Payment
     * @author 		WooThemes
     */
    class WC_Gateway_Binga extends WC_Payment_Gateway {
    
        /**
         * Constructor for the gateway.
         */
    	public function __construct() {
    		
    		session_start();
    
    		$this->id                 = 'binga';
    		$this->has_fields         = false;
    		$this->method_title       = 'Paiement par Binga';
    		$this->method_description = 'Payez en cash sur internet via Binga';
    
    		// Load the settings.
    		$this->init_form_fields();
    		$this->init_settings();
    
    		$this->version         = $this->settings['versions'];
    		$this->storeId         = $this->settings['storeId'];
    		$this->privateKey      = $this->settings['privateKey'];
    		$this->expirationDelay = $this->settings['expirationDelay'];
    		$this->environment     = ($this->settings['environment'] == 'yes') ? "https://api.binga.ma/bingaApi/order/prePay.action": "https://preprod.binga.ma/bingaApi/order/prePay.action";
    
    		// Define user set variables
    		$this->title        = $this->get_option( 'title' );
    		$this->description  = $this->get_option( 'description' );
    		$this->instructions = $this->get_option( 'instructions', $this->description );
    
    	
    
    		$_SESSION['binga'] = $this;
    
    		// Actions
    		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
    		
    		add_action( 'woocommerce_api_process', array( &$this, 'process' )  );
            add_action( 'woocommerce_api_payurl', array( &$this, 'pay_url' )  );
            add_action( 'woocommerce_api_bookurl', array( &$this, 'book_url' )  );
            add_action( 'woocommerce_api_failureurl', array( &$this, 'failure_url' )  );
            
    
        }
    
    	function process(){
    		@ob_clean();
    		if(isset($_GET["id"]))      $order_id = $_GET["id"];
    		if(isset($_GET["fname"]))   $fname    = $_GET["fname"];
    		if(isset($_GET["lname"]))   $lname    = $_GET["lname"];
    		if(isset($_GET["address"])) $address  = $_GET["address"];
    		if(isset($_GET["email"]))   $email    = $_GET["email"];
    		if(isset($_GET["phone"]))   $phone    = $_GET["phone"];
    
    		$order           = wc_get_order($order_id);
    		$base_url        = "https://".$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
    		$amount          = number_format(preg_replace('#[^\d.]#', '', WC()->cart->cart_contents_total), 2, '.', '');
    		$orderCheckSum   = md5("PRE-PAY".$amount.$this->storeId.$order_id.$email.$this->privateKey);
    		$expirationDelay = gmdate('Y-m-d\TH:i:s\G\M\T', time()+(($this->expirationDelay)*60*60));
                global $woocommerce;
                echo "<pre>";
                print_r($woocommerce->cart);
                echo "</pre>";die;
    		echo "Vous allez être redirigé vers le site Binga, patientez svp ...";
    	?>
    
    		<form method="POST" id='bingaForm' action="<?=$this->environment?>">
    			<input type="hidden" name="externalId" value="<?=$order_id?>"></input>
    			<input type="hidden" name="amount" value="<?=$amount?>"></input>
    			<input type="hidden" name="buyerFirstName" value="<?=$fname?>"></input>
    			<input type="hidden" name="buyerLastName" value="<?=$lname?>"></input>
    			<input type="hidden" name="buyerEmail" value="<?=$email?>"></input>
    			<input type="hidden" name="buyerAddress" value="<?=$address?>"></input>
    			<input type="hidden" name="buyerPhone" value="<?=$phone?>"></input>
    			<input type="hidden" name="storeId" value="<?=$this->storeId?>"></input>
    			<input type="hidden" name="failureUrl" value="<?=$base_url.'/wc-api/failureurl'?>"></input>
    			<input type="hidden" name="bookUrl" value="<?=$base_url.'/wc-api/bookurl'?>"></input>
    			<input type="hidden" name="successUrl" value="<?=$this->get_return_url($order)?>"></input>
    			<input type="hidden" name="payUrl" value="<?=$base_url.'/wc-api/payurl'?>"></input>
    			<input type="hidden" name="orderCheckSum" value="<?=$orderCheckSum?>"></input>
    			<input type="hidden" name="apiVersion" value="<?=$this->version?>"></input>
    			<input type="hidden" name="expirationDate" value="<?=$expirationDelay?>"></input>	
    		</form>
    		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    		<script type="text/javascript">
    		$(function() {
    			$("form#bingaForm").submit();
    		});	
    		</script>
    	<?php
    		exit();
    	}
    
    	function book_url(){
    		@ob_clean();
    		
    
    		if(isset($_POST['externalId']) && isset($_POST['expirationDate']) && isset($_POST['amount']) && isset($_POST['buyerEmail']) && isset($_POST['orderCheckSum']) ){
    		
    			$orderCheckSum   = md5("PRE-PAY".$_POST['amount'].$this->storeId.$_POST['externalId'].$_POST['buyerEmail'].$this->privateKey);
    			$order = wc_get_order($_POST['externalId']);
    
    			if($_POST['orderCheckSum'] == $orderCheckSum ){
    				$order->update_status( 'on-hold', 'En Attente de paiement chez wafacash' );
    				WC()->cart->empty_cart();
    			}else{
    				$order->update_status( 'cancelled', 'Erreur' );
    			}
    
    		}
    
    		exit();
    	}
    
    	function pay_url(){
    		@ob_clean();
    
    		if(isset($_POST['externalId']) && isset($_POST['expirationDate']) && isset($_POST['amount']) && isset($_POST['buyerEmail']) && isset($_POST['orderCheckSum']) ){
    		
    			$orderCheckSum   = md5("PAY".$_POST['amount'].$this->storeId.$_POST['externalId'].$_POST['buyerEmail'].$this->privateKey);
    			$order = wc_get_order($_POST['externalId']);
    
    			if($_POST['orderCheckSum'] == $orderCheckSum ){
    				$order->update_status( 'completed', 'Payé chez wafacash' );
    				echo '100;'.date('Y-m-d\TH:i:s\G\M\T',strtotime($_POST['expirationDate']));
    			}else{
    				var_dump($orderCheckSum);die();
    				$order->update_status( 'cancelled', 'Erreur' );
    				echo '000;'.date('Y-m-d\TH:i:s\G\M\T',strtotime($_POST['expirationDate']));				
    			}
    
    		}
    
    		exit();
    	}
    
    	function failure_url(){
    		@ob_clean();
    
    		if(isset($_POST['externalId']) && isset($_POST['expirationDate']) && isset($_POST['amount']) && isset($_POST['buyerEmail']) && isset($_POST['orderCheckSum']) ){
    			$order = wc_get_order($_POST['externalId']);
    			$order->update_status( 'cancelled', 'Erreur' );
    
    		}
    
    		exit();
    	}
    
        /**
         * Initialise Gateway Settings Form Fields.
         */
        public function init_form_fields() {
    
        	$this->form_fields = array(
    			'enabled' => array(
    				'title'   => __( 'Enable/Disable', 'woocommerce' ),
    				'type'    => 'checkbox',
    				'label'   => 'Activez les paiements par binga',
    				'default' => 'yes'
    			),
    			'title' => array(
    				'title'       => __( 'Title', 'woocommerce' ),
    				'type'        => 'text',
    				'desc_tip'    => true,
    			),
    			'description' => array(
    				'title'       => __( 'Description', 'woocommerce' ),
    				'type'        => 'textarea',
    				'desc_tip'    => true,
    			),
    			'instructions' => array(
    				'title'       => __( 'Instructions', 'woocommerce' ),
    				'type'        => 'textarea',
    				'desc_tip'    => true,
    			),
    			'versions' => array(
    				'title'       => __( 'Versions', 'woocommerce' ),
    				'type'        => 'text',
    				'desc_tip'    => true,
    			),
    			'storeId' => array(
    				'title'       => __( 'StoreId', 'woocommerce' ),
    				'type'        => 'text',
    				'desc_tip'    => true,
    			),
    			'privateKey' => array(
    				'title'       => __( 'Private Key', 'woocommerce' ),
    				'type'        => 'text',
    				'desc_tip'    => true,
    			),
    			'expirationDelay' => array(
    				'title'       => __( 'Expiration Delay (in hour)', 'woocommerce' ),
    				'type'        => 'text',
    				'desc_tip'    => true,
    			),
    			'environment' => array(
    				'title'   => __( 'Production/Pre-production', 'woocommerce' ),
    				'type'    => 'checkbox',
    				'label'   => 'Activez le module en mode production',
    				'default' => 'yes'
    			),
    		);
        }
        /**
         * Process the payment and return the result.
         *
         * @param int $order_id
         * @return array
         */
    	public function process_payment( $order_id ) {		
    
    		$order = wc_get_order( $order_id );
    		
    
    		// // Mark as on-hold
    		// $order->update_status( 'on-hold', 'En Attente de paiement par binga' );
    
    		// // Reduce stock levels
    		// //$order->reduce_order_stock();
    
    		// // Remove cart
    		// WC()->cart->empty_cart();
    
    		if(isset($_POST["billing_first_name"])) $billing_first_name = $_POST["billing_first_name"];
    		if(isset($_POST["billing_last_name"]))  $billing_last_name  = $_POST["billing_last_name"];
    		if(isset($_POST["billing_company"]))    $billing_company    = $_POST["billing_company"];
    		if(isset($_POST["billing_email"]))      $billing_email      = $_POST["billing_email"];
    		if(isset($_POST["billing_phone"]))      $billing_phone      = $_POST["billing_phone"];
    		if(isset($_POST["billing_country"]))    $billing_country    = $_POST["billing_country"];
    		if(isset($_POST["billing_address_1"]))  $billing_address_1  = $_POST["billing_address_1"];
    		if(isset($_POST["billing_address_2"]))  $billing_address_2  = $_POST["billing_address_2"];
    		if(isset($_POST["billing_city"]))       $billing_city       = $_POST["billing_city"];
    		if(isset($_POST["billing_state"]))      $billing_state      = $_POST["billing_state"];
    		if(isset($_POST["billing_postcode"]))   $billing_postcode   = $_POST["billing_postcode"];
    
    		$billing_address = $billing_address_1." ".$billing_address_2.", ".$billing_postcode." ".$billing_city.", ".$billing_state;
    		$base_url        = "https://".$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
    
    		return array(
    			'result' 	=> 'success',
    			'redirect'	=> $base_url.'/wc-api/process?id='.$order_id.'&fname='.$billing_first_name.'&lname='.$billing_last_name.'&address='.$billing_address.'&email='.$billing_email.'&phone='.$billing_phone,
    		);
    
    	}
    
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Payment Gateway Sending Cart Total as Zero on Checkout’ is closed to new replies.