<?php

/**
* @version 2.8.14
* @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 payway extends Payment{
	
	
	var $bywebservice = false;
	
	var $billing = true;
	
	function getPaymentInstance(){
	   global $payway_type;
	   $file = ($payway_type == "api")?JPATH_SITE.'/components/com_dtregister/lib/payment/payway/api.php':JPATH_SITE.'/components/com_dtregister/lib/payment/payway/net.php';
	   
	    require_once($file);
		$class = ($payway_type == "api")?'api':'net';
		
		$this->paymentObj = new $class();
		//echo get_class($this->paymentObj); die;
		return $this->paymentObj;
	  
	}
	
	function __construct() {
		return;
		global $godaddy_hosting ,$payway_merchant_id ,$payway_type;
		parent::__construct();
		$this->paymentmode = 'test';
		
		$payway_type = "net";
		$file = ($payway_type == "api")?JPATH_SITE.'/components/com_dtregister/lib/payment/payway/api.php':JPATH_SITE.'/components/com_dtregister/lib/payment/payway/net.php';
		
		  require_once($file);
		  $class = ($payway_type == "api")?'api':'net';
		  
		 $this->paymentObj = new $class();
		  
		  $this->bywebservice = $this->paymentObj->bywebservice;
		  
		  $this->paymentObj->init($this->paymentmode);
		  $this->godaddy_hosting = $godaddy_hosting;
		
		
		$this->customer_merchant = ($this->paymentmode=='test')?"TEST":$payway_merchant_id;
		$this->order_type = 'capture';	
		$this->url = "www.payway.com.au";
		$this->customer_username = 'Q00000';
		$this->customer_password = 'Ahl2jfi8n';
		
   }
   
   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[card_PAN]" class="inputbox" value="<?php echo isset($this->cb_creditcardnumber)?$this->cb_creditcardnumber:''?>" />

		              <br />

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

		 </dl>

        <?php
        echo   $this->expiry_fields(isset($this->cb_expdate)?$this->cb_expdate:'');
		?>
         <!--dl>

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

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

		            &nbsp;&nbsp;(mm/yy)</dd>

		 </dl-->
         <dl>

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

		     <dd class="dtfield"><input type="text" name="billing[card_CVN]" size="10" class="inputbox" value="<?php echo isset($this->x_card_code)?$this->x_card_code:'' ;?>" />

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

		 </dl>

       <?php

	   $html = ob_get_clean();

	   return $form.$html;

   }
   
     function setFields(){
	 	 global $currency_code;
	   $cc_num = "";
	   $currency_code = "AUD";
	   $year = substr($this->x_exp_date_year,2);
	   $exp_date=$this->x_exp_date_month."/".$year;
	   
       $this->x_exp_date = $exp_date;
	   $exp_date= explode("/",$this->x_exp_date);

	   $this->fields = array(

//  ###########3  MINIMUM TRANSACTION REQUIRED DETAILS  ################3

    "customer.merchant"  => $this->customer_merchant,

	"customer.username"				=> $this->customer_username,

	"customer.password"				=> $this->customer_password,

    "order.type"			=> $this->order_type,

	"order.amount"			=>  $this->cart->getAmount(),
    "card.PAN" =>				$this->card_PAN,
	"card.CVN" =>     $this->card_CVN ,
	"card.currency"					=> $currency_code,
    "card.cardHolderName" => trim($this->firstname." ".$this->lastname),
	"card.expiryMonth" => $exp_date[0],
	"card.expiryYear" => $exp_date[1],

	"x_amount"				=>  $this->cart->getAmount(),

    "customer.orderNumber"  => (!isset($this->confirmNum)|| $this->confirmNum=="")?'':$this->confirmNum
	
                     );

	 }
   
     function process(){
      
	  $this->paymentObj->process();
	  
	  $this->setFields();

	  $fields = $this->formatfields();
pr($fields);
	  $this->url;

	  $ch = curl_init($this->url);

	  if($this->godaddy_hosting){

		  curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

		  curl_setopt ($ch, CURLOPT_PROXY,"http://proxy.shr.secureserver.net:3128");

	  }
        curl_setopt($ch,CURLOPT_PORT,443);
	  	curl_setopt($ch, CURLOPT_HEADER, 1); // set to 0 to eliminate header info from response

		curl_setopt($ch, CURLOPT_VERBOSE, 1);

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)

		curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $fields, "& " )); // use HTTP POST to send form data

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. ###

		 $resp = curl_exec($ch); //execute post and get results
		 
		 $this->response = $resp;
prd($this->response);
		 $this->parseResponse();

   }
   function deformatNVP($nvpstr)
	{

		$intial=0;
		$nvpArray = array();
		while(strlen($nvpstr))
		{
			$keypos= strpos($nvpstr,'='); 
			$valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr); 
			$keyval=substr($nvpstr,$intial,$keypos);
			$valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
			$nvpArray[urldecode($keyval)] =urldecode( $valval);
			$nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
		 }
		return $nvpArray;
	}
    function parseResponse(){
         $this->deformatNVP($this->response);
		 $this->responseParts =  $this->deformatNVP($this->response);

	 }
}
?>