<?php
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

require_once( JPATH_SITE.'/components/com_dtregister/lib/class.payment.php');

class moneris_directpost extends Payment{

   	var $bywebservice = true;
    var $cardtype = "";
	var $save_on_return = true ;

    function __construct() {

        global $hpp_key,$ps_store_id ;

		parent::__construct();

		//$this->paymentmode = 'test';

		$this->url = ($this->paymentmode=='test')?"https://esqa.moneris.com/HPPDP/index.php":"https://www3.moneris.com/HPPDP/index.php";

		
		
        $this->add_field('ps_store_id', $ps_store_id);
		$this->add_field('hpp_key', $hpp_key);
		
   }
   
   function add_field($field, $value) {

      $this->fields["$field"] = $value;

   }
   
   function billingform(){
	    $form = parent::billingform();
       ob_start();
	   ?>
         <dl>

		    <dt class="dtlabel"><?php echo JText::_( 'CARD_NUMBER' );?>:<span class='dtrequired'>&nbsp;&nbsp;*&nbsp;&nbsp;</span></dt>

		    <dd class="dtfield"><input type="text" name="billing[cc_num]" class="inputbox" value="<?php echo isset($this->cb_creditcardnumber)?$this->cb_creditcardnumber:'4111111111111111'?>" />

		              <br />

		            <?php echo JText::_( 'CARD_NUMBER_EXPLANATION' );?></dd>

		 </dl>
         
         <dl>

		    <dt class="dtlabel"><?php echo JText::_( 'CARD_EXPIRY_DATE' );?>:<span class='dtrequired'>&nbsp;&nbsp;*&nbsp;&nbsp;</span></dt>

		    <dd class="dtfield"><?php echo JText::_( 'DT_MONTH' );?>:<input type="text" name="billing[expMonth]" class="inputbox" value="12" />

		             <br />
                     
                     <?php echo JText::_( 'DT_YEAR' );?>: <input type="text" name="billing[expYear]" class="inputbox" value="14" />
		            <?php echo JText::_( 'CARD_EXPIRY_DATE_EXPLAINED' );?></dd>

		 </dl>
         <?php
         global $moneris_cvv_enable ;
		 
		 if($moneris_cvv_enable) {
		 ?>
          <dl>

		     <dt class="dtlabel"><?php echo JText::_( 'CVV_CODE' );?>:<span class='dtrequired'>&nbsp;&nbsp;*&nbsp;&nbsp;</span></dt>

		     <dd class="dtfield"><input autocomplete="off" type="text" name="billing[cvd_value]" size="10" class="inputbox" value="1234" />

		            <?php echo JText::_( 'CVV_CODE_EXPLANATION' );?></dd>

		 </dl>
         <?php } ?>
       
       <?php
	    $html = ob_get_clean();

	    return $form.$html;
	   
   }
   
   function process(){

	   global $currency_code,$Itemid, $moneris_cvv_enable;
       
	   $mosConfig_live_site = JURI::root( false );

	   if($currency_code==""){$currency_code='USD';}

	  $session_id = $this->saveSession();

	  $amount = DTreg::numberFormat($this->cart->getAmount(),2);
      $this->add_field('cc_num',$this->cc_num);
	  $this->add_field('expMonth',$this->expMonth);
	  $this->add_field('expYear',$this->expYear);
	  if($moneris_cvv_enable) {
	  $this->add_field('cvd_value',$this->cvd_value);
	  }
	  if($this->paymentmode=='test') {
	    $this->add_field('charge_total', "1.00");
		$this->country = "CANADA";
	  } else {
	  $this->add_field('charge_total', $amount);
	  }
	  $this->add_field('description1',$this->description);
	  $this->add_field('id1',$this->eventId);
	  $this->add_field('quantity1',1);
	   if($this->paymentmode=='test') {
	    $this->add_field('subtotal1', "1.00");
		$this->add_field('price1', "1.00");
	  } else {
		  $this->add_field('price1', $amount);
	      $this->add_field('subtotal1', $amount);
	  }
	  
	   $this->add_field('cvd_value', $this->cvd_value);
	   $this->add_field('cvd_indicator', '1');
		
		$x_invoice_num = 	 (!isset($this->confirmNum)|| $this->confirmNum=="")?'':$this->confirmNum;
		$this->add_field('cust_id', $session_id);
		$this->add_field('note', $x_invoice_num);
		
		//$this->add_field('order_id', $x_invoice_num);
		
		$this->add_field('rvar_return' ,$session_id);
		
	  if(isset($this->firstname) && $this->firstname !="" ){
	  	 $this->add_field('bill_first_name', $this->firstname);
	  }
	  
	   if(isset($this->lastname) && $this->lastname !="" ){
	  	  $this->add_field('bill_last_name', $this->lastname);
	  }
	  
	   if(isset($this->address) && $this->address !="" ){
	  	  $this->add_field('bill_address_one', $this->address);
		 // $this->add_field('avs_street_name', $this->address);
	  }
	  
	   if(isset($this->city) && $this->city !="" ){
	  	  $this->add_field('bill_city', $this->city);
	  }
	  if(isset($this->state) && $this->state !="" ){
	  	 $this->add_field('bill_state_or_province', $this->state);
	  }
	  
	   if(isset($this->zip) && $this->zip !="" ){
	  	 $this->add_field('bill_postal_code', $this->zip);
		 $this->add_field('avs_ziocopde', $this->zip);
	  }
	  if(isset($this->phone) && $this->phone !="" ){
	  	 $this->add_field('bill_phone', $this->phone);
	  }
	  
	   if(isset($this->email) && $this->email !="" ){
	  	  $this->add_field('email', $this->email);
	  }
	   if(isset($this->country) && $this->country !="" ){
		   $fieldTable = new TableField();
		   $country = $fieldTable->fingbyName('country');
		    if($country){
			   $fieldType = DtrModel::getInstance('Fieldtype','DtregisterModel');
               $fieldTypes = $fieldType->getTypes();
			   $class = "Field_".$fieldTypes[$country->type];
			  
               $countryTable = new $class();
			   $countryTable->load($country->id);
			   $countryTable->values = explode("|",$countryTable->values);
			   
			   $this->country = $countryTable->values[$this->country];
			  
	        }
	  	  $this->add_field('bill_country', $this->country);
	  }

	   //$this->add_field('x_currency_code', "$currency_code");

	   $this->submit_post();

   }

   function submit_post() {

      echo "<center><h3>".JText::_( 'DT_MONERIS_REDIRECT_MSG')."</h3></center>\n";

      echo "<form method=\"post\" name=\"formRegister\" action=\"".$this->url."\">\n";

      foreach ($this->fields as $name => $value) {

         echo "<input type=\"hidden\" name=\"$name\" value=\"$value\"> <br/>";

      }

?>

		<script language="javascript">

			function rg_direc(){

				document.formRegister.submit();

			}

		  setTimeout("rg_direc()",5000);

		</script>

<?php

      echo "</form>\n";

   }
   
    function success(){
	     $_REQUEST['result'] = 1 ;
	    if($_REQUEST['result']=='1'){
		   $this->transactionId = $_REQUEST['txn_num'];
		   $process = DT_Session::get('Setting.process');
		   DT_Session::set($process.'.payment.transactionId', $this->transactionId);
				   return true;
			   }else{
				   echo $_REQUEST['message'];
				   return false;
	   }	
  }
  
   function notify(){
	   
	  if($_REQUEST['result']=='1'){
		   $this->transactionId = $_REQUEST['txn_num'];
		   $process = DT_Session::get('Setting.process');
		   DT_Session::set($process.'.payment.transactionId', $this->transactionId);
				   return true;
			   }else{
				  
				   return false;
	   }
	      
   }
   
}
?>