<?php
/**
 * WC wcCpg1 Gateway Class.
 * Built the wcCpg1 method.
 */

class WC_BOB_Payment_Gateway extends WC_Payment_Gateway {

    /**
     * Constructor for the gateway.
     *
     * @return void
     */
    public function __construct() {
        global $woocommerce;
		add_action('woocommerce_api_'.strtolower(get_class($this)), array(&$this, 'handle_callback'));

        $this->id             = 'wcCpg1';
        $this->icon           = apply_filters( 'woocommerce_wcCpg1_icon', '' );
        $this->has_fields     = false;
        $this->method_title   = __( 'Bank Of Baroda E-Gateway', 'wcwcCpg1' );
        $this->notify_url     = WC()->api_request_url( 'WC_BOB_Payment_Gateway' );
        // 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->instructions       = $this->get_option( 'instructions' );
		$this->key    = $this->settings['key'];
		$this->keyfilepath = $this->settings['keyfilepath'];
		$this->returnurl    = $this->settings['returnurl'];
		$this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
		

        // Actions.
        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' ) );


    }


    /* Admin Panel Options.*/
	function admin_options() {
		?>
		<h3><?php _e('Bank Of Baroda E-Gateway','wcwcCpg1'); ?></h3>
    	<table class="form-table">
    		<?php $this->generate_settings_html(); ?>
		</table> <?php
    }

    /* Initialise Gateway Settings Form Fields. */
    public function init_form_fields() {
    	global $woocommerce;

    	$shipping_methods = array();

    	if ( is_admin() )
	    	foreach ( $woocommerce->shipping->load_shipping_methods() as $method ) {
		    	$shipping_methods[ $method->id ] = $method->get_title();
	    	}
			
        $this->form_fields = array(
            'enabled' => array(
                'title' => __( 'Enable/Disable', 'wcwcCpg1' ),
                'type' => 'checkbox',
                'label' => __( 'Enable Bank Of Baroda E-Gateway', 'wcwcCpg1' ),
                'default' => 'no'
            ),
            'title' => array(
                'title' => __( 'Title', 'wcwcCpg1' ),
                'type' => 'text',
                'description' => __( 'This controls the title which the user sees during checkout.', 'wcwcCpg1' ),
                'desc_tip' => true,
                'default' => __( 'Title For Bank Of Baroda E-Gateway', 'wcwcCpg1' )
            ),
            'description' => array(
                'title' => __( 'Description', 'wcwcCpg1' ),
                'type' => 'textarea',
                'description' => __( 'This controls the description which the user sees during checkout.', 'wcwcCpg1' ),
                'default' => __( 'Desctiptions for Bank Of Baroda E-Gateway.', 'wcwcCpg1' )
            ),
			'instructions' => array(
				'title' => __( 'Instructions', 'wcwcCpg1' ),
				'type' => 'textarea',
				'description' => __( 'Instructions that will be added to the thank you page.', 'wcwcCpg1' ),
				'default' => __( 'Instructions for Bank Of Baroda E-Gateway.', 'wcwcCpg1' )
			),
				'key' => array(
                'title' => __( 'Key', 'wcwcCpg2' ),
                'type' => 'text',
                'description' => __( 'This is Key File name without .key extension', 'wcwcCpg2' ),
                'desc_tip' => true,
                'default' => __( 'Key File name without .key extension', 'wcwcCpg2' )
            ),
			'keyfilepath' => array(
                'title' => __( 'keyfilepath', 'wcwcCpg2' ),
                'type' => 'text',
                'description' => __( 'This is keyfilepath, replace WebsiteURL with site domain  ', 'wcwcCpg2' ),
                'desc_tip' => true,
                'default' => __( 'http://localhost/123456.key', 'wcwcCpg2' )
            ),
			
			'returnurl' => array(
                'title' => __( 'ReturnURL', 'wcwcCpg2' ),
                'type' => 'text',
                'description' => __( 'This is Response Page URL, replace WebsiteURL with site domain', 'wcwcCpg2' ),
                'desc_tip' => true,
                'default' => __( 'http://WebsiteDomain/wp-content/plugins/woocommerce-BankOfBaroda/SFAResponse.php', 'wcwcCpg2' )
            ),
			'enable_for_methods' => array(
				'title' 		=> __( 'Enable for shipping methods', 'wcwcCpg1' ),
				'type' 			=> 'multiselect',
				'class'			=> 'chosen_select',
				'css'			=> 'width: 450px;',
				'default' 		=> '',
				'description' 	=> __( 'If wcCpg1 is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'wcwcCpg1' ),
				'options'		=> $shipping_methods,
				'desc_tip'      => true,
			)
        );

    }




    /* Process the payment and return the result. */
	function process_payment ($order_id) {
		global $woocommerce;
		$order = new WC_Order( $order_id );
				
		//added by tanvi
		include("Sfa/BillToAddress.php");
		include("Sfa/CardInfo.php");
		include("Sfa/Merchant.php");
		include("Sfa/MPIData.php");
		include("Sfa/ShipToAddress.php");
		include("Sfa/PGResponse.php");
		include("Sfa/PostLibPHP.php");
		include("Sfa/PGReserveData.php");
		 $oMPI                 =         new         MPIData();
		 $oCI                =        new        CardInfo();
		 $oPostLibphp        =        new        PostLibPHP();
		 $oMerchant        =        new        Merchant();
		 $oBTA                =        new        BillToAddress();
		 $oSTA                =        new        ShipToAddress();
		 $oPGResp        =        new        PGResponse();
		 $oPGReserveData = new PGReserveData();
   
   
   
		// $oMerchant->setMerchantDetails("00024364","00024364","00024364","193.545.34.33",rand()."","Ord123","http://uns.techeasesystems.in/wp-content/plugins/woocommerce-custom-payment-gateways/SFAResponse.php","POST","INR","INV123","req.Sale",$order->get_total(),"","Ext1","true","Ext3","Ext4","Ext5");
		 $oMerchant->setMerchantDetails($this->key,$this->key,$this->key,"193.545.34.33",rand()."",$order_id,$this->returnurl,"POST","INR","INV123","req.Sale",$order->get_total(),"","Ext1","true","Ext3","Ext4","Ext5");

		 $oBTA->setAddressDetails ("CID",$order->billing_first_name,$order->billing_address_1,$order->billing_address_2,$order->billing_first_name,$order->billing_city,$order->billing_state,$order->billing_postcode,"IND",$order->billing_email);

		 $oSTA->setAddressDetails ($order->shipping_address_1,$order->shipping_address_2,"Add3",$order->shipping_city,$order->shipping_state,$order->shipping_postcode,"IND",$order->shipping_email);
      	  
		 #$oMPI->setMPIRequestDetails("100","1.00","356","2","2 shirts","","","","0","","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*","Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
		 $oMPI->setMPIRequestDetails("100",$order->get_total(),"356","2","2 shirts","","","","0","","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*","Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");

		 $oPGReserveData->setReserveObj($order_id,$order->billing_first_name,$order->billing_last_name,$order->billing_phone,$order->billing_email,$order->order_key,"7","8","9","10");

		$oPGResp=$oPostLibphp->postSSL($oBTA,$oSTA,$oMerchant,$oMPI,$oPGReserveData);
		//echo $oPGResp;	
		
        if($oPGResp->getRespCode() == '000'){	             

                $url = $oPGResp->getRedirectionUrl();
                 if(headers_sent()) {
				 $order->update_status('processing', __( 'Your order wont be shipped until the funds have cleared in our account.', 'woocommerce' ));
									
									//$order->payment_complete();

									// Reduce stock levels
									$order->reduce_order_stock();

									// Remove cart
									$woocommerce->cart->empty_cart();
									 ?>
									 <html><head>
									 <script language="javascript" type="text/javascript">

									  window.self.location='<?php print($url);?>';

									 </script>
									 </head></html>
									 <?php
									 exit;

									 } else {

									 header("Location: ".$url);
									 exit;

									 }								

                 }
				 else{
                        print "Error Occured.<br>";
                        print "Error Code:".$oPGResp->getRespCode()."<br>";
                        print "Error Message:".$oPGResp->getRespMessage()."<br>";
                     }
	
	}//end of process payment function
	

	  
		 
		 
 function redirect($url) {

         if(headers_sent()) {
         ?>
         <html><head>
         <script language="javascript" type="text/javascript">

          window.self.location='<?php print($url);?>';

         </script>
         </head></html>
         <?php
         exit;

         } else {

         header("Location: ".$url);
         exit;

         }
}
  

    // Output for the order received page.   */
	function thankyou() {
		echo $this->instructions != '' ? wpautop( $this->instructions ) : '';
	}
	
function handle_callback($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
    //Handle the thing here!
	echo "handle call back";
		// Mark as on-hold
									$order->update_status('processing', __( 'Your order wont be shipped until the funds have cleared in our account.', 'woocommerce' ));
									
									$order->payment_complete();

									// Reduce stock levels
									$order->reduce_order_stock();

									// Remove cart
									$woocommerce->cart->empty_cart();

									// Return thankyou redirect
										return array(
										'result' 	=> 'success',
										'redirect'	=> add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(woocommerce_get_page_id('thanks'))))
										 );
  }

	
 }
 




