<?php
#########################################################
#                                                       #
#  INVOICE payment method class                         #
#  This module is used for real time processing of      #
#  INVOICE payment of customers.                        #
#                                                       #
#  Copyright (c) 2012 Novalnet AG                       #
#                                                       #
#  Released under the GNU General Public License        #
#  novalnet_invoice module.                             #
#  This free contribution made by request.              #
#  If you have found this script usefull a small        #
#  recommendation as well as a comment on merchant form #
#  would be greatly appreciated.                        #
#                                                       #
#  Script : novalnet_invoice.php                        #
#  Version: 1.0.3  2012-06-13                           #
#                                                       #
#########################################################
class novalnet_invoice {
  var $code;
  var $title;
  var $description;
  var $enabled;
  var $proxy;
  var $is_ajax = false;
  var $KEY = 27;

  function novalnet_invoice() {
    global $order;

    $this->code         = 'novalnet_invoice';
    $this->title        = MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_TITLE;
    $this->public_title = MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_PUBLIC_TITLE;
    $this->description  = MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_DESCRIPTION;
    $this->sort_order   = MODULE_PAYMENT_NOVALNET_INVOICE_SORT_ORDER;
    $this->enabled      = ((MODULE_PAYMENT_NOVALNET_INVOICE_STATUS == 'True') ? true : false);
    $this->proxy        = MODULE_PAYMENT_NOVALNET_INVOICE_PROXY;
	
	if(MODULE_PAYMENT_NOVALNET_INVOICE_LOGO_STATUS != 'Disable'){
		$this->public_title = '<NOBR><A HREF="https://www.novalnet.de/de/unsere-produkte/zahlungsarten.html" TARGET="_new"><IMG SRC="https://www.novalnet.de/img/NN_Logo_T.png" ALT="Zahlung per Novalnet AG" title="Zahlung per Novalnet AG" BORDER="0"></A>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_PUBLIC_TITLE;
	}
	
	if (CHECKOUT_AJAX_STAT=='true') { $this->is_ajax = true; }
	// Check the tid in session and make the second call
	if($_SESSION['nn_tid_invoice'])
  	{
		//Check the time limit
		if($_SESSION['max_time_invoice'] && time() > $_SESSION['max_time_invoice']){
			unset($_SESSION['nn_tid_invoice']);
			$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode(utf8_encode( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SESSION_ERROR) );
			if ($this->is_ajax) {
				$_SESSION['checkout_payment_error'] = $payment_error_return;
				return;
			} else {
				xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
			}		
		}	

		if( $_GET['new_novalnet_pin_invoice'] == 'true')
		{
			$_SESSION['new_novalnet_pin_invoice'] = true;
			$this->secondCall();
		}
		
		if(!$this->is_ajax && $_SESSION['email_reply_check_invoice']=='Email Reply' && !isset($_SESSION['xml_resp_error_invoice']))
		{
			$this->secondCall();	
		}
	
	}	

    if ((int)MODULE_PAYMENT_NOVALNET_INVOICE_ORDER_STATUS_ID > 0) {
      $this->order_status = MODULE_PAYMENT_NOVALNET_INVOICE_ORDER_STATUS_ID;
    }
	
    // define callback types
    $this->isActivatedCallback = false;	
	if(MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS != 'False'){
		$this->isActivatedCallback = true;
	}
	
    if (is_object($order)) $this->update_status();
  }
  
  ### calculate zone matches and flag settings to determine whether this module should display to customers or not ###
  function update_status() {
    global $order;

    if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_NOVALNET_INVOICE_ZONE > 0) ) {
      $check_flag = false;
      $check_query = xtc_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_NOVALNET_INVOICE_ZONE . "' and 
      zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
        while ($check = xtc_db_fetch_array($check_query)) {
          if ($check['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check['zone_id'] == $order->billing['zone_id']) {
            $check_flag = true;
            break;
          }
        }

      if ($check_flag == false) {
        $this->enabled = false;
      }
    }
  }
  
  ### JS validation which does error-checking of data-entry if this module is selected for use ###
  ### the fields to be cheked are (Bank Owner, Bank Account Number and Bank Code Lengths)      ###
  ### currently this function is not in use ###
  // @return string
  function javascript_validation() {
    return false;
  }
  
  ### Builds set of input fields for collecting Bankdetail info ###
  // @return array
  function selection() {
    global $xtPrice, $order, $HTTP_POST_VARS, $_POST;

    $onFocus = '';
    if(count($HTTP_POST_VARS)==0 || $HTTP_POST_VARS=='') $HTTP_POST_VARS = $_POST;
	
	if ($this->is_ajax || (!$this->is_ajax && !$_SESSION['nn_tid_invoice'])) {
		$selection = array('id' => $this->code,
						   'module' => $this->public_title,
				'fields' => array(array('title' => '', 'field' => MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_INFO))
				  );
		#}
		// Display callback fields
		$amount_check = $this->findTotalAmount();
		$_SESSION['amount_first_invoice'] = $this->findTotalAmount();
		if($this->isActivatedCallback && strtolower($order->customer['country']['iso_code_2']) == 'de' && $amount_check >= MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT )	
		{
			if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Email Reply' )
			{
				$selection['fields'][] = array( 'title' => MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_EMAIL, 'field' => xtc_draw_input_field ( 'user_email_invoice', $_SESSION['user_email_invoice'], 'id="'.$this->code.'-callback" '.$onFocus ) );
				
			}
			else {
				if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Callback (Telefon & Handy)' )
				{ 
					$selection['fields'][] = array( 'title' => MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_TEL, 'field' => xtc_draw_input_field ( 'user_tel_invoice', $_SESSION['user_tel_invoice'], 'id="'.$this->code.'-callback" '.$onFocus ) );
				}
				if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'SMS (nur Handy)' )  
				{ 
					$selection['fields'][] = array( 'title' => MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_MOB, 'field' => xtc_draw_input_field ( 'user_tel_invoice', $_SESSION['user_tel_invoice'], 'id="'.$this->code.'-callback" '.$onFocus ) );
				}
				
			}
		}
	}
	//$amount_check = $this->findTotalAmount();
	$amount_check = $_SESSION['amount_first_invoice'];
	if($this->isActivatedCallback && strtolower($order->customer['country']['iso_code_2']) == 'de' && $amount_check >= MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT )	
	{
		if ($this->is_ajax || (!$this->is_ajax && $_SESSION['nn_tid_invoice'])) {
			if(!$this->is_ajax) {
				$selection = array('id' => $this->code,'module' => $this->public_title);
			}
			if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Email Reply' )
				//&& (!isset($_SESSION['email_reply_check_invoice']) || isset($_SESSION['xml_resp_error_invoice'])))
			{ 	
				if ($this->is_ajax) {
					$selection['fields'][] = array( 'title' => '','field' =>  MODULE_PAYMENT_NOVALNET_INVOICE_EMAIL_INFO_DESC);
					$selection['fields'][] = array('title' => '', 'field' => xtc_draw_checkbox_field('email_replied_invoice', '1', false).MODULE_PAYMENT_NOVALNET_INVOICE_EMAIL_REPLY_INFO);
				} else {
					$selection['fields'][] = array( 'title' => MODULE_PAYMENT_NOVALNET_INVOICE_EMAIL_INPUT_REQUEST_DESC);
				}
			}
			else {
				if ($this->is_ajax) {
					unset($_SESSION['email_reply_check_invoice']);
					if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Callback (Telefon & Handy)' )
					{ 
						$selection['fields'][] = array( 'title' => '','field' =>  MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_TEL_NUMBER);
					}
					if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'SMS (nur Handy)' )  
					{ 
						$selection['fields'][] = array( 'title' => '','field' =>  MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_MOB_NUMBER);
					}
					
					$selection['fields'][] = array( 'title' => '','field' =>  MODULE_PAYMENT_NOVALNET_INVOICE_PIN_INFO_DESC);
				
					$selection['fields'][] = array( 'title' => MODULE_PAYMENT_NOVALNET_INVOICE_PIN_INPUT_REQUEST_DESC, 'field' => xtc_draw_input_field( 'novalnet_pin_invoice', '', 'id="'.$this->code.'-callback" '.$onFocus.' maxlength="4" size="4" ') );
				
					$selection['fields'][] = array('title' => '', 'field' => xtc_draw_checkbox_field('forgot_pin_invoice', '1', false, 'id="' . $this->code . '-forgotpin"' . $onFocus).MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_NEW_PIN);
				
					$selection['fields'][] = array('title' => '', 'field' => MODULE_PAYMENT_NOVALNET_INVOICE_FORGOT_PIN_DIV);
				} else {
					// Show PIN field, after first call
					$selection['fields'][] = array( 'title' => MODULE_PAYMENT_NOVALNET_INVOICE_PIN_INPUT_REQUEST_DESC, 'field' => xtc_draw_input_field( 'novalnet_pin_invoice', '', 'id="'.$this->code.'-callback" '.$onFocus.' maxlength="4" size="4" ') );
							
					$selection['fields'][] = array( 'title' => '<a href="'.xtc_href_link( FILENAME_CHECKOUT_PAYMENT, 'new_novalnet_pin_invoice=true', 'SSL', true, false).'">'.MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_NEW_PIN.'</a>');
				}
			}
		}
	}
    if(function_exists(get_percent))
    {
        $selection['module_cost'] = $GLOBALS['ot_payment']->get_percent($this->code);
    }

    return $selection;
  }

  ### Precheck to Evaluate the Bank Datas ###
  function pre_confirmation_check($vars) {
    global $HTTP_POST_VARS, $_POST, $order;
	
	if(!$this->emailVaildate($order->customer['email_address'])) {
      $payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode(utf8_encode('Please enter the valid email Id'));
	  if ($this->is_ajax) {
			$_SESSION['checkout_payment_error'] = $payment_error_return;
	  } else {
		    xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
	  }
    }
	// Check shipping methods are selected or not;
	if(!$order->info['shipping_method']){
		$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode( utf8_encode(MODULE_PAYMENT_NOVALNET_INVOICE_REQUEST_FOR_CHOOSE_SHIPPING_METHOD) );
		$_SESSION['checkout_payment_error'] = $payment_error_return;
		return;			
	}
    if(!is_array($HTTP_POST_VARS) && !$HTTP_POST_VARS)
	{
		$HTTP_POST_VARS = array();
	}
	if ($this->is_ajax) {
		$HTTP_POST_VARS = array_merge($HTTP_POST_VARS,$vars);
	} else {
		$HTTP_POST_VARS = array_merge($HTTP_POST_VARS,$_POST);
	}

	if(isset($HTTP_POST_VARS['user_tel_invoice'])) {
		$HTTP_POST_VARS['user_tel_invoice'] 		= trim($HTTP_POST_VARS['user_tel_invoice']);
	}
	if(isset($HTTP_POST_VARS['user_email_invoice'])) {
		$HTTP_POST_VARS['user_email_invoice'] 		= trim($HTTP_POST_VARS['user_email_invoice']);
	}
	if(isset($HTTP_POST_VARS['novalnet_pin_invoice'])) {
		$HTTP_POST_VARS['novalnet_pin_invoice'] 		= trim($HTTP_POST_VARS['novalnet_pin_invoice']);
	}
	if($_SESSION['nn_tid_invoice'])
    {
		// Callback stuff....
		 if($order->info['subtotal'] != $_SESSION['prod_total_amount_invoice'] || $order->info['shipping_class'] != $_SESSION['shipping_name_invoice'] ){
			$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode(MODULE_PAYMENT_NOVALNET_INVOICE_AMOUNT_VARIATION_MESSAGE);
			unset($_SESSION['nn_tid_invoice']);
			unset($_SESSION['prod_total_amount_invoice']);
			unset($_SESSION['shipping_name_invoice']);
			if ($this->is_ajax) {
					$_SESSION['checkout_payment_error'] = $payment_error_return;
					return;
			}else{
					xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
			}
		}
		
		if($this->is_ajax && $HTTP_POST_VARS['forgot_pin_invoice'] && !$HTTP_POST_VARS['novalnet_pin_invoice'] && isset($_SESSION['nn_tid_invoice']))
		{
			$_SESSION['new_novalnet_pin_invoice'] = true;
			$this->secondCall();
			return;
		}
		if($this->is_ajax && MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Email Reply' )
		{
			if($this->is_ajax && !$HTTP_POST_VARS['email_replied_invoice'])
			{
				$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode( utf8_encode(MODULE_PAYMENT_NOVALNET_INVOICE_EMAIL_REPLY_CHECKBOX_INFO ));
				if ($this->is_ajax) {
						$_SESSION['checkout_payment_error'] = $payment_error_return;
				} else {
					xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
				}
			}
			else {
				$_SESSION['email_reply_check_invoice']='Email Reply';
			}
		}
		if(isset($HTTP_POST_VARS['novalnet_pin_invoice']) && isset($_SESSION['nn_tid_invoice'])) { 
			// check pin
			if( !is_numeric( $HTTP_POST_VARS['novalnet_pin_invoice'] ) || strlen( $HTTP_POST_VARS['novalnet_pin_invoice'] ) != 4 )
			{	
				$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode( utf8_encode(MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_PIN_NOTVALID ));
				if ($this->is_ajax) {
					$_SESSION['checkout_payment_error'] = $payment_error_return;
				} else {
					xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
				}
			}
			else
			{
				if( $HTTP_POST_VARS['novalnet_pin_invoice'] )
				$_SESSION['novalnet_pin_invoice'] = $HTTP_POST_VARS['novalnet_pin_invoice'];
			}
		}
	}
	else {
		$error = '';
		if ($this->is_ajax || (!$this->is_ajax && !isset($_SESSION['nn_tid_invoice']))) {
			if(!trim(MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID) || !trim(MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE) || !trim(MODULE_PAYMENT_NOVALNET_INVOICE_PRODUCT_ID) || 
			!trim(MODULE_PAYMENT_NOVALNET_INVOICE_TARIFF_ID))
			{
			  $error .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_JS_NN_MISSING;
			}
			if (isset($HTTP_POST_VARS['user_tel_invoice']))
			{ $_SESSION['user_tel_invoice']  = $HTTP_POST_VARS['user_tel_invoice']; }
			if (isset($HTTP_POST_VARS['user_email_invoice']))
			{ $_SESSION['user_email_invoice']  = $HTTP_POST_VARS['user_email_invoice']; }
			
			// Callback stuff....
			//$amount_check = $this->findTotalAmount();
			$amount_check = $_SESSION['amount_first_invoice'];
			if( $this->isActivatedCallback && strtolower($order->customer['country']['iso_code_2']) == 'de' && $amount_check >= MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT )
			{
			
				if($error=='') {
					//checking telephone number
					if (isset($HTTP_POST_VARS['user_email_invoice']))
					{
						if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $HTTP_POST_VARS['user_email_invoice'])){ 
							$error .= utf8_decode( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_EMAIL_NOTVALID );
						} 
					}
					if(isset($HTTP_POST_VARS['user_tel_invoice']))
					{
						if( strlen( $HTTP_POST_VARS['user_tel_invoice'] ) < 8 || !is_numeric( $HTTP_POST_VARS['user_tel_invoice'] )  )
						{
							$error .= utf8_decode( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS_TEL_NOTVALID );
						}		
					}
				}
				if($error!='') {		
					$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode(utf8_encode($error));
					if ($this->is_ajax) {
						$_SESSION['checkout_payment_error'] = $payment_error_return;
					} else {
						xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
					}
				}else{
					$this->before_process();
					if (!$this->is_ajax) {
						xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); 		
					}	
				}
				
			}
		}		
		if($error!='') {
			$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode(utf8_encode($error));
			if ($this->is_ajax) {
				$_SESSION['checkout_payment_error'] = $payment_error_return;
			} else {
				xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
			}
		}
	}
  }

  
  //This is user defined function used for getting order amount in cents with tax
  public function findTotalAmount(){
		global $order;
		if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
		  $total = $order->info['total'] + $order->info['tax'];
		} else {
		  $total = $order->info['total'];
		}
		if (preg_match('/[^\d\.]/', $total) or !$total){
		  ### $amount contains some unallowed chars or empty ###
		  $err                      = 'amount ('.$total.') is empty or has a wrong format';
		  $payment_error_return     = 'payment_error='.$this->code.'&error='.utf8_encode($err);
		  if ($this->is_ajax) {
				$_SESSION['checkout_payment_error'] = $payment_error_return;
			} else {
				xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
			}
		}
		$amount = sprintf('%0.2f', $total);
		$amount = preg_replace('/^0+/', '', $amount);
		$amount = str_replace('.', '', $amount);
	return $amount;
  }  

  	public function secondCall()
	{
		// If customer forgets PIN, send a new PIN
		if( $_SESSION['new_novalnet_pin_invoice'] )
			$request_type = 'TRANSMIT_PIN_AGAIN';
		else
		{
	        $request_type = 'PIN_STATUS';
	    }
		 if($_SESSION['email_reply_check_invoice']=='Email Reply') {
			 $request_type = 'REPLY_EMAIL_STATUS';
		 }
		 if( $_SESSION['new_novalnet_pin_invoice'] )
			$_SESSION['new_novalnet_pin_invoice'] = false;

		if($request_type == 'REPLY_EMAIL_STATUS') {
			$xml = '<?xml version="1.0" encoding="UTF-8"?>
				<nnxml>                               
		  			<info_request>
			    		<vendor_id>'.MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID.'</vendor_id>
			    		<vendor_authcode>'.MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE.'</vendor_authcode>
			    		<request_type>'.$request_type.'</request_type>
			    		<tid>'.$_SESSION['nn_tid_invoice'].'</tid>
		  			</info_request>
				</nnxml>';
		}
		else {
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
				<nnxml>                               
		  			<info_request>
			    		<vendor_id>'.MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID.'</vendor_id>
			    		<vendor_authcode>'.MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE.'</vendor_authcode>
			    		<request_type>'.$request_type.'</request_type>
			    		<tid>'.$_SESSION['nn_tid_invoice'].'</tid>
			    		<pin>'.$_SESSION['novalnet_pin_invoice'].'</pin>
		  			</info_request>
				</nnxml>'; 	
		}		
		$xml_response = $this->curl_xml_post( $xml );	
		
		// Parse XML Response to object
		$xml_response = simplexml_load_string( $xml_response );
		if( $xml_response->status != 100 ){
			$_SESSION['xml_resp_error_invoice']=$xml_response->status_message;
			$payment_error_return = 'payment_error='.$this->code.'&error='.urlencode($_SESSION['xml_resp_error_invoice'] );
			if ($this->is_ajax) {
				$_SESSION['checkout_payment_error'] = $payment_error_return;
				if($request_type == 'PIN_STATUS' || $request_type == 'REPLY_EMAIL_STATUS') {
					xtc_redirect(xtc_href_link(FILENAME_CHECKOUT, $payment_error_return, 'SSL', true, false));
				}
			} else {
				xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
			}
		}
		else
		{		
			$array = (array) $xml_response;
			// add tid, because it's missing in the response
			$array['tid'] = $_SESSION['nn_tid_invoice'];
			$array['statusdesc'] = $array['status_message']; // Param-name is changed
			$array['test_mode'] = $_SESSION['test_mode_invoice'];
			return $array;			
		}						 	
	}
	
    public function curl_xml_post( $request )
	{
	    $ch = curl_init( "https://payport.novalnet.de/nn_infoport.xml" );
	    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
		curl_setopt($ch, CURLOPT_POST, 1);  // a non-zero parameter tells the library to do a regular HTTP post.
		curl_setopt($ch, CURLOPT_POSTFIELDS, $request);  // add POST fields
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);  // don't allow redirects
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // decomment it if you want to have effective ssl checking
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // decomment it if you want to have effective ssl checking
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // return into a variable
		curl_setopt($ch, CURLOPT_TIMEOUT, 240);  // maximum time, in seconds, that you'll allow the CURL functions to take		  
		
		## establish connection
		$xml_response = curl_exec($ch);
		
		## determine if there were some problems on cURL execution
		$errno = curl_errno($ch);
		$errmsg = curl_error($ch);
		
		###bug fix for PHP 4.1.0/4.1.2 (curl_errno() returns high negative value in case of successful termination)
		if($errno < 0) $errno = 0;
		##bug fix for PHP 4.1.0/4.1.2
		
		if($debug)
		{
			print_r(curl_getinfo($ch));
			echo "\n<BR><BR>\n\n\nperform_https_request: cURL error number:" . $errno . "\n<BR>\n\n";
			echo "\n\n\nperform_https_request: cURL error:" . $errmsg . "\n<BR>\n\n";
		}
		
		#close connection
		curl_close($ch);
		
		return $xml_response;
	}
	
  ### Display Bank Information on the Checkout Confirmation Page ###
  // @return array
  function confirmation() {
    global $HTTP_POST_VARS, $_POST, $order;

    if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
      $total = $order->info['total'] + $order->info['tax'];
    } else {
      $total = $order->info['total'];
    }
    $_SESSION['nn_total_invoice'] = sprintf('%.2f', $total);
    $_SESSION['amount_first_invoice'] = sprintf('%.2f', $total);

    if(count($HTTP_POST_VARS)==0 || $HTTP_POST_VARS=='') $HTTP_POST_VARS = $_POST;

    $confirmation = array('fields' => array(array('title' => $this->public_title, 'field' => MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_INFO)));

    return $confirmation;
  }

  ### Build the data and actions to process when the "Submit" button is pressed on the order-confirmation screen. ###
  ### These are hidden fields on the checkout confirmation page ###
  // @return string
  function process_button() {
    global $HTTP_POST_VARS, $_POST;  
    if(count($HTTP_POST_VARS)==0 || $HTTP_POST_VARS=='') $HTTP_POST_VARS = $_POST;

    return $process_button_string;
  }

  ### Store the BANK info to the order ###
  ### This sends the data to the payment gateway for processing and Evaluates the Bankdatas for acceptance and the validity of the Bank Details ###
  function before_process($vars) {
    global $HTTP_POST_VARS, $_POST, $order, $xtPrice, $currencies, $customer_id;
	
    if ($this->is_ajax) {
		if(count($HTTP_POST_VARS)==0 || $HTTP_POST_VARS=='') 
		$HTTP_POST_VARS = $vars;
	} else {
		if(count($HTTP_POST_VARS)==0 || $HTTP_POST_VARS=='') $HTTP_POST_VARS = $_POST;
	}
	
	// First call is done, so check PIN / second call...
	if( $_SESSION['nn_tid_invoice'] && $this->isActivatedCallback )
	{
		
		 
		if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Email Reply' )
			$_SESSION['email_reply_check_invoice']='Email Reply';
		else
			unset($_SESSION['email_reply_check_invoice']);
			
		if($this->is_ajax && $HTTP_POST_VARS['forgot_pin_invoice'] && !$HTTP_POST_VARS['novalnet_pin_invoice'])
			$_SESSION['new_novalnet_pin_invoice']=true;
		else
			$_SESSION['new_novalnet_pin_invoice']=false;
			
		$aryResponse = $this->secondCall();
		
		if($aryResponse){
			    if( $this->order_status )
				     $order->info['order_status'] = $this->order_status;			
				$old_comments = $order->info['comments'];
				$order->info['comments'] = "";
				//Test mode based on the responsone test mode value
				$test_mode = (strtolower(MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE) == 'true' or MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE == '1')? 1: 0;
			if($aryResponse['test_mode']==1 || $test_mode == 1){
					$order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEST_ORDER_MESSAGE;	
				}
				  if($_SESSION['due_date_invoice'])
				  {
					$order->info['comments'] .= '<BR><B>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_DURATION_LIMIT_INFO.' '.$_SESSION['due_date_invoice'].' '.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_DURATION_LIMIT_END_INFO.".</B><BR><BR>";
				  }
				  else
				  {
					$order->info['comments'] .= '<BR><B>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_TRANSFER_INFO.'</B><BR><BR>';
				  }
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_ACCOUNT_OWNER.' NOVALNET AG<BR>';
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_ACCOUNT_NUMBER.' '.$_SESSION['nn_invoice_account'].'<BR>';
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_CODE.' '.$_SESSION['nn_invoice_bankcode'].'<BR>';
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_BANK.' '.$_SESSION['nn_invoice_bankname'].', 
				  '.$_SESSION['nn_invoice_bankplace'].'<BR><BR>';
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_IBAN.' '.$_SESSION['nn_invoice_iban'].'<BR>';
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_BIC.' '.$_SESSION['nn_invoice_bic'].'<BR><BR>';
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_AMOUNT.' '.$_SESSION['original_amount_invoice'].' '.$order->info['currency'].'<BR>';    
				  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TID_MESSAGE.'<B>'.$_SESSION['nn_tid_invoice'].'</B><BR>';
				  $order->info['comments'] .= '<B>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_REFERENCE_INFO.'</B><BR>';
				  $order->info['comments'] .= $old_comments;
				  $order->info['comments']  = str_replace(array('<b>', '</b>','<B>','</B>', '<br>','<br />','<BR>'), array('', '', '','',"\n", "\n","\n"), $order->info['comments']);	
				 // $order->info['comments']  = str_replace(array('<B>', '</B>', '<BR>', '<br />','<b>','</b>','<br>'), array('', '', "\n", "\n"), $order->info['comments']);	
				
		}
		return;
	}	

    #Get the required additional customer details from DB
    $customer_query = xtc_db_query("SELECT customers_gender, customers_dob, customers_fax FROM ". TABLE_CUSTOMERS . " WHERE customers_id='". (int)$customer_id 
    ."'");
    $customer = xtc_db_fetch_array($customer_query);

    list($customer['customers_dob'], $extra) = explode(' ', $customer['customers_dob']);

    ### Process the payment to paygate ##
    $url = 'https://payport.novalnet.de/paygate.jsp';

	//$amount = $this->findTotalAmount();
	$amount = $_SESSION['amount_first_invoice'];
    
    $product_id = trim(MODULE_PAYMENT_NOVALNET_INVOICE_PRODUCT_ID);
    $tariff_id = trim(MODULE_PAYMENT_NOVALNET_INVOICE_TARIFF_ID);

    $payment_duration = MODULE_PAYMENT_NOVALNET_INVOICE_DURATION;
    $payment_duration = trim($payment_duration);
    $payment_duration = str_replace(' ', '', $payment_duration);
    if(!preg_match("/^[0-9]*$/", $payment_duration)){$payment_duration = '';}

    $due_date = '';
    $due_date_string = '';
    if($payment_duration)
    {
	    $due_date = date("d.m.Y",mktime(0,0,0,date("m"),date("d")+$payment_duration,date("Y")));
	    $due_date_string = '&due_date='.date("Y-m-d",mktime(0,0,0,date("m"),date("d")+$payment_duration,date("Y")));
    }

    $user_ip = $this->getRealIpAddr();
	
	//set the user telephone
	if($_SESSION['user_tel_invoice']){
		$user_telephone = $_SESSION['user_tel_invoice'];
	}else{
		$user_telephone	= $order->customer['telephone'];
	}
	//set the user email
	if($_SESSION['user_email_invoice']){
		$user_email = $_SESSION['user_email_invoice'];
	}else{
		$user_email	= $this->ReplaceSpecialGermanChars($order->customer['email_address']);
	}
			// set post params
	if( $this->isActivatedCallback && strtolower($order->customer['country']['iso_code_2']) == 'de' && $amount >= MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT ){			
		if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Callback (Telefon & Handy)' )
			{ 
				$this->callback_type = '&pin_by_callback=1'; 
				$url_telephone = '&tel='.$user_telephone;
			}
			if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'SMS (nur Handy)' )  
			{ 
				$this->callback_type = '&pin_by_sms=1'; 
				$url_telephone = '&mobile='.$user_telephone;
			}
			if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Email Reply' )
		{ 
			$this->callback_type = '&reply_email_check=1'; 
		}
		
	}

    $urlparam = 'vendor='.trim(MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID).'&product='.$product_id.'&key='.$this->KEY.'&tariff='.$tariff_id;
    $urlparam .= '&auth_code='.trim(MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE).'&currency='.$order->info['currency'];
    $urlparam .= '&amount='.$amount.'&invoice_type=INVOICE'.$due_date_string;
    $urlparam .= '&first_name='.$this->ReplaceSpecialGermanChars($order->customer['firstname']).'&last_name='.$this->ReplaceSpecialGermanChars($order->customer['lastname']);
    $urlparam .= 
    '&street='.$this->ReplaceSpecialGermanChars($order->customer['street_address']).'&city='.$this->ReplaceSpecialGermanChars($order->customer['city']).'&zip='.$order->customer['postcode']
    ;
    $urlparam .= '&country='.$order->customer['country']['iso_code_2'].'&email='.$user_email;
    $urlparam .= '&search_in_street=1'.$url_telephone.'&remote_ip='.$user_ip;
    $urlparam .= '&gender='.$customer['customers_gender'].'&birth_date='.$customer['customers_dob'].'&fax='.$customer['customers_fax'];
    $urlparam .= '&language='.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_LANG;
    $test_mode = (strtolower(MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE) == 'true' or MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE == '1')? 1: 0;
    $urlparam .= '&test_mode='.$test_mode;
	// For PIN by call back
	
	$urlparam .= $this->callback_type;

    list($errno, $errmsg, $data) = $this->perform_https_request($url, $urlparam);

    if ($errno or $errmsg) {
      ### Payment Gateway Error ###
      $order->info['comments'] .= '. func perform_https_request returned Errorno : '.$errno.', Error Message : '.$errmsg;
      $payment_error_return = 'payment_error=' . $this->code . '&error=' .utf8_encode($errmsg). '('.$errno.')';
      if ($this->is_ajax) {
		$_SESSION['checkout_payment_error'] = $payment_error_return;
	  } else {
		xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
	  }
    }

    $aryResponse = array();
    #capture the result and message and other parameters from response data '$data' in an array
    $aryPaygateResponse = explode('&', $data);
    foreach($aryPaygateResponse as $key => $value)
    {
       if($value!="")
       {
          $aryKeyVal = explode("=",$value);
          $aryResponse[$aryKeyVal[0]] = $aryKeyVal[1];
       }
    }

    #Get the type of the comments field on TABLE_ORDERS
    $customer_query = xtc_db_query("SHOW COLUMNS FROM ". TABLE_ORDERS); # . " WHERE FIELD='comments'");#MySQL Version 3/4 dislike WHERE CLAUSE here :-(
    while ($customer = xtc_db_fetch_array($customer_query)) {
      if(strtolower($customer['Field']) == 'comments' and strtolower($customer['Type'])!='text') {
        ### ALTER TABLE ORDERS modify the column comments ###
        xtc_db_query("ALTER TABLE ". TABLE_ORDERS . " MODIFY comments text");
      }
    }

    if($aryResponse['status']==100)
    {
		$_SESSION['prod_total_amount_invoice']=$order->info['subtotal'];
		$_SESSION['shipping_name_invoice'] = $order->info['shipping_class'];
		$_SESSION['nn_tid_invoice'] = $aryResponse['tid'];
		### Passing through the Transaction ID from Novalnet's paygate into order-info ###
		if( $this->isActivatedCallback && strtolower($order->customer['country']['iso_code_2']) == 'de' && $amount >= MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT ){		
			
			// To avoide payment method confussion add code in session
			//set session for maximum time limit to 30 minutes
			$_SESSION['max_time_invoice'] = time() + (30 * 60);
			//TEST BILLING MESSAGE BASED ON THE RESPONSE TEST MODE
			$test_mode = (strtolower(MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE) == 'true' or MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE == '1')? 1: 0;
			if($test_mode)
			{
				$_SESSION['test_mode_invoice'] = $test_mode;
			}
			$_SESSION['original_amount_invoice'] = $aryResponse['amount'];
			$_SESSION['due_date_invoice'] = $due_date;
			### WRITE THE INVOICE BANK DATA ON SESSION ###     
			$_SESSION['nn_invoice_account'] = $aryResponse['invoice_account'];
			$_SESSION['nn_invoice_bankcode'] = $aryResponse['invoice_bankcode'];
			$_SESSION['nn_invoice_iban'] = $aryResponse['invoice_iban'];
			$_SESSION['nn_invoice_bic'] = $aryResponse['invoice_bic'];
			$_SESSION['nn_invoice_bankname'] = $aryResponse['invoice_bankname'];
			$_SESSION['nn_invoice_bankplace'] = $aryResponse['invoice_bankplace'];
			
				if( MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS == 'Email Reply' ){
					$checkoutmsg =MODULE_PAYMENT_NOVALNET_INVOICE_EMAIL_REPLY_CHECK_MSG;
				}else {
					$checkoutmsg =MODULE_PAYMENT_NOVALNET_INVOICE_PIN_CHECK_MSG;
				}
				$payment_error_return     = 'payment_error='.$this->code.'&error='.utf8_encode($checkoutmsg);

				if ($this->is_ajax) {
					$_SESSION['checkout_payment_error'] = $payment_error_return;
				} else {
					xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
				}
		}else{
			if( $this->order_status )
				$order->info['order_status'] = $this->order_status;
		    $old_comments = $order->info['comments'];
			$order->info['comments'] ="";
			$test_mode = (strtolower(MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE) == 'true' or MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE == '1')? 1: 0;
			if($aryResponse['test_mode']==1 || $test_mode == 1){			
				$order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEST_ORDER_MESSAGE;
			}
			  $amount = str_replace('.', ',', sprintf("%.2f", $amount/100));

			  if($due_date)
			  {
				$order->info['comments'] .= '<BR><B>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_DURATION_LIMIT_INFO." $due_date 
			".MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_DURATION_LIMIT_END_INFO.".</B><BR><BR>";
			  }
			  else
			  {
				$order->info['comments'] .= '<BR><B>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_TRANSFER_INFO.'</B><BR><BR>';
			  }
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_ACCOUNT_OWNER.' NOVALNET AG<BR>';
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_ACCOUNT_NUMBER.' '.$aryResponse['invoice_account'].'<BR>';
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_CODE.' '.$aryResponse['invoice_bankcode'].'<BR>';
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_BANK.' '.$aryResponse['invoice_bankname'].', 
			  '.$aryResponse['invoice_bankplace'].'<BR><BR>';
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_IBAN.' '.$aryResponse['invoice_iban'].'<BR>';
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_BANK_BIC.' '.$aryResponse['invoice_bic'].'<BR><BR>';
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_AMOUNT.' '.$aryResponse['amount'].' '.$order->info['currency'].'<BR>';    
			  $order->info['comments'] .= MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_REFERENCE.' <B>TID '.$aryResponse['tid'].'</B><BR>';
			  $order->info['comments'] .= '<B>'.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_REFERENCE_INFO.'</B><BR>';
			  $order->info['comments'] .= $old_comments;
			  $order->info['comments']  = str_replace(array('<b>', '</b>','<B>','</B>', '<br>','<br />','<BR>'), array('', '', '','',"\n", "\n","\n"), $order->info['comments']);	
			  //$order->info['comments']  = str_replace(array('<B>', '</B>', '<BR>', '<br />','<b>','</b>','<br>'), array('', '', "\n", "\n"),$order->info['comments']);			  
		}	
    }
    else
    {
      ### Passing through the Error Response from Novalnet's paygate into order-info ###
      $order->info['comments'] .= '. Novalnet Error Code : '.$aryResponse['status'].', Novalnet Error Message : '.$aryResponse['status_desc'];

      $payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode(utf8_encode($aryResponse['status_desc']));
      if ($this->is_ajax) {
		xtc_redirect(xtc_href_link(FILENAME_CHECKOUT, $payment_error_return, 'SSL', true, false));
	  } else {
		xtc_redirect(xtc_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
	  }
    }

    return;
  }

  ### Realtime accesspoint for communication to the Novalnet paygate ###
  function perform_https_request($nn_url, $urlparam)
  {
      $debug = 0;#set it to 1 if you want to activate the debug mode

      if($debug) print "<BR>perform_https_request: $nn_url<BR>\n\r\n";
      if($debug) print "perform_https_request: $urlparam<BR>\n\r\n";

      ## some prerquisites for the connection
      $ch = curl_init($nn_url);
      curl_setopt($ch, CURLOPT_POST, 1);  // a non-zero parameter tells the library to do a regular HTTP post.
      curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam);  // add POST fields
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);  // don't allow redirects
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // decomment it if you want to have effective ssl checking
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // decomment it if you want to have effective ssl checking
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // return into a variable
      curl_setopt($ch, CURLOPT_TIMEOUT, 240);  // maximum time, in seconds, that you'll allow the CURL functions to take
      if ($this->proxy) {curl_setopt($ch, CURLOPT_PROXY, $this->proxy); }

      ## establish connection
      $data = curl_exec($ch);
      $data = $this->ReplaceSpecialGermanChars($data);

      ## determine if there were some problems on cURL execution
      $errno = curl_errno($ch);
      $errmsg = curl_error($ch);

      ###bug fix for PHP 4.1.0/4.1.2 (curl_errno() returns high negative value in case of successful termination)
      if($errno < 0) $errno = 0;
      ##bug fix for PHP 4.1.0/4.1.2

      if($debug)
      {
        print_r(curl_getinfo($ch));
        echo "\n<BR><BR>\n\n\nperform_https_request: cURL error number:" . $errno . "\n<BR>\n\n";
        echo "\n\n\nperform_https_request: cURL error:" . $errmsg . "\n<BR>\n\n";
      }

      #close connection
      curl_close($ch);

      ## read and return data from novalnet paygate
      if($debug) print "<BR>\n\n" . $data . "\n<BR>\n\n";

      return array ($errno, $errmsg, $data);
  }

  function isPublicIP($value)
  {
        if(!$value || count(explode('.',$value)) != 4) return false;
        return !preg_match('~^((0|10|172\.16|192\.168|169\.254|255|127\.0)\.)~', $value);
  }

  ### get the real Ip Adress of the User ###
  function getRealIpAddr()
  {
        if($_SERVER['HTTP_X_FORWARDED_FOR'] and $this->isPublicIP($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        if($_SERVER['HTTP_X_FORWARDED_FOR'] and $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            if($this->isPublicIP($iplist[0])) return $iplist[0];
        }
        if ($_SERVER['HTTP_CLIENT_IP'] and $this->isPublicIP($_SERVER['HTTP_CLIENT_IP']))
        {
            return $_SERVER['HTTP_CLIENT_IP'];
        }
        if ($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] and $this->isPublicIP($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
        {
            return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
        }
        if ($_SERVER['HTTP_FORWARDED_FOR'] and $this->isPublicIP($_SERVER['HTTP_FORWARDED_FOR']) )
        {
            return $_SERVER['HTTP_FORWARDED_FOR'];
        }
        return $_SERVER['REMOTE_ADDR'];
  }

  ### replace the Special German Charectors ###
  function ReplaceSpecialGermanChars($string)
  {
     $what = array("ä", "ö", "ü", "Ä", "Ö", "Ü", "ß");
     $how = array("ae", "oe", "ue", "Ae", "Oe", "Ue", "ss");

     $string = str_replace($was, $wie, $string);

     return $string;
  }
  ### for email validation ###
  function emailVaildate($email){
	if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
		return false; //Invalid email
	}else{
		return true;  //Valid email
	} 
  }
  ### Send the order detail to Novalnet ###
  function after_process() {
    global $order, $customer_id, $insert_id;

    if ($this->order_status){
      xtc_db_query("UPDATE ".TABLE_ORDERS." SET orders_status='".$this->order_status."' WHERE orders_id='".$insert_id."'");
    }

    $product_id = trim(MODULE_PAYMENT_NOVALNET_INVOICE_PRODUCT_ID);
    $tariff_id = trim(MODULE_PAYMENT_NOVALNET_INVOICE_TARIFF_ID);
	if($_SESSION['nn_tid_invoice']){
		### Pass the Order Reference to paygate ##
		$url = 'https://payport.novalnet.de/paygate.jsp';
		$urlparam = 'vendor='.trim(MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID).'&product='.$product_id.'&key='.$this->KEY.'&tariff='.$tariff_id;
		$urlparam .= '&auth_code='.trim(MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE).'&status=100';
		$urlparam .= '&tid='.$_SESSION['nn_tid_invoice'];
		$urlparam .= '&order_no='.$insert_id.'&invoice_ref=BNR-'.$product_id.'-'.$insert_id;
		$urlparam .= '&vwz2='.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_ORDERNO.''.$insert_id.'&vwz3='.MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_ORDERDATE.''.date('Y-m-d H:i:s');
		$this->perform_https_request($url, $urlparam);
	}
	unset($_SESSION['user_tel_invoice']);
	unset($_SESSION['nn_tid_invoice']);			
	unset($_SESSION['max_time_invoice']);
	unset($_SESSION['email_reply_check_invoice']);
	unset($_SESSION['xml_resp_error_invoice']);
	unset($_SESSION['user_tel_invoice']);
	unset($_SESSION['user_email_invoice']);
	unset($_SESSION['amount_first_invoice']);
	unset($_SESSION['prod_total_amount_invoice']);
	unset($_SESSION['shipping_name_invoice']);
    ### Implement here the Emailversand and further functions, incase if you want to send a own email ###

    return false;
  }

  ### Used to display error message details ###
  // @return array
  function get_error() {
    global $HTTP_GET_VARS, $_GET;
	if ($this->is_ajax) {
		unset($_SESSION['shipping']);	
	}
    if(count($HTTP_GET_VARS)==0 || $HTTP_GET_VARS=='') $HTTP_GET_VARS = $_GET;

    $error = array('title' => MODULE_PAYMENT_NOVALNET_INVOICE_TEXT_ERROR,
                   'error' => stripslashes(utf8_decode($HTTP_GET_VARS['error'])));

    return $error;
  }

  ### Check to see whether module is installed ###
  // @return boolean
  function check() {
    if (!isset($this->_check)) {
      $check_query = xtc_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 
      'MODULE_PAYMENT_NOVALNET_INVOICE_STATUS'");
      $this->_check = xtc_db_num_rows($check_query);
    }
    return $this->_check;
  }

  ### Install the payment module and its configuration settings ###
  function install() {

    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_ALLOWED', '', '6', '0', now())");
	xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) values ('MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS', 'False', '6', '1', 'xtc_cfg_select_option(array( \'False\', \'Callback (Telefon & Handy)\', \'SMS (nur Handy)\', \'Email Reply\'), ', now())");    
	xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values ('MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT', '', '6', '2', now())");	
	xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) values ('MODULE_PAYMENT_NOVALNET_INVOICE_LOGO_STATUS', 'Enable', '6', '3', 'xtc_cfg_select_option(array(\'Enable\', \'Disable\'), ', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) 
    values ('MODULE_PAYMENT_NOVALNET_INVOICE_STATUS', 'True', '6', '4', 'xtc_cfg_select_option(array(\'True\', \'False\'), ', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) 
    values ('MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE', 'True', '6', '5', 'xtc_cfg_select_option(array(\'True\', \'False\'), ', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID', '', '6', '6', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE', '', '6', '7', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_PRODUCT_ID', '', '6', '8', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_TARIFF_ID', '', '6', '9', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_DURATION', '', '6', '10', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_INFO', '', '6', '11', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
    ('MODULE_PAYMENT_NOVALNET_INVOICE_SORT_ORDER', '0', '6', '12', now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, use_function, 
    date_added) values ('MODULE_PAYMENT_NOVALNET_INVOICE_ORDER_STATUS_ID', '0', '6', '13', 'xtc_cfg_pull_down_order_statuses(', 'xtc_get_order_status_name', 
    now())");
    xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, 
    date_added) values ('MODULE_PAYMENT_NOVALNET_INVOICE_ZONE', '0', '6', '14', 'xtc_get_zone_class_title', 'xtc_cfg_pull_down_zone_classes(', now())");
     xtc_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) values 
     ('MODULE_PAYMENT_NOVALNET_INVOICE_PROXY', '', '6', '15', now())");

  }
   
  ### Remove the module and all its settings ###
  function remove() {
    xtc_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  }

  ### Internal list of configuration keys used for configuration of the module ###
  // @return array
  function keys() {
    return array('MODULE_PAYMENT_NOVALNET_INVOICE_ALLOWED','MODULE_PAYMENT_NOVALNET_INVOICE_STATUS', 'MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_SMS','MODULE_PAYMENT_NOVALNET_INVOICE_PIN_BY_CALLBACK_MIN_LIMIT', 
	'MODULE_PAYMENT_NOVALNET_INVOICE_LOGO_STATUS',
	'MODULE_PAYMENT_NOVALNET_INVOICE_TEST_MODE', 
    'MODULE_PAYMENT_NOVALNET_INVOICE_VENDOR_ID', 'MODULE_PAYMENT_NOVALNET_INVOICE_AUTH_CODE', 'MODULE_PAYMENT_NOVALNET_INVOICE_PRODUCT_ID', 
    'MODULE_PAYMENT_NOVALNET_INVOICE_TARIFF_ID', 'MODULE_PAYMENT_NOVALNET_INVOICE_DURATION', 'MODULE_PAYMENT_NOVALNET_INVOICE_INFO', 
    'MODULE_PAYMENT_NOVALNET_INVOICE_SORT_ORDER', 'MODULE_PAYMENT_NOVALNET_INVOICE_ORDER_STATUS_ID', 'MODULE_PAYMENT_NOVALNET_INVOICE_ZONE', 
    'MODULE_PAYMENT_NOVALNET_INVOICE_PROXY');
  }

	function html_to_utf8 ($data)
	{
		return preg_replace("/\\&\\#([0-9]{3,10})\\;/e", '$this->_html_to_utf8("\\1")', $data);
	}

	function _html_to_utf8 ($data)
	{
		if ($data > 127)
		{
			$i = 5;
			while (($i--) > 0)
			{
				if ($data != ($a = $data % ($p = pow(64, $i))))
				{
					$ret = chr(base_convert(str_pad(str_repeat(1, $i + 1), 8, "0"), 2, 10) + (($data - $a) / $p));
					for ($i; $i > 0; $i--)
						$ret .= chr(128 + ((($data % pow(64, $i)) - ($data % ($p = pow(64, $i - 1)))) / $p));
					break;
				}
			}
		}
		else
		{
			$ret = "&#$data;";
		}
		return $ret;
	}
}
/*
order of functions:
selection              -> $order-info['total'] wrong, cause shipping_cost is net
pre_confirmation_check -> $order-info['total'] wrong, cause shipping_cost is net
confirmation           -> $order-info['total'] right, cause shipping_cost is gross
process_button         -> $order-info['total'] right, cause shipping_cost is gross
before_process         -> $order-info['total'] wrong, cause shipping_cost is net
after_process          -> $order-info['total'] right, cause shipping_cost is gross
*/