<?php
/**
 * DotPay.pl API Payment Module
 *
 * @package paymentMethod
 * @copyright Copyright 2003-2010 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @copyright Portions Copyright 2003 Jason LeBaron
 * @license Commercial Zen Cart Professional
 * @version $Id: linkpoint_api.php 18013 2010-10-22 03:38:29Z drbyte $
 */

class dotpaypl extends base {
	
	var $code;

	var $title;

	var $description;
	
	var $enabled;

	var $codeVersion;
	
	var $insert_id;
	
	var $payment_currency;
	
	var $order_status;
	
	var $form_action_url;
	
	var $currency_available = array('PLN', 'EUR', 'USD', 'GBP', 'JPY', 'CZK', 'SEK');

	var $langs_available = array('pl', 'en', 'de', 'it', 'fr', 'es', 'cz', 'ru', 'bg');
	
	var $dotpaypl_status = array(
									
									'2' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_2, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_STATUS_2'),
									'1' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_1, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_STATUS_1'),
									'3' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_3, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_STATUS_3'),
									'4' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_4, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_STATUS_4'),
									'5' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_5, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_STATUS_5'),									
									'BLAD' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_BLAD, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_STATUS_BLAD'),
									'KWOTA' => array('desc'=>MODULE_PAYMENT_DOTPAYPL_STATUS_TEXT_KWOTA, 'conf_key'=>'MODULE_PAYMENT_DOTPAYPL_ORDER_STATUS_KWOTA')
								);

	var $error_message = array (
										'100' => MODULE_PAYMENT_DOTPAYPL_ERROR_100,
										'110' => MODULE_PAYMENT_DOTPAYPL_ERROR_110,
									);
	
	var $channels = array (
						0 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_000, 'type'=>9, 'img'=>'', 'enable'=>true),
						1 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_001, 'type'=>1, 'img'=>'', 'enable'=>true),
						2 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_002, 'type'=>1, 'img'=>'', 'enable'=>true),
						3 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_003, 'type'=>1, 'img'=>'', 'enable'=>true),
						4 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_004, 'type'=>1, 'img'=>'', 'enable'=>true),

						6 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_006, 'type'=>1, 'img'=>'', 'enable'=>true),
						7 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_007, 'type'=>0, 'img'=>'', 'enable'=>true),
						8 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_008, 'type'=>0, 'img'=>'', 'enable'=>true),
						9 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_009, 'type'=>0, 'img'=>'', 'enable'=>true),
						10 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_010, 'type'=>0, 'img'=>'', 'enable'=>true),
						11 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_011, 'type'=>2, 'img'=>'', 'enable'=>true),

						13 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_013, 'type'=>0, 'img'=>'', 'enable'=>true),
						14 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_014, 'type'=>0, 'img'=>'', 'enable'=>true),
						15 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_015, 'type'=>0, 'img'=>'', 'enable'=>true),
						16 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_016, 'type'=>0, 'img'=>'', 'enable'=>true),
						17 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_017, 'type'=>1, 'img'=>'', 'enable'=>true),
						18 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_018, 'type'=>1, 'img'=>'', 'enable'=>true),
						19 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_019, 'type'=>0, 'img'=>'', 'enable'=>true),

						21 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_021, 'type'=>2, 'img'=>'', 'enable'=>true),
						22 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_022, 'type'=>0, 'img'=>'', 'enable'=>true),
						23 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_023, 'type'=>3, 'img'=>'', 'enable'=>true),
						24 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_024, 'type'=>3, 'img'=>'', 'enable'=>true),
						25 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_025, 'type'=>0, 'img'=>'', 'enable'=>true),
						26 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_026, 'type'=>0, 'img'=>'', 'enable'=>true),
						27 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_027, 'type'=>0, 'img'=>'', 'enable'=>true),

						31 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_031, 'type'=>2, 'img'=>'', 'enable'=>true),
						32 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_032, 'type'=>0, 'img'=>'', 'enable'=>true),
						33 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_033, 'type'=>0, 'img'=>'', 'enable'=>true),

						35 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_035, 'type'=>2, 'img'=>'', 'enable'=>true),

						38 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_038, 'type'=>1, 'img'=>'', 'enable'=>true),
						39 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_039, 'type'=>0, 'img'=>'', 'enable'=>true),

						41 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_041, 'type'=>2, 'img'=>'', 'enable'=>true),
						42 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_042, 'type'=>0, 'img'=>'', 'enable'=>true),
						43 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_043, 'type'=>0, 'img'=>'', 'enable'=>true),
						44 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_044, 'type'=>1, 'img'=>'', 'enable'=>true),
						45 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_045, 'type'=>1, 'img'=>'', 'enable'=>true),

						210 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_210, 'type'=>9, 'img'=>'', 'enable'=>true),
						212 => array('name'=>MODULE_PAYMENT_DOTPAYPL_CHANNEL_212, 'type'=>3, 'img'=>'', 'enable'=>true),
					);

	function dotpaypl() {
		global $order;

		$this->code = 'dotpaypl';
    	$this->codeVersion = MODULE_PAYMENT_DOTPAYPL_CODE_VERSION;
    	$this->payment_currency = 'PLN';
		if (IS_ADMIN_FLAG === true) {
			$this->title = MODULE_PAYMENT_DOTPAYPL_TEXT_TITLE;
			if (MODULE_PAYMENT_DOTPAYPL_ID_SPRZEDAWCY == MODULE_PAYMENT_DOTPAYPL_TEXT_013 || MODULE_PAYMENT_DOTPAYPL_ID_SPRZEDAWCY == '') $this->title .= MODULE_PAYMENT_DOTPAYPL_TEXT_046;
			elseif (MODULE_PAYMENT_DOTPAYPL_PIN_URLC == MODULE_PAYMENT_DOTPAYPL_TEXT_016 || MODULE_PAYMENT_DOTPAYPL_PIN_URLC == '') $this->title .= MODULE_PAYMENT_DOTPAYPL_TEXT_047;
			if (defined('MODULE_PAYMENT_DOTPAYPL_DEBUG') && MODULE_PAYMENT_DOTPAYPL_DEBUG != 'Nie') $this->title .= MODULE_PAYMENT_DOTPAYPL_TEXT_048;
			if (MODULE_PAYMENT_DOTPAYPL_DIAG == 'Tak') $this->title .= MODULE_PAYMENT_DOTPAYPL_TEXT_049;
		} 
		else {
			$this->title = MODULE_PAYMENT_DOTPAYPL_TEXT_TITLE;
		}
		$this->sort_order = MODULE_PAYMENT_DOTPAYPL_SORT_ORDER;
		$this->enabled = ((MODULE_PAYMENT_DOTPAYPL_STATUS == 'True') ? true : false);
		$this->list_on = ((MODULE_PAYMENT_DOTPAYPL_LIST_ON == 'Tak') ? true : false);
		$this->list_enabled = ((MODULE_PAYMENT_DOTPAYPL_LIST_STATUS == 'Lista') ? true : false);
		if ((int)MODULE_PAYMENT_DOTPAYPL_PREPARE_ORDER_STATUS_ID > 0) {
			$this->order_status = MODULE_PAYMENT_DOTPAYPL_PREPARE_ORDER_STATUS_ID;
		}
		$this -> form_action_url = 'https://ssl.dotpay.pl/';

		$this->active_flag = "aktywny";
		$this->inactive_flag = "nieaktywny";

		if (is_object($order)) 
			$this->update_status();
	} 

	function update_status() {
		global $order;
		global $db;

		if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_DOTPAYPL_ZONE > 0) ) {
			$check_flag = false;
			$check = $db->Execute("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_DOTPAYPL_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
			while (!$check->EOF) {
				if ($check->fields['zone_id'] < 1) {
					$check_flag = true;
					break;
				}
				elseif ($check->fields['zone_id'] == $order->billing['zone_id']) {
					$check_flag = true;
					break;
				}
				$check->MoveNext();
			}

			if ($check_flag == false) {
				$this->enabled = false;
			}
		}
	} 

	function javascript_validation() {
		
		$js='';
		if (MODULE_PAYMENT_DOTPAYPL_LIST_ON == 'Tak') {
			$js = '//dotpaypl' . "\n";
			$js = '  if (payment_value == "' . $this->code . '") {' . "\n" .
				  '    var pay_type = document.checkout_payment.channel.value;' . "\n" .
				  '    if ((pay_type == undefined) || (pay_type == "") ) {' . "\n" .
				  '      error_message = error_message + "' . TEXT_DOTPAYPL_VAL_ERROR_CHANNEL_NOT_SELECTED . '";' . "\n" .
				  '      error = 1;' . "\n" .
				  '    }' . "\n" .
				  '  }' . "\n";
		}
		return $js;
	} 

	function selection() {

		$fields = array();

		if($this->list_on) {
			$ch_arr = array();
			foreach ($this->channels as $ch=>$settings) {
				
				if($settings['enable']) {
					$ch_arr[] = array('id'=>$ch, 'text'=>$settings['name']);
				}
			}
			
			if ($this->list_enabled) {
				
				$default='';
				if ($_SESSION['dotpaypl_channel']) 
					$default = $_SESSION['dotpaypl_channel'];
				
				$ch_arr[] = array('id' => '', 'text' => MODULE_PAYMENT_DOTPAYPL_TEXT_CHANNEL_SEL);
				
				$fields[] = array(
									'title' => MODULE_PAYMENT_DOTPAYPL_TEXT_CHANNEL,
									'field' => zen_draw_pull_down_menu('channel', $ch_arr, $default)
								);
			}
			else {
				
				$default='';
				if ($_SESSION['dotpaypl_channel']) 
					$default = $_SESSION['dotpaypl_channel'];
				foreach ($this->channels as $ch=>$settings) {
					$img = $settings['img'];
					
					$title = (empty($settings['img']) ? $settings['name'] : '<img src="'.$settings['img'].'" alt="'.$settings['name'].'" title="'.$settings['name'].'" onclick="javascript:dotpaypl_radiobutton_sel(\''.$ch.'\')" />');
					$fields[] = array(
										'title' => $title,
										'field' => zen_draw_radio_field('channel', (string)$ch, ((string)$ch == $default), 'id="'.$ch.'"') 
									);
				}
			}
		}

		$image_link = '<div id="dotpayplLink"><a href="http://www.dotpay.pl" target="_blank"><img src="/images/dotpaypl/Logo_Dotpay_147x56.jpg" border="0" /></a></div>';
		$title = '<div id="dotpayplTitle">'.$this->title.'</div>';
		return array(
						'id' => $this->code,
						'module' => '<div id="dotpaypl"><div id="dotpayplRow">'.$title.$image_link.'</div></div>',
						'fields' => $fields
					);
	} 
 
	function pre_confirmation_check() {
		if ( (strlen(urlencode($_POST['channel'])) < 1) && $this->list_on ) {
			$error = sprintf(TEXT_DOTPAYPL_VAL_ERROR_CHANNEL_NOT_SELECTED);
			$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($error) . '&channel=' . urlencode($_POST['channel']);
			zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true));
		}
		else {
			$_SESSION['dotpaypl_channel'] = urlencode($_POST['channel']);
		}
	} 

	function confirmation() {
		global $languages_id, $order, $order_total_modules, $currencies;
		global $db;

	    $last_order_id = $db->Execute("select * from " . TABLE_ORDERS . " order by orders_id desc limit 1");
	    $new_order_id = $last_order_id->fields['orders_id'];
	    $new_order_id = ($new_order_id + 1);
	    $_SESSION['dotpaypl_new_order_id'] = $new_order_id;
	    $new_order_id = (string)$new_order_id . '-' . zen_create_random_value(6);
	    $_SESSION['dotpaypl_ref_id'] = $new_order_id;
		
		$fields = array();

		$fields[] = array(
							'title' => MODULE_PAYMENT_DOTPAYPL_TEXT_REF_ID,
							'field' => $new_order_id
						);

		if($this->payment_currency != $order->info['currency']) {
			
			$fields[] = array (
							'title' => sprintf(MODULE_PAYMENT_DOTPAYPL_TEXT_CURRENCY, $this->payment_currency),
							'field' => ''
						);
			$fields[] = array (
							'title' => sprintf(MODULE_PAYMENT_DOTPAYPL_TEXT_TO_PAY, $currencies->format($order->info['total'], true, $this->payment_currency), $this->payment_currency),
							'field' => ''
						);
		}
		
         return array('id' => $this->code,
				'module' => $this->title,
				'fields' => $fields
		);

	} 

	function process_button() {
		global $db, $order, $currencies, $currency;
		global $customer_id;

		
		switch($_SESSION['language']) {
			case "english"	: $mylang = "en"; break;
			case "polish"	: 
			default : $mylang = "pl";
		}

		$payment_total = $order->info['total'] * $currencies->get_value($this->payment_currency);
		
		$parameters = array();
		
		$parameters['id'] = MODULE_PAYMENT_DOTPAYPL_ID_SPRZEDAWCY;
		$parameters['amount'] = number_format($payment_total, 2, ".", "");
		$parameters['currency'] = $this->payment_currency;
		$parameters['description'] = sprintf(MODULE_PAYMENT_DOTPAYPL_TEXT_200,$_SESSION['dotpaypl_new_order_id']) .
										" | ".sprintf(MODULE_PAYMENT_DOTPAYPL_TEXT_210, $_SESSION['dotpaypl_ref_id']) . 
										" | ".$order->billing['firstname'] . ' ' . $order->billing['lastname'] .
										" | ".$order->billing['city'].'; '.$order->billing['country']['iso_code_2'];

		$parameters['lang'] = $mylang;

		
		$parameters['control'] = $_SESSION['dotpaypl_ref_id'];

		
		if($this->list_on) {
			
			$parameters['channel'] = urlencode($_POST['channel']);
			$parameters['blokuj'] = 1;
		}
		
		$parameters['firstname'] = $order->billing['firstname'];
		$parameters['surname'] = $order->billing['lastname'];
		$parameters['email'] = $order->customer['email_address'];
		$parameters['street'] = $order->billing['street_address'];
		$parameters['street_n1'] = $order->billing['suburb'];
		$parameters['state'] = $order->billing['state'];
		$parameters['city'] = $order->billing['city'];
		$parameters['postcode'] = $order->billing['postcode'];
		$parameters['phone'] = $order->customer['telephone'];
		$parameters['country'] = $order->billing['country']['iso_code_3'];
		
		$parameters['osC']='1'; 
		$parameters['session_id']=session_id();
		$parameters['return_url'] = zen_href_link(FILENAME_CHECKOUT_PROCESS,'referer=dotpaypl&control='.$_SESSION['dotpaypl_ref_id'], 'SSL');
		$parameters['pay']='yes'; 
		$parameters['vmodule']=$this->codeVersion; 
		if(MODULE_PAYMENT_DOTPAYPL_EXT_PARAM == 'Tak') {
			
			$oscname = $order->delivery['firstname']." ".$order->delivery['lastname'];
			
			$oscdesc = STORE_NAME." - ". $_SESSION['customer_id']."\n";
			if (is_array($order->products)) {
			    foreach ($order->products as $product) {
					if (is_array($product)) { 
						$oscdesc .= $product['qty']."x ".$product['name']." {".$product['model']."} = ".$currencies->format($product['final_price'], true, $order->info['currency'])."\n"; 
					}
				}
				$oscdesc .= $order->info['shipping_method'].": ".$currencies->format($order->info['shipping_cost'],true,$order->info['currency'])."\n";
				$oscdesc .= ' = '.$currencies->format($order->info['total'],true,$order->info['currency']);
			}
			
			$deladdr = '';
			if ($order->delivery['company']) {
				$deladdr = $order->delivery['company']."\n";
			} 
			$deladdr .= $order->delivery['street_address']." ".$order->delivery['suburb']."\n".
			$deladdr .= $order->delivery['city'].", ".$order->delivery['postcode']." ".$order->delivery['state']."\n".
			$deladdr .= $order->delivery['country']['title'];
			
			$parameters['oscname']= $oscname;
			
			$parameters['oscdesc']= $oscdesc;
			 
			$parameters['deladdr']=$deladdr; 
			
			$parameters['comments']=$order->info['comments']; 
		}

		
		if(defined('MODULE_PAYMENT_DOTPAYPL_DOTPAY_URLC') && MODULE_PAYMENT_DOTPAYPL_DOTPAY_URLC != 'Tak') {
			
			if(defined('MODULE_PAYMENT_DOTPAYPL_URLC') && MODULE_PAYMENT_DOTPAYPL_URLC != '') {
				
				$parameters['URLC'] = MODULE_PAYMENT_DOTPAYPL_URLC;
			}
			else {
				$parameters['URLC'] = zen_href_link(FILENAME_DOTPAYPL_HANDLER,'','SSL', true, true, true);
			}
		}

		
		$_SESSION['dotpaypl_amount'] = $parameters['amount'];
		
		dotpaypl_store_session(session_id());
		
		$process_button_string='';    
		while (list($key, $value) = each($parameters)) {
			$process_button_string .= zen_draw_hidden_field($key, $value);
		}
        
		return $process_button_string;

	} 

	
	function before_process() {
	    global $order, $order_total_modules;

		
		if(MODULE_PAYMENT_DOTPAYPL_DIAG == 'Tak') {
			$message = "---- BOF: CLASS INFO -----\n";
			$message .= "GET=".print_r($_GET,true)."\n";
			$message .= "POST=".print_r($_POST,true)."\n";
			$message .= "REQUEST=".print_r($_REQUEST,true)."\n";
			$message .= "SERVER=".print_r($_SERVER,true)."\n";
			$message .= "---- EOF: CLASS INFO -----\n";
			dotpaypl_debug_email($message);
		}

		if (isset($_GET['referer']) && $_GET['referer'] == 'dotpaypl') {
			$this->notify('NOTIFY_PAYMENT_DOTPAYPL_RETURN_TO_STORE');
		}
		
		if(($orders_id = dotpaypl_get_orders_id($_REQUEST['control'])) !== false) {
			
	        $_SESSION['cart']->reset(true);
	        unset($_SESSION['sendto']);
	        unset($_SESSION['billto']);
	        unset($_SESSION['shipping']);
	        unset($_SESSION['payment']);
	        unset($_SESSION['comments']);
	        unset($_SESSION['cot_gv']);
	        $order_total_modules->clear_posts();
			zen_session_recreate();
			
	        zen_redirect(zen_href_link(FILENAME_CHECKOUT_SUCCESS, '', 'SSL', true));
		}
		else {
            $dotpaypl_info = empty($order->info['comments']) ? '' : "\n".MODULE_PAYMENT_DOTPAYPL_TEXT_100;
            $dotpaypl_info =       MODULE_PAYMENT_DOTPAYPL_TEXT_105.zen_db_input($_REQUEST['control']);
            $dotpaypl_info .= empty($_REQUEST['status']) ? '' : "\n".MODULE_PAYMENT_DOTPAYPL_TEXT_110.zen_db_input($_REQUEST['status']);
            
			$order->info['comments'] .= $dotpaypl_info;
		}
	} 

	
	function after_order_create($insert_id) {
		$this->insert_id = $insert_id;
	} 

	function after_process() {
		global $db;

		$session_id = (isset($_POST['osCsid']) ? $_POST['osCsid'] : $_POST['session_id'] );

		dotpaypl_store_URL_transaction($this->insert_id, $session_id, $_POST['control'], $_POST['status'], $_SESSION['amount']);

		zen_session_recreate();
	} 

	function get_error() {
		if (isset($_GET['ErrMsg']) && zen_not_null($_GET['ErrMsg'])) {
			$error = stripslashes(urldecode($_GET['ErrMsg']));
		}
		elseif (isset($_GET['Err']) && zen_not_null($_GET['Err'])) {
			$error = stripslashes(urldecode($_GET['Err']));
		}
		elseif (isset($_GET['error']) && zen_not_null($_GET['error'])) {
			$error = stripslashes(urldecode($_GET['error']));
		}
		else {
			$error = MODULE_PAYMENT_DOTPAYPL_TEXT_ERROR_MESSAGE;
		}
		$errormsg = isset($this->error_message[$error])?$this->error_message[$error]:$error;
		return array(
					'title' => MODULE_PAYMENT_DOTPAYPL_TITLE_ERROR_MESSAGE,
		   			'error' => $errormsg
		   			);
	}
	
	function check() {
		global $db;
		
		if (!isset($this->_check)) {
			$check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_DOTPAYPL_STATUS'");
			$this->_check = $check_query->RecordCount();
		}
		return $this->_check;
	} 

	function install() {
		global $db, $messageStack;
		
		if(!defined('TABLE_DOTPAYPL')) {
		    $messageStack->add_session(sprintf(MODULE_PAYMENT_DOTPAYPL_TEXT_120 , TABLE_DOTPAYPL), 'error');
			return 'failed';
		}
		if(!defined('TABLE_DOTPAYPL_SESSION')) {
		    $messageStack->add_session(sprintf(MODULE_PAYMENT_DOTPAYPL_TEXT_120 , TABLE_DOTPAYPL_SESSION), 'error');
			return 'failed';
		}
		
		$db->Execute("CREATE TABLE IF NOT EXISTS ".TABLE_DOTPAYPL." ( " .
						
						"dotpaypl_id int(11) unsigned NOT NULL auto_increment, " . 
						"orders_id int(11) unsigned NOT NULL default '0', " .  
						"amount varchar(30), " . 
						"session_id text NOT NULL, " . 
						"ref_id varchar(30), " . 
						"status varchar(4), " . 
						"t_id varchar(30) NOT NULL, " . 
						"original_amount varchar(30), " . 
						"channel varchar(30), " .  
						"email varchar(128), " . 
						"service varchar(128), " . 
						"code varchar(64), " . 
						"username varchar(64), " . 
						"password varchar(64), " . 
						"t_status varchar(64), " . 
						"description varchar(255), " . 
						"p_info varchar(255), " . 
						"p_email varchar(128), " . 
						"t_date varchar(20), " . 
						"last_modified datetime NOT NULL default '0000-00-00 00:00:00', " . 
						"date_added timestamp NOT NULL default CURRENT_TIMESTAMP, " .
						"PRIMARY KEY  (dotpaypl_id), " .
						"KEY idx_orders_id_zen (orders_id), " .
						"KEY idx_session_id_zen (session_id(36))" .
					") ENGINE=MyISAM DEFAULT CHARSET=utf8"
				);
		$db->Execute("CREATE TABLE IF NOT EXISTS ".TABLE_DOTPAYPL_SESSION." ( " .
						"unique_id int(11) NOT NULL auto_increment, " . 
						"session_id text NOT NULL, " . 
						"saved_session mediumblob NOT NULL, " . 
						"expiry int(17) NOT NULL default '0', " . 
						"PRIMARY KEY  (`unique_id`), " .
						"KEY `idx_session_id_zen` (`session_id`(36))" .
					") ENGINE=MyISAM DEFAULT CHARSET=utf8"
				);

		foreach ($this->dotpaypl_status as $key=>$value) {
			$orders_status_name = 'Transakcja DotPay - ' . $value['desc'];
			$check_query = $db->Execute("select orders_status_id from " . TABLE_ORDERS_STATUS . " where orders_status_name = '" . $orders_status_name . "' limit 1");
			if ($check_query->RecordCount() < 1) {
				$status_query = $db->Execute("select max(orders_status_id) as status_id from " . TABLE_ORDERS_STATUS);
				$status = $status_query->fields;
				$status_id = $status['status_id']+1;
				if ($key == 1) {
					
					$status_new = $status_id;
				}
				if ($key == 2) {
					
					$status_processing = $status_id;
				}
				if ($key == 'KWOTA') {
					
					$status_kwota = $status_id;
				}
				$languages = zen_get_languages();
				foreach ($languages as $lang) {
					$db->Execute("insert into " . TABLE_ORDERS_STATUS . " (orders_status_id, language_id, orders_status_name) values ('" . $status_id . "', '" . $lang['id'] . "', '" . $orders_status_name . "') on duplicate key update orders_status_name=values(orders_status_name)");
				}
			}
			else {
				$check = $check_query->fields;
				$status_id = $check['orders_status_id'];
				if ($key == 1) {
					
					$status_new = $status_id;
				}
				if ($key == 2) {
					
					$status_processing = $status_id;
				}
				if ($key == 'KWOTA') {
					
					$status_kwota = $status_id;
				}
			}
			if ($key != 'KWOTA') {
				$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, date_added) values ('Status - " . $orders_status_name . "', '" . $value['conf_key'] . "', '" . $status_id . "', 'Status obsługi płatności zamówienia w systemie DotPay.pl', '6', now())");
			}
		};

		
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_010."', 'MODULE_PAYMENT_DOTPAYPL_STATUS', 'True', '".MODULE_PAYMENT_DOTPAYPL_TEXT_011."', '6', '1', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_012."', 'MODULE_PAYMENT_DOTPAYPL_ID_SPRZEDAWCY', '".MODULE_PAYMENT_DOTPAYPL_TEXT_013."', '".MODULE_PAYMENT_DOTPAYPL_TEXT_014."', '6', '2', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_015."', 'MODULE_PAYMENT_DOTPAYPL_PIN_URLC', '".MODULE_PAYMENT_DOTPAYPL_TEXT_016."', '".MODULE_PAYMENT_DOTPAYPL_TEXT_017."', '6', '3', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_018."', 'MODULE_PAYMENT_DOTPAYPL_SORT_ORDER', '0', '".MODULE_PAYMENT_DOTPAYPL_TEXT_019."', '6', '0', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_020."', 'MODULE_PAYMENT_DOTPAYPL_PREPARE_ORDER_STATUS_ID', '" . $status_new . "', '".MODULE_PAYMENT_DOTPAYPL_TEXT_021."', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_022."', 'MODULE_PAYMENT_DOTPAYPL_ORDER_STATUS_ID', '" . $status_processing . "', '".MODULE_PAYMENT_DOTPAYPL_TEXT_023."', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_024."', 'MODULE_PAYMENT_DOTPAYPL_MAIL_STATUS_ID', 'Tak', '".MODULE_PAYMENT_DOTPAYPL_TEXT_025."', '6', '0', 'zen_cfg_select_option(array(\'Tak\', \'Nie\'), ', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_026."', 'MODULE_PAYMENT_DOTPAYPL_ORDER_STATUS_KWOTA', '" . $status_kwota . "', '".MODULE_PAYMENT_DOTPAYPL_TEXT_027."', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_028."', 'MODULE_PAYMENT_DOTPAYPL_MAIL_STATUS_KWOTA', 'Nie', '".MODULE_PAYMENT_DOTPAYPL_TEXT_029."', '6', '0', 'zen_cfg_select_option(array(\'Tak\', \'Nie\'), ', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_030."', 'MODULE_PAYMENT_DOTPAYPL_ZONE', '0', '".MODULE_PAYMENT_DOTPAYPL_TEXT_031."', '6', '4', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_032."', 'MODULE_PAYMENT_DOTPAYPL_DEBUG_EMAIL', '', '".MODULE_PAYMENT_DOTPAYPL_TEXT_033."', '6', '0', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_034."', 'MODULE_PAYMENT_DOTPAYPL_ORDER_NOTIFICATION', 'Tak', '".MODULE_PAYMENT_DOTPAYPL_TEXT_035."', '6', '1', 'zen_cfg_select_option(array(\'Tak\', \'Nie\'), ', now())");
		
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_036."', 'MODULE_PAYMENT_DOTPAYPL_LIST_STATUS', 'Lista', '".MODULE_PAYMENT_DOTPAYPL_TEXT_037."', '6', '0', 'zen_cfg_select_option(array(\'Lista\', \'Radio\'), ', now())");
		 
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_038."', 'MODULE_PAYMENT_DOTPAYPL_LIST_ON', 'Nie', '".MODULE_PAYMENT_DOTPAYPL_TEXT_039."', '6', '0', 'zen_cfg_select_option(array(\'Tak\', \'Nie\'), ', now())");
		
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_040."', 'MODULE_PAYMENT_DOTPAYPL_EXT_PARAM', 'Nie', '".MODULE_PAYMENT_DOTPAYPL_TEXT_041."', '6', '0', 'zen_cfg_select_option(array(\'Tak\', \'Nie\'), ', now())");
		
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_042."', 'MODULE_PAYMENT_DOTPAYPL_DEBUG', 'Nie', '".MODULE_PAYMENT_DOTPAYPL_TEXT_043."', '6', '44', 'zen_cfg_select_option(array(\'Nie\',\'Plik\',\'Plik i Email\'), ', now())");
		$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('".MODULE_PAYMENT_DOTPAYPL_TEXT_044."', 'MODULE_PAYMENT_DOTPAYPL_DIAG', 'Nie', '".MODULE_PAYMENT_DOTPAYPL_TEXT_045."', '6', '44', 'zen_cfg_select_option(array(\'Nie\',\'Tak\'), ', now())");

		$this->notify('NOTIFY_PAYMENT_DOTPAYPL_INSTALLED');
	} 

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

		foreach ($this->dotpaypl_status as $key=>$value) 
			$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key = '" . $value['conf_key'] . "'");
		
	    $this->notify('NOTIFY_PAYMENT_DOTPAYPL_UNINSTALLED');
	} 

	function keys() {
		$keys =  array(
					'MODULE_PAYMENT_DOTPAYPL_STATUS',
					'MODULE_PAYMENT_DOTPAYPL_ID_SPRZEDAWCY',
					'MODULE_PAYMENT_DOTPAYPL_PIN_URLC',
					'MODULE_PAYMENT_DOTPAYPL_SORT_ORDER',
					'MODULE_PAYMENT_DOTPAYPL_PREPARE_ORDER_STATUS_ID',
					'MODULE_PAYMENT_DOTPAYPL_ORDER_STATUS_ID',
					'MODULE_PAYMENT_DOTPAYPL_MAIL_STATUS_ID',
					'MODULE_PAYMENT_DOTPAYPL_ORDER_STATUS_KWOTA',
					'MODULE_PAYMENT_DOTPAYPL_MAIL_STATUS_KWOTA',
					'MODULE_PAYMENT_DOTPAYPL_ZONE',
					'MODULE_PAYMENT_DOTPAYPL_DEBUG_EMAIL',
					'MODULE_PAYMENT_DOTPAYPL_ORDER_NOTIFICATION',
					'MODULE_PAYMENT_DOTPAYPL_LIST_ON',
					'MODULE_PAYMENT_DOTPAYPL_LIST_STATUS',
					'MODULE_PAYMENT_DOTPAYPL_EXT_PARAM',
					'MODULE_PAYMENT_DOTPAYPL_DEBUG',
					'MODULE_PAYMENT_DOTPAYPL_DIAG',
					);
		return $keys;
	} 

}
