<?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 api extends Payment{
	
	
	var $bywebservice = true;
	
	var $billing =  true ;
	
	function __construct() {
		global $godaddy_hosting ,$payway_merchant_id ,$payway_type , $payway_username , $payway_password , $payway_cert ;
		parent::__construct();
		
		
		
		  $this->godaddy_hosting = $godaddy_hosting;
		
		$payway_type = "api";
		$this->customer_merchant = ($this->paymentmode=='test')?"TEST":$payway_merchant_id;
		$this->order_type = 'capture';	
		$this->url = "https://ccapi.client.qvalent.com/payway/ccapi" ;"www.payway.com.au";
		$this->customer_username = $payway_username;// = 'Q00000';
		$this->customer_password = $payway_password ;// = 'Ahl2jfi8n';
		$this->certificate = $payway_cert ;
		
   }
   
   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(),
	"order.ECI" => 'SSL',

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

                     );

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

	  $fields = $this->formatfields();
pr($fields);
	  $this->url;
pr($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_CAINFO, $this->certificate );
		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. ###
		 curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );   

		 $resp = curl_exec($ch); //execute post and get results
		 
		 $this->response = $resp ; /*= "response.summaryCode=0&response.responseCode=08&response.text=Honour with identification&response.receiptNo=505228832&response.settlementDate=20060125&response.transactionDate=25-JAN-2006 14:09:49&response.cardSchemeName=VISA&response.creditGroup=VI/BC/MC";*/
		 pr(curl_error($ch));

		 $this->parseResponse();
		 
		 if($this->responseParts['response_summaryCode'] == "0") {
		    $this->transactionId = $this->responseParts['response_receiptNo'];
             $process = DT_Session::get('Setting.process');
			 DT_Session::set($process.'.payment.transactionId',$this->transactionId);
		   return true ; 
		 } else {
			  echo $this->errorMsg = $this->responseParts['response_text'];
		    return false ;
		 }
         

   }
   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);
		
		$data =  "";
		  parse_str($this->response,$data);
		
		 $this->responseParts = $data ;
		

	 }
}
?>