<?php
/*
 osCommRes, Services Online 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare 

Released under the GNU General Public License 
*/
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
  class commbank {
    var $code, $title, $description, $enabled;

// class constructor
	function commbank() {
		$this->code = 'commbank';
		$name = "Commonwealth Bank";
		$image = "";
		if(MODULE_PAYMENT_COMMBANK_DISPLAY_NAME != "MODULE_PAYMENT_COMMBANK_DISPLAY_NAME")$name = MODULE_PAYMENT_COMMBANK_DISPLAY_NAME;
		if(MODULE_PAYMENT_COMMBANK_IMAGE != "MODULE_PAYMENT_COMMBANK_IMAGE")$image = MODULE_PAYMENT_COMMBANK_IMAGE;
		if($image != "" && file_exists("../" . DIR_WS_IMAGES . $image)){
			$image = '<img src="'. HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_IMAGES . $image . '" width="103" height="33">';
		}else{
			$image_array = array('.gif','.jpg','.jpeg','.png');
			$image_check = true;
			for($i=0;$i<sizeof($image_array);$i++){
				if($image_check && $image != "" && file_exists(DIR_WS_IMAGES . $image . $image_array[$i])){
					$image = '<img src="'. HTTP_SERVER . DIR_WS_CATALOG . DIR_WS_IMAGES . $image . $image_array[$i] . '" width="103" height="33">';
					$image_check = false;
				}
			}
			if($image_check)$image = "";
		}
		define('MODULE_PAYMENT_COMMBANK_TEXT_TITLE', $name . '&nbsp;&nbsp;' . $image);
		define('MODULE_PAYMENT_COMMBANK_TEXT_TEXT_TITLE', $name);
		$this->title = MODULE_PAYMENT_COMMBANK_TEXT_TITLE;
		$this->text_title = MODULE_PAYMENT_COMMBANK_TEXT_TEXT_TITLE;
		$this->description = MODULE_PAYMENT_COMMBANK_TEXT_DESCRIPTION;
		$this->enabled = ((MODULE_PAYMENT_COMMBANK_STATUS == 'True')?true:false);
		$this->sort_order=MODULE_PAYMENT_COMMBANK_SORT_ORDER;
		if ((int)MODULE_PAYMENT_COMMBANK_ORDER_STATUS_ID > 0) {
			$this->order_status = MODULE_PAYMENT_COMMBANK_ORDER_STATUS_ID;
		}
		$this->form_action_url = tep_href_link(FILENAME_CHECKOUT_COMMBANK_RESULT,'','SSL');
	}

// class methods
    function javascript_validation() {
      return false;
    }

	function update_status() {
		return true;
	}
    function selection() {
      global $order;

      for ($i=1; $i<13; $i++) {
        $expires_month[] = array('id' => sprintf('%02d', $i), 'text' =>sprintf('%02d', $i)); //strftime('%B',mktime(0,0,0,$i,1,2000))
      }

      $today = getdate(); 
      for ($i=$today['year']; $i < $today['year']+10; $i++) {
        $expires_year[] = array('id' => strftime('%y',mktime(0,0,0,1,1,$i)), 'text' => strftime('%Y',mktime(0,0,0,1,1,$i)));
      }
      
      $selection = array('id' => $this->code,
                         'module' => $this->title,
                         'fields' => array(array('title' => MODULE_PAYMENT_COMMBANK_TEXT_CREDIT_CARD_NUMBER,
                                                 'field' => tep_draw_input_field('Ecom_Payment_Card_Number')),
                                           array('title' => MODULE_PAYMENT_COMMBANK_TEXT_CREDIT_CARD_EXPIRES,
                                                 'field' => tep_draw_pull_down_menu('Ecom_Payment_Card_Month', $expires_month) . '&nbsp;' . tep_draw_pull_down_menu('Ecom_Payment_Card_Year', $expires_year)),
										   array('title' => MODULE_PAYMENT_COMMBANK_TEXT_SECURITY_CODE,
                                                 'field' => tep_draw_input_field('Ecom_Card_Security_Code','',' size=5').'&nbsp;'.MODULE_PAYMENT_CODE_CONDITION)
										));

      return $selection;
	}

    function pre_confirmation_check() {
     global $HTTP_POST_VARS,$HTTP_SESSION_VARS;

      include(DIR_WS_CLASSES . 'cc_validation.php');

      $cc_validation = new cc_validation();
      $result = $cc_validation->validate($HTTP_POST_VARS['Ecom_Payment_Card_Number'], $HTTP_POST_VARS['Ecom_Payment_Card_Month'], $HTTP_POST_VARS['Ecom_Payment_Card_Year']);

      $error = '';
      switch ($result) {
        case -1:
          $error = sprintf(TEXT_CCVAL_ERROR_UNKNOWN_CARD, substr($cc_validation->cc_number, 0, 4));
          break;
        case -2:
        case -3:
        case -4:
          $error = TEXT_CCVAL_ERROR_INVALID_DATE;
          break;
        case false:
          $error = TEXT_CCVAL_ERROR_INVALID_NUMBER;
          break;
      }

      if ( ($result == false) || ($result < 1) ) {
        $payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($error) .'&securepay_cc_expires_month=' . $HTTP_POST_VARS['Ecom_Payment_Card_Month'] . '&securepay_cc_expires_year=' . $HTTP_POST_VARS['Ecom_Payment_Card_Year'];
		
        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
      }

      $this->Ecom_Payment_Card_Number = $cc_validation->cc_number;
      $this->Ecom_Payment_Card_Month = $cc_validation->cc_expiry_month;
      $this->Ecom_Payment_Card_Year = $cc_validation->cc_expiry_year;
	  $this->Ecom_Card_Security_Code=$HTTP_POST_VARS["Ecom_Card_Security_Code"];

      /*$this->Ecom_Payment_Card_Number = $HTTP_POST_VARS['Ecom_Payment_Card_Number'];
      $this->Ecom_Payment_Card_Month = $HTTP_POST_VARS['Ecom_Payment_Card_Month'];
      $this->Ecom_Payment_Card_Year = $HTTP_POST_VARS['Ecom_Payment_Card_Year'];
	  $this->Ecom_Card_Security_Code=$HTTP_POST_VARS["Ecom_Card_Security_Code"];*/
    }

    function confirmation() {  
     global $HTTP_POST_VARS;

      $confirmation = array('title' => $this->title,
                            'fields' => array(
											  array('title' => MODULE_PAYMENT_COMMBANK_TEXT_CREDIT_CARD_NUMBER,
                                                    'field' => substr($this->Ecom_Payment_Card_Number, 0, 4) . str_repeat('X', (strlen($this->Ecom_Payment_Card_Number) - 8)) . substr($this->Ecom_Payment_Card_Number, -4)),
                                              array('title' => MODULE_PAYMENT_COMMBANK_TEXT_CREDIT_CARD_EXPIRES,
                                                    'field' => strftime('%B, %Y', mktime(0,0,0,$HTTP_POST_VARS['Ecom_Payment_Card_Month'], 1, '20' . $HTTP_POST_VARS['Ecom_Payment_Card_Year']))),
											  array('title' => MODULE_PAYMENT_COMMBANK_TEXT_SECURITY_CODE,
											  		'field' => $HTTP_POST_VARS['Ecom_Card_Security_Code'])));

      return $confirmation;
    }

    function process_button() {
      global $HTTP_POST_VARS, $order, $currencies, $currency,$HTTP_SESSION_VARS;

	  $process_button_string = tep_draw_hidden_field('HostName','127.0.0.1') .
							   tep_draw_hidden_field('Port','9050') .
	  						   tep_draw_hidden_field('MerchTxnRef',$HTTP_SESSION_VARS["referenceID"]) .
	  						   tep_draw_hidden_field('MerchantID', (MODULE_PAYMENT_COMMBANK_TESTMODE=="Production"?MODULE_PAYMENT_COMMBANK_MERCHANT_ID:"TEST" .MODULE_PAYMENT_COMMBANK_MERCHANT_ID)) .
                               tep_draw_hidden_field('PurchaseAmount', number_format(($order->info['total']*100), 0, '', '')) .
							   tep_draw_hidden_field('OrderInfo',$order->customer['firstname'] . ' ' . $order->customer['lastname']) .
                               tep_draw_hidden_field('Ecom_Payment_Card_Number', $this->Ecom_Payment_Card_Number) .
                               tep_draw_hidden_field('Ecom_Payment_Card_Month', $this->Ecom_Payment_Card_Month) .
                               tep_draw_hidden_field('Ecom_Payment_Card_Year', substr($this->Ecom_Payment_Card_Year, -2)) .
							   tep_draw_hidden_field('Ecom_Card_Security_Code',$this->Ecom_Card_Security_Code);
							   
      return $process_button_string;
    }

    function before_process() {
      return false;
    }

    function after_process() {
	    return false;
    }

    function get_error() {
      global $HTTP_GET_VARS;

      $msg = "";
      if (stripslashes(urldecode($HTTP_GET_VARS['response_text'])) != "")
        $msg = stripslashes(urldecode($HTTP_GET_VARS['response_text']));
      else if (stripslashes(urldecode($HTTP_GET_VARS['error'])) != "") 
        $msg = stripslashes(urldecode($HTTP_GET_VARS['error']));
	  else
	  	$msg=MODULE_PAYMENT_COMMBANK_TEXT_ERROR_MESSAGE;

      $error = array('title' => MODULE_PAYMENT_COMMBANK_TEXT_ERROR,
                     'error' => $msg);

      return $error;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_COMMBANK_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Allow CommBank', 'MODULE_PAYMENT_COMMBANK_STATUS', 'True', 'Do you want to accept Commonwealth Bank of Australia payments?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Merchant ID', 'MODULE_PAYMENT_COMMBANK_MERCHANT_ID', 'Enter Your Merchant ID Here', 'Your Merchant ID.', '6', '2', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Mode', 'MODULE_PAYMENT_COMMBANK_TESTMODE', 'Test', 'Transaction mode used for processing orders', '6', '3', 'tep_cfg_select_option(array(\'Test\', \'Production\'), ', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_COMMBANK_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '4', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_COMMBANK_ORDER_STATUS_ID', '2', 'Set the status of orders made with this payment module to this value', '6', '5', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Display Name', 'MODULE_PAYMENT_COMMBANK_DISPLAY_NAME', 'Commonwealth Bank', 'Set the Display name to payment module', '6', '6', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Image', 'MODULE_PAYMENT_COMMBANK_IMAGE', 'commbank', 'Set the Image of payment module', '6', '7', 'tep_cfg_file_field(', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {

      return array('MODULE_PAYMENT_COMMBANK_STATUS', 'MODULE_PAYMENT_COMMBANK_MERCHANT_ID', 'MODULE_PAYMENT_COMMBANK_TESTMODE','MODULE_PAYMENT_COMMBANK_SORT_ORDER','MODULE_PAYMENT_COMMBANK_ORDER_STATUS_ID','MODULE_PAYMENT_COMMBANK_DISPLAY_NAME','MODULE_PAYMENT_COMMBANK_IMAGE');
    }
	function get_comments() {
		global $payment_return1, $payment_return2;
		$comments = 'Receipt No: ' . $payment_return1 . '<br>' . 'Transaction No: ' . $payment_return2;
		if($comments!="")
			return $comments;
		else
			return "";	
	}
  }
?>