<?php
/**
 * Multisafepay Payment Integration
 * Date 15-07-2011
 * @version 1.5
 * @package 1.5
 * @copyright 2011 MultiSafePay.
 */
class ControllerPaymentMultiSafePay extends Controller {
	private $error = array(); 
	
	public function index() {
	
		$this->load->language('payment/multisafepay');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->load->model('setting/setting');
			
			$this->model_setting_setting->editSetting('multisafepay', $this->request->post);				
			
			$this->session->data['success'] = $this->language->get('text_success');

			
			$this->redirect($this->setup_link('extension/payment'));
		}
		
		$this->data['action'] = $this->setup_link('payment/multisafepay');
		$this->data['cancel'] = $this->setup_link('extension/payment');
		$this->data['text_set_order_status']							= $this->language->get('text_set_order_status');
		$this->data['text_free_account']							    = $this->language->get('text_free_account');
		
		$this->data['heading_title']									= $this->language->get('heading_title');
		$this->data['entry_multisafepay_merchantid']					= $this->language->get('entry_multisafepay_merchantid');
		$this->data['entry_multisafepay_siteid']						= $this->language->get('entry_multisafepay_siteid');
		$this->data['entry_multisafepay_secure_code']					= $this->language->get('entry_multisafepay_secure_code');
		$this->data['entry_multisafepay_merchantid']					= $this->language->get('bno_multisafepay_merchantid');
		$this->data['entry_multisafepay_siteid']						= $this->language->get('bno_multisafepay_siteid');
		$this->data['entry_multisafepay_secure_code']					= $this->language->get('bno_multisafepay_secure_code');
		$this->data['entry_environment']								= $this->language->get('entry_environment');		
		$this->data['select_methods']									= $this->language->get('select_methods');	
		$this->data['entry_multisafepay_redirect_url']					= $this->language->get('entry_multisafepay_redirect_url');		
		$this->data['entry_multisafepay_gateway_selection']				= $this->language->get('entry_multisafepay_gateway_selection');		
		$this->data['entry_multisafepay_order_status_id_initialized']	= $this->language->get('entry_multisafepay_order_status_id_initialized');
		$this->data['entry_multisafepay_order_status_id_completed']		= $this->language->get('entry_multisafepay_order_status_id_completed');
		$this->data['entry_multisafepay_order_status_id_uncleared']		= $this->language->get('entry_multisafepay_order_status_id_uncleared');
		$this->data['entry_multisafepay_order_status_id_reserved']		= $this->language->get('entry_multisafepay_order_status_id_reserved');
		$this->data['entry_multisafepay_order_status_id_void']			= $this->language->get('entry_multisafepay_order_status_id_void');
		$this->data['entry_multisafepay_order_status_id_refunded']		= $this->language->get('entry_multisafepay_order_status_id_refunded');
		$this->data['entry_multisafepay_order_status_id_declined']		= $this->language->get('entry_multisafepay_order_status_id_declined');
		$this->data['entry_multisafepay_order_status_id_expired']		= $this->language->get('entry_multisafepay_order_status_id_expired');
		//start bno data
		$this->data['text_min_bno_amount']								= $this->language->get('text_min_bno_amount');
		$this->data['text_max_bno_amount']								= $this->language->get('text_max_bno_amount');
		$this->data['text_set_bno_data']								= $this->language->get('text_set_bno_data');
		$this->data['text_bno_product_fee_id']							= $this->language->get('text_bno_product_fee_id');
		$this->data['text_bno_ip_validation_option']					= $this->language->get('text_bno_ip_validation_option');
		$this->data['text_bno_ip_validation_address']					= $this->language->get('text_bno_ip_validation_address');
		//end bno data
		$this->data['entry_status']										= $this->language->get('entry_status');
		$this->data['entry_sort_order']									= $this->language->get('entry_sort_order');
		$this->data['enable_checkout_button']							= $this->language->get('enable_checkout_button');
		
		if (isset($this->request->post['bno_product_fee_id'])) {
			$this->data['bno_product_fee_id'] = $this->request->post['bno_product_fee_id'];
		} else {
			$this->data['bno_product_fee_id'] = $this->config->get('bno_product_fee_id');
		}
		
		if (isset($this->request->post['bno_ip_validation_address'])) {
			$this->data['bno_ip_validation_address'] = $this->request->post['bno_ip_validation_address'];
		} else {
			$this->data['bno_ip_validation_address'] = $this->config->get('bno_ip_validation_address');
		}
		
		
		if (isset($this->request->post['bno_ip_validation_enabler'])) {
			$this->data['bno_ip_validation_enabler'] = $this->request->post['bno_ip_validation_enabler'];
		} else {
			$this->data['bno_ip_validation_enabler'] = $this->config->get('bno_ip_validation_enabler');
		}
		
		if (isset($this->request->post['max_bno_amount'])) {
			$this->data['max_bno_amount'] = $this->request->post['max_bno_amount'];
		} else {
			$this->data['max_bno_amount'] = $this->config->get('max_bno_amount');
		}
		if (isset($this->request->post['min_bno_amount'])) {
			$this->data['min_bno_amount'] = $this->request->post['min_bno_amount'];
		} else {
			$this->data['min_bno_amount'] = $this->config->get('min_bno_amount');
		}

		if (isset($this->request->post['multisafepay_merchant_id'])) {
			$this->data['multisafepay_merchant_id'] = $this->request->post['multisafepay_merchant_id'];
		} else {
			$this->data['multisafepay_merchant_id'] = $this->config->get('multisafepay_merchant_id');
		}		
	
		if (isset($this->request->post['multisafepay_site_id'])) {
			$this->data['multisafepay_site_id'] = $this->request->post['multisafepay_site_id'];
		} else {
			$this->data['multisafepay_site_id'] = $this->config->get('multisafepay_site_id');
		}

		if (isset($this->request->post['multisafepay_secure_code'])) {
			$this->data['multisafepay_secure_code'] = $this->request->post['multisafepay_secure_code'];
		} else {
			$this->data['multisafepay_secure_code'] = $this->config->get('multisafepay_secure_code');
		}
		
		
		if (isset($this->request->post['bno_multisafepay_merchant_id'])) {
			$this->data['bno_multisafepay_merchant_id'] = $this->request->post['bno_multisafepay_merchant_id'];
		} else {
			$this->data['bno_multisafepay_merchant_id'] = $this->config->get('bno_multisafepay_merchant_id');
		}		
	
		if (isset($this->request->post['bno_multisafepay_site_id'])) {
			$this->data['bno_multisafepay_site_id'] = $this->request->post['bno_multisafepay_site_id'];
		} else {
			$this->data['bno_multisafepay_site_id'] = $this->config->get('bno_multisafepay_site_id');
		}

		if (isset($this->request->post['bno_multisafepay_secure_code'])) {
			$this->data['bno_multisafepay_secure_code'] = $this->request->post['bno_multisafepay_secure_code'];
		} else {
			$this->data['bno_multisafepay_secure_code'] = $this->config->get('bno_multisafepay_secure_code');
		}
		
		if (isset($this->request->post['bno_multisafepay_environment'])) {
			$this->data['bno_multisafepay_environment'] = $this->request->post['bno_multisafepay_environment'];
		} else {
			$this->data['bno_multisafepay_environment'] = $this->config->get('bno_multisafepay_environment');
		}
		
		


		if (isset($this->request->post['multisafepay_environment'])) {
			$this->data['multisafepay_environment'] = $this->request->post['multisafepay_environment'];
		} else {
			$this->data['multisafepay_environment'] = $this->config->get('multisafepay_environment');
		}
		
		if (isset($this->request->post['enable_checkout_button_connect'])) {
			$this->data['enable_checkout_button_connect'] = $this->request->post['enable_checkout_button_connect'];
		} else {
			$this->data['enable_checkout_button_connect'] = $this->config->get('enable_checkout_button_connect');
		}
		
		
		if (isset($this->request->post['multisafepay_account_type'])) {
			$this->data['multisafepay_account_type'] = $this->request->post['multisafepay_account_type'];
		} else {
			$this->data['multisafepay_account_type'] = $this->config->get('multisafepay_account_type');
		}

		if (isset($this->request->post['multisafepay_redirect_url'])) {
			$this->data['multisafepay_redirect_url'] = $this->request->post['multisafepay_redirect_url'];
		} else {
			$this->data['multisafepay_redirect_url'] = $this->config->get('multisafepay_redirect_url');
		}
	 
		if (isset($this->request->post['multisafepay_gateway_selection'])) {
			$this->data['multisafepay_gateway_selection'] = $this->request->post['multisafepay_gateway_selection'];
		} else {
			$this->data['multisafepay_gateway_selection'] = $this->config->get('multisafepay_gateway_selection');
		}
	 
		if (isset($this->request->post['MSP_MISTERCASH'])) {
			$this->data['MSP_MISTERCASH'] = $this->request->post['MSP_MISTERCASH'];
		} else {
			$this->data['MSP_MISTERCASH'] = $this->config->get('MSP_MISTERCASH');
		}
	 
		if (isset($this->request->post['MSP_DIRDEB'])) {
			$this->data['MSP_DIRDEB'] = $this->request->post['MSP_DIRDEB'];
		} else {
			$this->data['MSP_DIRDEB'] = $this->config->get('MSP_DIRDEB');
		}
	 
		if (isset($this->request->post['MSP_WALLET'])) {
			$this->data['MSP_WALLET'] = $this->request->post['MSP_WALLET'];
		} else {
			$this->data['MSP_WALLET'] = $this->config->get('MSP_WALLET');
		}
	 
		if (isset($this->request->post['MSP_BANKTRANS'])) {
			$this->data['MSP_BANKTRANS'] = $this->request->post['MSP_BANKTRANS'];
		} else {
			$this->data['MSP_BANKTRANS'] = $this->config->get('MSP_BANKTRANS');
		}
	 
		if (isset($this->request->post['MSP_IDEAL'])) {
			$this->data['MSP_IDEAL'] = $this->request->post['MSP_IDEAL'];
		} else {
			$this->data['MSP_IDEAL'] = $this->config->get('MSP_IDEAL');
		}
	 
		if (isset($this->request->post['MSP_VISA'])) {
			$this->data['MSP_VISA'] = $this->request->post['MSP_VISA'];
		} else {
			$this->data['MSP_VISA'] = $this->config->get('MSP_VISA');
		}
	 
		if (isset($this->request->post['MSP_MASTERCARD'])) {
			$this->data['MSP_MASTERCARD'] = $this->request->post['MSP_MASTERCARD'];
		} else {
			$this->data['MSP_MASTERCARD'] = $this->config->get('MSP_MASTERCARD');
		}
	 
		if (isset($this->request->post['MSP_DIRECTBANK'])) {
			$this->data['MSP_DIRECTBANK'] = $this->request->post['MSP_DIRECTBANK'];
		} else {
			$this->data['MSP_DIRECTBANK'] = $this->config->get('MSP_DIRECTBANK');
		}
	 
		if (isset($this->request->post['MSP_GIROPAY'])) {
			$this->data['MSP_GIROPAY'] = $this->request->post['MSP_GIROPAY'];
		} else {
			$this->data['MSP_GIROPAY'] = $this->config->get('MSP_GIROPAY');
		}
	 
		if (isset($this->request->post['MSP_MAESTRO'])) {
			$this->data['MSP_MAESTRO'] = $this->request->post['MSP_MAESTRO'];
		} else {
			$this->data['MSP_MAESTRO'] = $this->config->get('MSP_MAESTRO');
		}
		if (isset($this->request->post['MSP_PAYAFTER'])) {
			$this->data['MSP_PAYAFTER'] = $this->request->post['MSP_PAYAFTER'];
		} else {
			$this->data['MSP_PAYAFTER'] = $this->config->get('MSP_PAYAFTER');
		}
	 
		
		if (isset($this->request->post['multisafepay_order_status_id_completed'])) {
			$this->data['multisafepay_order_status_id_completed'] = $this->request->post['multisafepay_order_status_id_completed'];
		} else {
			$this->data['multisafepay_order_status_id_completed'] = $this->config->get('multisafepay_order_status_id_completed');
		}


		if (isset($this->request->post['multisafepay_order_status_id_initialized'])) {
			$this->data['multisafepay_order_status_id_initialized'] = $this->request->post['multisafepay_order_status_id_initialized'];
		} else {
			$this->data['multisafepay_order_status_id_initialized'] = $this->config->get('multisafepay_order_status_id_initialized');
		}
		
		if (isset($this->request->post['multisafepay_order_status_id_uncleared'])) {
			$this->data['multisafepay_order_status_id_uncleared'] = $this->request->post['multisafepay_order_status_id_uncleared'];
		} else {
			$this->data['multisafepay_order_status_id_uncleared'] = $this->config->get('multisafepay_order_status_id_uncleared');
		}

		if (isset($this->request->post['multisafepay_order_status_id_reserved'])) {
			$this->data['multisafepay_order_status_id_reserved'] = $this->request->post['multisafepay_order_status_id_reserved'];
		} else {
			$this->data['multisafepay_order_status_id_reserved'] = $this->config->get('multisafepay_order_status_id_reserved');
		}

		if (isset($this->request->post['multisafepay_order_status_id_void'])) {
			$this->data['multisafepay_order_status_id_void'] = $this->request->post['multisafepay_order_status_id_void'];
		} else {
			$this->data['multisafepay_order_status_id_void'] = $this->config->get('multisafepay_order_status_id_void');
		}
		
		if (isset($this->request->post['multisafepay_order_status_id_refunded'])) {
			$this->data['multisafepay_order_status_id_refunded'] = $this->request->post['multisafepay_order_status_id_refunded'];
		} else {
			$this->data['multisafepay_order_status_id_refunded'] = $this->config->get('multisafepay_order_status_id_refunded');
		}

		if (isset($this->request->post['multisafepay_order_status_id_declined'])) {
			$this->data['multisafepay_order_status_id_declined'] = $this->request->post['multisafepay_order_status_id_declined'];
		} else {
			$this->data['multisafepay_order_status_id_declined'] = $this->config->get('multisafepay_order_status_id_declined');
		}

		if (isset($this->request->post['multisafepay_order_status_id_expired'])) {
			$this->data['multisafepay_order_status_id_expired'] = $this->request->post['multisafepay_order_status_id_expired'];
		} else {
			$this->data['multisafepay_order_status_id_expired'] = $this->config->get('multisafepay_order_status_id_expired');
		}


		if (isset($this->request->post['multisafepay_status'])) {
			$this->data['multisafepay_status'] = $this->request->post['multisafepay_status'];
		} else {
			$this->data['multisafepay_status'] = $this->config->get('multisafepay_status');
		}
		

		if (isset($this->request->post['multisafepay_sort_order'])) {
			$this->data['multisafepay_sort_order'] = $this->request->post['multisafepay_sort_order'];
		} else {
			$this->data['multisafepay_sort_order'] = $this->config->get('multisafepay_sort_order');
		}

		// callback url
		$this->data['callback'] = HTTP_CATALOG . 'index.php?route=payment/multisafepay/callback';
		$this->load->model('localisation/order_status');		
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();


		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['tab_general'] = $this->language->get('tab_general');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['merchant'])) {
			$this->data['error_merchant'] = $this->error['merchant'];
		} else {
			$this->data['error_merchant'] = '';
		}

 		if (isset($this->error['password'])) {
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}
		
		$this->data['breadcrumbs'] = array();
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->setup_link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_payment'),
			'href'      => $this->setup_link('extension/payment'),      		
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->setup_link('payment/multisafepay'),
      		'separator' => ' :: '
   		);
				
			
		$this->template = 'payment/multisafepay.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'payment/multisafepay')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->request->post['multisafepay_merchant_id']) {
			$this->error['merchant'] = $this->language->get('error_merchant');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
	
	private function setup_link($route){
		$old_version = (substr(str_replace(".", "", VERSION), 0, 2) < 15) ? true : false;
		$link = ($old_version) ? HTTPS_SERVER . 'index.php?route='.$route.'&token=' . $this->session->data['token'] : $this->url->link($route, 'token=' . $this->session->data['token'], 'SSL');
		return $link;
	}
}



?>