<?php
/**
 * Mediaflex Sp. z O.O.
 * @author krystian.kuczek@mediaflex.pl
 * @package Moneybookers Payment Plugin
 */

include_once(DIR_FS_CATALOG . DIR_WS_LANGUAGES . $_SESSION['language'] . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'payment' . DIRECTORY_SEPARATOR . 'mb.php');
include_once(DIR_FS_CATALOG . DIR_WS_MODULES . 'payment' . DIRECTORY_SEPARATOR . 'moneybookers' . DIRECTORY_SEPARATOR . 'moneybookers_main.php');

/**
 * Description of moneybookers_payment
 *
 * @author krun
 */
class moneybookers_payment extends moneybookers_main {
	var $_returnUrl;
	var $_imgLink;
	var $_paymentMethod;
	var $_zoneId;
	var $_notifyUrl;

	function moneybookers_payment() {
		global $messageStack, $order;

		$this->_notifyUrl = tep_href_link('mb_handler.php', '', 'SSL',false,false,true);
		$this->_cancelUrl = tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL');
		$this->_moneybookersUrl = tep_href_link('payment_moneybookers.php', '', 'SSL');

		try {
			if ((int)$this->_get('ORDER_STATUS_ID') > 0) {
				$this->order_status = $this->_get('ORDER_STATUS_ID');
			}

			$this->sort_order = $this->_get('SORT_ORDER');

			if (!$this->checkMainEnabled()) {
				$this->title .= '<b style="color: red"> (Main module is not configured)</b>';
				$this->enabled = false;
			} else {
				$this->enabled = $this->_get('STATUS');
			}
		} catch (Exception $e) {
			$this->enabled = false;
		}

		if (is_object($order)) {
			$this->update_status();
		}

		$this->form_action_url = $this->_moneybookersUrl;

//		if (IS_ADMIN_FLAG === true) $this->tableCheckup();

		$this->moneybookers_main();
	}

	function _get($index) {
		if (defined($this->_prefix . $index)) {
			return constant($this->_prefix . $index);
		} else {
			return null;
		}
	}

	function _defined($index) {
		return defined($this->_prefix . $index);
	}

	function _mainGet($index) {
		if (defined($this->_mainPrefix . $index)) {
			return constant($this->_mainPrefix . $index);
		} else {
			throw new Exception("Bad constant '$this->_mainPrefix$index'");
		}
	}

	function _mainDefined($index) {
		return defined($this->_mainPrefix . $index);
	}

	/**
	 * Funkcja sprawdzająca czy moduł jest zainstalowany
	 *
	 * @return bool
	 */
	function check() {
		global $db;

		if (!isset($this->_check)) {
			$check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = '" . $this->_prefix . "STATUS' OR configuration_key = '" . $this->_mainPrefix . "STATUS'");
			$this->_check = (tep_db_num_rows($check_query) == 2);
		}

		return $this->_check;
	}

	function checkMainEnabled() {
		return ($this->_mainDefined('STATUS') && ($this->_mainDefined('EMAIL') && $this->_mainGet('EMAIL')) ||
				($this->_mainDefined('ID') && $this->_mainGet('ID')) && $this->_mainDefined('SECRET_WORD'));
	}

	function keys() {
		 $keys_list = array(
			$this->_prefix . 'STATUS',
			$this->_prefix . 'SORT_ORDER',
			$this->_prefix . 'CURRENCY',
			$this->_prefix . 'ORDER_STATUS_ID',
			$this->_prefix . 'PROCESSING_ORDER_STATUS_ID',
			$this->_prefix . 'ZONE',
			$this->_prefix . 'TITLE'
			);

		 return $keys_list;
	}

	/**
	 * Instalacja modułu moneybookers.
	 *
	 */
	function install() {
		global $db, $messageStack;

		$langFile = DIR_FS_CATALOG_LANGUAGES . $_SESSION['language'] . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'payment' . DIRECTORY_SEPARATOR . 'moneybookers.php';

		if (@file_exists($langFile)) {
			include_once($langFile);
		}

		if (!$this->checkMainEnabled()) {
			$messageStack->add_session(sprintf(MODULE_PAYMENT_MONEYBOOKERS_TEXT_NO_MAIN_MODULE, tep_href_link('modules.php?set=payment&amp;module=moneybookers&amp;action=edit', '', 'NONSSL')));
			return 'failed';
		}

		tep_db_query("DELETE from " . TABLE_CONFIGURATION . " where configuration_key LIKE '" . $this->_prefix . "%'");

		$title = explode("(powered by", $this->title);
		$title = trim($title[0]);

		$s = 0;
//		$this->_toConf($key, $value, $title, $description, $sortOrder, $setFunction, $useFunction)
		$this->_toConf('STATUS',  'True', MODULE_PAYMENT_MONEYBOOKERS_TEXT_ENABLED, MODULE_PAYMENT_MONEYBOOKERS_TEXT_ENABLED_DESC, $s++, 'tep_cfg_select_option(array(\'True\', \'False\'), ');
		$this->_toConf('SORT_ORDER', 0, MODULE_PAYMENT_MONEYBOOKERS_TEXT_ORDEROFDISPLAY, MODULE_PAYMENT_MONEYBOOKERS_TEXT_ORDEROFDISPLAY_DESC, $s++);
		$this->_toConf('CURRENCY', 'USER', MODULE_PAYMENT_MONEYBOOKERS_TEXT_CURRENCY, MODULE_PAYMENT_MONEYBOOKERS_TEXT_CURRENCY_DESC, $s++, 'mb_cfg_currency(');
		$this->_toConf('PROCESSING_ORDER_STATUS_ID', $this->_mainGet('PROCESSING_ORDER_STATUS_ID'), MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_ORDER, MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_ORDER_DESC, $s++, "tep_cfg_pull_down_order_statuses(", "tep_get_order_status_name");
		$this->_toConf('ZONE', $this->_zoneId, MODULE_PAYMENT_MONEYBOOKERS_TEXT_PAYMENTZONE, MODULE_PAYMENT_MONEYBOOKERS_TEXT_PAYMENTZONE_DESC, $s++, 'tep_cfg_pull_down_zone_classes(', 'tep_get_zone_class_title');
		$this->_toConf('ORDER_STATUS_ID', $this->_mainGet('ORDER_STATUS_ID'), MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_PENDING, MODULE_PAYMENT_MONEYBOOKERS_TEXT_STATUS_PENDING_DESC, $s++, "tep_cfg_pull_down_order_statuses(", "tep_get_order_status_name");
		$this->_toConf('TITLE', $title, MODULE_PAYMENT_MONEYBOOKERS_TEXT_TITLE, MODULE_PAYMENT_MONEYBOOKERS_TEXT_TITLE_DESC, $s++);
	}

	/**
	 * Usunięcie głównego modułu moneybookers.
	 *
	 * TODO: Trzeba sprawdzić czy moduły zależne nie są zainstalowane, jeśli są to nie pozowlić usunąć.
	 */
	function remove() {
		global $db;
		tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key LIKE '" . $this->_prefix . "%'");
	}

	function _toConf($key, $value, $title, $description, $sortOrder = null, $setFunction = null, $useFunction = null) {
		global $db;

		$key = $this->_prefix . $key;

		$sql = "INSERT INTO " . TABLE_CONFIGURATION . "(configuration_title, configuration_key, configuration_value, configuration_group_id, date_added, configuration_description";
		$val = "'" . mysql_real_escape_string($title) . "', '" . mysql_real_escape_string($key) . "', '" . mysql_real_escape_string($value) . "', 6, now(), '" .
					mysql_real_escape_string($description) . "'";

		if ($sortOrder !== null) {
			$sql .= ", sort_order";
			$val .= ", $sortOrder";
		}

		if ($setFunction !== null) {
			$sql .= ", set_function";
			$val .= ", '" . mysql_real_escape_string($setFunction) . "'";
		}

		if ($useFunction !== null) {
			$sql .= ", use_function";
			$val .= ", '" . mysql_real_escape_string($useFunction) . "'";
		}

		$sql .= ") VALUES(" . $val . ")";

		tep_db_query($sql);
	}

	function  __get($name) {
		if ($name == 'title') {
			$dbg = debug_backtrace();

//			var_dump($dbg);

			if (sizeof($dbg) == 1) {
				return '&nbsp;&nbsp;&nbsp;&nbsp; -> ' . $this->var['title'];
			} else {
				return $this->var['title'];
			}
		} else {
			return isset($this->var[$name]) ? $this->var[$name] : null;
		}
	}

	function __set($name, $value) {
		$this->var[$name] = $value;
	}

	function __isset($name) {
		return isset($this->var[$name]);
	}



	/**
	 * Button processu
	 */
	function process_button() {
		global $db, $order, $currencies, $currency;

		if ($this->_get('CURRENCY') == 'USER') {
			$curr = ($_SESSION['currency'] ? $_SESSION['currency'] : 'EUR');
		} else {
			$curr = $this->_get('CURRENCY');
		}

		$curl = new moneybookers_curl();

		$this->totalsum = $order->info['total'];

		$this->transaction_amount = ($this->totalsum * ($currencies->get_value($curr) ? $currencies->get_value($curr) : 1));

		$sql = "CREATE TABLE IF NOT EXISTS `moneybookers` (
				`id_moneybookers` int(10) unsigned NOT NULL auto_increment,
				`id_order` int(11) NOT NULL,
				`cart_id` varchar(10) NOT NULL,
				`status` int(10) NOT NULL DEFAULT 0,
				`plugin` varchar(255) NOT NULL,
				PRIMARY KEY  (`id_moneybookers`),
				UNIQUE KEY `SecurityToken` (`cart_id`,`id_order`)
			) ENGINE=InnoDB  DEFAULT CHARSET=latin2 AUTO_INCREMENT=1 ;";

		tep_db_query($sql);

		
		if ($_SESSION['cartID']) {
			$ident = $_SESSION['cartID'];
		} else {
			$ident = $_SESSION['customer_id'];
		}

		$sql = "INSERT INTO moneybookers (cart_id, plugin) VALUES('" . tep_db_prepare_input($ident) . "', '" . tep_db_prepare_input($this->code) . "') ON DUPLICATE KEY UPDATE id_moneybookers=(LAST_INSERT_ID(id_moneybookers) + 1); ";
		tep_db_query($sql);

		$idMb = tep_db_insert_id();

		$table = array(
			//Merchant
			'pay_to_email' => $this->_mainGet('EMAIL'),
			'recipient_description' => STORE_NAME,
			'transaction_id' => $idMb,
			'language' => 'IT', //FIXME

			//Przekierowania
			'return_url' => $this->_returnUrl,
			'return_url_text' => MODULE_PAYMENT_MONEYBOOKERS_RETURN_URL_TEXT,
			'return_url_target' => '_parent',
			'cancel_url' => $this->_cancelUrl,
			'cancel_url_target' => '_parent',

			//Customer
			'pay_for_email' => $order->customer['email_address'],
			'firstname' => html_entity_decode(($order->customer['firstname'])),
			'lastname' => html_entity_decode(($order->customer['lastname'])),
			'address' => html_entity_decode(($order->customer['street_address'])),
			'address2' => html_entity_decode(($order->customer['street_address'])),
			'city' => html_entity_decode(($order->customer['city'])),
			'state' => html_entity_decode(($order->customer['state'])),
			'postal_code' => html_entity_decode((str_replace('-', '', $order->customer['postcode']))),
			'country' => $order->customer['country']['iso_code_3'],
			'pay_from_email' => $order->customer['email_address'],

			//Price
			'amount' => number_format($this->transaction_amount, (int)$currencies->get_decimal_places($curr)),
			'currency' => $curr,

			'status_url' => $this->_notifyUrl,
			'status_url2' => 'mailto: ' . $this->_mainGet('INFO_MAIL'),
			'new_window_redirect' => 1,

			"merchant_fields" => "referring_platform",
			"referring_platform" => "OScommerce",
		);

		//Products
		$x = 2;
		foreach ($order->products as $product) {
			$table["amount{$x}_description"] = $product['name'];
			$table["amount$x"] = number_format($product['price'], $currencies->get_decimal_places($curr)) . " $curr";
			$x++;
		}

		$table["amount{$x}_description"] = MODULE_PAYMENT_MONEYBOOKERS_TEXT_SUBTOTAL;
		$table["amount{$x}"] = $order->info['subtotal'] . " $curr";
		$x++;

		//Tax
		foreach ($order->info['tax_groups'] as $key => $v) {
			if ($v != 0) {
				$table["amount{$x}_description"] = $key;
				$table["amount$x"] = number_format($v, $currencies->get_decimal_places($curr)) . " $curr";
				$x++;
			}
		}

		if (is_array($this->_paymentMethod)) {
			$table['payment_methods'] = implode(",", $this->_paymentMethod) . ',';
		} else if (is_string($this->_paymentMethod)) {

			if ($this->_paymentMethod == 'WLT') {
				$table['payment_methods'] = '';
			} else {
				$table['hide_login'] = 1;
				$paymentMethod = trim($this->_paymentMethod);

				if (substr($paymentMethod, strlen($paymentMethod)-1, 1) != ',') {
					$paymentMethod .= ',';
				}

				$table['payment_methods'] = $paymentMethod;
			}
		}

		foreach ($table as $key => $val) {
			$data[$key] = html_entity_decode(iconv(trim(CHARSET), 'utf-8', $val), ENT_QUOTES, 'utf-8');
		}

		$sid = $curl->SessionStart($data);

		$table = array('sid' => $sid);

		$ret = '';
		foreach ($table as $key => $val) {
			$ret .= tep_draw_hidden_field($key, $val);
		}

		return $ret;
	}

	function selection() {
		return array('id' => $this->code,
			'module' => $this->_get('TITLE') . '
				<div style="text-align: left; margin: auto;"><img src="' . $this->_imgLink . '" alt="' . $this->_get('TITLE') . '" /></div>
				',
			);
	}

	function javascript_validation() {
		return false;
	}

	/**
	 * Przed potwierdzeniem
	 */
	function pre_confirmation_check() {
	}

	/**
	 * Po potwierdzeniu
	 */
	function confirmation() {
	}

	function before_process() {
		return false;
	}

	function after_process() {
		global $insert_id;


		if ($_SESSION['cartID']) {
			$ident = $_SESSION['cartID'];
		} else {
			$ident = $_SESSION['customer_id'];
		}

		$sql = "SELECT status FROM moneybookers WHERE cart_id='" . tep_db_prepare_input($ident) . "' LIMIT 1";
		$r = tep_db_query($sql);
		$e = tep_db_fetch_array($r);

		if($e['status'] == 2) {
			$sql = "UPDATE orders SET orders_status='" . $this->_get('PROCESSING_ORDER_STATUS_ID') . "' WHERE orders_id='$insert_id';";
			tep_db_query($sql);
		}
		$sql = "UPDATE moneybookers SET id_order=$insert_id WHERE cart_id='" . tep_db_prepare_input($ident) . "' AND id_order=0";

		tep_db_query($sql);
	}

	/**
	 * 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)$this->_get('ZONE') > 0) ) {
			$check_flag = false;
			$check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" .$this->_get('ZONE') . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");

			while ($check = tep_db_fetch_array($check_query)) {
				if ($check['zone_id'] < 1) {
					$check_flag = true;
					break;
				} elseif ($check['zone_id'] == $order->delivery['zone_id']) {
					$check_flag = true;
					break;
				}
			}

			if ($check_flag == false) {
				$this->enabled = false;
			}
		}
	}
}
?>
