<?php

/**
* @version 2.8.18
* @package Joomla 2.5
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

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 extends Payment{

   	var $bywebservice = false;
    var $cardtype = "";

    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 process(){

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

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

	  $session_id = $this->saveSession();

	  $amount = $this->cart->getAmount();

	  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");
	  } else {
	  $this->add_field('subtotal1', $amount);
	  }
	  
		$x_invoice_num = (!isset($this->confirmNum)|| $this->confirmNum=="")?'':$this->confirmNum;
		$this->add_field('cust_id', $session_id);
		
		$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);
	  }
	  
	   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);
	  }
	  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 !="" ){
	  	  $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\">";

      }

?>

		<script language="javascript">

			function rg_direc(){

				document.formRegister.submit();

			}

			setTimeout("rg_direc()",5000);

		</script>

<?php

      echo "</form>\n";

   }
   
    function success(){
	  
	    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;
	   }
	      
   }
   
}
?>