<?php

	/**
	 * Modul für die Zahlungsart Sofortüberweisung
	 */
	class wpsg_mod_su extends wpsg_mod_basic
	{
		
		var $lizenz = 2;
		var $id = 5;
		var $url = false; 
		var $hilfeURL = 'http://wpshopgermany.de/?p=1335';
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Sofortüberweisung', 'wpsg');
			$this->group = __('Zahlungsarten', 'wpsg');
			$this->desc = __('Ermöglicht die Zahlungsart Sofortüberweisung.', 'wpsg');
						
		} // public function __construct()
		
		public function install()
		{

			$this->shop->checkDefault('wpsg_mod_su_bezeichnung', $this->name, false, true);
			$this->shop->checkDefault('wpsg_mod_su_hint', __('Zahlen Sie die per Sofortüberweisung. Ihre Bank muss dieses Verfahren unterstützen und sie benötigen ihre PIN/TAN zur Zahlung.', 'wpsg'), false, true);
			$this->shop->checkDefault('wpsg_mod_su_subject1', 'O%order_id% - K%kunde_id%', false, true);
			$this->shop->checkDefault('wpsg_mod_su_subject2', '', false, true);			
			
			$this->shop->checkDefault('wpsg_mod_su_gebuehr', '0');
			$this->shop->checkDefault('wpsg_mod_su_mwst', '0');
			$this->shop->checkDefault('wpsg_mod_su_mwstland', '0');
			
			$this->shop->checkDefault('wpsg_mod_su_currency', 'EUR');
			$this->shop->checkDefault('wpsg_mod_su_userid', __('<!-- Die User ID bekommen sie von Sofortüberweisung.de --!>', 'wpsg'));
			$this->shop->checkDefault('wpsg_mod_su_projectid', __('<!-- Die Projekt ID bekommen sie von Sofortüberweisung.de --!>', 'wpsg'));
			$this->shop->checkDefault('wpsg_mod_su_projectpassword', __('<!-- Das Projektpasswort bekommen sie von Sofortüberweisung.de --!>', 'wpsg'));
			$this->shop->checkDefault('wpsg_mod_su_noticepassword', __('<!-- Das Benachrichtigungspasswort bekommen sie von Sofortüberweisung.de --!>', 'wpsg'));
			$this->shop->checkDefault('wpsg_mod_su_language', 'DE');
			$this->shop->checkDefault('wpsg_mod_su_hash', 'md5');
			$this->shop->checkDefault('wpsg_mod_su_autostart', '0');
					
		} // public function install()
		
		public function init() 
		{
			
			if ($this->shop->get_option('wpsg_mod_su_sandbox') == 1)
			{
				$this->url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
			}
			else		
			{		
				$this->url = "https://www.paypal.com/cgi-bin/webscr";
			}
			
		} // public function init()
		
		public function settings_edit()
		{
			 
			$pages = get_pages();
			
			$arPages = array(
				'-1' => __('Neu anlegen und zuordnen', 'wpsg')
			);
			
			$arPagesURL = array();
			
			foreach ($pages as $k => $v)
			{
				
				$arPages[$v->ID] = $v->post_title.' (ID:'.$v->ID.')';
				$arPagesURL[$v->ID] = get_permalink($v->ID);
				
			}
			
			$basket_url = $this->shop->getURL(wpsg_ShopController::URL_BASKET);
			
			if (strpos($basket_url, '?') === false)
			{
				
				$basket_url .= '?wpsg_plugin=wpsg_mod_su&confirm=su';
				
			}
			else
			{
				
				$basket_url .= '&wpsg_plugin=wpsg_mod_su&confirm=su';
				
			}
			
			$this->shop->view['wpsg_mod_su_confirmurl'] = $basket_url;			
			$this->shop->view['pagesurl'] = $arPagesURL;
			$this->shop->view['pages'] = $arPages;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_su/settings_edit.phtml');
			
		} // public function settings_edit()
				
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_su_bezeichnung', $_REQUEST['wpsg_mod_su_bezeichnung']);
			$this->shop->update_option('wpsg_mod_su_aktiv', $_REQUEST['wpsg_mod_su_aktiv']);
			$this->shop->update_option('wpsg_mod_su_hint', $_REQUEST['wpsg_mod_su_hint']);
			
			$this->shop->update_option('wpsg_mod_su_subject1', $_REQUEST['wpsg_mod_su_subject1']);
			$this->shop->addTranslationString('wpsg_mod_su_subject1', $_REQUEST['wpsg_mod_su_subject1']);
			$this->shop->update_option('wpsg_mod_su_subject2', $_REQUEST['wpsg_mod_su_subject2']);
			$this->shop->addTranslationString('wpsg_mod_su_subject2', $_REQUEST['wpsg_mod_su_subject2']);
						
			$this->shop->update_option('wpsg_mod_su_gebuehr', wpsg_tf($_REQUEST['wpsg_mod_su_gebuehr'], true));
			$this->shop->update_option('wpsg_mod_su_mwst', $_REQUEST['wpsg_mod_su_mwst']);			 
			$this->shop->update_option('wpsg_mod_su_mwstland', $_REQUEST['wpsg_mod_su_mwstland']);
			
			$this->shop->update_option('wpsg_mod_su_currency', $_REQUEST['wpsg_mod_su_currency']);
			$this->shop->update_option('wpsg_mod_su_userid', $_REQUEST['wpsg_mod_su_userid']);
			$this->shop->update_option('wpsg_mod_su_projectid', $_REQUEST['wpsg_mod_su_projectid']);
			$this->shop->update_option('wpsg_mod_su_projectpassword', $_REQUEST['wpsg_mod_su_projectpassword']);
			$this->shop->update_option('wpsg_mod_su_noticepassword', $_REQUEST['wpsg_mod_su_noticepassword']);
			$this->shop->update_option('wpsg_mod_su_language', $_REQUEST['wpsg_mod_su_language']);
			$this->shop->update_option('wpsg_mod_su_hash', $_REQUEST['wpsg_mod_su_hash']);			
			$this->shop->update_option('wpsg_mod_su_autostart', $_REQUEST['wpsg_mod_su_autostart']);
			 			
		} // public function settings_save()
		
		public function addPayment(&$arPayment) 
		{ 

			if (!is_admin() && $this->shop->get_option('wpsg_mod_su_aktiv') != '1') return;
			
			$mwst_value = $this->getTaxValue($this->shop->get_option('wpsg_mod_su_mwst'), $this->shop->getDefaultCountry());
			
			$arPayment[$this->id] = array(
				'id' => $this->id,
				'name' => __($this->shop->get_option('wpsg_mod_su_bezeichnung'), 'wpsg'),
				'hint' => __($this->shop->get_option('wpsg_mod_su_hint')),
				'preis' => $this->shop->get_option('wpsg_mod_su_gebuehr'),
				'mwst' => $this->shop->get_option('wpsg_mod_su_mwst'),
				'mwst_value' => $mwst_value,
				'mwst_null' => $this->shop->get_option('wpsg_mod_su_mwstland'),
			    'logo' => $this->shop->getRessourceURL('gfx/su.png'),
				'logo_100x25' => $this->shop->getRessourceURL('mods/mod_su/gfx/logo_100x25.png')
			);
			
			if (isset($_REQUEST['wpsg_checkout2']))
			{

				$arBasket = $this->shop->basket->toArray();
				
				if ($this->shop->get_option('wpsg_preisangaben') == WPSG_NETTO || $arBasket['noMwSt'])
				{
						
					$payment_netto = $this->getPreis($this->shop->get_option('wpsg_mod_su_gebuehr'), $arBasket['sum']['preis']);
					$payment_brutto = wpsg_calculatePreis($payment_netto, WPSG_BRUTTO, $mwst_value);
						
				}
				else
				{
												
					$payment_brutto = $this->getPreis($this->shop->get_option('wpsg_mod_su_gebuehr'), $arBasket['sum']['preis']);																								
					$payment_netto = wpsg_calculatePreis($payment_brutto, WPSG_NETTO, $mwst_value);						
						
				}
				
				// Entsprechenden Preis anzeigen
				if ($this->shop->getFrontendTaxview() == WPSG_NETTO)
				{
					
					$arPayment[$this->id]['price'] = $payment_netto;
					
				}
				else 
				{
					
					$arPayment[$this->id]['price'] = $payment_brutto;
					
				}
				
			}
			
		} // public function addPayment(&$arPayment)
		
		public function calcPayment(&$arBasket) 
		{ 
			
			if ($arBasket['checkout']['payment'] == $this->id)
			{
							
				$payment_price = $this->getPreis($this->shop->get_option('wpsg_mod_su_gebuehr'), $arBasket['sum']['preis']);
				
				if ($this->shop->get_option('wpsg_mod_su_mwstland') == '1' && $arBasket['noMwSt'])
				{
					
					// Keine MwSt. berechnen !
					$arBasket['sum']['preis_payment'] = $payment_price;
					$arBasket['sum']['preis_payment_brutto'] = $payment_price;
					$arBasket['sum']['preis_payment_netto'] = $payment_price;
					
					$arBasket['payment']['mwst'] = 0;
					$this->shop->arPayment[$arBasket['checkout']['payment']]['mwst_value'] = 0;
						
					$arBasket['payment']['tax_rata'] = false;
					
				}
				else
				{
					
					// MwSt berechnen (Angaben im Backend sind immer Brutto)
					$this->setPaymentKosten($arBasket, $payment_price, $this->shop->get_option('wpsg_mod_su_mwst'));

				}
								
			}
			
		} // public function calcPayment(&$arBasket)
		
		public function order_done(&$order_id, &$done_view) 
		{ 

			// Bestellungen mit 0 geben nix aus
			if ($done_view['basket']['sum']['preis_gesamt_brutto'] <= 0) return;
			
			if ($this->shop->view['basket']['checkout']['payment'] == $this->id)
			{
			
				$this->shop->view['suLink'] = $this->getPayLink($order_id);
			
				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_su/order_done.phtml', false);

			}
						
		} // public function order_done(&$order_id)
		
		public function mail_payment() 
		{ 
			
			if ($this->shop->view['basket']['checkout']['payment'] != $this->id) return;
			
			if ($this->shop->htmlMail === true)
			{
				
				echo '<a href="'.$this->shop->getDoneURL($this->shop->view['o_id']).'">'.__('Zahlungslink', 'wpsg').'</a>'.__(', um die Zahlung durchzuführen', 'wpsg');
				
			}
			else
			{
			
				echo wpsg_pad_right(__('Zahlungslink', 'wpsg').':', 35).$this->shop->getDoneURL($this->shop->view['o_id']);
				
			}
			
		} // public function mail_payment()
				
		public function wp_loaded() 
		{ 
			
			if ($_REQUEST['wpsg_plugin'] == 'wpsg_mod_su' && $_REQUEST['confirm'] == 'su')			
			{

				$order = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_ORDER."` WHERE `id` = '".wpsg_q($_REQUEST['user_variable_2'])."'");				
				if ($order['id'] <= 0) die();
				
				$arHash = array(
					'transaction' => $_REQUEST['transaction'],
					'user_id' => $_REQUEST['user_id'],
					'project_id' => $_REQUEST['project_id'],
					'sender_holder' => $_REQUEST['sender_holder'],
					'sender_account_number' => $_REQUEST['sender_account_number'],
					'sender_bank_code' => $_REQUEST['sender_bank_code'],
					'sender_bank_name' => $_REQUEST['sender_bank_name'],
					'sender_bank_bic' => $_REQUEST['sender_bank_bic'],
					'sender_iban' => $_REQUEST['sender_iban'],
					'sender_country_id' => $_REQUEST['sender_country_id'],
					'recipient_holder' => $_REQUEST['recipient_holder'],
					'recipient_account_number' => $_REQUEST['recipient_account_number'],
					'recipient_bank_code' => $_REQUEST['recipient_bank_code'],
					'recipient_bank_name' => $_REQUEST['recipient_bank_name'],
					'recipient_bank_bic' => $_REQUEST['recipient_bank_bic'],
					'recipient_iban' => $_REQUEST['recipient_iban'],
					'recipient_country_id' => $_REQUEST['recipient_country_id'],
					'international_transaction' => $_REQUEST['international_transaction'],
					'amount' => number_format(wpsg_tf($order['price_gesamt']), 2, '.', ''),
					'currency_id' => $_REQUEST['currency_id'],
					'reason_1' => $_REQUEST['reason_1'],
					'reason_2' => $_REQUEST['reason_2'],
					'security_criteria' => $_REQUEST['security_criteria'],
					'user_variable_0' => $_REQUEST['user_variable_0'],
					'user_variable_1' => $_REQUEST['user_variable_1'],
					'user_variable_2' => $_REQUEST['user_variable_2'],
					'user_variable_3' => $_REQUEST['user_variable_3'],
					'user_variable_4' => $_REQUEST['user_variable_4'],
					'user_variable_5' => $_REQUEST['user_variable_5'],
					'created' => $_REQUEST['created'],
					'notification_password' => $this->shop->get_option('wpsg_mod_su_noticepassword')
				);
 
				switch ($this->shop->get_option('wpsg_mod_su_hash'))
				{
					case 'md5':
						$strHash = md5(implode("|", $arHash));
						break;
					case 'sha1':
						$strHash = sha1(implode("|", $arHash));
						break;
					case 'sha256':
						$strHash = hash("sha256", implode("|", $arHash));
						break;
					case 'sha512':
						$strHash = hash("sha512", implode("|", $arHash));
						break;
				}
				
				if ($strHash == $_REQUEST['hash'])
				{

					$this->db->ImportQuery(WPSG_TBL_OL, array(
						"title" => __("Sofortüberweisung VERIFIED", 'wpsg'),
						"cdate" => "NOW()",
						"o_id" => wpsg_q($order['id']),
						"mailtext" => print_r($_REQUEST, 1)
					));
					
					$this->shop->setOrderStatus($order['id'], 100, true);

				}
				else
				{
					
					$this->db->ImportQuery(WPSG_TBL_OL, array(
						"title" =>  __("Sofortüberweisung FAILED", 'wpsg'),
						"cdate" => "NOW()",
						"o_id" => wpsg_q($order['id']),
						"mailtext" => print_r($_REQUEST, 1)
					));
					
				}
				
				die();
				
			}
		
		} // public function wp_loaded()
			
		/**
		 * Gibt den Link für die Bezahlung anhand der BestellID zurück
		 */
		public function getPayLink($order_id)
		{

			$basket_link = get_permalink($this->shop->get_option('wpsg_page_basket'));
			
			if (strpos($basket_link, "?") > 0)
			{
				$basket_link .= "&wpsg_plugin=wpsg_mod_paypal&confirm=pp";
			}
			else
			{
				$basket_link .= "?wpsg_plugin=wpsg_mod_paypal&confirm=pp";
			}
 
			$order = $this->db->fetchRow("
				SELECT
					O.`id` AS o_id, O.`onr`, O.`price_gesamt`,
					K.`id` AS k_id, K.`knr`
				FROM
					`".WPSG_TBL_ORDER."` AS O
						LEFT JOIN `".WPSG_TBL_KU."` AS K ON (O.`k_id` = K.`id`)
				WHERE
					O.`id` = '".wpsg_q($order_id)."'						
			");
			
			$subject1 = $this->shop->replaceUniversalPlatzhalter(__($this->shop->get_option('wpsg_mod_su_subject1'), 'wpsg'), $order_id);
			$subject2 = $this->shop->replaceUniversalPlatzhalter(__($this->shop->get_option('wpsg_mod_su_subject2'), 'wpsg'), $order_id);
			
			$arHash = array(
				$this->shop->get_option('wpsg_mod_su_userid'), // user_id
				$this->shop->get_option('wpsg_mod_su_projectid'), // project_id
				"", // sender_holder
				"", // sender_account_number
				"", // sender_bank_code
				"", // sender_country_id
				number_format(wpsg_tf($order['price_gesamt']), 2, '.', ''), // amount
				$this->shop->get_option("wpsg_mod_su_currency"), // currency_id
				$subject1, // reason_1
				$subject2, // reason_2
				"Kundennummer: ".$order['k_id'], // user_variable_0
				"Bestellnummer: ".((trim($order['onr']) != '')?$order['onr']:$order['o_id']), // user_variable_1
				$order['o_id'], // user_variable_2
				"", // user_variable_3
				"", // user_variable_4
				"", // user_variable_5
				$this->shop->get_option('wpsg_mod_su_projectpassword'), // project_password
			);
			
			switch ($this->shop->get_option('wpsg_mod_su_hash'))
			{
				case 'md5':
					$strHash = md5(implode("|", $arHash));
					break;
				case 'sha1':
					$strHash = sha1(implode("|", $arHash));
					break;
				case 'sha256':
					$strHash = hash("sha256", implode("|", $arHash));
					break;
				case 'sha512':
					$strHash = hash("sha512", implode("|", $arHash));
					break;
			}
			
			return "https://www.sofortueberweisung.de/payment/start?".
				"user_id=".urlencode($this->shop->get_option('wpsg_mod_su_userid'))."&".
				"project_id=".urlencode($this->shop->get_option('wpsg_mod_su_projectid'))."&".
				"amount=".urlencode(number_format(wpsg_tf($order['price_gesamt']), 2, '.', ''))."&".
				"reason_1=".urlencode($subject1)."&".
				"reason_2=".urlencode($subject2)."&".
				"hash=".urlencode($strHash)."&".
				"currency_id=".urlencode($this->shop->get_option('wpsg_mod_su_currency'))."&".
				"language_id=".urlencode($this->shop->get_option('wpsg_mod_su_language'))."&".
				"user_variable_0=".urlencode("Kundennummer: ".$order['k_id'])."&".
				"user_variable_1=".urlencode("Bestellnummer: ".((trim($order['onr']) != '')?$order['onr']:$order['o_id']))."&".
				"user_variable_2=".$order['o_id'];
			
		} // public function getPayLink($order_id)
		
	} // class wpsg_mod_su extends wpsg_mod_basic

?>