<?php
require_once( JPATH_SITE.'/components/com_dtregister/lib/class.payment.php');
class authorizenet extends Payment{
   
    var $error_tries =  2 ;
   
    function authorizenet(){
	    global $merchid , $authnetmode, $transkey,$godaddy_hosting;
		$this->godaddy_hosting = $godaddy_hosting ;
		
		$TESTING =($authnetmode=='test')?1:0;
		
		
		$this->url = ($authnetmode=='test')?"https://test.authorize.net/gateway/transact.dll":"https://secure.authorize.net/gateway/transact.dll" ;
		
		$this->x_test_request = ($TESTING)?'TRUE':'FALSE';
		
		$this->x_login			= $merchid;
		
		$this->x_version  =  "3.1";
		
		$this->x_tran_key = $transkey;
		$this->x_delim_char = "|";
		$this->x_delim_data = "TRUE";
		$this->x_url = "FALSE";
		$this->x_type = "AUTH_CAPTURE";
		$this->x_method = "CC";
		$this->x_relay_response = "FALSE";
		$this->x_type = "AUTH_CAPTURE";
	}
	 function setformData(){
	     $this->billing_form = $this->billingform();
		 $this->getLoginUserData();
	 }
	function drawform($options){
	    
		global $cardtype ,$save_payment_info ,$cardtype ,$cb_integrated  ;
		echo "<pre>";
		  //print_r($this);
		echo "</pre>";
		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 (isset($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="processNetDeposit" />
                  
                  </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(){
	   echo "<pre>";
	   //  print_r($this);
		 print_r($_REQUEST);
	   echo "</pre>";
	   $this->generateconfirmNum();
	   $num = strlen($this->x_card_num);
	   for($i=0;$i<=$num-5;$i++){
	
			$cc_num.="x";
	
		}

	   $cc_num.="-";
	   $cc_num.=substr($this->x_card_num,$num-4,4);
	   $exp_date=$this->x_exp_date;
	   $this->fields	= array(
	                  

//  ###########3  MINIMUM TRANSACTION REQUIRED DETAILS  ################3
    "x_allow_partial_Auth"  => "True",
	"x_login"				=> $this->x_login,

	"x_version"				=> $this->x_version,

    "x_delim_char"			=> $this->x_delim_char,

	"x_delim_data"			=> $this->x_delim_data,

	"x_url"					=> $this->x_url,

	"x_type"				=> $this->x_type,

	"x_method"				=> $this->x_method,

 	"x_tran_key"			=> $this->x_tran_key,

 	"x_relay_response"		=> $this->x_relay_response,

 	"x_test_request"		=> $this->x_test_request,

	"x_card_num"			=> $this->x_card_num,

	"x_exp_date"			=> $this->x_exp_date,

	"x_description"			=> $this->eventName,

    "x_card_code"           => $this->x_card_code,

	"x_amount"				=>  $this->amount,

    "x_test_request"        => $this->x_test_request,

//  ###########3  CUSTOMER DETAILS  ################3

	"x_first_name"			=>$this->billing_firstname,

	"x_last_name"			=>$this->billing_lastname,

	"x_company"      		=>$this->organization,

	"x_address"				=>$this->billing_address,

	"x_city"				=>$this->billing_city,

	"x_state"				=>$this->billing_state,

	"x_phone"				=>$this->phone,

	"x_zip"					=>$this->billing_zipcode,

	"x_cust_id"				=>substr($this->confirmNum,3),

	"x_email"				=>$this->billing_email,

//  ###########3  SHIPPING DETAILS  ################3

	"x_ship_to_first_name" 	=> $this->firstname,

	"x_ship_to_last_name" 	=> $this->lastname,

	 "x_ship_to_address"  	=> $this->address,

	"x_ship_to_city" 		=> $this->city,

	"x_ship_to_state" 		=> $this->state,

	"x_ship_to_country" 	=> $this->country,

	"x_ship_to_zip" 		=> $this->zip,

	"x_ship_to_phone" 		=> $this->phone,

	"x_ship_to_email" 		=> $this->billing_email,

//  ###########3  MERCHANT REQUIRED DETAILS  ################3

	"x_invoice_num" 		=> (!isset($this->confirmNum)|| $this->confirmNum=="")?$this->generateconfirmNum():$this->confirmNum,

	"cc_number" 			=> $cc_num,

	"cc_expdate" 			=> $exp_date,

	"cc_emailid" 			=> $this->billing_email,

                     );

	   
	 
	   
	   
	   
	 }
	 
	 
	 function formatPostfields(){
	     
		 $fields = "";

         foreach( $this->fields as $key => $value ){ 
		    if(trim($value)!="")
		    $fields .= "$key=" . urlencode( $value ) . "&";
		 }
	     return $fields ;
	 }
	function processPayment(){
	    
		$this->setfields();
		//echo "<pre>";
		 // print_r($this);
		//echo "</pre>";
		$fields =  $this->formatPostfields();
		$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_HEADER, 0); // 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. ###
		
		echo $resp = curl_exec($ch); //execute post and get results
		//var_dump($resp);
		echo  "<br />".$this->paymentmethod = get_class($this) ;
		$gatewaydata = $this->parseResponse($resp);
		return false ;
		
		
		if($gatewaydata['Return_Code']==0){
		   $this->TransactionID = $gatewaydata['TransactionID'] ;
		   return 1 ;
		}else{
		   $this->TransactionID = $gatewaydata['TransactionID'] ;
		    return false ;
		}
		

		
		
		
	 }
	
    function parseResponse($resp){
	     
		 echo "<pre>";
		 print_R(explode("|",$resp));
		 echo "</pre>";
		 return $gatewaydata ;
	 }
	 
	 function cancelpayment(){
	    
		
		
	 }
}
?>