<?php

	/**
	 * Modul für die Zahlungsart "Skrill"
	 * @author daniel
	 */
	class wpsg_mod_skrill extends wpsg_mod_basic 
	{

		var $lizenz = 2;
		var $id = 7;
		var $hilfeURL = 'http://wpshopgermany.de/?p=2498';
		var $version = "9.9.9";
		var $free = true;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Skrill', 'wpsg');
			$this->group = __('Zahlungsarten', 'wpsg');
			$this->desc = __('Ermöglicht die Zahlungsart "Skrill".', 'wpsg');
									
		} // public function __construct()
	
		public function install()
		{

			$this->shop->checkDefault('wpsg_mod_skrill_bezeichnung', $this->name, false, true);
			$this->shop->checkDefault('wpsg_mod_skrill_hint', __('Zahlen Sie mit Skrill (Moneybookers).', 'wpsg'), false, true);
			
			$this->shop->checkDefault('wpsg_mod_skrill_gebuehr', '0');
			$this->shop->checkDefault('wpsg_mod_skrill_mwst', '0');
			$this->shop->checkDefault('wpsg_mod_skrill_mwstland', '0');

			$this->shop->checkDefault('wpsg_mod_skrill_currency', 'EUR');
			$this->shop->checkDefault('wpsg_mod_skrill_recipient_description', '<!-- Die Beschreibung erscheint während der Zahlung --!>');
			$this->shop->checkDefault('wpsg_mod_skrill_code', '<!-- Das Geheimwort erhalten sie von Skrill --!>');
			$this->shop->checkDefault('wpsg_mod_skrill_email', '<!-- Ihre Skrill Konto E-Mail Adresse --!>');
			$this->shop->checkDefault('wpsg_mod_skrill_id', '<!-- Die Händler ID erhalten sie von Skrill --!>');
			$this->shop->checkDefault('wpsg_mod_skrill_logo_url', '');
			$this->shop->checkDefault('wpsg_mod_skrill_autostart', '0'); 
			
		} // public function install()
		
		public function init() 
		{
			
			$this->url = "https://www.moneybookers.com/app/payment.pl";
						
		} // public function init()
		
		public function settings_edit()
		{
			 
			$pages = get_pages();
			
			$arPages = array(
				'-1' => __('Neu anlegen und zuordnen', 'wpsg')
			);
			
			foreach ($pages as $k => $v)
			{
				$arPages[$v->ID] = $v->post_title.' (ID:'.$v->ID.')';
			}
			
			$this->shop->view['pages'] = $arPages;
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_skrill/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_skrill_bezeichnung', $_REQUEST['wpsg_mod_skrill_bezeichnung']);
			$this->shop->update_option('wpsg_mod_skrill_aktiv', $_REQUEST['wpsg_mod_skrill_aktiv']);
			$this->shop->update_option('wpsg_mod_skrill_hint', $_REQUEST['wpsg_mod_skrill_hint']);
						
			$this->shop->update_option('wpsg_mod_skrill_gebuehr', wpsg_tf($_REQUEST['wpsg_mod_skrill_gebuehr'], true));
			$this->shop->update_option('wpsg_mod_skrill_mwst', $_REQUEST['wpsg_mod_skrill_mwst']);
			$this->shop->update_option('wpsg_mod_skrill_mwstland', $_REQUEST['wpsg_mod_skrill_mwstland']);
			
			$this->shop->update_option('wpsg_mod_skrill_currency', $_REQUEST['wpsg_mod_skrill_currency']);
			$this->shop->update_option('wpsg_mod_skrill_recipient_description', $_REQUEST['wpsg_mod_skrill_recipient_description']);
			$this->shop->update_option('wpsg_mod_skrill_code', $_REQUEST['wpsg_mod_skrill_code']);			
			$this->shop->update_option('wpsg_mod_skrill_email', $_REQUEST['wpsg_mod_skrill_email']);
			$this->shop->update_option('wpsg_mod_skrill_id', $_REQUEST['wpsg_mod_skrill_id']);
			$this->shop->update_option('wpsg_mod_skrill_logo_url', $_REQUEST['wpsg_mod_skrill_logo_url']);
			$this->shop->update_option('wpsg_mod_skrill_autostart', $_REQUEST['wpsg_mod_skrill_autostart']); 
			
			$this->shop->createPage(__('Erfolgreiche Skrill Zahlung', 'wpsg'), 'wpsg_page_mod_skrill_success', $_REQUEST['wpsg_page_mod_skrill_success']);
			$this->shop->createPage(__('Fehlgeschlagene Skrill Zahlung', 'wpsg'), 'wpsg_page_mod_skrill_error', $_REQUEST['wpsg_page_mod_skrill_error']);
			
			$this->shop->addTranslationString('wpsg_mod_skrill_bezeichnung', $_REQUEST['wpsg_mod_skrill_bezeichnung']);
			$this->shop->addTranslationString('wpsg_mod_skrill_hint', $_REQUEST['wpsg_mod_skrill_hint']);
			
		} // public function settings_save()
		
		public function addPayment(&$arPayment) 
		{ 
						
			if (!is_admin() && $this->shop->get_option('wpsg_mod_skrill_aktiv') != '1') return;
			
			$mwst_value = $this->getTaxValue($this->shop->get_option('wpsg_mod_skrill_mwst'), $this->shop->getDefaultCountry());
			
			$arPayment[$this->id] = array(
				'id' => $this->id,
				'name' => __($this->shop->get_option('wpsg_mod_skrill_bezeichnung'), 'wpsg'),
				'hint' => __($this->shop->get_option('wpsg_mod_skrill_hint'), 'wpsg'),
				'preis' => $this->shop->get_option('wpsg_mod_skrill_gebuehr'),
				'mwst' => $this->shop->get_option('wpsg_mod_skrill_mwst'),
				'mwst_value' => $mwst_value,
				'mwst_null' => $this->shop->get_option('wpsg_mod_skrill_mwstland'),
				'logo' => WPSG_URL_CONTENT.'plugins/wpshopgermany/views/gfx/skrill.png',
				'logo_100x25' => $this->shop->getRessourceURL('mods/mod_skrill/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_skrill_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_skrill_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_skrill_gebuehr'), $arBasket['sum']['preis']);
				
				if ($this->shop->get_option('wpsg_mod_skrill_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_skrill_mwst'));

				}
								
			}
			
		} // public function calcPayment(&$arBasket)
		
		public function wp_loaded() { 
			
			if ($_REQUEST['wpsg_plugin'] == 'wpsg_mod_skrill' && $_REQUEST['confirm'] == 'skrill')			
			{
				
				// Bestellung heraussuchen
				$order_id = substr($_REQUEST['transaction_id'], 1);				
				$order = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_ORDER."` WHERE `id` = '".wpsg_q($order_id)."'");
				
				// Die Fehlermeldung geht ins leere
				if (!isset($order['id']) || $order['id'] <= 0 || $order['id'] != $order_id) die(__('Bestellung wurde nicht gefunden!', 'wpsg'));
				
				if ($_REQUEST['merchant_id'] != $this->shop->get_option('wpsg_mod_skrill_id'))
				{
					
					$this->db->ImportQuery(WPSG_TBL_OL, array(
						'o_id' => wpsg_q($order_id),
						'cdate' => 'NOW()',
						'title' => __('Skrill Modul: Händler ID passt nicht!', 'wpsg'),
						'mailtext' => print_r($_REQUEST, 1)
					));
					
					die(__('Händler ID passt nicht!', 'wpsg'));
					
				}

				if ($this->shop->setPayMent($order_id, $_REQUEST['mb_amount']))
				{
				
					// Hash berechnen
					$calc_hash = strtoupper(md5(
						$this->shop->get_option('wpsg_mod_skrill_id').
						$_REQUEST['transaction_id'].
						strtoupper(md5($this->shop->get_option('wpsg_mod_skrill_code'))).
						$_REQUEST['mb_amount'].
						$_REQUEST['mb_currency'].
						$_REQUEST['status']));
											
					if ($calc_hash != $_REQUEST['md5sig'])
					{
						
						$this->db->ImportQuery(WPSG_TBL_OL, array(
							'o_id' => wpsg_q($order_id),
							'cdate' => 'NOW()',
							'title' => __('Skrill Modul: Ungültiger Hash!', 'wpsg'),
							'mailtext' => print_r($_REQUEST, 1)."\r\n\r\nBerechneter Hash: ".$calc_hash
						));
						
						die(__('Hash stimmt nicht überein!', 'wpsg'));
						
					}
					else
					{
						
						// Hash ist gleich
						$this->db->ImportQuery(WPSG_TBL_OL, array(
							'o_id' => wpsg_q($order_id),
							'cdate' => 'NOW()',
							'title' => __('Skrill Modul: Erfolgreiche Zahlungsbestätigung', 'wpsg'),
							'mailtext' => print_r($_REQUEST, 1)
						));
													
						$this->shop->setOrderStatus($order_id, 100, 1);
						
						die("1");
						
					}
					
				}
				
			}
			
		} // public function wp_loaded()
		
		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)
			{
			
				$status_url = get_permalink($this->shop->get_option('wpsg_page_basket'));
				
				if (strpos($basket_link, "?") > 0)
				{
					$status_url .= "&wpsg_plugin=wpsg_mod_skrill&confirm=skrill";
				}
				else
				{
					$status_url .= "?wpsg_plugin=wpsg_mod_skrill&confirm=skrill";
				}
				
				//$this->shop->view['paypalLink'] = $this->getPayPalLink($order_id);
			
				$this->shop->view['order'] = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_ORDER."` WHERE `id` = '".wpsg_q($order_id)."'");
				$this->shop->view['kunde'] = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_KU."` WHERE `id` = '".wpsg_q($this->shop->view['order']['k_id'])."'");
				$this->shop->view['kunde']['land'] = $this->db->fetchRow("SELECT * FROM `".WPSG_TBL_LAND."` WHERE `id` = '".wpsg_q($this->shop->view['kunde']['land'])."'");
				
				$this->shop->view['wpsg_mod_skrill']['url'] = 'https://www.moneybookers.com/app/payment.pl';
				$this->shop->view['wpsg_mod_skrill']['transaction_id'] = 'O'.$order_id;
				$this->shop->view['wpsg_mod_skrill']['return_url'] = get_permalink($this->shop->get_option('wpsg_page_mod_skrill_success'));
				$this->shop->view['wpsg_mod_skrill']['cancel_url'] = get_permalink($this->shop->get_option('wpsg_page_mod_skrill_error'));
				$this->shop->view['wpsg_mod_skrill']['status_url'] = $status_url;
				
				echo $this->shop->render(WPSG_PATH_VIEW.'/mods/mod_skrill/order_done.phtml', false);

			}
						
		} // public function order_done(&$order_id)
		
	} // class wpsg_mod_skrill extends wpsg_mod_basic

?>