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

class Eway extends Payment{
    
	var $return_url =  array('registeration'=>'paymentReturn','duepay'=>'duepaymentReturn','change'=>'chgReturn','cancel'=>'cancelReturn');
    function Eway(){
	     
		  global $eway_customerid , $ewaymode , $godaddy_hosting ,$ewaytype;
		  
		  $this->eway_customerid = $netdeposit_clientid ;
		  $this->ewaymode = $ewaymode ;
		  
		  $this->ewaytype = $ewaytype ;
		  
		   $file =  ($ewaytype == "live")?JPATH_SITE.'/components/com_dtregister/lib/payment/eway/EwayPaymentLive.php':JPATH_SITE.'/components/com_dtregister/lib/payment/eway/EwayPaymentHosted.php';
		  
		  require_once( $file);
		   $class =  ($ewaytype == "live")?'EwayPaymentLive':'EwayPaymentHosted';
		  $gateway = ($ewaymode=='test')?false:true ;
		  $this->payEway = new $class($eway_customerid,REAL_TIME,$gateway);
		  $this->godaddy_hosting = $godaddy_hosting ;
		 
	 }
	 function setformData(){
	     $this->billing_form = $this->billingform();
		 $this->getLoginUserData();
	 }
	 
	
  function drawform($options){
        
		global $Itemid ;
	    if(!method_exists($this->payEway,'setCurlPreferences')){
		   $this->payEway->message  =  $options['message'];
		   $this->setfields();
		   $_SESSION['register']['billingInfo']['confirmNum'] = $this->confirmNum ;
		 
		  
	   $session_id = $this->saveSession();
	   $this->private_data = $this->paymentType.'|session='.$session_id ;
	   JURI::root( false );
	   $this->return_url[$this->paymentType] ; 
	    $this->continue_url = JURI::root( false )."index.php?option=com_dtregister&dtevent=".$session_id."&task=".$this->return_url[$this->paymentType]."&Itemid=".$Itemid ;
		$this->payEway->setTransactionData("URL",$this->continue_url);
		$this->payEway->setTransactionData("AutoRedirect",1);
		
		$this->formatPostfields();
		 return $this->payEway->doPayment();
		 
	}
	 
		global $cardtype ,$save_payment_info ,$cardtype ;
		
		ob_start();
		 
		 ?>
         
           <table>
             <?php
                 echo  $this->billingform();
			 ?>
		     
                 <tr>

		          <td width="31%" ><?php echo JText::_( 'CARD_NUMBER' );?>:<span class='required'>&nbsp;&nbsp;*&nbsp;&nbsp;</span></td>

		         <td width="69%" align="left" ><input type="text" name="x_card_num" onKeyUp="chknumber(this)" class="inputbox" value="<?php echo $this->credircardNo; ?>" />

		              <br />

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

		        </tr>
                
                 <tr>

		          <td width="31%" ><?php echo JText::_( 'CARD_EXPIRY_DATE' );?>:<span class='required'>&nbsp;&nbsp;*&nbsp;&nbsp;</span></td>

		          <td width="69%" align="left" ><input type="text" name="x_exp_date" value="<?php echo $this->credircardExp; ?>" class="inputbox" />

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

		        </tr>
                
                 <tr>

		          <td width="31%" ><?php echo JText::_( 'CVV_CODE' );?>:<span class='required'>&nbsp;&nbsp;*&nbsp;&nbsp;</span></td>

		         <td width="69%" align="left" ><input type="text" name="x_card_code" size="10" onKeyUp="chknumber(this)" class="inputbox" value="<?php echo ($this->x_card_code)?$this->x_card_code:'' ; ?>" />

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

		        </tr>
                <?php

				$my = &JFactory::getUser();

				if ($cb_integrated == 1 && $save_payment_info == 1 && $my->id){?>

	    		<tr>

			    	<td><?php echo JText::_( 'DT_SAVE_INFO' ); ?>: </td>

			    	<td> <input type="checkbox" name="ckb_save_info" value="1" /> <?php echo JText::_( 'DT_SAVE_INFO_FUTURE' );?> </td>

			    </tr>

				<?php } 
				
				?>
                
                 <tr>

		          <td>

                  <input type="button" onclick="this.form.type.value='back';this.form.submit();" class="button" value="<?php echo JText::_('DT_BACK'); ?>" id="next" name="billingInfo"/>

                  <input type="button" name="send" value="<?php echo JText::_('DT_NEXT_BUTTON'); ?>" class="button" onclick="sendPayment();" /></td>

		          <td align="left" >&nbsp;<input type="hidden" name="authPayment" value="1" />
                  <input type="hidden" name="option" value="com_dtregister" />
                  <input type="hidden" name="task" value="<?php echo $this->component_tasks[$this->paymentType]; ?>" />
                  <input type="hidden" name="type" value="processpayment" />
                  
                  </td>

		        </tr>

		        </table>

		        </form>
          
         
          	<script language="javascript" type="text/javascript">
                    //<![CDATA[
					function chknumber(txtname){

						var num = txtname.value

					    if(isNaN(num)){

							alert("<?php echo JText::_( 'DT_NUMBERS_ONLY' ); ?>");

							txtname.value = "";

					    	txtname.focus();

						}

					}

					function sendPayment(){

						var form=document.frmcart;

						<?php

						if($_SESSION['register']['memtot']==1){

						?>

						if(form.billing_firstname.value==""){

							alert ("<?php echo JText::_( 'DT_CARD_FIRSTNAME_ERROR' ); ?>");

							form.billing_firstname.focus();

							return ;

						}

						if(form.billing_address.value==""){

							alert ("<?php echo JText::_( 'DT_ADDRESS_ERROR' ); ?>");

							form.billing_address.focus();

							return ;

						}

						if(form.billing_city.value==""){

							alert ("<?php echo JText::_( 'DT_CITY_ERROR' ); ?>");

							form.billing_city.focus();

							return ;

						}

						if(form.billing_state.value==""){

							alert ("<?php echo JText::_( 'DT_STATE_ERROR' ); ?>");

							form.billing_state.focus();

							return ;

						}

						if(form.billing_zipcode.value==""){

							alert ("<?php echo JText::_( 'DT_ZIP_ERROR' ); ?>");

							form.billing_zipcode.focus();

							return ;

						}

						if(form.billing_email.value==""){

							alert ("<?php echo JText::_( 'DT_EMAIL_ERROR' ); ?>");

							form.billing_email.focus();

							return ;

						}

						<?php

						}

						?>

						

						

						if(form.x_card_num.value==""){

							alert ("<?php echo JText::_( 'DT_CARD_NUMBER' ); ?>");

							form.x_card_num.focus();

							return ;

						}

						if(form.x_exp_date.value==""){

							alert ("<?php echo JText::_( 'DT_CARD_EXPIRE_DATE' ); ?>");

							form.x_exp_date.focus();

							return ;

						}

						if(form.x_card_code.value==""){

							alert ("<?php echo JText::_( 'DT_CARD_CODE' ); ?>");

							form.x_card_code.focus();

							return ;

						}

						confirmText = "<?php echo JText::_( 'DT_CONFIRM_CC_INFO' ); ?>\n\n";

						
						
						
						confirmText = confirmText+"<?php echo JText::_( 'CARD_NUMBER' ); ?>: "+form.x_card_num.value+"\n";

						confirmText = confirmText+"<?php echo JText::_( 'CARD_EXPIRY_DATE' ); ?>: "+form.x_exp_date.value+"\n";

						confirmText = confirmText+"<?php echo JText::_( 'CVV_CODE' ); ?>:\t "+form.x_card_code.value+"\n";
						
						confirmText = confirmText+"<?php echo JText::_( 'DT_AMOUNT' ); ?>:\t <?php echo $this->amount; ?>";

						var answer = confirm(confirmText);

						if (answer){

							form.submit();

						}

					}

				</script>
          
          
                
        <?php
		return ob_get_clean();
		
	 }
	  function setfields(){
	  
	   $expiry =  explode('/',$this->x_exp_date);
	  
	   $this->fields['TotalAmount'] = $this->amount ;
	   if(method_exists($this->payEway,'setCurlPreferences')){
	      $this->fields['CardNumber'] = $this->x_card_num ;
	      $this->fields['CardHoldersName'] = $this->billing_firstname.' '.$this->billing_lastname ;
	      $this->fields['CardExpiryMonth'] =  $expiry[0];
	      $this->fields['CardExpiryYear'] = $expiry[1];
		  $this->fields['CVN'] = $this->x_card_code ;
	  }
	  $this->confirmNum = (!isset($this->confirmNum)|| $this->confirmNum=="")?$this->generateconfirmNum():$this->confirmNum;
	   $this->fields['CustomerInvoiceRef'] = $this->confirmNum ;
	  // $this->fields['CompanyName'] = $this->organization ;
	   $this->fields['CustomerFirstName'] = $this->firstname ;
	   $this->fields['CustomerInvoiceDescription'] = $this->eventName ;
	   $this->fields['CustomerLastName'] = $this->lastname ;
	   $this->fields['CustomerAddress'] = $this->address ;
	   $this->fields['CustomerEmail'] = ($this->billing_email=="")?$this->email:$this->billing_email ;
	   $this->fields['TrxnNumber'] = '' ;
	   $this->fields['Option1'] = '' ;
	   $this->fields['Option2'] = '' ;
	   $this->fields['Option3'] = '' ;
	   $this->fields['CustomerPostcode'] = $this->zip ;
	  // $this->fields['Phone'] = $this->phone ;
	  // $this->fields['Email'] = $this->email ;
	   
	   //$this->fields['Credit'] = 0 ;
	   
	   
	   
	   //die ;
	   
	 }
	 
	 
	 function formatPostfields(){
	     
		 $fields = "";

         foreach( $this->fields as $key => $value ){ 
		    //if(trim($value)!="")
		    $this->payEway->setTransactionData($key, $value);
		 }
	     
	 }
	 
	 function processPayment(){
	    global $Itemid , $curreny_code;
		$this->setfields();
		//echo "<pre>";
		 // print_r($this);
		//echo "</pre>";
		$this->formatPostfields();
		//var_dump(method_exists($this->payEway,'getVisitorIP'));
		if(method_exists($this->payEway,'getVisitorIP'))
		$this->payEway->setTransactionData("CustomerIPAddress", $this->payEway->getVisitorIP()); //mandatory field when using Geo-IP Anti-Fraud
	    $this->payEway->setTransactionData("CustomerBillingCountry", $curreny_code); //mandatory field when using Geo-IP Anti-Fraud
	
	
	//special preferences for php Curl
	if(method_exists($this->payEway,'setCurlPreferences'))
	$this->payEway->setCurlPreferences(CURLOPT_SSL_VERIFYPEER, 0);  
	
	//pass a long that is set to a zero value to stop curl from verifying the peer's certificate 
	//$eway->setCurlPreferences(CURLOPT_CAINFO, "/usr/share/ssl/certs/my.cert.crt"); //Pass a filename of a file holding one or more certificates to verify the peer with. This only makes sense when used in combination with the CURLOPT_SSL_VERIFYPEER option. 
	//$eway->setCurlPreferences(CURLOPT_CAPATH, "/usr/share/ssl/certs/my.cert.path");
	
	
		
		if($this->godaddy_hosting && method_exists($this->payEway,'setCurlPreferences')){

			$this->payEway->setCurlPreferences(CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //use CURL proxy, for example godaddy.com hosting requires it
	        $this->payEway->setCurlPreferences(CURLOPT_PROXY, "http://proxy.shr.secureserver.net:3128"); //use CURL proxy, for example godaddy.com hosting requires it
		
		}
		
	
	
	   
	   
	$ewayResponseFields = $this->payEway->doPayment();
    //echo "<pre>";
	//print_r($ewayResponseFields);
	
	if($ewayResponseFields["EWAYTRXNSTATUS"]=="False"){
		print "Transaction Error: " . $ewayResponseFields["EWAYTRXNERROR"] . "<br>\n";		
		foreach($ewayResponseFields as $key => $value)
			//print "\n<br>\$ewayResponseFields[\"$key\"] = $value";
		
		//exit();
		
		return false ;
	}else if($ewayResponseFields["EWAYTRXNSTATUS"]=="True"){
	       $this->TransactionID = $ewayResponseFields['ewayTrxnReference'];
		//print "Transaction Success: " . $ewayResponseFields["EWAYTRXNERROR"]  . "<br>\n";
		foreach($ewayResponseFields as $key => $value)
			//print "\n<br>\$ewayResponseFields[\"$key\"] = $value";
			
			return true  ;
		
		//exit();
	}
		
		
	
		

		
		
		
	 }
	  function afterpayment(){
	     
		  $this->TransactionID = $_REQUEST['ewayTrxnReference'];
		   $this->confirmNum =  $_SESSION['register']['billingInfo']['confirmNum'];
		   unset($_SESSION['register']['billingInfo']['confirmNum']);
	 }
  
  
}
?>