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

   var $last_error;                 // holds the last error encountered

   var $ipn_log;                    // bool: log IPN results to text file?

   var $ipn_log_file;               // filename of the IPN log

   var $ipn_response;               // holds the IPN response from paypal

   var $ipn_data = array();         // array contains the POST values for IPN

   var $fields = array();           // array holds the fields to submit to paypal

   var $bywebservice = false;

   function __construct() {

      // initialization constructor.  Called when class is created.
      global $paybox_site , $paybox_rank ;
      parent::__construct();
      
      if($this->paymentmode=='test'){
		  $this->add_field('PBX_ERRORCODETEST','00005');
	   }

      $this->last_error = '';

     

      $this->add_field('PBX_MODE','1');          
      $this->add_field('PBX_SITE',$paybox_site);

	 
	  $this->add_field('PBX_RANG',$paybox_rank);


   }

   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';}

       $iso_codes = $this->ISO4217_codes();
       $currency_code = $iso_codes[$currency_code];
	   $session_id = $this->saveSession();

	   $this->add_field('PBX_EFFECTUE', "{$mosConfig_live_site}components/com_dtregister/success.php?return=$session_id&Itemid=$Itemid&task=restore");

	   $this->add_field('PBX_REFUSE', "{$mosConfig_live_site}components/com_dtregister/success.php?return=$session_id&Itemid=$Itemid&task=cancel");
	   
	   $this->add_field('PBX_ANNULE', "{$mosConfig_live_site}components/com_dtregister/success.php?return=$session_id&Itemid=$Itemid&task=cancel");

	   $this->add_field('item_name',$this->description);

		
	  if(isset($this->firstname) && $this->firstname !="" ){
	  	// $this->add_field('first_name', $this->firstname);
	  }
	  
	   if(isset($this->lastname) && $this->lastname !="" ){
	  	// $this->add_field('last_name', $this->lastname);
	  }
	  
	   if(isset($this->address) && $this->address !="" ){
	  	// $this->add_field('address1', $this->address);
	  }
	  
	   if(isset($this->city) && $this->city !="" ){
	  	// $this->add_field('city', $this->city);
	  }
	  if(isset($this->state) && $this->state !="" ){
	  	// $this->add_field('state', $this->state);
	  }
	  
	   if(isset($this->zip) && $this->zip !="" ){
	  	// $this->add_field('zip', $this->zip);
	  }
	  
	  
		
	   $this->add_field('PBX_TOTAL', $this->cart->getAmount()*100);

	   $this->add_field('PBX_DEVISE', "$currency_code");
	   
	   $this->add_field('PBX_CMD',$this->confirmNum);

	   $this->add_field('PBX_PORTEUR', $this->email); //email
	   
	   $this->add_field('PBX_RETOUR', "montant:M;ref:R;auto:A;trans:T");

	   $this->submit_post();

   }

   function submit_post() {

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

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

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

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

      }

?>

		<script language="javascript">

			function rg_direc_to_paypal(){

				document.formRegister.submit();

			}

			setTimeout("rg_direc_to_paypal()",5000);

		</script>

<?php

      echo "</form>\n";

   }

  
   function dump_fields() {

      echo "<h3>paypal_class->dump_fields() Output:</h3>";

      echo "<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">

            <tr>

               <td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>

               <td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>

            </tr>";

      ksort($this->fields);

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

         echo "<tr><td>$key</td><td>".urldecode($value)."&nbsp;</td></tr>";

      }

      echo "</table><br>";

   }
   
     function success(){
          
		  $this->transactionId = $_POST['trans'];
		  if($this->transactionId === false){
		  	 return false ;
		  }
		    $process = DT_Session::get('Setting.process') ;
			  DT_Session::set($process.'.payment.transactionId',$this->transactionId);
		   return true;
		 

	 }  
 
}


?>