Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /* * * @author David Mrzek * @contact <[email protected]> * @since 2015..

Decoded Output download

<?php 
 
/* 
 *  
 * @author David Mrzek 
 * @contact <[email protected]> 
 * @since 2015 
 * @copyright Copyright (c) 2015 - 2023 David Mrzek 
 * 
 * @link https://prestashop.valasinec.cz/content/2-obchodni-podminky 
 * @license https://prestashop.valasinec.cz/content/3-licencni-podminky 
 * 
 * Tento kd je chrnn autorskm prvem a nesm bt en bez pedchozho psemnho souhlasu autora. 
 * V rmci licennch podmnek mete tento kd pouvat a upravovat pouze pro vlastn poteby. 
 * Pro jakkoli dotazy nebo dal informace ns kontaktujte. 
 * 
 */ 
  
/* 
use Valasinec\Module\Dm_Comgate\Install\DmInstaller; 
use Valasinec\Module\Dm_Comgate\Configuration\DmConfiguration; 
use Valasinec\Module\Dm_Comgate\Update\DmUpdate; 
use Valasinec\Module\Dm_Comgate\Tools\DmTools; 
use Valasinec\Module\Dm_Comgate\Tools\DmToolsTime; 
use Valasinec\Module\Dm_Comgate\Tools\DmToolsXml; 
use Valasinec\Module\Dm_Comgate\Helper\DmBuilder; 
use Valasinec\Module\Dm_Comgate\Log\DmLogger; 
*/ 
 
if (!defined('_PS_VERSION_')) 
	exit; 
 
use PrestaShop\PrestaShop\Core\Payment\PaymentOption; 
 
require_once(dirname(__FILE__).'/classes/DmComgateFormBuilder.php'); 
require_once(dirname(__FILE__).'/classes/DmComgateInstall.php'); 
require_once(dirname(__FILE__).'/classes/DmComgateUpdate.php'); 
require_once(dirname(__FILE__).'/classes/DmComgateApi.php'); 
 
// Autoload (po vygenerovn composerem) 
if (file_exists(__DIR__ . '/vendor/autoload.php')) { 
    require_once __DIR__ . '/vendor/autoload.php'; 
} 
 
class dm_comgate extends PaymentModule 
{ 
    const INSTALL_SQL_FILE = 'install.sql'; 
    const UNINSTALL_SQL_FILE = 'uninstall.sql'; 
 
    public $default_lang; 
    public $module_hooks = array( 
        'paymentOptions', 
        'displayPaymentReturn', 
        'displayOrderDetail', 
        'displayBackOfficeHeader', 
        'displayLeftColumn', 
        'displayRightColumn', 
        'displayReassurance', 
        'displayAdminOrder', 
        'displayAdminOrderMain', 
        'displayDmPaymentButtonHistory', 
        'actionGetExtraMailTemplateVars', 
    ); 
    public $update; 
    public $install; 
    private $_html = ''; 
    private $_postErrors = array(); 
 
    public $author_email; 
    public $author_website; 
    public $path; 
 
    function __construct() 
    { 
        // Zkladn nastaven 
        $this->name = 'dm_comgate'; 
        $this->tab = 'payments_gateways'; 
        $this->version = '3.2.0'; 
        $this->author = 'David Mrzek'; 
        $this->author_email = '[email protected]'; 
        $this->author_website = 'https://prestashop.valasinec.cz'; 
        $this->need_instance = 1; 
        $this->controllers = array('validation', 'cgpayment'); 
 
        $this->currencies = true; 
        $this->currencies_mode = 'checkbox'; 
         
        $this->path = $this->_path; 
         
        $this->bootstrap = true; 
        parent::__construct(); 
         
        $this->displayName = $this->l('COMGATE - platebn brna'); 
        $this->description = $this->l('Napojen na platebn brnu COMGATE.'); 
        $this->confirmUninstall = $this->l('Opravdu chcete odinstalovat tento modul?'); 
        $this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);         
         
        $this->default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); 
 
        // Zjitn aktualizace 
        $this->update = new DmComgateUpdate($this->name, $this->version); 
 
        if($this->update->version_data) 
        { 
            $this->warning = $this->l('Je k dispozici nov verze modulu').' ('.$this->update->version_data['version'].').'; 
        } 
    } 
         
    // Metoda pro instalaci modulu 
    public function install() 
    {         
        if (Shop::isFeatureActive()) 
        { 
            Shop::setContext(Shop::CONTEXT_ALL); 
        } 
 
        // Install tda 
        $this->install = new DmComgateInstall($this); 
 
        if (!parent::install() 
            || !$this->install->registerHookForModule() 
            || !$this->install->changeDatabase(self::INSTALL_SQL_FILE) 
           ) 
        { 
            return false; 
        } 
 
        Configuration::updateValue(''.$this->name.'_CHECK_UPDATE', 1); 
 
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
        // Stavy objednvky 
        Configuration::updateValue('DM_COMGATE_STATUS_PAID', 2); 
        Configuration::updateValue('DM_COMGATE_STATUS_PAID_NOT_STOCK', Configuration::get('PS_OS_OUTOFSTOCK_PAID')); 
        Configuration::updateValue('DM_COMGATE_STATUS_ERROR', 8); 
        Configuration::updateValue('DM_COMGATE_STATUS_REFUND', 0); 
         
        // Pidn novho stavu objednvek - COMGATE Objednvka vytvoena 
        $id_order_state_exist_create = Db::getInstance()->getValue(' 
            SELECT `id_order_state` 
            FROM `'._DB_PREFIX_.'order_state_lang` 
            WHERE `name` = "COMGATE - ek se na proveden platby" 
        '); 
 
        if($id_order_state_exist_create) 
        { 
            Configuration::updateValue('DM_COMGATE_STATUS_CREATED', $id_order_state_exist_create); 
        } 
        else 
        { 
            $id_new_state_create = $this->install->createNewOrderState('COMGATE - ek se na proveden platby', '#3ED2F0', 1, false, false, false, false); 
            Configuration::updateValue('DM_COMGATE_STATUS_CREATED', $id_new_state_create);     
        } 
 
        // Pidn novho stavu objednvek - COMGATE Objednvka peruena 
        $id_order_state_exist_create = Db::getInstance()->getValue(' 
            SELECT `id_order_state` 
            FROM `'._DB_PREFIX_.'order_state_lang` 
            WHERE `name` = "COMGATE - platba peruena" 
        '); 
         
        if($id_order_state_exist_create) 
        { 
            Configuration::updateValue('DM_COMGATE_STATUS_CANCELED', $id_order_state_exist_create); 
        } 
        else 
        { 
            $id_new_state_create = $this->install->createNewOrderState('COMGATE - platba peruena', '#FF5B04', 1, false, false, false, false); 
            Configuration::updateValue('DM_COMGATE_STATUS_CANCELED', $id_new_state_create);     
        } 
 
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
        $id_order_state_exist_pre_authorized = Db::getInstance()->getValue(' 
            SELECT `id_order_state` 
            FROM `'._DB_PREFIX_.'order_state_lang` 
            WHERE `name` = "COMGATE - autorizace platby potvrzena" 
        '); 
 
        if($id_order_state_exist_pre_authorized) 
        { 
            Configuration::updateValue('DM_COMGATE_STATUS_PRE_AUTHORIZED', $id_order_state_exist_pre_authorized); 
        } 
        else 
        { 
            $id_new_state_pre_authorized = $this->install->createNewOrderState('COMGATE - autorizace platby potvrzena', '#00BB00', 1, false, false, false, false); 
            Configuration::updateValue('DM_COMGATE_STATUS_PRE_AUTHORIZED', $id_new_state_pre_authorized);     
        } 
 
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
        // Pstupy 
        Configuration::updateValue('DM_COMGATE_TEST_ACTIVE', 1); 
        Configuration::updateValue('DM_COMGATE_TEST_IP_ADDRESS_CLIENT', ''); 
        Configuration::updateValue('DM_COMGATE_MERCHANT', ''); 
        Configuration::updateValue('DM_COMGATE_SECRET', ''); 
 
        // Typy plateb 
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED', 'CARD_ALL,BANK_ALL'); 
        Configuration::updateValue('DM_COMGATE_REPEAT_PAYMENT_ACTIVE', 1); 
        Configuration::updateValue('DM_COMGATE_TYPE_SHOW_PAYMENT', 'offline'); 
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_GROUP', 'CARD_ALL,BANK_ALL'); 
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_CARD', ''); 
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_BANK', ''); 
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_LATER', ''); 
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_PART', ''); 
 
        // Identifikteor a loga 
        Configuration::updateValue('DM_COMGATE_PRE_AUTHORIZED', 2); 
        Configuration::updateValue('DM_COMGATE_IDENT_PAYMENT', 1); 
        Configuration::updateValue('DM_COMGATE_LOGOS_PAYMENT_CART', ''); 
 
        // Logovn 
        Configuration::updateValue('DM_COMGATE_LOG_ACTIVE', 1); 
        Configuration::updateValue('DM_COMGATE_LOG_DAYS', 60); 
 
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
 
        // Mailov servis 
        Configuration::updateValue('DM_COMGATE_MAIL_ACTIVE', 2); 
        Configuration::updateValue('DM_COMGATE_TYPE_CUSTOMER_MAIL', 'eshop'); 
        Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE', 2); 
        Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT', ''); 
        Configuration::updateValue('DM_COMGATE_MAIL_POSITION_ALLOWED', 'return'); 
 
        // ty 
        $currencies = Currency::getCurrencies(); 
 
        foreach ($currencies as $itemCurrency) 
        { 
            Configuration::updateValue('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].'', ''); 
        } 
 
        $languages = Language::getLanguages(false); 
 
        foreach($languages as $language) 
        { 
            $subject = 'Vae platba za objednvku'; 
 
            if($language['iso_code'] == 'sk') 
            { 
                $subject = 'Vaa platba za objednvku'; 
            } 
            elseif($language['iso_code'] == 'en') 
            { 
                $subject = 'Your payment for the order'; 
            } 
 
            Configuration::updateValue('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].'', $subject); 
        } 
 
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
               
        return true; 
    } 
 
    // Metoda pro odinstalace modulu 
    public function uninstall() 
    {  
        if (Shop::isFeatureActive()) 
        { 
            Shop::setContext(Shop::CONTEXT_ALL); 
        } 
 
        // Install tda 
        $this->install = new DmComgateInstall($this); 
             
        if (!parent::uninstall() 
            || !$this->install->unregisterHookForModule() 
            || !$this->install->changeDatabase(self::UNINSTALL_SQL_FILE) 
           ) 
        { 
            return false; 
        } 
 
        Configuration::deleteByName(''.$this->name.'_CHECK_UPDATE'); 
 
        // Stavy objednvky 
        Configuration::deleteByName('DM_COMGATE_STATUS_PRE_AUTHORIZED'); 
        Configuration::deleteByName('DM_COMGATE_STATUS_PAID'); 
        Configuration::deleteByName('DM_COMGATE_STATUS_PAID_NOT_STOCK'); 
        Configuration::deleteByName('DM_COMGATE_STATUS_ERROR'); 
        Configuration::deleteByName('DM_COMGATE_STATUS_CREATED'); 
        Configuration::deleteByName('DM_COMGATE_STATUS_CANCELED'); 
        Configuration::deleteByName('DM_COMGATE_STATUS_REFUND'); 
 
        // Pstupy 
        Configuration::deleteByName('DM_COMGATE_TEST_ACTIVE'); 
        Configuration::deleteByName('DM_COMGATE_TEST_IP_ADDRESS_CLIENT'); 
        Configuration::deleteByName('DM_COMGATE_MERCHANT'); 
        Configuration::deleteByName('DM_COMGATE_SECRET'); 
 
        // Typy plateb 
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_ALLOWED'); 
        Configuration::deleteByName('DM_COMGATE_REPEAT_PAYMENT_ACTIVE'); 
 
        Configuration::deleteByName('DM_COMGATE_TYPE_SHOW_PAYMENT'); 
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_GROUP'); 
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_CARD'); 
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_BANK'); 
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_LATER'); 
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_PART'); 
 
        // Identifikteor a loga 
        Configuration::deleteByName('DM_COMGATE_PRE_AUTHORIZED'); 
        Configuration::deleteByName('DM_COMGATE_IDENT_PAYMENT'); 
        Configuration::deleteByName('DM_COMGATE_LOGOS_PAYMENT_CART'); 
 
        // Logovn 
        Configuration::deleteByName('DM_COMGATE_LOG_ACTIVE'); 
        Configuration::deleteByName('DM_COMGATE_LOG_DAYS'); 
 
        // Mailov servis 
        Configuration::deleteByName('DM_COMGATE_MAIL_ACTIVE'); 
        Configuration::deleteByName('DM_COMGATE_TYPE_CUSTOMER_MAIL'); 
        Configuration::deleteByName('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE'); 
        Configuration::deleteByName('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT'); 
        Configuration::deleteByName('DM_COMGATE_MAIL_POSITION_ALLOWED'); 
 
        // ty 
        $currencies = Currency::getCurrencies(); 
 
        foreach ($currencies as $itemCurrency) 
        { 
            Configuration::deleteByName('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].''); 
        } 
 
        $languages = Language::getLanguages(false); 
 
        foreach($languages as $language) 
        { 
            Configuration::deleteByName('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].''); 
        } 
   
        return true; 
    } 
 
    // Metoda pro zpracovn dat z formul 
    private function _postProcess() 
    { 
        $context = Context::getContext(); 
 
        if(Tools::isSubmit('submitSetting')) 
        { 
            Configuration::updateValue('DM_COMGATE_TEST_ACTIVE', Tools::getValue('DM_COMGATE_TEST_ACTIVE')); 
            Configuration::updateValue('DM_COMGATE_TEST_IP_ADDRESS_CLIENT', trim(Tools::getValue('DM_COMGATE_TEST_IP_ADDRESS_CLIENT'))); 
            Configuration::updateValue('DM_COMGATE_MERCHANT', trim(Tools::getValue('DM_COMGATE_MERCHANT'))); 
            Configuration::updateValue('DM_COMGATE_SECRET', trim(Tools::getValue('DM_COMGATE_SECRET'))); 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingOrderState')) 
        { 
            Configuration::updateValue('DM_COMGATE_STATUS_CREATED', Tools::getValue('DM_COMGATE_STATUS_CREATED')); 
            Configuration::updateValue('DM_COMGATE_STATUS_PRE_AUTHORIZED', Tools::getValue('DM_COMGATE_STATUS_PRE_AUTHORIZED')); 
            Configuration::updateValue('DM_COMGATE_STATUS_PAID', Tools::getValue('DM_COMGATE_STATUS_PAID')); 
            Configuration::updateValue('DM_COMGATE_STATUS_PAID_NOT_STOCK', Tools::getValue('DM_COMGATE_STATUS_PAID_NOT_STOCK')); 
            Configuration::updateValue('DM_COMGATE_STATUS_CANCELED', Tools::getValue('DM_COMGATE_STATUS_CANCELED')); 
            Configuration::updateValue('DM_COMGATE_STATUS_ERROR', Tools::getValue('DM_COMGATE_STATUS_ERROR')); 
            Configuration::updateValue('DM_COMGATE_STATUS_REFUND', Tools::getValue('DM_COMGATE_STATUS_REFUND')); 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven stav objednvek bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(2); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingOther')) 
        { 
            if (Configuration::get('DM_COMGATE_TYPE_SHOW_PAYMENT') == 'online') 
            { 
                // Skupinov ALIASY 
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED')) 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED')))); 
                } 
                else 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED', ''); 
                } 
 
                // Platby kartou 
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED')) 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED')))); 
                } 
                else 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED', ''); 
                } 
 
                // Platby bankou 
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED')) 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED')))); 
                } 
                else 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED', ''); 
                } 
 
                // Platby platm pozdji 
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED')) 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED')))); 
                } 
                else 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED', ''); 
                } 
 
                // Platm na tetiny 
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED')) 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED')))); 
                } 
                else 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED', ''); 
                } 
            } 
            else 
            { 
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED')) 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED')))); 
                } 
                else 
                { 
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED', ''); 
                } 
            } 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven zobrazen platebnch metod bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(3); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingPayment')) 
        { 
            Configuration::updateValue('DM_COMGATE_TYPE_SHOW_PAYMENT', Tools::getValue('DM_COMGATE_TYPE_SHOW_PAYMENT')); 
            Configuration::updateValue('DM_COMGATE_REPEAT_PAYMENT_ACTIVE', Tools::getValue('DM_COMGATE_REPEAT_PAYMENT_ACTIVE')); 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven plateb bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(3); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingEmail')) 
        { 
            Configuration::updateValue('DM_COMGATE_MAIL_ACTIVE', Tools::getValue('DM_COMGATE_MAIL_ACTIVE')); 
            Configuration::updateValue('DM_COMGATE_TYPE_CUSTOMER_MAIL', Tools::getValue('DM_COMGATE_TYPE_CUSTOMER_MAIL')); 
            Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE', Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE')); 
            Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT', Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT')); 
 
            $languages = Language::getLanguages(false, $context->shop->id); 
 
            foreach($languages as $language) 
            { 
                Configuration::updateValue('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].'', Tools::getValue('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].'')); 
            } 
 
            if (Tools::getValue('DM_COMGATE_MAIL_POSITION_ALLOWED')) 
            { 
                Configuration::updateValue('DM_COMGATE_MAIL_POSITION_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_MAIL_POSITION_ALLOWED')))); 
            } 
            else 
            { 
                Configuration::updateValue('DM_COMGATE_MAIL_POSITION_ALLOWED', ''); 
            } 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven zobrazen platebnch metod bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(4); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingAccount')) 
        { 
            $currencies = Currency::getCurrencies(); 
 
            foreach ($currencies as $itemCurrency) 
            { 
                Configuration::updateValue('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].'', trim(Tools::getValue('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].''))); 
            } 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven oznaen t bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(16); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingLog')) 
        { 
            Configuration::updateValue('DM_COMGATE_LOG_ACTIVE', Tools::getValue('DM_COMGATE_LOG_ACTIVE')); 
            Configuration::updateValue('DM_COMGATE_LOG_DAYS', (int)Tools::getValue('DM_COMGATE_LOG_DAYS')); 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven logovn udlost bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(5); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingDetail')) 
        { 
            Configuration::updateValue('DM_COMGATE_PRE_AUTHORIZED', Tools::getValue('DM_COMGATE_PRE_AUTHORIZED')); 
            Configuration::updateValue('DM_COMGATE_IDENT_PAYMENT', Tools::getValue('DM_COMGATE_IDENT_PAYMENT')); 
            Configuration::updateValue('DM_COMGATE_LOGOS_PAYMENT_CART', (Tools::getValue('DM_COMGATE_LOGOS_PAYMENT_CART_ALLOWED') ? implode(',', array_keys(Tools::getValue('DM_COMGATE_LOGOS_PAYMENT_CART_ALLOWED'))) : '')); 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(7); }; </script>'; 
        } 
 
        if (Tools::isSubmit('status_comgate_active_EditAllowedProduct')) 
        { 
            $status = (int)Db::getInstance()->getValue(' 
                SELECT `dm_comgate_active` 
                FROM `'._DB_PREFIX_.'product_shop` 
                WHERE `id_product` = '.Tools::getValue('id_product').' 
                    AND id_shop = '.(int)$context->shop->id.' 
            '); 
     
            Db::getInstance()->Execute(' 
                UPDATE `'._DB_PREFIX_.'product_shop` 
                SET `dm_comgate_active` = \''.($status == 1 ? 0 : 1).'\' 
                WHERE `id_product` = '.Tools::getValue('id_product').' 
                    AND id_shop = '.(int)$context->shop->id.' 
            '); 
                                             
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7'); 
        } 
 
        if(Tools::isSubmit('submitBulkedittoactive_EditAllowedProduct')) 
        { 
            foreach(Tools::getValue('_EditAllowedProductBox') as $id) 
            { 
                Db::getInstance()->Execute(' 
                    UPDATE `'._DB_PREFIX_.'product_shop` 
                    SET `dm_comgate_active` = 1 
                    WHERE `id_product` = '.(int)$id.' 
                        AND id_shop = '.(int)$context->shop->id.' 
                '); 
            } 
 
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7'); 
        } 
 
        if(Tools::isSubmit('submitBulkedittodeactive_EditAllowedProduct')) 
        { 
            foreach(Tools::getValue('_EditAllowedProductBox') as $id) 
            { 
                Db::getInstance()->Execute(' 
                    UPDATE `'._DB_PREFIX_.'product_shop` 
                    SET `dm_comgate_active` = 0 
                    WHERE `id_product` = '.(int)$id.' 
                        AND id_shop = '.(int)$context->shop->id.' 
                '); 
            } 
 
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7'); 
        } 
 
        if(Tools::isSubmit('submitSettingUpdate')) 
        { 
            Configuration::updateValue(''.$this->name.'_CHECK_UPDATE', Tools::getValue(''.$this->name.'_CHECK_UPDATE')); 
 
            $this->_html .= $this->displayConfirmation($this->l('Nastaven bylo uloeno.')); 
 
            $this->_html .= '<script> window.onload=function() { show_list(10); }; </script>'; 
        } 
 
        if (Tools::isSubmit('submitUploadFile')) 
        { 
            copy($_FILES['file']['tmp_name'], dirname(__FILE__).'/crt/'.$context->shop->id.'_'.$this->name.'.crt'); 
 
            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>'; 
        } 
    } 
 
    // Metoda pro kontrolu sprvnosti vyplnnch dat 
    private function _postValidation() 
    { 
        if(Tools::isSubmit('submitSetting')) 
        { 
            if(Tools::getValue('DM_COMGATE_MERCHANT') == '') 
            { 
                $this->_postErrors[] = $this->l('Prosm vyplte polko Merchant ID klienta.'); 
            } 
 
            if(Tools::getValue('DM_COMGATE_SECRET') == '') 
            { 
                $this->_postErrors[] = $this->l('Prosm vyplte polko Secret heslo pro pstup ke slub COMGATE.'); 
            } 
 
            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingOther')) 
        { 
            if (Configuration::get('DM_COMGATE_TYPE_SHOW_PAYMENT') == 'offline') 
            { 
                if(!Tools::getValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED')) 
                { 
                    $this->_postErrors[] = $this->l('Muste vybrat njakou platebn metodu pro zobrazen v koku.'); 
                } 
            } 
 
            $this->_html .= '<script> window.onload=function() { show_list(3); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingEmail')) 
        { 
            if(Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE') == 1) 
            { 
                if(Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT') == '') 
                { 
                    $this->_postErrors[] = $this->l('Zadejte prosm email obchodnka, ktermu budou chodit upozornn.'); 
                } 
            } 
 
            $this->_html .= '<script> window.onload=function() { show_list(4); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitSettingLog')) 
        { 
            if(trim(Tools::getValue('DM_COMGATE_LOG_DAYS')) == '') 
            { 
                $this->_postErrors[] = $this->l('Prosm vyplte poet dn pro mazn starch zznam logu.'); 
            } 
 
            $this->_html .= '<script> window.onload=function() { show_list(5); }; </script>'; 
        } 
 
       	if (Tools::isSubmit('submitUploadFile')) 
        { 
            if(!file_exists($_FILES['file']['tmp_name']) OR !in_array(strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)), array('crt'))) 
            { 
                $this->_postErrors[] = $this->l('Nepodailo se nast sprvn licenn soubor.'); 
            }   
 
            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>';   
        } 
 
        if(Tools::isSubmit('submitBulkedittoactive_EditAllowedProduct')) 
        { 
            if (!is_array(Tools::getValue('_EditAllowedProductBox'))) 
            { 
                $this->_postErrors[] = $this->l('Nen vybrn dn produkt k aktivaci pro platebn brnu!'); 
            } 
 
            $this->_html .= '<script> window.onload=function() { show_list(7); }; </script>'; 
        } 
 
        if(Tools::isSubmit('submitBulkedittodeactive_EditAllowedProduct')) 
        { 
            if (!is_array(Tools::getValue('_EditAllowedProductBox'))) 
            { 
                $this->_postErrors[] = $this->l('Nen vybrn dn produkt k deaktivaci pro platebn brnu!'); 
            } 
 
            $this->_html .= '<script> window.onload=function() { show_list(7); }; </script>'; 
        } 
    } 
 
    public function displayInfo() 
    { 
        $this->context->smarty->assign(array( 
                'modul_name' => $this->displayName, 
                'modul_description' => $this->description, 
            )); 
 
        if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
        { 
            return $this->display(__FILE__, ''.$this->name.'.tpl'); 
        } 
        else 
        { 
            return $this->fetch('module:'.$this->name.'/views/templates/hook/'.$this->name.'.tpl'); 
        } 
    } 
 
    // Metoda pro zobrazen obsahu 
    public function getContent() 
    { 
        $this->context->controller->addCss($this->_path.'css/style.css'); 
        $this->context->controller->addJS($this->_path.'js/function.js'); 
 
        if (Tools::isSubmit('submitResetlog')) 
        { 
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=5'); 
        } 
 
        if (Tools::isSubmit('submitReset_EditAllowedProduct')) 
        { 
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7'); 
        } 
 
        if ( 
            Tools::isSubmit('btnSubmit') 
            || Tools::isSubmit('status_comgate_active_EditAllowedProduct') 
            || Tools::isSubmit('submitBulkedittoactive_EditAllowedProduct') 
            || Tools::isSubmit('submitBulkedittodeactive_EditAllowedProduct') 
        ) 
        { 
 
            $this->_postValidation(); 
             
            if (!count($this->_postErrors)) 
            { 
                $this->_postProcess(); 
            } 
            else 
            { 
                foreach ($this->_postErrors as $err) 
                { 
                    $this->_html .= $this->displayError($err); 
                } 
            }   
        } 
        else 
        { 
            if (Tools::isSubmit('list')) 
            { 
                $this->_html .= '<script> window.onload=function() { show_list('.Tools::getValue('list').'); }; </script>'; 
            } 
            else 
            { 
                $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>'; 
            } 
 
            $this->_html .= '<br />';          
        } 
 
        // Zjitn aktualiazce modulu 
        if($this->update->version_data) 
        { 
            $this->_html .= ' 
                <div class="alert alert-warning"> 
                    '.$this->l('Je k dispozici nov verze modulu').' ('.$this->update->version_data['version'].').<br /> 
                    '.$this->update->version_data['changelog'].'         
                </div> 
            '; 
        } 
 
        if(Configuration::get('DM_COMGATE_PRE_AUTHORIZED') == 1) 
        { 
            $this->_html .= ' 
                <div class="alert alert-danger"> 
                    '.$this->l('Modul je v reimu "Pedautorizace", nezapomete potvrdit staen financ od zkaznka na v et COMGATE po kontrole objednvky.').' 
                    '.$this->l('Uite tak dve ne zsilku zalete a vypr vm platnost zaloen platby.').' 
                </div> 
            '; 
        } 
 
        if (Shop::isFeatureActive() AND Shop::getContext() == Shop::CONTEXT_ALL) 
        { 
            $this->_html .= $this->displayError('Prosm pepnte modul v kontextu jednotlivch STORE!'); 
        } 
        elseif (Shop::isFeatureActive() AND Shop::getContext() == Shop::CONTEXT_GROUP) 
        { 
            $this->_html .= $this->displayError('Prosm pepnte modul v kontextu jednotlivch STORE!'); 
        } 
        else 
        { 
            // Install tda 
            $this->install = new DmComgateInstall($this); 
            $this->install->repairRegisterHooks(); 
 
            $form = new DmComgateFormBuilder($this); 
 
            $this->_html .= $form->init();  
        } 
 
        return $this->_html; 
    } 
 
    // Metoda pro generovn formul s nastavenm 
    public function renderForm($form, $fields_value) 
    { 
        $helper = new HelperForm(); 
        $helper->show_toolbar = false; 
        $helper->table =  $this->table; 
        $lang = new Language($this->default_lang); 
        $helper->default_form_language = $lang->id; 
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0; 
        $helper->identifier = $this->identifier; 
        $helper->submit_action = 'btnSubmit'; 
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name; 
        $helper->token = Tools::getAdminTokenLite('AdminModules'); 
        $helper->tpl_vars = array( 
            'fields_value' => $fields_value, 
            'languages' => $this->context->controller->getLanguages(), 
            'id_language' => $this->context->language->id 
        ); 
 
        return $helper->generateForm($form); 
    } 
 
    public function renderLogList($list, $fields, $count) 
    { 
        $helper = new HelperList(); 
        $helper->shopLinkType = ''; 
        $helper->simple_header = false; 
        $helper->actions = array(); 
        $helper->show_toolbar = false; 
        $helper->module = $this; 
        $helper->identifier = 'id_log'; 
        $helper->title = $this->l('Log udlost').' ('.$count.')'; 
        $helper->table = 'log'; 
        $helper->token = Tools::getAdminTokenLite('AdminModules'); 
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name.'&module_name='.$this->name.'&list=5'; 
        $helper->no_link = true; 
 
        //$helper->_default_pagination = 50; 
        $helper->listTotal = $count; 
 
        return $helper->generateList($list, $fields); 
    } 
 
    public function renderProductList($list, $fields, $count) 
    { 
        $helper = new HelperList(); 
        $helper->shopLinkType = ''; 
        $helper->simple_header = false; 
        $helper->actions = array(); 
        $helper->show_toolbar = false; 
        $helper->module = $this; 
 
        if(version_compare(_PS_VERSION_, "1.6.0.9", ">")) 
        { 
            $helper->_pagination = array(20, 50, 100, 300, 1000, 10000); 
        } 
 
        $helper->bulk_actions = array( 
            'edittoactive' => array( 
                'text'    => $this->l('Zmnit status na POVOLENO'), 
                'icon'    => 'icon-edit', 
            ), 
            'edittodeactive' => array( 
                'text'    => $this->l('Zmnit status na ZAKZNO'), 
                'icon'    => 'icon-edit', 
            ), 
        ); 
        $helper->identifier = 'id_product'; 
        $helper->title = $this->l('Povolen modulu u produkt').' ('.count($count).')'; 
        $helper->table = '_EditAllowedProduct'; 
        $helper->token = Tools::getAdminTokenLite('AdminModules'); 
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name.'&module_name='.$this->name.'&list=7'; 
        $helper->no_link = true; 
 
        $helper->listTotal = count($count); 
 
        return $helper->generateList($list, $fields); 
    } 
 
    // Metoda pro kontrolu povolen mny 
    public function checkCurrency($cart) 
    { 
        $currency_order = new Currency($cart->id_currency); 
        $currencies_module = $this->getCurrency($cart->id_currency); 
         
        if (is_array($currencies_module)) 
        { 
            foreach ($currencies_module as $currency_module) 
            { 
                if ($currency_order->id == $currency_module['id_currency']) 
                { 
                    return true; 
                } 
            } 
        } 
                   
        return false; 
    } 
 
    public function getGroupPaymentMethod() 
    { 
        return array( 
            array( 
                'name' => $this->l('Platebn brna COMGATE'), 
                'id' => 'ALLOWED[ALL]', 
                'val' => 'ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-payment.png', 
                'currency' => 'ALL', 
                'intro' => true, 
            ), 
            array( 
                'name' => $this->l('Platebn kartou online'), 
                'id' => 'ALLOWED[CARD_ALL]', 
                'val' => 'CARD_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-payment-card.png', 
                'currency' => 'ALL', 
                'intro' => true, 
            ), 
            array( 
                'name' => $this->l('Okamit bankovn pevod online'), 
                'id' => 'ALLOWED[BANK_ALL]', 
                'val' => 'BANK_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-bank.png', 
                'currency' => 'ALL', 
                'intro' => true, 
            ), 
            array( 
                'name' => $this->l('Odloen platba - Nakupte hned, zaplate pozdji'), 
                'id' => 'ALLOWED[LATER_ALL]', 
                'val' => 'LATER_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
            ), 
            array( 
                'name' => $this->l('Nkup na tetiny - Nakupte hned, zaplate jen tetinu'), 
                'id' => 'ALLOWED[PART_ALL]', 
                'val' => 'PART_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 1500, 
                'limit_max' => 15000, 
            ), 
        ); 
    } 
 
    public function getPaymentMethod() 
    { 
        return array( 
            array( 
                'name' => $this->l('Platebn brna COMGATE'), 
                'id' => 'ALLOWED[ALL]', 
                'val' => 'ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-payment.png', 
                'currency' => 'ALL', 
                'intro' => true, 
            ), 
            array( 
                'name' => $this->l('Platebn kartou online'), 
                'id' => 'ALLOWED[CARD_ALL]', 
                'val' => 'CARD_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-payment-card.png', 
                'currency' => 'ALL', 
                'intro' => true, 
            ), 
            array( 
                'name' => $this->l('Okamit bankovn pevod online'), 
                'id' => 'ALLOWED[BANK_ALL]', 
                'val' => 'BANK_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-bank.png', 
                'currency' => 'ALL', 
                'intro' => true, 
            ), 
             
            array( 
                'name' => $this->l('Odloen platba - Nakupte hned, zaplate pozdji'), 
                'id' => 'ALLOWED[LATER_ALL]', 
                'val' => 'LATER_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
            ), 
             
            array( 
                'name' => $this->l('Platm pak odloen platba - Nakupte hned, zaplate pozdji'), 
                'id' => 'ALLOWED[LATER_PLATIMPAK]', 
                'val' => 'LATER_PLATIMPAK', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-pay-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 500, 
                'limit_max' => 25000, 
            ), 
            array( 
                'name' => $this->l('Skip Pay odloen platba - Nakupte hned, zaplate za 14 dn'), 
                'id' => 'ALLOWED[LATER_SKIPPAY]', 
                'val' => 'LATER_SKIPPAY', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 0, 
                'limit_max' => 25000, 
            ), 
            array( 
                'name' => $this->l('TWISTO odloen platba - Nakupte hned, zaplate pozdji'), 
                'id' => 'ALLOWED[LATER_TWISTO]', 
                'val' => 'LATER_TWISTO', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-twisto.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 0, 
                'limit_max' => 25000, 
            ), 
            array( 
                'name' => $this->l('Nkup na tetiny - Nakupte hned, zaplate jen tetinu'), 
                'id' => 'ALLOWED[PART_ALL]', 
                'val' => 'PART_ALL', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 1500, 
                'limit_max' => 15000, 
            ), 
            array( 
                'name' => $this->l('Skip Pay nkup na tetiny - Nakupte hned, zaplate jen tetinu'), 
                'id' => 'ALLOWED[PART_SKIPPAY]', 
                'val' => 'PART_SKIPPAY', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 3000, 
                'limit_max' => 15000, 
            ), 
            array( 
                'name' => $this->l('TWISTO nkup na tetiny - Nakupte hned, zaplate jen tetinu'), 
                'id' => 'ALLOWED[PART_TWISTO]', 
                'val' => 'PART_TWISTO', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-twisto.png', 
                'currency' => 'CZK', 
                'intro' => true, 
                'limit_min' => 1500, 
                'limit_max' => 15000, 
            ), 
            array( 
                'name' => $this->l('Apple Pay'), 
                'id' => 'ALLOWED[APPLEPAY_REDIRECT]', 
                'val' => 'APPLEPAY_REDIRECT', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-applepay.png', 
                'currency' => 'ALL', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('Google Pay'), 
                'id' => 'ALLOWED[GOOGLEPAY_REDIRECT]', 
                'val' => 'GOOGLEPAY_REDIRECT', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-gpay.png', 
                'currency' => 'ALL', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('Raiffeisen Bank - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_RB]', 
                'val' => 'BANK_CZ_RB', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-rb.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('Komern banka - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_KB]', 
                'val' => 'BANK_CZ_KB', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-kb.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('MONETA - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_MO]', 
                'val' => 'BANK_CZ_MO', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-moneta.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('FIO banka - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_FB_P]', 
                'val' => 'BANK_CZ_FB_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-fio.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('esk spoitelna - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_CS_P]', 
                'val' => 'BANK_CZ_CS_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-cs.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('mBank - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_MB_P]', 
                'val' => 'BANK_CZ_MB_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-mbank.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('SOB - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_CSOB_P]', 
                'val' => 'BANK_CZ_CSOB_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-csob.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('era Potovn spoitelna - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_PS_P]', 
                'val' => 'BANK_CZ_PS_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-era.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('UniCredit Bank - okamit pevod'), 
                'id' => 'ALLOWED[BANK_CZ_UC]', 
                'val' => 'BANK_CZ_UC', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-uni.png', 
                'currency' => 'CZK', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('Slovensk sporitea - okamit pevod'), 
                'id' => 'ALLOWED[BANK_SK_SLSP]', 
                'val' => 'BANK_SK_SLSP', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-ssp.png', 
                'currency' => 'EUR', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('VB Banka - okamit pevod'), 
                'id' => 'ALLOWED[BANK_SK_VUB_P]', 
                'val' => 'BANK_SK_VUB_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-vub.png', 
                'currency' => 'EUR', 
                'intro' => false, 
            ), 
            array( 
                'name' => $this->l('Tatra Banka - okamit pevod'), 
                'id' => 'ALLOWED[BANK_SK_TB_P]', 
                'val' => 'BANK_SK_TB_P', 
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-tb.png', 
                'currency' => 'EUR', 
                'intro' => false, 
            ), 
        ); 
    } 
 
    public function getLimit($currency_iso_code) 
    { 
        $limit = 0; 
 
        if($currency_iso_code == 'CZK') 
        { 
            $limit = 10; 
        } 
        elseif($currency_iso_code == 'EUR') 
        { 
            $limit = 1; 
        } 
        elseif($currency_iso_code == 'PLN') 
        { 
            $limit = 10; 
        } 
        elseif($currency_iso_code == 'HUF') 
        { 
            $limit = 100; 
        } 
        elseif($currency_iso_code == 'USD') 
        { 
            $limit = 1; 
        } 
        elseif($currency_iso_code == 'GBP') 
        { 
            $limit = 1; 
        } 
 
        return $limit; 
    } 
 
 
    // Metoda pro zobrazen platebn metody 
    public function hookPaymentOptions($params) 
    { 
        $amount_cart = (float)$this->context->cart->getOrderTotal(); 
        $currency = Currency::getCurrency($this->context->cart->id_currency); 
        $currency_iso_code = $currency['iso_code']; 
 
        $limit = $this->getLimit($currency_iso_code); 
 
        if($limit > $amount_cart) 
        { 
            return; 
        } 
 
        // Oven aktivnho modulu 
        if (!$this->active) { 
            return; 
        } 
         
        // Kontrola piazen mny     
        if (!$this->checkCurrency($params['cart'])) 
        { 
            return; 
        } 
 
        // Kontrola povolench produkt 
        if($this->isDisallowedProductInCart($params['cart'])) 
        { 
            return; 
        } 
 
        $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
 
        // Kontrola IP adresy pro test 
        if($test_mode) 
        { 
            $allowed_ip = (string)trim(Configuration::get('DM_COMGATE_TEST_IP_ADDRESS_CLIENT')); 
 
            if($allowed_ip != '') 
            { 
                $client_ip = (string)Tools::getRemoteAddr(); 
 
                if($client_ip != $allowed_ip) 
                { 
                    return; 
                } 
            } 
        } 
 
        // Pole s platebnmi metodami 
        $array_payment_options = array(); 
 
        $fields_allowed = explode(',', Configuration::get('DM_COMGATE_LOGOS_PAYMENT_CART')); 
 
        if (Configuration::get('DM_COMGATE_TYPE_SHOW_PAYMENT') == 'online') 
        { 
            $poc = 0; 
 
            // Skupiny ALIASY 
            $group_payments_methods = $this->getGroupPaymentMethod(); 
            $group_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED')); 
 
            foreach($group_payments_methods as $item_group_method) 
            { 
                if(in_array($item_group_method['val'], $group_payments_allowed) AND ($currency_iso_code == $item_group_method['currency'] OR $item_group_method['currency'] == 'ALL')) 
                { 
                    // Idividuln limit na danou platbu, zpravidla odloen platby 
                    if(isset($item_group_method['limit_min']) AND ($item_group_method['limit_min'] >= $amount_cart OR $item_group_method['limit_max'] <= $amount_cart)) 
                        continue; 
 
                    $array_payment_options[(int)$poc] = new PaymentOption(); 
                    $array_payment_options[(int)$poc]->setCallToActionText($item_group_method['name'].''.($test_mode ? ' '.$this->l('(Testovac reim)').'' : '').''); 
                    $array_payment_options[(int)$poc]->setAction($this->context->link->getModuleLink($this->name, 'validation', array('preferpayment' => $item_group_method['val']), true).''); 
 
                    if($item_group_method['intro']) 
                    { 
                        if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
                        { 
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->display(__FILE__, 'dm_comgate_intro_'.$item_group_method['val'].'.tpl')); 
                        } 
                        else 
                        { 
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_intro_'.$item_group_method['val'].'.tpl')); 
                        } 
                    } 
 
                    if(in_array('payment', $fields_allowed) AND isset($item_group_method['link'])) 
                    { 
                        $array_payment_options[(int)$poc]->setLogo(Media::getMediaPath($item_group_method['link'])); 
                    } 
 
                    $poc++; 
                } 
            } 
 
            // Pipojen a staen ONLINE plateb 
 
            // Volm objekt pro API COMGATE 
            $comgate = new DmComgateApi($this, $test_mode); 
            $total_amount_for_payment = $comgate->formatPriceForPayment($amount_cart, (int)$this->context->cart->id_currency); 
 
            $invoiceAddress = new Address((int)$this->context->cart->id_address_invoice); 
            $countryInvoice = new Country((int)$invoiceAddress->id_country); 
 
            $all_payment_list = $comgate->getPaymentMethods($this->context->language->iso_code, $currency_iso_code, mb_strtoupper($countryInvoice->iso_code), $total_amount_for_payment); 
 
            if ($all_payment_list) 
            { 
                $card_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED')); 
                $bank_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED')); 
                $later_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED')); 
                $part_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED')); 
 
                foreach($all_payment_list['methods'] as $item_payment) 
                { 
                    if( 
                        in_array($item_payment['id'], $card_payments_allowed) 
                        || in_array($item_payment['id'], $bank_payments_allowed) 
                        || in_array($item_payment['id'], $later_payments_allowed) 
                        || in_array($item_payment['id'], $part_payments_allowed) 
                    ) 
                    { 
                        $array_payment_options[(int)$poc] = new PaymentOption(); 
                        $array_payment_options[(int)$poc]->setCallToActionText($item_payment['name'].''.($test_mode ? ' '.$this->l('(Testovac reim)').'' : '').''); 
                        $array_payment_options[(int)$poc]->setAction($this->context->link->getModuleLink($this->name, 'validation', array('preferpayment' => $item_payment['id']), true).''); 
 
                        $this->smarty->assign(array( 
                            'dmComgateOnlineDescription' => $item_payment['description'], 
                        )); 
 
                        if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
                        { 
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->display(__FILE__, 'dm_comgate_intro_ONLINE.tpl')); 
                        } 
                        else 
                        { 
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_intro_ONLINE.tpl')); 
                        } 
 
                        if(in_array('payment', $fields_allowed) AND isset($item_payment['logo']) AND $item_payment['logo'] != '') 
                        { 
                            $array_payment_options[(int)$poc]->setLogo(Media::getMediaPath($item_payment['logo'])); 
                        } 
                    } 
 
                    $poc++; 
                } 
            } 
        } 
        else 
        { 
            $payments_methods = $this->getPaymentMethod(); 
 
            $payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_ALLOWED')); 
            $poc = 0; 
             
            foreach($payments_methods as $item_method) 
            { 
                if(in_array($item_method['val'], $payments_allowed) AND ($currency_iso_code == $item_method['currency'] OR $item_method['currency'] == 'ALL')) 
                {  
                    // Idividuln limit na danou platbu, zpravidla odloen platby 
                    if(isset($item_method['limit_min']) AND ($item_method['limit_min'] >= $amount_cart OR $item_method['limit_max'] <= $amount_cart)) 
                        continue; 
                     
                    $array_payment_options[(int)$poc] = new PaymentOption(); 
 
                    $array_payment_options[(int)$poc]->setCallToActionText($item_method['name'].''.($test_mode ? ' '.$this->l('(Testovac reim)').'' : '').''); 
                    $array_payment_options[(int)$poc]->setAction($this->context->link->getModuleLink($this->name, 'validation', array('preferpayment' => $item_method['val']), true).''); 
 
                    if($item_method['intro']) 
                    { 
                        if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
                        { 
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->display(__FILE__, 'dm_comgate_intro_'.$item_method['val'].'.tpl')); 
                        } 
                        else 
                        { 
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_intro_'.$item_method['val'].'.tpl')); 
                        } 
                    } 
 
                    if(in_array('payment', $fields_allowed) AND isset($item_method['link'])) 
                    { 
                        $array_payment_options[(int)$poc]->setLogo(Media::getMediaPath($item_method['link'])); 
                    } 
 
                    $poc++; 
                } 
            } 
        } 
 
        return $array_payment_options; 
    } 
 
    public function hookDisplayPaymentReturn($params) 
    { 
        if (!$this->active) { 
            return; 
        } 
 
        // Nastavm zobrazen potvrzovac strnky 
        Db::getInstance()->Execute(' 
            UPDATE `'._DB_PREFIX_.'dm_comgate_payment` 
            SET `show_confirmation` = 1 
            WHERE `id_order` = '.(int)$params['order']->id.' 
        '); 
 
        // Chyba na brn, nezaloen platba 
        if(Tools::getValue('dm_comgate_status') == 'ERROR') 
        { 
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
            { 
                return $this->display(__FILE__, 'dm_comgate_confirmation_errors.tpl'); 
            } 
            else 
            { 
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_confirmation_errors.tpl'); 
            } 
        } 
        // S njakm vsledkem jdu z brny 
        else 
        { 
            // Zjiuji produkn mod nebo TEST 
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
 
            $payment_check_url = $this->context->link->getModuleLink( 
                'dm_comgate', 
                'cgpayment', 
                array( 
                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$params['order']->id : (string)$params['order']->reference), 
                ), 
                true 
            ); 
 
            // Vracm TPL 
            $this->smarty->assign(array( 
                'dm_comgate_check_url' => $payment_check_url, 
                'dm_comgate_test_mode' => $test_mode, 
                'dm_comgate_status_payment' => Tools::getValue('dm_comgate_status'), 
            )); 
 
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
            { 
                return $this->display(__FILE__, 'dm_comgate_confirmation.tpl'); 
            } 
            else 
            { 
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_confirmation.tpl'); 
            } 
        } 
    } 
 
    public function hookDisplayDmPaymentButtonHistory($params) 
    { 
        if ((int)$params['order']->id > 0) 
        { 
            $order_data = new Order((int)$params['order']->id); 
        } 
 
        if($order_data->module != $this->name) 
            return; 
 
        $allowed_state = array( 
            Configuration::get('DM_COMGATE_STATUS_CREATED'), 
            Configuration::get('DM_COMGATE_STATUS_CANCELED'), 
            Configuration::get('PS_OS_OUTOFSTOCK_PAID'), 
            Configuration::get('PS_OS_OUTOFSTOCK_UNPAID'), 
        ); 
 
        // Stle stav, e ek na gopay platbu 
        if(in_array($order_data->current_state, $allowed_state) AND Configuration::get('DM_COMGATE_REPEAT_PAYMENT_ACTIVE') == 1) 
        { 
            $context = Context::getContext(); 
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
            $comgate = new DmComgateApi($this, $test_mode); 
            $exist_payment = $comgate->getPaymentForOrder($order_data->id); 
            $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL'); 
 
            if($type_url_link == 'original') 
            { 
                // Platba nen zaloena pouiji odkaz pes eshop / kontroler, kde se autoamticky paltba zalo 
                if(!$exist_payment) 
                { 
                    $url_payment_link = $this->context->link->getModuleLink( 
                        'dm_comgate', 
                        'cgpayment', 
                        array( 
                            'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                            'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference), 
                        ), 
                        true, 
                        (int)$order_data->id_lang, 
                        (int)$order_data->id_shop 
                    ); 
                } 
                else 
                { 
                    $url_payment_link = 'https://payments.comgate.cz/'.$exist_payment['id_comgate'].''; 
                } 
 
            } 
            else 
            { 
                $url_payment_link = $this->context->link->getModuleLink( 
                    'dm_comgate', 
                    'cgpayment', 
                    array( 
                        'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                        'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference), 
                    ), 
                    true, 
                    (int)$order_data->id_lang, 
                    (int)$order_data->id_shop 
                );  
            } 
 
            // Vracm TPL 
            $this->smarty->assign(array( 
                'dm_comgate_url_payment' => $url_payment_link, 
            )); 
 
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
            { 
                return $this->display(__FILE__, '/views/templates/front/dm_comgate_payment_history_button.tpl'); 
            } 
            else 
            { 
                return $this->fetch('module:dm_comgate/views/templates/front/dm_comgate_payment_history_button.tpl'); 
            } 
        } 
 
        return; 
    } 
 
    // Metoda pro zobrazen platby v historii objednvek, pokud jet nen zaplacen 
    public function hookDisplayOrderDetail($params) 
    { 
        if($params['order']->module != $this->name) 
            return; 
 
        $allowed_state = array( 
            Configuration::get('DM_COMGATE_STATUS_CREATED'), 
            Configuration::get('DM_COMGATE_STATUS_CANCELED'), 
            Configuration::get('PS_OS_OUTOFSTOCK_PAID'), 
            Configuration::get('PS_OS_OUTOFSTOCK_UNPAID'), 
        ); 
 
        // Stle stav, e ek na comgate platbu 
        if(in_array($params['order']->current_state, $allowed_state) AND Configuration::get('DM_COMGATE_REPEAT_PAYMENT_ACTIVE') == 1) 
        { 
            $context = Context::getContext(); 
            // Zjiuji produkn mod nebo TEST 
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
 
            // Volm objekt pro API COMGATE 
            $comgate = new DmComgateApi($this, $test_mode); 
 
            // Zjistm pro jistotu jestli neexistuje njak ji zaloen platba v systmu (pojistka pro opakovan naten v prohlei), koda zakldat novou 
            $exist_payment = $comgate->getPaymentForOrder($params['order']->id); 
 
            // Platba v databzi existuje 
            if($exist_payment) 
            { 
                // Zjiuji na brn stav 
                $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']); 
 
                if($cg_status['status'] == 'PENDING') 
                { 
                    $currency = new Currency($params['order']->id_currency); 
 
                    // Vracm TPL 
                    $this->smarty->assign(array( 
                        'dm_comgate_test_mode' => $test_mode, 
                        'dm_comgate_id_payment' => $exist_payment['id_comgate'], 
                        'dm_comgate_payment_url' => 'https://payments.comgate.cz/client/instructions/index?id='.$exist_payment['id_comgate'].'', 
                        'dm_comgate_id_order' => $params['order']->id, 
                        'dm_comgate_reference_order' => $params['order']->reference, 
                        'dm_comgate_total_to_pay' => Tools::displayPrice($params['order']->total_paid, $currency, false), 
                    )); 
 
                    if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
                    { 
                        return $this->display(__FILE__, '/views/templates/front/dm_comgate_payment.tpl'); 
                    } 
                    else 
                    { 
                        return $this->fetch('module:dm_comgate/views/templates/front/dm_comgate_payment.tpl'); 
                    } 
                } 
                else 
                { 
                    return; 
                } 
 
            } 
            else 
            { 
                return; 
            } 
        } 
 
        return; 
    } 
 
    public function hookDisplayBackOfficeHeader() 
    { 
        Media::addJsDef(array( 
            'dm_comgate_id_default_lang' => $this->default_lang, 
            'dm_comgate_ip_client' => ''.Tools::getRemoteAddr().'', 
        )); 
 
        // Odesln mailu s odkazem na platbu 
        if (Tools::isSubmit('SubmitComgateLinkToCustomer')) 
        { 
            $id_order = (int)Tools::getValue('dm_comgate_id_order'); 
            $this->sendMailPayment($id_order); 
            $this->context->controller->confirmations[] = $this->l('Email odesln zkaznkovi.'); 
        } 
 
        if (Tools::isSubmit('dm_comgate_refund')) 
        { 
            if (Tools::getValue('dm_comgate_refund') == 'OK') 
            { 
                $this->context->controller->confirmations[] = ''.$this->l('Poadovan stka byla vrcena!').''; 
            } 
            elseif (Tools::getValue('dm_comgate_refund') == 'ERROR') 
            { 
                $this->context->controller->errors[] = $this->l('Nepodailo se vrtit stku zkaznkovi.'); 
            } 
            elseif (Tools::getValue('dm_comgate_refund') == 'UNPAID') 
            { 
                $this->context->controller->errors[] = $this->l('Nelze provst refund, platba se nenachz ve stavu zaplaceno.'); 
            } 
        } 
 
        // Refundace objednvky 
        if (Tools::isSubmit('SubmitDmComgateRefund')) 
        { 
            $limit_amount = (float)Tools::getValue('Submit_dm_comgate_total_pay'); 
            $amount_to_refund = (float)str_replace(array(',', ' '), array('.', ''), Tools::getValue('dm_comgate_amount_to_refund')); 
            $id_order = (int)Tools::getValue('dm_comgate_id_order'); 
            $id_currency = (int)Tools::getValue('dm_comgate_id_currency'); 
 
            if($amount_to_refund > $limit_amount) 
            { 
                $this->context->controller->errors[] = $this->l('Nelze vrtit vy stku ne byla pijata.'); 
            } 
            elseif($amount_to_refund == 0) 
            { 
                $this->context->controller->errors[] = $this->l('Uvete stku vy ne 0,-.'); 
            } 
            else 
            { 
                // Zjiuji produkn mod nebo TEST 
                $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
                // Volm objekt pro API ComGate 
                $comgate = new DmComgateApi($this, $test_mode); 
                // Ovm platbu 
                $exist_payment = $comgate->getPaymentForOrder($id_order); 
 
                // Platba existuje, ve jsem nael 
                if($exist_payment) 
                { 
                    // Zjiuji na brn stav 
                    $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']); 
 
                    if($cg_status['status'] == 'PAID') 
                    { 
                        // Data pro refund 
                        $data_for_refund = $comgate->getDataForRefund( 
                            $id_order, 
                            (string)$exist_payment['id_comgate'], 
                            $amount_to_refund, 
                            $id_currency 
                        ); 
 
                        $cg_refund = $comgate->refundPayment($data_for_refund); 
 
                        // Zaloit refundaci se zdailo 
                        if(isset($cg_refund['code']) AND $cg_refund['code'] === '0') 
                        { 
                            $order = new Order($id_order); 
 
                            // Ppadn zmna stavu po refundaci v ppad vratky cel stky 
                            $id_order_state_refund = (int)Configuration::get('DM_COMGATE_STATUS_REFUND'); 
 
                            if($id_order_state_refund > 0 AND (int)$id_order_state_refund != (int)$order->current_state AND $amount_to_refund == $limit_amount) 
                            { 
                                $history = new OrderHistory(); 
                                $history->id_order = (int)$id_order; 
                                $history->changeIdOrderState((int)$id_order_state_refund, $order, true); 
                                $history->addWithemail(); 
                            } 
 
                            ////////////////////////////////////////////////////////////////////////// 
 
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'info', ''.$this->l('Vratka na stku').' '.Tools::displayPrice($amount_to_refund, $id_currency).' '.$this->l('byla provedena!').''); 
 
                            $order_invoice = null; 
 
                            // Zjiuji vytvoenou fakturu 
                            $invoice = Db::getInstance()->getRow(' 
                                SELECT * 
                                FROM `'._DB_PREFIX_.'order_invoice` 
                                WHERE `id_order` = '.(int)$id_order.' 
                            '); 
 
                            if($invoice) 
                            { 
                                $order_invoice = new OrderInvoice($invoice['id_order_invoice']); 
                            } 
 
                            if(version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
                            { 
                                $order->addOrderPayment((float)($amount_to_refund * -1), $this->l('ComGate refundace'), (string)$exist_payment['id_comgate'], null, null, $order_invoice); 
                            } 
                            else 
                            { 
                                $order_data = new Order($id_order); 
 
                                // Zapu platbu s mnusovm stavem 
                                Db::getInstance()->execute(' 
                                    INSERT INTO `'._DB_PREFIX_.'order_payment` (`order_reference`, `id_currency`, `amount`, `payment_method`, `conversion_rate`, `transaction_id`, `card_number`, `card_brand`, `card_expiration`, `card_holder`, `date_add`) 
                                    VALUES(\''.(string)pSQL($order_data->reference).'\', '.(int)$id_currency.', '.(float)($amount_to_refund * -1).', \'ComGate refundace\', 1, \''.(string)$exist_payment['id_comgate'].'\', \'\', \'\', \'\', \'\', NOW() ) 
                                '); 
 
                                $last_id = Db::getInstance()->Insert_ID(); 
 
                                // Zapu invoice_payment 
                                if($invoice) 
                                { 
                                    Db::getInstance()->execute(' 
                                        INSERT INTO `'._DB_PREFIX_.'order_invoice_payment` (`id_order_invoice`, `id_order_payment`, `id_order`) 
                                        VALUES('.(int)$order_invoice->id.', '.(int)$last_id.', '.(int)$id_order.') 
                                    '); 
                                } 
                            } 
 
                            //$this->context->controller->confirmations[] = ''.$this->l('Vratka na stku').' '.Tools::displayPrice($amount_to_refund, $id_currency).' '.$this->l('byla provedena!').''; 
 
                            $link_order = $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => $id_order, 'vieworder' => 1, 'dm_comgate_refund' => 'OK']) . '&id_order='.$id_order.'&vieworder&dm_comgate_refund=OK'; 
 
                            Tools::redirectAdmin($link_order); 
                        } 
                        else 
                        { 
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', $this->l('Refundace platby se nezdaila!')); 
 
                            if(isset($cg_refund['code']) AND isset($cg_refund['message'])) 
                            { 
                                $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', ''.$cg_refund['code'].' - '.$cg_refund['message'].''); 
                            } 
 
                            //$this->context->controller->errors[] = $this->l('Nepodailo se vrtit stku zkaznkovi.'); 
 
                            $link_order = $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => $id_order, 'vieworder' => 1, 'dm_comgate_refund' => 'ERROR']) . '&id_order='.$id_order.'&vieworder&dm_comgate_refund=ERROR'; 
 
                            Tools::redirectAdmin($link_order); 
                        } 
                    } 
                    else 
                    { 
                        //$this->context->controller->errors[] = $this->l('Nelze provst refund, platba se nenachz ve stavu zaplaceno.'); 
 
                        $link_order = $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => $id_order, 'vieworder' => 1, 'dm_comgate_refund' => 'UNPAID']) . '&id_order='.$id_order.'&vieworder&dm_comgate_refund=UNPAID'; 
 
                        Tools::redirectAdmin($link_order); 
                    } 
                } 
            } 
        } 
 
        if (Tools::isSubmit('SubmitDmComgateCapturePreAuth')) 
        { 
            // Zjiuji produkn mod nebo TEST 
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
            // Volm objekt pro API ComGate 
            $comgate = new DmComgateApi($this, $test_mode); 
 
            $limit_amount = (float)Tools::getValue('Submit_dm_comgate_total_pay'); 
            $amount_to_capture = (float)str_replace(array(',', ' '), array('.', ''), Tools::getValue('dm_comgate_amount_to_capture')); 
            $id_order = (int)Tools::getValue('dm_comgate_id_order'); 
            $id_currency = (int)Tools::getValue('dm_comgate_preauth_id_currency'); 
 
            $limit_amount_valid = $comgate->formatPriceForPayment($limit_amount, $id_currency); 
            $amount_to_capture_valid = $comgate->formatPriceForPayment($amount_to_capture, $id_currency); 
 
            if($amount_to_capture_valid > $limit_amount_valid) 
            { 
                $this->context->controller->errors[] = $this->l('Nelze sthnout vy stku ne je blokovna na tu klienta.'); 
            } 
            elseif($amount_to_capture_valid == 0) 
            { 
                $this->context->controller->errors[] = $this->l('Uvete stku vy ne 0,-.'); 
            } 
            else 
            { 
                // Ovm platbu 
                $exist_payment = $comgate->getPaymentForOrder($id_order); 
 
                // Platba existuje, ve jsem nael 
                if($exist_payment) 
                { 
                    // Zjiuji na brn stav 
                    $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']); 
 
                    if($cg_status['status'] == 'AUTHORIZED') 
                    { 
                        $cg_capture_pre_auth = $comgate->getCapturePreAuthPayment((string)$exist_payment['id_comgate'], $amount_to_capture, new Currency($id_currency)); 
 
                        // Staen prostedk se zdailo 
                        if(isset($cg_capture_pre_auth['code']) AND $cg_capture_pre_auth['code'] === '0') 
                        { 
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'info', ''.$this->l('Staen blokovanch prostedk ve vi').' '.Tools::displayPrice($amount_to_capture, $id_currency).' '.$this->l('bylo provedeno!').''); 
 
                            $comgate->notifyPayment($id_order, (string)$exist_payment['id_comgate']); 
 
                            $this->context->controller->confirmations[] = ''.$this->l('Staen blokovanch prostedk ve vi').' '.Tools::displayPrice($amount_to_capture, $id_currency).' '.$this->l('bylo provedeno!').''; 
                        } 
                        else 
                        { 
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', $this->l('Staen blokovanch prostedk se nezdailo!')); 
 
                            if(isset($cg_capture_pre_auth['code']) AND isset($cg_capture_pre_auth['message'])) 
                            { 
                                $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', ''.$cg_capture_pre_auth['code'].' - '.$cg_capture_pre_auth['message'].''); 
                            } 
 
                            $this->context->controller->errors[] = $this->l('Staen blokovanch prostedk se nezdailo!'); 
                        } 
                    } 
                    else 
                    { 
                        $this->context->controller->errors[] = $this->l('Nelze provst staen blokovanch prostedk, platba se nenachz ve stavu autorizovno.'); 
                    } 
                } 
            } 
        } 
 
        if (Tools::isSubmit('SubmitDmComgateCancelPreAuth')) 
        { 
            // Zjiuji produkn mod nebo TEST 
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false ); 
            // Volm objekt pro API ComGate 
            $comgate = new DmComgateApi($this, $test_mode); 
 
            $id_order = (int)Tools::getValue('dm_comgate_id_order'); 
 
            // Ovm platbu 
            $exist_payment = $comgate->getPaymentForOrder($id_order); 
 
            // Platba existuje, ve jsem nael 
            if($exist_payment) 
            { 
                // Zjiuji na brn stav 
                $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']); 
 
                if($cg_status['status'] == 'AUTHORIZED') 
                { 
                    $cg_cancel_pre_auth = $comgate->getCancelPreAuthPayment((string)$exist_payment['id_comgate']); 
 
                    // Zruen prostedk se zdailo 
                    if(isset($cg_cancel_pre_auth['code']) AND $cg_cancel_pre_auth['code'] === '0') 
                    { 
                        $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'info', ''.$this->l('Zruen blokovanch prostedk bylo provedeno.').''); 
 
                        $comgate->notifyPayment($id_order, (string)$exist_payment['id_comgate']); 
                         
                        $this->context->controller->confirmations[] = ''.$this->l('Zruen blokovanch prostedk bylo provedeno.').''; 
                    } 
                    else 
                    { 
                        $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', $this->l('Zruen blokovanch prostedk se nezdailo!')); 
                        $this->context->controller->errors[] = $this->l('Zruen blokovanch prostedk se nezdailo!'); 
                    } 
                } 
                else 
                { 
                    $this->context->controller->errors[] = $this->l('Nelze provst zruen blokovanch prostedk, platba se nenachz ve stavu autorizovno.'); 
                } 
            } 
        } 
 
        return; 
    } 
 
    // Lev sloupec 
    public function hookDisplayLeftColumn() 
    { 
        return $this->getPaymentsLogosInColumn(); 
    } 
 
    // Prav sloupec 
    public function hookDisplayRightColumn() 
    { 
        return $this->getPaymentsLogosInColumn(); 
    } 
 
    // Loga v nkupnm koku a v detailu produktu 
    public function hookDisplayReassurance() 
    { 
        $fields_allowed = explode(',', Configuration::get('DM_COMGATE_LOGOS_PAYMENT_CART')); 
 
        if(in_array('product', $fields_allowed) AND $this->context->controller->php_self == 'product') 
        { 
            $shop_url = Context::getContext()->shop->getBaseURL(true); 
 
            $this->smarty->assign( 
                array( 
                    'shop_url' => $shop_url, 
                ) 
            ); 
 
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
            { 
                return $this->display(__FILE__, 'dm_comgate_payment_logos_reaseurance_product.tpl'); 
            } 
            else 
            { 
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_payment_logos_reaseurance_product.tpl'); 
            } 
        } 
        elseif(in_array('cart', $fields_allowed) AND ($this->context->controller->php_self == 'cart' OR $this->context->controller->php_self == 'order')) 
        { 
            $shop_url = Context::getContext()->shop->getBaseURL(true); 
 
            $this->smarty->assign( 
                array( 
                    'shop_url' => $shop_url, 
                ) 
            ); 
 
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
            { 
                return $this->display(__FILE__, 'dm_comgate_payment_logos_reaseurance_cart.tpl'); 
            } 
            else 
            { 
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_payment_logos_reaseurance_cart.tpl'); 
            } 
        } 
    } 
 
    public function hookDisplayAdminOrder() 
    { 
        if(version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
        { 
            $id_order = (int)Tools::getValue('id_order'); 
 
            if($id_order) 
            { 
                $order_data = new Order((int)$id_order); 
 
                if($order_data->module == 'dm_comgate') 
                { 
                    $info = Db::getInstance()->executeS(' 
                        SELECT * 
                        FROM `'._DB_PREFIX_.'dm_comgate_log` 
                        WHERE `id_order` = '.$id_order.' 
                        ORDER BY `id_log` DESC 
                    '); 
 
                    $payment = Db::getInstance()->getRow(' 
                        SELECT * 
                        FROM `'._DB_PREFIX_.'dm_comgate_payment` 
                        WHERE `id_order` = '.$id_order.' 
                    '); 
 
                    $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL'); 
 
                    if($type_url_link == 'original') 
                    { 
                        // Platba nen zaloena pouiji odkaz pes eshop / kontroler, kde se autoamticky paltba zalo 
                        if(!$payment) 
                        { 
                            $url_payment_link = $this->context->link->getModuleLink( 
                                'dm_comgate', 
                                'cgpayment', 
                                array( 
                                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference), 
                                ), 
                                true, 
                                (int)$order_data->id_lang, 
                                (int)$order_data->id_shop 
                            ); 
                        } 
                        else 
                        { 
                            $url_payment_link = 'https://payments.comgate.cz/'.$payment['id_comgate'].''; 
                        } 
 
                    } 
                    else 
                    { 
                        $url_payment_link = $this->context->link->getModuleLink( 
                            'dm_comgate', 
                            'cgpayment', 
                            array( 
                                'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                                'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference), 
                            ), 
                            true, 
                            (int)$order_data->id_lang, 
                            (int)$order_data->id_shop 
                        );  
                    } 
 
                    if ($payment) 
                    { 
                        $payment_in_order = Db::getInstance()->getRow(' 
                            SELECT `id_currency`, SUM(`amount`) as `total` 
                            FROM `'._DB_PREFIX_.'order_payment` 
                            WHERE `transaction_id` = \''.$payment['id_comgate'].'\' 
                        '); 
                    } 
 
                    $currency = new Currency($order_data->id_currency); 
 
                    if(isset($currency->precision)) 
                    { 
                        if(_PS_PRICE_DISPLAY_PRECISION_ < $currency->precision) 
                        { 
                            $c_decimals = (int)_PS_PRICE_DISPLAY_PRECISION_; 
                        } 
                        else 
                        { 
                            $c_decimals = (int)$currency->precision; 
                        } 
                    } 
                    else 
                    { 
                        $c_decimals = (int)$currency->decimals * _PS_PRICE_DISPLAY_PRECISION_; 
                    } 
 
                    $this->context->smarty->assign(array( 
                        'dm_comgate_id_order' => $id_order, 
                        'dm_comgate_id_currency' => ($payment ? (int)$payment_in_order['id_currency'] : false), 
                        'dm_comgate_decimals' => (int)$c_decimals, 
                        'dm_comgate_info' => $info, 
                        'dm_comgate_payment' => $payment, 
                        'dm_comgate_payment_link' => $url_payment_link, 
                        'dm_comgate_mail_allowed' => (Configuration::get('DM_COMGATE_MAIL_ACTIVE') == 1 ? true : false ), 
                        'dm_comgate_refund_data' => (($payment AND $payment_in_order['total'] != null AND $payment['state'] == 'PAID') ? $payment_in_order : false), 
                        'dm_comgate_preauth_data' => (($payment AND $payment['state'] == 'AUTHORIZED') ? $payment_in_order : false), 
                        'dm_comgate_currency_sign' => $currency->sign, 
                        'dm_comgate_order_total_paid' => $order_data->total_paid, 
                        'dm_comgate_preauth_id_currency' => (int)$order_data->id_currency, 
                    )); 
 
                    if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
                    { 
                        return $this->display(__FILE__, 'dm_comgate_detail_order.tpl'); 
                    } 
                    else 
                    { 
                        return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_detail_order.tpl'); 
                    } 
                } 
            } 
        } 
 
        return; 
    } 
 
    public function hookDisplayAdminOrderMain($params) 
    { 
        if(version_compare(_PS_VERSION_, "1.7.7.0", ">=")) 
        { 
            $id_order = (int)$params['id_order']; 
 
            if($id_order) 
            { 
                $order_data = new Order((int)$id_order); 
 
                if($order_data->module == 'dm_comgate') 
                { 
                    $info = Db::getInstance()->executeS(' 
                        SELECT * 
                        FROM `'._DB_PREFIX_.'dm_comgate_log` 
                        WHERE `id_order` = '.$id_order.' 
                        ORDER BY `id_log` DESC 
                    '); 
 
                    $payment = Db::getInstance()->getRow(' 
                        SELECT * 
                        FROM `'._DB_PREFIX_.'dm_comgate_payment` 
                        WHERE `id_order` = '.$id_order.' 
                    '); 
 
                    $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL'); 
 
                    if($type_url_link == 'original') 
                    { 
                        // Platba nen zaloena pouiji odkaz pes eshop / kontroler, kde se automaticky platba zalo 
                        if(!$payment) 
                        { 
                            $url_payment_link = $this->context->link->getModuleLink( 
                                'dm_comgate', 
                                'cgpayment', 
                                array( 
                                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference), 
                                ), 
                                true, 
                                (int)$order_data->id_lang, 
                                (int)$order_data->id_shop 
                            ); 
                        } 
                        else 
                        { 
                            $url_payment_link = 'https://payments.comgate.cz/'.$payment['id_comgate'].''; 
                        } 
 
                    } 
                    else 
                    { 
                        $url_payment_link = $this->context->link->getModuleLink( 
                            'dm_comgate', 
                            'cgpayment', 
                            array( 
                                'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                                'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference), 
                            ), 
                            true, 
                            (int)$order_data->id_lang, 
                            (int)$order_data->id_shop 
                        );  
                    } 
 
                    $payment_in_order = false; 
 
                    if ($payment) 
                    { 
                        $payment_in_order = Db::getInstance()->getRow(' 
                            SELECT `id_currency`, SUM(`amount`) as `total` 
                            FROM `'._DB_PREFIX_.'order_payment` 
                            WHERE `transaction_id` = \''.$payment['id_comgate'].'\' 
                        '); 
                    } 
                     
 
                    $currency = new Currency($order_data->id_currency); 
 
                    if(isset($currency->precision)) 
                    { 
                        if(_PS_PRICE_DISPLAY_PRECISION_ < $currency->precision) 
                        { 
                            $c_decimals = (int)_PS_PRICE_DISPLAY_PRECISION_; 
                        } 
                        else 
                        { 
                            $c_decimals = (int)$currency->precision; 
                        } 
                    } 
                    else 
                    { 
                        $c_decimals = (int)$currency->decimals * _PS_PRICE_DISPLAY_PRECISION_; 
                    } 
 
                    $this->context->smarty->assign(array( 
                        'dm_comgate_id_order' => $id_order, 
                        'dm_comgate_id_currency' => ($payment_in_order ? (int)$payment_in_order['id_currency'] : false), 
                        'dm_comgate_decimals' => (int)$c_decimals, 
                        'dm_comgate_info' => $info, 
                        'dm_comgate_payment' => $payment, 
                        'dm_comgate_payment_link' => $url_payment_link, 
                        'dm_comgate_mail_allowed' => (Configuration::get('DM_COMGATE_MAIL_ACTIVE') == 1 ? true : false ), 
                        'dm_comgate_refund_data' => (($payment AND $payment_in_order['total'] != null AND $payment['state'] == 'PAID') ? $payment_in_order : false), 
                        'dm_comgate_preauth_data' => (($payment AND $payment['state'] == 'AUTHORIZED') ? $payment_in_order : false), 
                        'dm_comgate_currency_sign' => $currency->sign, 
                        'dm_comgate_order_total_paid' => $order_data->total_paid, 
                        'dm_comgate_preauth_id_currency' => (int)$order_data->id_currency, 
                    )); 
 
                    //return $this->display(__FILE__, 'dm_comgate_detail_order_177.tpl'); 
                    return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_detail_order_177.tpl'); 
                } 
            } 
        } 
 
        return; 
    } 
 
    // Loga ve sloupcch 
    public function getPaymentsLogosInColumn() 
    { 
        $fields_allowed = explode(',', Configuration::get('DM_COMGATE_LOGOS_PAYMENT_CART')); 
 
        if(in_array('column', $fields_allowed)) 
        { 
            $shop_url = Context::getContext()->shop->getBaseURL(true); 
 
            $this->smarty->assign( 
                array( 
                    'shop_url' => $shop_url, 
                ) 
            ); 
 
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<")) 
            { 
                return $this->display(__FILE__, 'dm_comgate_payment_logos_column.tpl'); 
            } 
            else 
            { 
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_payment_logos_column.tpl'); 
            } 
        } 
        else 
        { 
            return; 
        } 
    } 
 
    // Zasln mailu zkaznkovi 
    public function sendMailPayment($id_order) 
    { 
        if(Configuration::get('DM_COMGATE_MAIL_ACTIVE') != 1) 
        { 
            return true; 
        } 
 
        $context = Context::getContext(); 
        $order = new Order((int)$id_order); 
        $customer = new Customer((int)$order->id_customer); 
        $currency = new Currency($order->id_currency); 
 
        $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL'); 
 
        if($type_url_link == 'original') 
        { 
            $payment_number = Db::getInstance()->getValue(' 
                SELECT `id_comgate` 
                FROM `'._DB_PREFIX_.'dm_comgate_payment` 
                WHERE `id_order` = '.(int)$id_order.' 
            '); 
 
            // Pokud nen platba, pouiji odkaz na eshop / kontroler, platba se zalo po navtven odkazu 
            if(!$payment_number) 
            { 
                $url_payment = $context->link->getModuleLink( 
                    'dm_comgate', 
                    'cgpayment', 
                    array( 
                        'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                        'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference), 
                    ), 
                    true, 
                    (int)$order->id_lang, 
                    (int)$order->id_shop 
                ); 
            } 
            else 
            { 
                $url_payment = 'https://payments.comgate.cz/'.$payment_number.''; 
            } 
        } 
        else 
        { 
            $url_payment = $context->link->getModuleLink( 
                'dm_comgate', 
                'cgpayment', 
                array( 
                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference), 
                ), 
                true, 
                (int)$order->id_lang, 
                (int)$order->id_shop 
            ); 
        } 
 
        $id_lang = $order->id_lang; 
        $templates = 'dm_comgate_mail_customer'; 
        $subject_mail = Configuration::get('DM_COMGATE_SUBJECT_MAIL_'.$id_lang.''); 
        $email = $customer->email; 
        $to_name = ''.$customer->firstname.' '.$customer->lastname.''; 
 
        $template_vars = array( 
            '{lastname}' => $customer->lastname, 
            '{firstname}' => $customer->firstname, 
            '{id_order}' => $order->id, 
            '{order_reference}' => $order->reference, 
            '{total_payment}' => Tools::displayPrice($order->total_paid, $currency, false), 
            '{url_payment}' => $url_payment, 
        ); 
 
        Mail::Send( 
            $id_lang, 
            $templates, 
            $subject_mail, 
            $template_vars, 
            $email, 
            $to_name, 
            null, 
            null, 
            null, 
            null, 
            dirname(__FILE__).'/mails/' 
        ); 
 
        return true; 
    } 
 
 
    // Zasln mailu obchodnkovi 
    public function sendMailConfirmationPaymentEmployees($id_order) 
    { 
        if(Configuration::get('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE') != 1) 
        { 
            return true; 
        } 
 
        $order = new Order((int)$id_order); 
        $currency = new Currency($order->id_currency); 
        $id_lang = $order->id_lang; 
        $templates = 'dm_comgate_mail_employees'; 
        $subject_mail = $this->l('Platba byla spn potvrzena'); 
        $email = Configuration::get('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT'); 
        $to_name = $email; 
 
        $template_vars = array( 
            '{id_order}' => $order->id, 
            '{order_reference}' => $order->reference, 
            '{total_payment}' => Tools::displayPrice($order->total_paid, $currency, false), 
        ); 
 
        Mail::Send( 
            $id_lang, 
            $templates, 
            $subject_mail, 
            $template_vars, 
            $email, 
            $to_name, 
            null, 
            null, 
            null, 
            null, 
            dirname(__FILE__).'/mails/' 
        ); 
 
        return true; 
    } 
 
    public function hookActionGetExtraMailTemplateVars($params) 
    { 
        if (isset($params['template_vars']['{id_order}'])) 
        { 
            $id_order = (int)$params['template_vars']['{id_order}']; 
 
            if($id_order != 0) 
            { 
                $order = new Order((int)$id_order); 
 
                if($order->module == 'dm_comgate') 
                { 
                    $context = Context::getContext(); 
 
                    $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL'); 
 
                    if($type_url_link == 'original') 
                    { 
                        $payment_number = Db::getInstance()->getValue(' 
                            SELECT `id_comgate` 
                            FROM `'._DB_PREFIX_.'dm_comgate_payment` 
                            WHERE `id_order` = '.(int)$id_order.' 
                        '); 
 
                        // Pokud nen platba, pouiji odkaz na eshop / kontroler, platba se zalo po navtven odkazu 
                        if(!$payment_number) 
                        { 
                            $url_payment = $context->link->getModuleLink( 
                                'dm_comgate', 
                                'cgpayment', 
                                array( 
                                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference), 
                                ), 
                                true, 
                                (int)$order->id_lang, 
                                (int)$order->id_shop 
                            ); 
                        } 
 
                        $url_payment = 'https://payments.comgate.cz/'.$payment_number.''; 
                    } 
                    else 
                    { 
                        $url_payment = $context->link->getModuleLink( 
                            'dm_comgate', 
                            'cgpayment', 
                            array( 
                                'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10),  
                                'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference), 
                            ), 
                            true, 
                            (int)$order->id_lang, 
                            (int)$order->id_shop 
                        ); 
                    } 
 
                    $params['extra_template_vars']['{dm_comgate_url_payment}'] = $url_payment; 
                } 
            } 
        } 
 
        return true; 
    } 
 
    public function isDisallowedProductInCart($cart) 
    { 
        // vylouen ID produkt 
        $excluded_ids = $this->getExcludedIdProduct(); 
        $product_in_cart = $cart->getProducts(); 
 
        foreach ($product_in_cart AS $product) 
        { 
            if(in_array((int)$product['id_product'], $excluded_ids)) 
            { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    // Vrac ID produkt na kter nelze platit kartou 
    public function getExcludedIdProduct() 
    { 
        $ids_products = Db::getInstance()->executeS(' 
            SELECT ps.`id_product` 
            FROM `'._DB_PREFIX_.'product_shop` ps 
            WHERE ps.`dm_comgate_active` = 0 
                AND ps.`id_shop` = '.(int)$this->context->shop->id.' 
        '); 
 
        $return = array(); 
 
        foreach($ids_products as $item_id) 
        { 
            $return[] = (int)$item_id['id_product']; 
        } 
 
        return $return; 
    } 
} 
 ?>

Did this file decode correctly?

Original Code

<?php

/*
 * 
 * @author David Mrzek
 * @contact <[email protected]>
 * @since 2015
 * @copyright Copyright (c) 2015 - 2023 David Mrzek
 *
 * @link https://prestashop.valasinec.cz/content/2-obchodni-podminky
 * @license https://prestashop.valasinec.cz/content/3-licencni-podminky
 *
 * Tento kd je chrnn autorskm prvem a nesm bt en bez pedchozho psemnho souhlasu autora.
 * V rmci licennch podmnek mete tento kd pouvat a upravovat pouze pro vlastn poteby.
 * Pro jakkoli dotazy nebo dal informace ns kontaktujte.
 *
 */
 
/*
use Valasinec\Module\Dm_Comgate\Install\DmInstaller;
use Valasinec\Module\Dm_Comgate\Configuration\DmConfiguration;
use Valasinec\Module\Dm_Comgate\Update\DmUpdate;
use Valasinec\Module\Dm_Comgate\Tools\DmTools;
use Valasinec\Module\Dm_Comgate\Tools\DmToolsTime;
use Valasinec\Module\Dm_Comgate\Tools\DmToolsXml;
use Valasinec\Module\Dm_Comgate\Helper\DmBuilder;
use Valasinec\Module\Dm_Comgate\Log\DmLogger;
*/

if (!defined('_PS_VERSION_'))
	exit;

use PrestaShop\PrestaShop\Core\Payment\PaymentOption;

require_once(dirname(__FILE__).'/classes/DmComgateFormBuilder.php');
require_once(dirname(__FILE__).'/classes/DmComgateInstall.php');
require_once(dirname(__FILE__).'/classes/DmComgateUpdate.php');
require_once(dirname(__FILE__).'/classes/DmComgateApi.php');

// Autoload (po vygenerovn composerem)
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}

class dm_comgate extends PaymentModule
{
    const INSTALL_SQL_FILE = 'install.sql';
    const UNINSTALL_SQL_FILE = 'uninstall.sql';

    public $default_lang;
    public $module_hooks = array(
        'paymentOptions',
        'displayPaymentReturn',
        'displayOrderDetail',
        'displayBackOfficeHeader',
        'displayLeftColumn',
        'displayRightColumn',
        'displayReassurance',
        'displayAdminOrder',
        'displayAdminOrderMain',
        'displayDmPaymentButtonHistory',
        'actionGetExtraMailTemplateVars',
    );
    public $update;
    public $install;
    private $_html = '';
    private $_postErrors = array();

    public $author_email;
    public $author_website;
    public $path;

    function __construct()
    {
        // Zkladn nastaven
        $this->name = 'dm_comgate';
        $this->tab = 'payments_gateways';
        $this->version = '3.2.0';
        $this->author = 'David Mrzek';
        $this->author_email = '[email protected]';
        $this->author_website = 'https://prestashop.valasinec.cz';
        $this->need_instance = 1;
        $this->controllers = array('validation', 'cgpayment');

        $this->currencies = true;
        $this->currencies_mode = 'checkbox';
        
        $this->path = $this->_path;
        
        $this->bootstrap = true;
        parent::__construct();
        
        $this->displayName = $this->l('COMGATE - platebn brna');
        $this->description = $this->l('Napojen na platebn brnu COMGATE.');
        $this->confirmUninstall = $this->l('Opravdu chcete odinstalovat tento modul?');
        $this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);        
        
        $this->default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

        // Zjitn aktualizace
        $this->update = new DmComgateUpdate($this->name, $this->version);

        if($this->update->version_data)
        {
            $this->warning = $this->l('Je k dispozici nov verze modulu').' ('.$this->update->version_data['version'].').';
        }
    }
        
    // Metoda pro instalaci modulu
    public function install()
    {        
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        // Install tda
        $this->install = new DmComgateInstall($this);

        if (!parent::install()
            || !$this->install->registerHookForModule()
            || !$this->install->changeDatabase(self::INSTALL_SQL_FILE)
           )
        {
            return false;
        }

        Configuration::updateValue(''.$this->name.'_CHECK_UPDATE', 1);

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        // Stavy objednvky
        Configuration::updateValue('DM_COMGATE_STATUS_PAID', 2);
        Configuration::updateValue('DM_COMGATE_STATUS_PAID_NOT_STOCK', Configuration::get('PS_OS_OUTOFSTOCK_PAID'));
        Configuration::updateValue('DM_COMGATE_STATUS_ERROR', 8);
        Configuration::updateValue('DM_COMGATE_STATUS_REFUND', 0);
        
        // Pidn novho stavu objednvek - COMGATE Objednvka vytvoena
        $id_order_state_exist_create = Db::getInstance()->getValue('
            SELECT `id_order_state`
            FROM `'._DB_PREFIX_.'order_state_lang`
            WHERE `name` = "COMGATE - ek se na proveden platby"
        ');

        if($id_order_state_exist_create)
        {
            Configuration::updateValue('DM_COMGATE_STATUS_CREATED', $id_order_state_exist_create);
        }
        else
        {
            $id_new_state_create = $this->install->createNewOrderState('COMGATE - ek se na proveden platby', '#3ED2F0', 1, false, false, false, false);
            Configuration::updateValue('DM_COMGATE_STATUS_CREATED', $id_new_state_create);    
        }

        // Pidn novho stavu objednvek - COMGATE Objednvka peruena
        $id_order_state_exist_create = Db::getInstance()->getValue('
            SELECT `id_order_state`
            FROM `'._DB_PREFIX_.'order_state_lang`
            WHERE `name` = "COMGATE - platba peruena"
        ');
        
        if($id_order_state_exist_create)
        {
            Configuration::updateValue('DM_COMGATE_STATUS_CANCELED', $id_order_state_exist_create);
        }
        else
        {
            $id_new_state_create = $this->install->createNewOrderState('COMGATE - platba peruena', '#FF5B04', 1, false, false, false, false);
            Configuration::updateValue('DM_COMGATE_STATUS_CANCELED', $id_new_state_create);    
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        $id_order_state_exist_pre_authorized = Db::getInstance()->getValue('
            SELECT `id_order_state`
            FROM `'._DB_PREFIX_.'order_state_lang`
            WHERE `name` = "COMGATE - autorizace platby potvrzena"
        ');

        if($id_order_state_exist_pre_authorized)
        {
            Configuration::updateValue('DM_COMGATE_STATUS_PRE_AUTHORIZED', $id_order_state_exist_pre_authorized);
        }
        else
        {
            $id_new_state_pre_authorized = $this->install->createNewOrderState('COMGATE - autorizace platby potvrzena', '#00BB00', 1, false, false, false, false);
            Configuration::updateValue('DM_COMGATE_STATUS_PRE_AUTHORIZED', $id_new_state_pre_authorized);    
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        // Pstupy
        Configuration::updateValue('DM_COMGATE_TEST_ACTIVE', 1);
        Configuration::updateValue('DM_COMGATE_TEST_IP_ADDRESS_CLIENT', '');
        Configuration::updateValue('DM_COMGATE_MERCHANT', '');
        Configuration::updateValue('DM_COMGATE_SECRET', '');

        // Typy plateb
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED', 'CARD_ALL,BANK_ALL');
        Configuration::updateValue('DM_COMGATE_REPEAT_PAYMENT_ACTIVE', 1);
        Configuration::updateValue('DM_COMGATE_TYPE_SHOW_PAYMENT', 'offline');
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_GROUP', 'CARD_ALL,BANK_ALL');
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_CARD', '');
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_BANK', '');
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_LATER', '');
        Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_PART', '');

        // Identifikteor a loga
        Configuration::updateValue('DM_COMGATE_PRE_AUTHORIZED', 2);
        Configuration::updateValue('DM_COMGATE_IDENT_PAYMENT', 1);
        Configuration::updateValue('DM_COMGATE_LOGOS_PAYMENT_CART', '');

        // Logovn
        Configuration::updateValue('DM_COMGATE_LOG_ACTIVE', 1);
        Configuration::updateValue('DM_COMGATE_LOG_DAYS', 60);

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        // Mailov servis
        Configuration::updateValue('DM_COMGATE_MAIL_ACTIVE', 2);
        Configuration::updateValue('DM_COMGATE_TYPE_CUSTOMER_MAIL', 'eshop');
        Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE', 2);
        Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT', '');
        Configuration::updateValue('DM_COMGATE_MAIL_POSITION_ALLOWED', 'return');

        // ty
        $currencies = Currency::getCurrencies();

        foreach ($currencies as $itemCurrency)
        {
            Configuration::updateValue('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].'', '');
        }

        $languages = Language::getLanguages(false);

        foreach($languages as $language)
        {
            $subject = 'Vae platba za objednvku';

            if($language['iso_code'] == 'sk')
            {
                $subject = 'Vaa platba za objednvku';
            }
            elseif($language['iso_code'] == 'en')
            {
                $subject = 'Your payment for the order';
            }

            Configuration::updateValue('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].'', $subject);
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
              
        return true;
    }

    // Metoda pro odinstalace modulu
    public function uninstall()
    { 
        if (Shop::isFeatureActive())
        {
            Shop::setContext(Shop::CONTEXT_ALL);
        }

        // Install tda
        $this->install = new DmComgateInstall($this);
            
        if (!parent::uninstall()
            || !$this->install->unregisterHookForModule()
            || !$this->install->changeDatabase(self::UNINSTALL_SQL_FILE)
           )
        {
            return false;
        }

        Configuration::deleteByName(''.$this->name.'_CHECK_UPDATE');

        // Stavy objednvky
        Configuration::deleteByName('DM_COMGATE_STATUS_PRE_AUTHORIZED');
        Configuration::deleteByName('DM_COMGATE_STATUS_PAID');
        Configuration::deleteByName('DM_COMGATE_STATUS_PAID_NOT_STOCK');
        Configuration::deleteByName('DM_COMGATE_STATUS_ERROR');
        Configuration::deleteByName('DM_COMGATE_STATUS_CREATED');
        Configuration::deleteByName('DM_COMGATE_STATUS_CANCELED');
        Configuration::deleteByName('DM_COMGATE_STATUS_REFUND');

        // Pstupy
        Configuration::deleteByName('DM_COMGATE_TEST_ACTIVE');
        Configuration::deleteByName('DM_COMGATE_TEST_IP_ADDRESS_CLIENT');
        Configuration::deleteByName('DM_COMGATE_MERCHANT');
        Configuration::deleteByName('DM_COMGATE_SECRET');

        // Typy plateb
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_ALLOWED');
        Configuration::deleteByName('DM_COMGATE_REPEAT_PAYMENT_ACTIVE');

        Configuration::deleteByName('DM_COMGATE_TYPE_SHOW_PAYMENT');
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_GROUP');
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_CARD');
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_BANK');
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_LATER');
        Configuration::deleteByName('DM_COMGATE_SHOW_PAYMENT_PART');

        // Identifikteor a loga
        Configuration::deleteByName('DM_COMGATE_PRE_AUTHORIZED');
        Configuration::deleteByName('DM_COMGATE_IDENT_PAYMENT');
        Configuration::deleteByName('DM_COMGATE_LOGOS_PAYMENT_CART');

        // Logovn
        Configuration::deleteByName('DM_COMGATE_LOG_ACTIVE');
        Configuration::deleteByName('DM_COMGATE_LOG_DAYS');

        // Mailov servis
        Configuration::deleteByName('DM_COMGATE_MAIL_ACTIVE');
        Configuration::deleteByName('DM_COMGATE_TYPE_CUSTOMER_MAIL');
        Configuration::deleteByName('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE');
        Configuration::deleteByName('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT');
        Configuration::deleteByName('DM_COMGATE_MAIL_POSITION_ALLOWED');

        // ty
        $currencies = Currency::getCurrencies();

        foreach ($currencies as $itemCurrency)
        {
            Configuration::deleteByName('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].'');
        }

        $languages = Language::getLanguages(false);

        foreach($languages as $language)
        {
            Configuration::deleteByName('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].'');
        }
  
        return true;
    }

    // Metoda pro zpracovn dat z formul
    private function _postProcess()
    {
        $context = Context::getContext();

        if(Tools::isSubmit('submitSetting'))
        {
            Configuration::updateValue('DM_COMGATE_TEST_ACTIVE', Tools::getValue('DM_COMGATE_TEST_ACTIVE'));
            Configuration::updateValue('DM_COMGATE_TEST_IP_ADDRESS_CLIENT', trim(Tools::getValue('DM_COMGATE_TEST_IP_ADDRESS_CLIENT')));
            Configuration::updateValue('DM_COMGATE_MERCHANT', trim(Tools::getValue('DM_COMGATE_MERCHANT')));
            Configuration::updateValue('DM_COMGATE_SECRET', trim(Tools::getValue('DM_COMGATE_SECRET')));

            $this->_html .= $this->displayConfirmation($this->l('Nastaven bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>';
        }

        if(Tools::isSubmit('submitSettingOrderState'))
        {
            Configuration::updateValue('DM_COMGATE_STATUS_CREATED', Tools::getValue('DM_COMGATE_STATUS_CREATED'));
            Configuration::updateValue('DM_COMGATE_STATUS_PRE_AUTHORIZED', Tools::getValue('DM_COMGATE_STATUS_PRE_AUTHORIZED'));
            Configuration::updateValue('DM_COMGATE_STATUS_PAID', Tools::getValue('DM_COMGATE_STATUS_PAID'));
            Configuration::updateValue('DM_COMGATE_STATUS_PAID_NOT_STOCK', Tools::getValue('DM_COMGATE_STATUS_PAID_NOT_STOCK'));
            Configuration::updateValue('DM_COMGATE_STATUS_CANCELED', Tools::getValue('DM_COMGATE_STATUS_CANCELED'));
            Configuration::updateValue('DM_COMGATE_STATUS_ERROR', Tools::getValue('DM_COMGATE_STATUS_ERROR'));
            Configuration::updateValue('DM_COMGATE_STATUS_REFUND', Tools::getValue('DM_COMGATE_STATUS_REFUND'));

            $this->_html .= $this->displayConfirmation($this->l('Nastaven stav objednvek bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(2); }; </script>';
        }

        if(Tools::isSubmit('submitSettingOther'))
        {
            if (Configuration::get('DM_COMGATE_TYPE_SHOW_PAYMENT') == 'online')
            {
                // Skupinov ALIASY
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED'))
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED'))));
                }
                else
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED', '');
                }

                // Platby kartou
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED'))
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED'))));
                }
                else
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED', '');
                }

                // Platby bankou
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED'))
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED'))));
                }
                else
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED', '');
                }

                // Platby platm pozdji
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED'))
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED'))));
                }
                else
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED', '');
                }

                // Platm na tetiny
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED'))
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED'))));
                }
                else
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED', '');
                }
            }
            else
            {
                if(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED'))
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED'))));
                }
                else
                {
                    Configuration::updateValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED', '');
                }
            }

            $this->_html .= $this->displayConfirmation($this->l('Nastaven zobrazen platebnch metod bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(3); }; </script>';
        }

        if(Tools::isSubmit('submitSettingPayment'))
        {
            Configuration::updateValue('DM_COMGATE_TYPE_SHOW_PAYMENT', Tools::getValue('DM_COMGATE_TYPE_SHOW_PAYMENT'));
            Configuration::updateValue('DM_COMGATE_REPEAT_PAYMENT_ACTIVE', Tools::getValue('DM_COMGATE_REPEAT_PAYMENT_ACTIVE'));

            $this->_html .= $this->displayConfirmation($this->l('Nastaven plateb bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(3); }; </script>';
        }

        if(Tools::isSubmit('submitSettingEmail'))
        {
            Configuration::updateValue('DM_COMGATE_MAIL_ACTIVE', Tools::getValue('DM_COMGATE_MAIL_ACTIVE'));
            Configuration::updateValue('DM_COMGATE_TYPE_CUSTOMER_MAIL', Tools::getValue('DM_COMGATE_TYPE_CUSTOMER_MAIL'));
            Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE', Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE'));
            Configuration::updateValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT', Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT'));

            $languages = Language::getLanguages(false, $context->shop->id);

            foreach($languages as $language)
            {
                Configuration::updateValue('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].'', Tools::getValue('DM_COMGATE_SUBJECT_MAIL_'.$language['id_lang'].''));
            }

            if (Tools::getValue('DM_COMGATE_MAIL_POSITION_ALLOWED'))
            {
                Configuration::updateValue('DM_COMGATE_MAIL_POSITION_ALLOWED', implode(',', array_keys(Tools::getValue('DM_COMGATE_MAIL_POSITION_ALLOWED'))));
            }
            else
            {
                Configuration::updateValue('DM_COMGATE_MAIL_POSITION_ALLOWED', '');
            }

            $this->_html .= $this->displayConfirmation($this->l('Nastaven zobrazen platebnch metod bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(4); }; </script>';
        }

        if(Tools::isSubmit('submitSettingAccount'))
        {
            $currencies = Currency::getCurrencies();

            foreach ($currencies as $itemCurrency)
            {
                Configuration::updateValue('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].'', trim(Tools::getValue('DM_COMGATE_ACCOUNT_PAYMENT_'.$itemCurrency['iso_code'].'')));
            }

            $this->_html .= $this->displayConfirmation($this->l('Nastaven oznaen t bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(16); }; </script>';
        }

        if(Tools::isSubmit('submitSettingLog'))
        {
            Configuration::updateValue('DM_COMGATE_LOG_ACTIVE', Tools::getValue('DM_COMGATE_LOG_ACTIVE'));
            Configuration::updateValue('DM_COMGATE_LOG_DAYS', (int)Tools::getValue('DM_COMGATE_LOG_DAYS'));

            $this->_html .= $this->displayConfirmation($this->l('Nastaven logovn udlost bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(5); }; </script>';
        }

        if(Tools::isSubmit('submitSettingDetail'))
        {
            Configuration::updateValue('DM_COMGATE_PRE_AUTHORIZED', Tools::getValue('DM_COMGATE_PRE_AUTHORIZED'));
            Configuration::updateValue('DM_COMGATE_IDENT_PAYMENT', Tools::getValue('DM_COMGATE_IDENT_PAYMENT'));
            Configuration::updateValue('DM_COMGATE_LOGOS_PAYMENT_CART', (Tools::getValue('DM_COMGATE_LOGOS_PAYMENT_CART_ALLOWED') ? implode(',', array_keys(Tools::getValue('DM_COMGATE_LOGOS_PAYMENT_CART_ALLOWED'))) : ''));

            $this->_html .= $this->displayConfirmation($this->l('Nastaven uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(7); }; </script>';
        }

        if (Tools::isSubmit('status_comgate_active_EditAllowedProduct'))
        {
            $status = (int)Db::getInstance()->getValue('
                SELECT `dm_comgate_active`
                FROM `'._DB_PREFIX_.'product_shop`
                WHERE `id_product` = '.Tools::getValue('id_product').'
                    AND id_shop = '.(int)$context->shop->id.'
            ');
    
            Db::getInstance()->Execute('
                UPDATE `'._DB_PREFIX_.'product_shop`
                SET `dm_comgate_active` = \''.($status == 1 ? 0 : 1).'\'
                WHERE `id_product` = '.Tools::getValue('id_product').'
                    AND id_shop = '.(int)$context->shop->id.'
            ');
                                            
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7');
        }

        if(Tools::isSubmit('submitBulkedittoactive_EditAllowedProduct'))
        {
            foreach(Tools::getValue('_EditAllowedProductBox') as $id)
            {
                Db::getInstance()->Execute('
                    UPDATE `'._DB_PREFIX_.'product_shop`
                    SET `dm_comgate_active` = 1
                    WHERE `id_product` = '.(int)$id.'
                        AND id_shop = '.(int)$context->shop->id.'
                ');
            }

            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7');
        }

        if(Tools::isSubmit('submitBulkedittodeactive_EditAllowedProduct'))
        {
            foreach(Tools::getValue('_EditAllowedProductBox') as $id)
            {
                Db::getInstance()->Execute('
                    UPDATE `'._DB_PREFIX_.'product_shop`
                    SET `dm_comgate_active` = 0
                    WHERE `id_product` = '.(int)$id.'
                        AND id_shop = '.(int)$context->shop->id.'
                ');
            }

            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7');
        }

        if(Tools::isSubmit('submitSettingUpdate'))
        {
            Configuration::updateValue(''.$this->name.'_CHECK_UPDATE', Tools::getValue(''.$this->name.'_CHECK_UPDATE'));

            $this->_html .= $this->displayConfirmation($this->l('Nastaven bylo uloeno.'));

            $this->_html .= '<script> window.onload=function() { show_list(10); }; </script>';
        }

        if (Tools::isSubmit('submitUploadFile'))
        {
            copy($_FILES['file']['tmp_name'], dirname(__FILE__).'/crt/'.$context->shop->id.'_'.$this->name.'.crt');

            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>';
        }
    }

    // Metoda pro kontrolu sprvnosti vyplnnch dat
    private function _postValidation()
    {
        if(Tools::isSubmit('submitSetting'))
        {
            if(Tools::getValue('DM_COMGATE_MERCHANT') == '')
            {
                $this->_postErrors[] = $this->l('Prosm vyplte polko Merchant ID klienta.');
            }

            if(Tools::getValue('DM_COMGATE_SECRET') == '')
            {
                $this->_postErrors[] = $this->l('Prosm vyplte polko Secret heslo pro pstup ke slub COMGATE.');
            }

            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>';
        }

        if(Tools::isSubmit('submitSettingOther'))
        {
            if (Configuration::get('DM_COMGATE_TYPE_SHOW_PAYMENT') == 'offline')
            {
                if(!Tools::getValue('DM_COMGATE_SHOW_PAYMENT_ALLOWED'))
                {
                    $this->_postErrors[] = $this->l('Muste vybrat njakou platebn metodu pro zobrazen v koku.');
                }
            }

            $this->_html .= '<script> window.onload=function() { show_list(3); }; </script>';
        }

        if(Tools::isSubmit('submitSettingEmail'))
        {
            if(Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE') == 1)
            {
                if(Tools::getValue('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT') == '')
                {
                    $this->_postErrors[] = $this->l('Zadejte prosm email obchodnka, ktermu budou chodit upozornn.');
                }
            }

            $this->_html .= '<script> window.onload=function() { show_list(4); }; </script>';
        }

        if(Tools::isSubmit('submitSettingLog'))
        {
            if(trim(Tools::getValue('DM_COMGATE_LOG_DAYS')) == '')
            {
                $this->_postErrors[] = $this->l('Prosm vyplte poet dn pro mazn starch zznam logu.');
            }

            $this->_html .= '<script> window.onload=function() { show_list(5); }; </script>';
        }

       	if (Tools::isSubmit('submitUploadFile'))
        {
            if(!file_exists($_FILES['file']['tmp_name']) OR !in_array(strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)), array('crt')))
            {
                $this->_postErrors[] = $this->l('Nepodailo se nast sprvn licenn soubor.');
            }  

            $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>';  
        }

        if(Tools::isSubmit('submitBulkedittoactive_EditAllowedProduct'))
        {
            if (!is_array(Tools::getValue('_EditAllowedProductBox')))
            {
                $this->_postErrors[] = $this->l('Nen vybrn dn produkt k aktivaci pro platebn brnu!');
            }

            $this->_html .= '<script> window.onload=function() { show_list(7); }; </script>';
        }

        if(Tools::isSubmit('submitBulkedittodeactive_EditAllowedProduct'))
        {
            if (!is_array(Tools::getValue('_EditAllowedProductBox')))
            {
                $this->_postErrors[] = $this->l('Nen vybrn dn produkt k deaktivaci pro platebn brnu!');
            }

            $this->_html .= '<script> window.onload=function() { show_list(7); }; </script>';
        }
    }

    public function displayInfo()
    {
        $this->context->smarty->assign(array(
                'modul_name' => $this->displayName,
                'modul_description' => $this->description,
            ));

        if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
        {
            return $this->display(__FILE__, ''.$this->name.'.tpl');
        }
        else
        {
            return $this->fetch('module:'.$this->name.'/views/templates/hook/'.$this->name.'.tpl');
        }
    }

    // Metoda pro zobrazen obsahu
    public function getContent()
    {
        $this->context->controller->addCss($this->_path.'css/style.css');
        $this->context->controller->addJS($this->_path.'js/function.js');

        if (Tools::isSubmit('submitResetlog'))
        {
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=5');
        }

        if (Tools::isSubmit('submitReset_EditAllowedProduct'))
        {
            Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name.'&list=7');
        }

        if (
            Tools::isSubmit('btnSubmit')
            || Tools::isSubmit('status_comgate_active_EditAllowedProduct')
            || Tools::isSubmit('submitBulkedittoactive_EditAllowedProduct')
            || Tools::isSubmit('submitBulkedittodeactive_EditAllowedProduct')
        )
        {

            $this->_postValidation();
            
            if (!count($this->_postErrors))
            {
                $this->_postProcess();
            }
            else
            {
                foreach ($this->_postErrors as $err)
                {
                    $this->_html .= $this->displayError($err);
                }
            }  
        }
        else
        {
            if (Tools::isSubmit('list'))
            {
                $this->_html .= '<script> window.onload=function() { show_list('.Tools::getValue('list').'); }; </script>';
            }
            else
            {
                $this->_html .= '<script> window.onload=function() { show_list(1); }; </script>';
            }

            $this->_html .= '<br />';         
        }

        // Zjitn aktualiazce modulu
        if($this->update->version_data)
        {
            $this->_html .= '
                <div class="alert alert-warning">
                    '.$this->l('Je k dispozici nov verze modulu').' ('.$this->update->version_data['version'].').<br />
                    '.$this->update->version_data['changelog'].'        
                </div>
            ';
        }

        if(Configuration::get('DM_COMGATE_PRE_AUTHORIZED') == 1)
        {
            $this->_html .= '
                <div class="alert alert-danger">
                    '.$this->l('Modul je v reimu "Pedautorizace", nezapomete potvrdit staen financ od zkaznka na v et COMGATE po kontrole objednvky.').'
                    '.$this->l('Uite tak dve ne zsilku zalete a vypr vm platnost zaloen platby.').'
                </div>
            ';
        }

        if (Shop::isFeatureActive() AND Shop::getContext() == Shop::CONTEXT_ALL)
        {
            $this->_html .= $this->displayError('Prosm pepnte modul v kontextu jednotlivch STORE!');
        }
        elseif (Shop::isFeatureActive() AND Shop::getContext() == Shop::CONTEXT_GROUP)
        {
            $this->_html .= $this->displayError('Prosm pepnte modul v kontextu jednotlivch STORE!');
        }
        else
        {
            // Install tda
            $this->install = new DmComgateInstall($this);
            $this->install->repairRegisterHooks();

            $form = new DmComgateFormBuilder($this);

            $this->_html .= $form->init(); 
        }

        return $this->_html;
    }

    // Metoda pro generovn formul s nastavenm
    public function renderForm($form, $fields_value)
    {
        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table =  $this->table;
        $lang = new Language($this->default_lang);
        $helper->default_form_language = $lang->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
        $helper->identifier = $this->identifier;
        $helper->submit_action = 'btnSubmit';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->tpl_vars = array(
            'fields_value' => $fields_value,
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id
        );

        return $helper->generateForm($form);
    }

    public function renderLogList($list, $fields, $count)
    {
        $helper = new HelperList();
        $helper->shopLinkType = '';
        $helper->simple_header = false;
        $helper->actions = array();
        $helper->show_toolbar = false;
        $helper->module = $this;
        $helper->identifier = 'id_log';
        $helper->title = $this->l('Log udlost').' ('.$count.')';
        $helper->table = 'log';
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name.'&module_name='.$this->name.'&list=5';
        $helper->no_link = true;

        //$helper->_default_pagination = 50;
        $helper->listTotal = $count;

        return $helper->generateList($list, $fields);
    }

    public function renderProductList($list, $fields, $count)
    {
        $helper = new HelperList();
        $helper->shopLinkType = '';
        $helper->simple_header = false;
        $helper->actions = array();
        $helper->show_toolbar = false;
        $helper->module = $this;

        if(version_compare(_PS_VERSION_, "1.6.0.9", ">"))
        {
            $helper->_pagination = array(20, 50, 100, 300, 1000, 10000);
        }

        $helper->bulk_actions = array(
            'edittoactive' => array(
                'text'    => $this->l('Zmnit status na POVOLENO'),
                'icon'    => 'icon-edit',
            ),
            'edittodeactive' => array(
                'text'    => $this->l('Zmnit status na ZAKZNO'),
                'icon'    => 'icon-edit',
            ),
        );
        $helper->identifier = 'id_product';
        $helper->title = $this->l('Povolen modulu u produkt').' ('.count($count).')';
        $helper->table = '_EditAllowedProduct';
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name.'&module_name='.$this->name.'&list=7';
        $helper->no_link = true;

        $helper->listTotal = count($count);

        return $helper->generateList($list, $fields);
    }

    // Metoda pro kontrolu povolen mny
    public function checkCurrency($cart)
    {
        $currency_order = new Currency($cart->id_currency);
        $currencies_module = $this->getCurrency($cart->id_currency);
        
        if (is_array($currencies_module))
        {
            foreach ($currencies_module as $currency_module)
            {
                if ($currency_order->id == $currency_module['id_currency'])
                {
                    return true;
                }
            }
        }
                  
        return false;
    }

    public function getGroupPaymentMethod()
    {
        return array(
            array(
                'name' => $this->l('Platebn brna COMGATE'),
                'id' => 'ALLOWED[ALL]',
                'val' => 'ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-payment.png',
                'currency' => 'ALL',
                'intro' => true,
            ),
            array(
                'name' => $this->l('Platebn kartou online'),
                'id' => 'ALLOWED[CARD_ALL]',
                'val' => 'CARD_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-payment-card.png',
                'currency' => 'ALL',
                'intro' => true,
            ),
            array(
                'name' => $this->l('Okamit bankovn pevod online'),
                'id' => 'ALLOWED[BANK_ALL]',
                'val' => 'BANK_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-bank.png',
                'currency' => 'ALL',
                'intro' => true,
            ),
            array(
                'name' => $this->l('Odloen platba - Nakupte hned, zaplate pozdji'),
                'id' => 'ALLOWED[LATER_ALL]',
                'val' => 'LATER_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-later.png',
                'currency' => 'CZK',
                'intro' => true,
            ),
            array(
                'name' => $this->l('Nkup na tetiny - Nakupte hned, zaplate jen tetinu'),
                'id' => 'ALLOWED[PART_ALL]',
                'val' => 'PART_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 1500,
                'limit_max' => 15000,
            ),
        );
    }

    public function getPaymentMethod()
    {
        return array(
            array(
                'name' => $this->l('Platebn brna COMGATE'),
                'id' => 'ALLOWED[ALL]',
                'val' => 'ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-payment.png',
                'currency' => 'ALL',
                'intro' => true,
            ),
            array(
                'name' => $this->l('Platebn kartou online'),
                'id' => 'ALLOWED[CARD_ALL]',
                'val' => 'CARD_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-payment-card.png',
                'currency' => 'ALL',
                'intro' => true,
            ),
            array(
                'name' => $this->l('Okamit bankovn pevod online'),
                'id' => 'ALLOWED[BANK_ALL]',
                'val' => 'BANK_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-bank.png',
                'currency' => 'ALL',
                'intro' => true,
            ),
            
            array(
                'name' => $this->l('Odloen platba - Nakupte hned, zaplate pozdji'),
                'id' => 'ALLOWED[LATER_ALL]',
                'val' => 'LATER_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-later.png',
                'currency' => 'CZK',
                'intro' => true,
            ),
            
            array(
                'name' => $this->l('Platm pak odloen platba - Nakupte hned, zaplate pozdji'),
                'id' => 'ALLOWED[LATER_PLATIMPAK]',
                'val' => 'LATER_PLATIMPAK',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-pay-later.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 500,
                'limit_max' => 25000,
            ),
            array(
                'name' => $this->l('Skip Pay odloen platba - Nakupte hned, zaplate za 14 dn'),
                'id' => 'ALLOWED[LATER_SKIPPAY]',
                'val' => 'LATER_SKIPPAY',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 0,
                'limit_max' => 25000,
            ),
            array(
                'name' => $this->l('TWISTO odloen platba - Nakupte hned, zaplate pozdji'),
                'id' => 'ALLOWED[LATER_TWISTO]',
                'val' => 'LATER_TWISTO',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-twisto.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 0,
                'limit_max' => 25000,
            ),
            array(
                'name' => $this->l('Nkup na tetiny - Nakupte hned, zaplate jen tetinu'),
                'id' => 'ALLOWED[PART_ALL]',
                'val' => 'PART_ALL',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 1500,
                'limit_max' => 15000,
            ),
            array(
                'name' => $this->l('Skip Pay nkup na tetiny - Nakupte hned, zaplate jen tetinu'),
                'id' => 'ALLOWED[PART_SKIPPAY]',
                'val' => 'PART_SKIPPAY',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-skippay-later.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 3000,
                'limit_max' => 15000,
            ),
            array(
                'name' => $this->l('TWISTO nkup na tetiny - Nakupte hned, zaplate jen tetinu'),
                'id' => 'ALLOWED[PART_TWISTO]',
                'val' => 'PART_TWISTO',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-twisto.png',
                'currency' => 'CZK',
                'intro' => true,
                'limit_min' => 1500,
                'limit_max' => 15000,
            ),
            array(
                'name' => $this->l('Apple Pay'),
                'id' => 'ALLOWED[APPLEPAY_REDIRECT]',
                'val' => 'APPLEPAY_REDIRECT',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-applepay.png',
                'currency' => 'ALL',
                'intro' => false,
            ),
            array(
                'name' => $this->l('Google Pay'),
                'id' => 'ALLOWED[GOOGLEPAY_REDIRECT]',
                'val' => 'GOOGLEPAY_REDIRECT',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-gpay.png',
                'currency' => 'ALL',
                'intro' => false,
            ),
            array(
                'name' => $this->l('Raiffeisen Bank - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_RB]',
                'val' => 'BANK_CZ_RB',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-rb.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('Komern banka - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_KB]',
                'val' => 'BANK_CZ_KB',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-kb.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('MONETA - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_MO]',
                'val' => 'BANK_CZ_MO',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-moneta.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('FIO banka - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_FB_P]',
                'val' => 'BANK_CZ_FB_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-fio.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('esk spoitelna - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_CS_P]',
                'val' => 'BANK_CZ_CS_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-cs.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('mBank - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_MB_P]',
                'val' => 'BANK_CZ_MB_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-mbank.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('SOB - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_CSOB_P]',
                'val' => 'BANK_CZ_CSOB_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-csob.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('era Potovn spoitelna - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_PS_P]',
                'val' => 'BANK_CZ_PS_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-era.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('UniCredit Bank - okamit pevod'),
                'id' => 'ALLOWED[BANK_CZ_UC]',
                'val' => 'BANK_CZ_UC',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-uni.png',
                'currency' => 'CZK',
                'intro' => false,
            ),
            array(
                'name' => $this->l('Slovensk sporitea - okamit pevod'),
                'id' => 'ALLOWED[BANK_SK_SLSP]',
                'val' => 'BANK_SK_SLSP',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-ssp.png',
                'currency' => 'EUR',
                'intro' => false,
            ),
            array(
                'name' => $this->l('VB Banka - okamit pevod'),
                'id' => 'ALLOWED[BANK_SK_VUB_P]',
                'val' => 'BANK_SK_VUB_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-vub.png',
                'currency' => 'EUR',
                'intro' => false,
            ),
            array(
                'name' => $this->l('Tatra Banka - okamit pevod'),
                'id' => 'ALLOWED[BANK_SK_TB_P]',
                'val' => 'BANK_SK_TB_P',
                'link' => _PS_MODULE_DIR_.$this->name.'/img/comgate-logo-tb.png',
                'currency' => 'EUR',
                'intro' => false,
            ),
        );
    }

    public function getLimit($currency_iso_code)
    {
        $limit = 0;

        if($currency_iso_code == 'CZK')
        {
            $limit = 10;
        }
        elseif($currency_iso_code == 'EUR')
        {
            $limit = 1;
        }
        elseif($currency_iso_code == 'PLN')
        {
            $limit = 10;
        }
        elseif($currency_iso_code == 'HUF')
        {
            $limit = 100;
        }
        elseif($currency_iso_code == 'USD')
        {
            $limit = 1;
        }
        elseif($currency_iso_code == 'GBP')
        {
            $limit = 1;
        }

        return $limit;
    }


    // Metoda pro zobrazen platebn metody
    public function hookPaymentOptions($params)
    {
        $amount_cart = (float)$this->context->cart->getOrderTotal();
        $currency = Currency::getCurrency($this->context->cart->id_currency);
        $currency_iso_code = $currency['iso_code'];

        $limit = $this->getLimit($currency_iso_code);

        if($limit > $amount_cart)
        {
            return;
        }

        // Oven aktivnho modulu
        if (!$this->active) {
            return;
        }
        
        // Kontrola piazen mny    
        if (!$this->checkCurrency($params['cart']))
        {
            return;
        }

        // Kontrola povolench produkt
        if($this->isDisallowedProductInCart($params['cart']))
        {
            return;
        }

        $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );

        // Kontrola IP adresy pro test
        if($test_mode)
        {
            $allowed_ip = (string)trim(Configuration::get('DM_COMGATE_TEST_IP_ADDRESS_CLIENT'));

            if($allowed_ip != '')
            {
                $client_ip = (string)Tools::getRemoteAddr();

                if($client_ip != $allowed_ip)
                {
                    return;
                }
            }
        }

        // Pole s platebnmi metodami
        $array_payment_options = array();

        $fields_allowed = explode(',', Configuration::get('DM_COMGATE_LOGOS_PAYMENT_CART'));

        if (Configuration::get('DM_COMGATE_TYPE_SHOW_PAYMENT') == 'online')
        {
            $poc = 0;

            // Skupiny ALIASY
            $group_payments_methods = $this->getGroupPaymentMethod();
            $group_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_GROUP_ALLOWED'));

            foreach($group_payments_methods as $item_group_method)
            {
                if(in_array($item_group_method['val'], $group_payments_allowed) AND ($currency_iso_code == $item_group_method['currency'] OR $item_group_method['currency'] == 'ALL'))
                {
                    // Idividuln limit na danou platbu, zpravidla odloen platby
                    if(isset($item_group_method['limit_min']) AND ($item_group_method['limit_min'] >= $amount_cart OR $item_group_method['limit_max'] <= $amount_cart))
                        continue;

                    $array_payment_options[(int)$poc] = new PaymentOption();
                    $array_payment_options[(int)$poc]->setCallToActionText($item_group_method['name'].''.($test_mode ? ' '.$this->l('(Testovac reim)').'' : '').'');
                    $array_payment_options[(int)$poc]->setAction($this->context->link->getModuleLink($this->name, 'validation', array('preferpayment' => $item_group_method['val']), true).'');

                    if($item_group_method['intro'])
                    {
                        if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
                        {
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->display(__FILE__, 'dm_comgate_intro_'.$item_group_method['val'].'.tpl'));
                        }
                        else
                        {
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_intro_'.$item_group_method['val'].'.tpl'));
                        }
                    }

                    if(in_array('payment', $fields_allowed) AND isset($item_group_method['link']))
                    {
                        $array_payment_options[(int)$poc]->setLogo(Media::getMediaPath($item_group_method['link']));
                    }

                    $poc++;
                }
            }

            // Pipojen a staen ONLINE plateb

            // Volm objekt pro API COMGATE
            $comgate = new DmComgateApi($this, $test_mode);
            $total_amount_for_payment = $comgate->formatPriceForPayment($amount_cart, (int)$this->context->cart->id_currency);

            $invoiceAddress = new Address((int)$this->context->cart->id_address_invoice);
            $countryInvoice = new Country((int)$invoiceAddress->id_country);

            $all_payment_list = $comgate->getPaymentMethods($this->context->language->iso_code, $currency_iso_code, mb_strtoupper($countryInvoice->iso_code), $total_amount_for_payment);

            if ($all_payment_list)
            {
                $card_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_CARD_ALLOWED'));
                $bank_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_BANK_ALLOWED'));
                $later_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_LATER_ALLOWED'));
                $part_payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_PART_ALLOWED'));

                foreach($all_payment_list['methods'] as $item_payment)
                {
                    if(
                        in_array($item_payment['id'], $card_payments_allowed)
                        || in_array($item_payment['id'], $bank_payments_allowed)
                        || in_array($item_payment['id'], $later_payments_allowed)
                        || in_array($item_payment['id'], $part_payments_allowed)
                    )
                    {
                        $array_payment_options[(int)$poc] = new PaymentOption();
                        $array_payment_options[(int)$poc]->setCallToActionText($item_payment['name'].''.($test_mode ? ' '.$this->l('(Testovac reim)').'' : '').'');
                        $array_payment_options[(int)$poc]->setAction($this->context->link->getModuleLink($this->name, 'validation', array('preferpayment' => $item_payment['id']), true).'');

                        $this->smarty->assign(array(
                            'dmComgateOnlineDescription' => $item_payment['description'],
                        ));

                        if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
                        {
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->display(__FILE__, 'dm_comgate_intro_ONLINE.tpl'));
                        }
                        else
                        {
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_intro_ONLINE.tpl'));
                        }

                        if(in_array('payment', $fields_allowed) AND isset($item_payment['logo']) AND $item_payment['logo'] != '')
                        {
                            $array_payment_options[(int)$poc]->setLogo(Media::getMediaPath($item_payment['logo']));
                        }
                    }

                    $poc++;
                }
            }
        }
        else
        {
            $payments_methods = $this->getPaymentMethod();

            $payments_allowed = explode(',', Configuration::get('DM_COMGATE_SHOW_PAYMENT_ALLOWED'));
            $poc = 0;
            
            foreach($payments_methods as $item_method)
            {
                if(in_array($item_method['val'], $payments_allowed) AND ($currency_iso_code == $item_method['currency'] OR $item_method['currency'] == 'ALL'))
                { 
                    // Idividuln limit na danou platbu, zpravidla odloen platby
                    if(isset($item_method['limit_min']) AND ($item_method['limit_min'] >= $amount_cart OR $item_method['limit_max'] <= $amount_cart))
                        continue;
                    
                    $array_payment_options[(int)$poc] = new PaymentOption();

                    $array_payment_options[(int)$poc]->setCallToActionText($item_method['name'].''.($test_mode ? ' '.$this->l('(Testovac reim)').'' : '').'');
                    $array_payment_options[(int)$poc]->setAction($this->context->link->getModuleLink($this->name, 'validation', array('preferpayment' => $item_method['val']), true).'');

                    if($item_method['intro'])
                    {
                        if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
                        {
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->display(__FILE__, 'dm_comgate_intro_'.$item_method['val'].'.tpl'));
                        }
                        else
                        {
                            $array_payment_options[(int)$poc]->setAdditionalInformation($this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_intro_'.$item_method['val'].'.tpl'));
                        }
                    }

                    if(in_array('payment', $fields_allowed) AND isset($item_method['link']))
                    {
                        $array_payment_options[(int)$poc]->setLogo(Media::getMediaPath($item_method['link']));
                    }

                    $poc++;
                }
            }
        }

        return $array_payment_options;
    }

    public function hookDisplayPaymentReturn($params)
    {
        if (!$this->active) {
            return;
        }

        // Nastavm zobrazen potvrzovac strnky
        Db::getInstance()->Execute('
            UPDATE `'._DB_PREFIX_.'dm_comgate_payment`
            SET `show_confirmation` = 1
            WHERE `id_order` = '.(int)$params['order']->id.'
        ');

        // Chyba na brn, nezaloen platba
        if(Tools::getValue('dm_comgate_status') == 'ERROR')
        {
            if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
            {
                return $this->display(__FILE__, 'dm_comgate_confirmation_errors.tpl');
            }
            else
            {
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_confirmation_errors.tpl');
            }
        }
        // S njakm vsledkem jdu z brny
        else
        {
            // Zjiuji produkn mod nebo TEST
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );

            $payment_check_url = $this->context->link->getModuleLink(
                'dm_comgate',
                'cgpayment',
                array(
                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$params['order']->id : (string)$params['order']->reference),
                ),
                true
            );

            // Vracm TPL
            $this->smarty->assign(array(
                'dm_comgate_check_url' => $payment_check_url,
                'dm_comgate_test_mode' => $test_mode,
                'dm_comgate_status_payment' => Tools::getValue('dm_comgate_status'),
            ));

            if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
            {
                return $this->display(__FILE__, 'dm_comgate_confirmation.tpl');
            }
            else
            {
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_confirmation.tpl');
            }
        }
    }

    public function hookDisplayDmPaymentButtonHistory($params)
    {
        if ((int)$params['order']->id > 0)
        {
            $order_data = new Order((int)$params['order']->id);
        }

        if($order_data->module != $this->name)
            return;

        $allowed_state = array(
            Configuration::get('DM_COMGATE_STATUS_CREATED'),
            Configuration::get('DM_COMGATE_STATUS_CANCELED'),
            Configuration::get('PS_OS_OUTOFSTOCK_PAID'),
            Configuration::get('PS_OS_OUTOFSTOCK_UNPAID'),
        );

        // Stle stav, e ek na gopay platbu
        if(in_array($order_data->current_state, $allowed_state) AND Configuration::get('DM_COMGATE_REPEAT_PAYMENT_ACTIVE') == 1)
        {
            $context = Context::getContext();
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );
            $comgate = new DmComgateApi($this, $test_mode);
            $exist_payment = $comgate->getPaymentForOrder($order_data->id);
            $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL');

            if($type_url_link == 'original')
            {
                // Platba nen zaloena pouiji odkaz pes eshop / kontroler, kde se autoamticky paltba zalo
                if(!$exist_payment)
                {
                    $url_payment_link = $this->context->link->getModuleLink(
                        'dm_comgate',
                        'cgpayment',
                        array(
                            'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                            'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference),
                        ),
                        true,
                        (int)$order_data->id_lang,
                        (int)$order_data->id_shop
                    );
                }
                else
                {
                    $url_payment_link = 'https://payments.comgate.cz/'.$exist_payment['id_comgate'].'';
                }

            }
            else
            {
                $url_payment_link = $this->context->link->getModuleLink(
                    'dm_comgate',
                    'cgpayment',
                    array(
                        'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                        'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference),
                    ),
                    true,
                    (int)$order_data->id_lang,
                    (int)$order_data->id_shop
                ); 
            }

            // Vracm TPL
            $this->smarty->assign(array(
                'dm_comgate_url_payment' => $url_payment_link,
            ));

            if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
            {
                return $this->display(__FILE__, '/views/templates/front/dm_comgate_payment_history_button.tpl');
            }
            else
            {
                return $this->fetch('module:dm_comgate/views/templates/front/dm_comgate_payment_history_button.tpl');
            }
        }

        return;
    }

    // Metoda pro zobrazen platby v historii objednvek, pokud jet nen zaplacen
    public function hookDisplayOrderDetail($params)
    {
        if($params['order']->module != $this->name)
            return;

        $allowed_state = array(
            Configuration::get('DM_COMGATE_STATUS_CREATED'),
            Configuration::get('DM_COMGATE_STATUS_CANCELED'),
            Configuration::get('PS_OS_OUTOFSTOCK_PAID'),
            Configuration::get('PS_OS_OUTOFSTOCK_UNPAID'),
        );

        // Stle stav, e ek na comgate platbu
        if(in_array($params['order']->current_state, $allowed_state) AND Configuration::get('DM_COMGATE_REPEAT_PAYMENT_ACTIVE') == 1)
        {
            $context = Context::getContext();
            // Zjiuji produkn mod nebo TEST
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );

            // Volm objekt pro API COMGATE
            $comgate = new DmComgateApi($this, $test_mode);

            // Zjistm pro jistotu jestli neexistuje njak ji zaloen platba v systmu (pojistka pro opakovan naten v prohlei), koda zakldat novou
            $exist_payment = $comgate->getPaymentForOrder($params['order']->id);

            // Platba v databzi existuje
            if($exist_payment)
            {
                // Zjiuji na brn stav
                $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']);

                if($cg_status['status'] == 'PENDING')
                {
                    $currency = new Currency($params['order']->id_currency);

                    // Vracm TPL
                    $this->smarty->assign(array(
                        'dm_comgate_test_mode' => $test_mode,
                        'dm_comgate_id_payment' => $exist_payment['id_comgate'],
                        'dm_comgate_payment_url' => 'https://payments.comgate.cz/client/instructions/index?id='.$exist_payment['id_comgate'].'',
                        'dm_comgate_id_order' => $params['order']->id,
                        'dm_comgate_reference_order' => $params['order']->reference,
                        'dm_comgate_total_to_pay' => Tools::displayPrice($params['order']->total_paid, $currency, false),
                    ));

                    if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
                    {
                        return $this->display(__FILE__, '/views/templates/front/dm_comgate_payment.tpl');
                    }
                    else
                    {
                        return $this->fetch('module:dm_comgate/views/templates/front/dm_comgate_payment.tpl');
                    }
                }
                else
                {
                    return;
                }

            }
            else
            {
                return;
            }
        }

        return;
    }

    public function hookDisplayBackOfficeHeader()
    {
        Media::addJsDef(array(
            'dm_comgate_id_default_lang' => $this->default_lang,
            'dm_comgate_ip_client' => ''.Tools::getRemoteAddr().'',
        ));

        // Odesln mailu s odkazem na platbu
        if (Tools::isSubmit('SubmitComgateLinkToCustomer'))
        {
            $id_order = (int)Tools::getValue('dm_comgate_id_order');
            $this->sendMailPayment($id_order);
            $this->context->controller->confirmations[] = $this->l('Email odesln zkaznkovi.');
        }

        if (Tools::isSubmit('dm_comgate_refund'))
        {
            if (Tools::getValue('dm_comgate_refund') == 'OK')
            {
                $this->context->controller->confirmations[] = ''.$this->l('Poadovan stka byla vrcena!').'';
            }
            elseif (Tools::getValue('dm_comgate_refund') == 'ERROR')
            {
                $this->context->controller->errors[] = $this->l('Nepodailo se vrtit stku zkaznkovi.');
            }
            elseif (Tools::getValue('dm_comgate_refund') == 'UNPAID')
            {
                $this->context->controller->errors[] = $this->l('Nelze provst refund, platba se nenachz ve stavu zaplaceno.');
            }
        }

        // Refundace objednvky
        if (Tools::isSubmit('SubmitDmComgateRefund'))
        {
            $limit_amount = (float)Tools::getValue('Submit_dm_comgate_total_pay');
            $amount_to_refund = (float)str_replace(array(',', ' '), array('.', ''), Tools::getValue('dm_comgate_amount_to_refund'));
            $id_order = (int)Tools::getValue('dm_comgate_id_order');
            $id_currency = (int)Tools::getValue('dm_comgate_id_currency');

            if($amount_to_refund > $limit_amount)
            {
                $this->context->controller->errors[] = $this->l('Nelze vrtit vy stku ne byla pijata.');
            }
            elseif($amount_to_refund == 0)
            {
                $this->context->controller->errors[] = $this->l('Uvete stku vy ne 0,-.');
            }
            else
            {
                // Zjiuji produkn mod nebo TEST
                $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );
                // Volm objekt pro API ComGate
                $comgate = new DmComgateApi($this, $test_mode);
                // Ovm platbu
                $exist_payment = $comgate->getPaymentForOrder($id_order);

                // Platba existuje, ve jsem nael
                if($exist_payment)
                {
                    // Zjiuji na brn stav
                    $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']);

                    if($cg_status['status'] == 'PAID')
                    {
                        // Data pro refund
                        $data_for_refund = $comgate->getDataForRefund(
                            $id_order,
                            (string)$exist_payment['id_comgate'],
                            $amount_to_refund,
                            $id_currency
                        );

                        $cg_refund = $comgate->refundPayment($data_for_refund);

                        // Zaloit refundaci se zdailo
                        if(isset($cg_refund['code']) AND $cg_refund['code'] === '0')
                        {
                            $order = new Order($id_order);

                            // Ppadn zmna stavu po refundaci v ppad vratky cel stky
                            $id_order_state_refund = (int)Configuration::get('DM_COMGATE_STATUS_REFUND');

                            if($id_order_state_refund > 0 AND (int)$id_order_state_refund != (int)$order->current_state AND $amount_to_refund == $limit_amount)
                            {
                                $history = new OrderHistory();
                                $history->id_order = (int)$id_order;
                                $history->changeIdOrderState((int)$id_order_state_refund, $order, true);
                                $history->addWithemail();
                            }

                            //////////////////////////////////////////////////////////////////////////

                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'info', ''.$this->l('Vratka na stku').' '.Tools::displayPrice($amount_to_refund, $id_currency).' '.$this->l('byla provedena!').'');

                            $order_invoice = null;

                            // Zjiuji vytvoenou fakturu
                            $invoice = Db::getInstance()->getRow('
                                SELECT *
                                FROM `'._DB_PREFIX_.'order_invoice`
                                WHERE `id_order` = '.(int)$id_order.'
                            ');

                            if($invoice)
                            {
                                $order_invoice = new OrderInvoice($invoice['id_order_invoice']);
                            }

                            if(version_compare(_PS_VERSION_, "1.7.7.0", "<"))
                            {
                                $order->addOrderPayment((float)($amount_to_refund * -1), $this->l('ComGate refundace'), (string)$exist_payment['id_comgate'], null, null, $order_invoice);
                            }
                            else
                            {
                                $order_data = new Order($id_order);

                                // Zapu platbu s mnusovm stavem
                                Db::getInstance()->execute('
                                    INSERT INTO `'._DB_PREFIX_.'order_payment` (`order_reference`, `id_currency`, `amount`, `payment_method`, `conversion_rate`, `transaction_id`, `card_number`, `card_brand`, `card_expiration`, `card_holder`, `date_add`)
                                    VALUES(\''.(string)pSQL($order_data->reference).'\', '.(int)$id_currency.', '.(float)($amount_to_refund * -1).', \'ComGate refundace\', 1, \''.(string)$exist_payment['id_comgate'].'\', \'\', \'\', \'\', \'\', NOW() )
                                ');

                                $last_id = Db::getInstance()->Insert_ID();

                                // Zapu invoice_payment
                                if($invoice)
                                {
                                    Db::getInstance()->execute('
                                        INSERT INTO `'._DB_PREFIX_.'order_invoice_payment` (`id_order_invoice`, `id_order_payment`, `id_order`)
                                        VALUES('.(int)$order_invoice->id.', '.(int)$last_id.', '.(int)$id_order.')
                                    ');
                                }
                            }

                            //$this->context->controller->confirmations[] = ''.$this->l('Vratka na stku').' '.Tools::displayPrice($amount_to_refund, $id_currency).' '.$this->l('byla provedena!').'';

                            $link_order = $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => $id_order, 'vieworder' => 1, 'dm_comgate_refund' => 'OK']) . '&id_order='.$id_order.'&vieworder&dm_comgate_refund=OK';

                            Tools::redirectAdmin($link_order);
                        }
                        else
                        {
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', $this->l('Refundace platby se nezdaila!'));

                            if(isset($cg_refund['code']) AND isset($cg_refund['message']))
                            {
                                $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', ''.$cg_refund['code'].' - '.$cg_refund['message'].'');
                            }

                            //$this->context->controller->errors[] = $this->l('Nepodailo se vrtit stku zkaznkovi.');

                            $link_order = $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => $id_order, 'vieworder' => 1, 'dm_comgate_refund' => 'ERROR']) . '&id_order='.$id_order.'&vieworder&dm_comgate_refund=ERROR';

                            Tools::redirectAdmin($link_order);
                        }
                    }
                    else
                    {
                        //$this->context->controller->errors[] = $this->l('Nelze provst refund, platba se nenachz ve stavu zaplaceno.');

                        $link_order = $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => $id_order, 'vieworder' => 1, 'dm_comgate_refund' => 'UNPAID']) . '&id_order='.$id_order.'&vieworder&dm_comgate_refund=UNPAID';

                        Tools::redirectAdmin($link_order);
                    }
                }
            }
        }

        if (Tools::isSubmit('SubmitDmComgateCapturePreAuth'))
        {
            // Zjiuji produkn mod nebo TEST
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );
            // Volm objekt pro API ComGate
            $comgate = new DmComgateApi($this, $test_mode);

            $limit_amount = (float)Tools::getValue('Submit_dm_comgate_total_pay');
            $amount_to_capture = (float)str_replace(array(',', ' '), array('.', ''), Tools::getValue('dm_comgate_amount_to_capture'));
            $id_order = (int)Tools::getValue('dm_comgate_id_order');
            $id_currency = (int)Tools::getValue('dm_comgate_preauth_id_currency');

            $limit_amount_valid = $comgate->formatPriceForPayment($limit_amount, $id_currency);
            $amount_to_capture_valid = $comgate->formatPriceForPayment($amount_to_capture, $id_currency);

            if($amount_to_capture_valid > $limit_amount_valid)
            {
                $this->context->controller->errors[] = $this->l('Nelze sthnout vy stku ne je blokovna na tu klienta.');
            }
            elseif($amount_to_capture_valid == 0)
            {
                $this->context->controller->errors[] = $this->l('Uvete stku vy ne 0,-.');
            }
            else
            {
                // Ovm platbu
                $exist_payment = $comgate->getPaymentForOrder($id_order);

                // Platba existuje, ve jsem nael
                if($exist_payment)
                {
                    // Zjiuji na brn stav
                    $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']);

                    if($cg_status['status'] == 'AUTHORIZED')
                    {
                        $cg_capture_pre_auth = $comgate->getCapturePreAuthPayment((string)$exist_payment['id_comgate'], $amount_to_capture, new Currency($id_currency));

                        // Staen prostedk se zdailo
                        if(isset($cg_capture_pre_auth['code']) AND $cg_capture_pre_auth['code'] === '0')
                        {
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'info', ''.$this->l('Staen blokovanch prostedk ve vi').' '.Tools::displayPrice($amount_to_capture, $id_currency).' '.$this->l('bylo provedeno!').'');

                            $comgate->notifyPayment($id_order, (string)$exist_payment['id_comgate']);

                            $this->context->controller->confirmations[] = ''.$this->l('Staen blokovanch prostedk ve vi').' '.Tools::displayPrice($amount_to_capture, $id_currency).' '.$this->l('bylo provedeno!').'';
                        }
                        else
                        {
                            $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', $this->l('Staen blokovanch prostedk se nezdailo!'));

                            if(isset($cg_capture_pre_auth['code']) AND isset($cg_capture_pre_auth['message']))
                            {
                                $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', ''.$cg_capture_pre_auth['code'].' - '.$cg_capture_pre_auth['message'].'');
                            }

                            $this->context->controller->errors[] = $this->l('Staen blokovanch prostedk se nezdailo!');
                        }
                    }
                    else
                    {
                        $this->context->controller->errors[] = $this->l('Nelze provst staen blokovanch prostedk, platba se nenachz ve stavu autorizovno.');
                    }
                }
            }
        }

        if (Tools::isSubmit('SubmitDmComgateCancelPreAuth'))
        {
            // Zjiuji produkn mod nebo TEST
            $test_mode = (Configuration::get('DM_COMGATE_TEST_ACTIVE') == 1 ? true : false );
            // Volm objekt pro API ComGate
            $comgate = new DmComgateApi($this, $test_mode);

            $id_order = (int)Tools::getValue('dm_comgate_id_order');

            // Ovm platbu
            $exist_payment = $comgate->getPaymentForOrder($id_order);

            // Platba existuje, ve jsem nael
            if($exist_payment)
            {
                // Zjiuji na brn stav
                $cg_status = $comgate->getStatusPayment((string)$exist_payment['id_comgate']);

                if($cg_status['status'] == 'AUTHORIZED')
                {
                    $cg_cancel_pre_auth = $comgate->getCancelPreAuthPayment((string)$exist_payment['id_comgate']);

                    // Zruen prostedk se zdailo
                    if(isset($cg_cancel_pre_auth['code']) AND $cg_cancel_pre_auth['code'] === '0')
                    {
                        $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'info', ''.$this->l('Zruen blokovanch prostedk bylo provedeno.').'');

                        $comgate->notifyPayment($id_order, (string)$exist_payment['id_comgate']);
                        
                        $this->context->controller->confirmations[] = ''.$this->l('Zruen blokovanch prostedk bylo provedeno.').'';
                    }
                    else
                    {
                        $comgate->writeLog($id_order, (string)$exist_payment['id_comgate'], 'error', $this->l('Zruen blokovanch prostedk se nezdailo!'));
                        $this->context->controller->errors[] = $this->l('Zruen blokovanch prostedk se nezdailo!');
                    }
                }
                else
                {
                    $this->context->controller->errors[] = $this->l('Nelze provst zruen blokovanch prostedk, platba se nenachz ve stavu autorizovno.');
                }
            }
        }

        return;
    }

    // Lev sloupec
    public function hookDisplayLeftColumn()
    {
        return $this->getPaymentsLogosInColumn();
    }

    // Prav sloupec
    public function hookDisplayRightColumn()
    {
        return $this->getPaymentsLogosInColumn();
    }

    // Loga v nkupnm koku a v detailu produktu
    public function hookDisplayReassurance()
    {
        $fields_allowed = explode(',', Configuration::get('DM_COMGATE_LOGOS_PAYMENT_CART'));

        if(in_array('product', $fields_allowed) AND $this->context->controller->php_self == 'product')
        {
            $shop_url = Context::getContext()->shop->getBaseURL(true);

            $this->smarty->assign(
                array(
                    'shop_url' => $shop_url,
                )
            );

            if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
            {
                return $this->display(__FILE__, 'dm_comgate_payment_logos_reaseurance_product.tpl');
            }
            else
            {
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_payment_logos_reaseurance_product.tpl');
            }
        }
        elseif(in_array('cart', $fields_allowed) AND ($this->context->controller->php_self == 'cart' OR $this->context->controller->php_self == 'order'))
        {
            $shop_url = Context::getContext()->shop->getBaseURL(true);

            $this->smarty->assign(
                array(
                    'shop_url' => $shop_url,
                )
            );

            if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
            {
                return $this->display(__FILE__, 'dm_comgate_payment_logos_reaseurance_cart.tpl');
            }
            else
            {
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_payment_logos_reaseurance_cart.tpl');
            }
        }
    }

    public function hookDisplayAdminOrder()
    {
        if(version_compare(_PS_VERSION_, "1.7.7.0", "<"))
        {
            $id_order = (int)Tools::getValue('id_order');

            if($id_order)
            {
                $order_data = new Order((int)$id_order);

                if($order_data->module == 'dm_comgate')
                {
                    $info = Db::getInstance()->executeS('
                        SELECT *
                        FROM `'._DB_PREFIX_.'dm_comgate_log`
                        WHERE `id_order` = '.$id_order.'
                        ORDER BY `id_log` DESC
                    ');

                    $payment = Db::getInstance()->getRow('
                        SELECT *
                        FROM `'._DB_PREFIX_.'dm_comgate_payment`
                        WHERE `id_order` = '.$id_order.'
                    ');

                    $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL');

                    if($type_url_link == 'original')
                    {
                        // Platba nen zaloena pouiji odkaz pes eshop / kontroler, kde se autoamticky paltba zalo
                        if(!$payment)
                        {
                            $url_payment_link = $this->context->link->getModuleLink(
                                'dm_comgate',
                                'cgpayment',
                                array(
                                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference),
                                ),
                                true,
                                (int)$order_data->id_lang,
                                (int)$order_data->id_shop
                            );
                        }
                        else
                        {
                            $url_payment_link = 'https://payments.comgate.cz/'.$payment['id_comgate'].'';
                        }

                    }
                    else
                    {
                        $url_payment_link = $this->context->link->getModuleLink(
                            'dm_comgate',
                            'cgpayment',
                            array(
                                'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                                'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference),
                            ),
                            true,
                            (int)$order_data->id_lang,
                            (int)$order_data->id_shop
                        ); 
                    }

                    if ($payment)
                    {
                        $payment_in_order = Db::getInstance()->getRow('
                            SELECT `id_currency`, SUM(`amount`) as `total`
                            FROM `'._DB_PREFIX_.'order_payment`
                            WHERE `transaction_id` = \''.$payment['id_comgate'].'\'
                        ');
                    }

                    $currency = new Currency($order_data->id_currency);

                    if(isset($currency->precision))
                    {
                        if(_PS_PRICE_DISPLAY_PRECISION_ < $currency->precision)
                        {
                            $c_decimals = (int)_PS_PRICE_DISPLAY_PRECISION_;
                        }
                        else
                        {
                            $c_decimals = (int)$currency->precision;
                        }
                    }
                    else
                    {
                        $c_decimals = (int)$currency->decimals * _PS_PRICE_DISPLAY_PRECISION_;
                    }

                    $this->context->smarty->assign(array(
                        'dm_comgate_id_order' => $id_order,
                        'dm_comgate_id_currency' => ($payment ? (int)$payment_in_order['id_currency'] : false),
                        'dm_comgate_decimals' => (int)$c_decimals,
                        'dm_comgate_info' => $info,
                        'dm_comgate_payment' => $payment,
                        'dm_comgate_payment_link' => $url_payment_link,
                        'dm_comgate_mail_allowed' => (Configuration::get('DM_COMGATE_MAIL_ACTIVE') == 1 ? true : false ),
                        'dm_comgate_refund_data' => (($payment AND $payment_in_order['total'] != null AND $payment['state'] == 'PAID') ? $payment_in_order : false),
                        'dm_comgate_preauth_data' => (($payment AND $payment['state'] == 'AUTHORIZED') ? $payment_in_order : false),
                        'dm_comgate_currency_sign' => $currency->sign,
                        'dm_comgate_order_total_paid' => $order_data->total_paid,
                        'dm_comgate_preauth_id_currency' => (int)$order_data->id_currency,
                    ));

                    if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
                    {
                        return $this->display(__FILE__, 'dm_comgate_detail_order.tpl');
                    }
                    else
                    {
                        return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_detail_order.tpl');
                    }
                }
            }
        }

        return;
    }

    public function hookDisplayAdminOrderMain($params)
    {
        if(version_compare(_PS_VERSION_, "1.7.7.0", ">="))
        {
            $id_order = (int)$params['id_order'];

            if($id_order)
            {
                $order_data = new Order((int)$id_order);

                if($order_data->module == 'dm_comgate')
                {
                    $info = Db::getInstance()->executeS('
                        SELECT *
                        FROM `'._DB_PREFIX_.'dm_comgate_log`
                        WHERE `id_order` = '.$id_order.'
                        ORDER BY `id_log` DESC
                    ');

                    $payment = Db::getInstance()->getRow('
                        SELECT *
                        FROM `'._DB_PREFIX_.'dm_comgate_payment`
                        WHERE `id_order` = '.$id_order.'
                    ');

                    $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL');

                    if($type_url_link == 'original')
                    {
                        // Platba nen zaloena pouiji odkaz pes eshop / kontroler, kde se automaticky platba zalo
                        if(!$payment)
                        {
                            $url_payment_link = $this->context->link->getModuleLink(
                                'dm_comgate',
                                'cgpayment',
                                array(
                                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference),
                                ),
                                true,
                                (int)$order_data->id_lang,
                                (int)$order_data->id_shop
                            );
                        }
                        else
                        {
                            $url_payment_link = 'https://payments.comgate.cz/'.$payment['id_comgate'].'';
                        }

                    }
                    else
                    {
                        $url_payment_link = $this->context->link->getModuleLink(
                            'dm_comgate',
                            'cgpayment',
                            array(
                                'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                                'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order_data->id : (string)$order_data->reference),
                            ),
                            true,
                            (int)$order_data->id_lang,
                            (int)$order_data->id_shop
                        ); 
                    }

                    $payment_in_order = false;

                    if ($payment)
                    {
                        $payment_in_order = Db::getInstance()->getRow('
                            SELECT `id_currency`, SUM(`amount`) as `total`
                            FROM `'._DB_PREFIX_.'order_payment`
                            WHERE `transaction_id` = \''.$payment['id_comgate'].'\'
                        ');
                    }
                    

                    $currency = new Currency($order_data->id_currency);

                    if(isset($currency->precision))
                    {
                        if(_PS_PRICE_DISPLAY_PRECISION_ < $currency->precision)
                        {
                            $c_decimals = (int)_PS_PRICE_DISPLAY_PRECISION_;
                        }
                        else
                        {
                            $c_decimals = (int)$currency->precision;
                        }
                    }
                    else
                    {
                        $c_decimals = (int)$currency->decimals * _PS_PRICE_DISPLAY_PRECISION_;
                    }

                    $this->context->smarty->assign(array(
                        'dm_comgate_id_order' => $id_order,
                        'dm_comgate_id_currency' => ($payment_in_order ? (int)$payment_in_order['id_currency'] : false),
                        'dm_comgate_decimals' => (int)$c_decimals,
                        'dm_comgate_info' => $info,
                        'dm_comgate_payment' => $payment,
                        'dm_comgate_payment_link' => $url_payment_link,
                        'dm_comgate_mail_allowed' => (Configuration::get('DM_COMGATE_MAIL_ACTIVE') == 1 ? true : false ),
                        'dm_comgate_refund_data' => (($payment AND $payment_in_order['total'] != null AND $payment['state'] == 'PAID') ? $payment_in_order : false),
                        'dm_comgate_preauth_data' => (($payment AND $payment['state'] == 'AUTHORIZED') ? $payment_in_order : false),
                        'dm_comgate_currency_sign' => $currency->sign,
                        'dm_comgate_order_total_paid' => $order_data->total_paid,
                        'dm_comgate_preauth_id_currency' => (int)$order_data->id_currency,
                    ));

                    //return $this->display(__FILE__, 'dm_comgate_detail_order_177.tpl');
                    return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_detail_order_177.tpl');
                }
            }
        }

        return;
    }

    // Loga ve sloupcch
    public function getPaymentsLogosInColumn()
    {
        $fields_allowed = explode(',', Configuration::get('DM_COMGATE_LOGOS_PAYMENT_CART'));

        if(in_array('column', $fields_allowed))
        {
            $shop_url = Context::getContext()->shop->getBaseURL(true);

            $this->smarty->assign(
                array(
                    'shop_url' => $shop_url,
                )
            );

            if (version_compare(_PS_VERSION_, "1.7.7.0", "<"))
            {
                return $this->display(__FILE__, 'dm_comgate_payment_logos_column.tpl');
            }
            else
            {
                return $this->fetch('module:dm_comgate/views/templates/hook/dm_comgate_payment_logos_column.tpl');
            }
        }
        else
        {
            return;
        }
    }

    // Zasln mailu zkaznkovi
    public function sendMailPayment($id_order)
    {
        if(Configuration::get('DM_COMGATE_MAIL_ACTIVE') != 1)
        {
            return true;
        }

        $context = Context::getContext();
        $order = new Order((int)$id_order);
        $customer = new Customer((int)$order->id_customer);
        $currency = new Currency($order->id_currency);

        $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL');

        if($type_url_link == 'original')
        {
            $payment_number = Db::getInstance()->getValue('
                SELECT `id_comgate`
                FROM `'._DB_PREFIX_.'dm_comgate_payment`
                WHERE `id_order` = '.(int)$id_order.'
            ');

            // Pokud nen platba, pouiji odkaz na eshop / kontroler, platba se zalo po navtven odkazu
            if(!$payment_number)
            {
                $url_payment = $context->link->getModuleLink(
                    'dm_comgate',
                    'cgpayment',
                    array(
                        'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                        'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference),
                    ),
                    true,
                    (int)$order->id_lang,
                    (int)$order->id_shop
                );
            }
            else
            {
                $url_payment = 'https://payments.comgate.cz/'.$payment_number.'';
            }
        }
        else
        {
            $url_payment = $context->link->getModuleLink(
                'dm_comgate',
                'cgpayment',
                array(
                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference),
                ),
                true,
                (int)$order->id_lang,
                (int)$order->id_shop
            );
        }

        $id_lang = $order->id_lang;
        $templates = 'dm_comgate_mail_customer';
        $subject_mail = Configuration::get('DM_COMGATE_SUBJECT_MAIL_'.$id_lang.'');
        $email = $customer->email;
        $to_name = ''.$customer->firstname.' '.$customer->lastname.'';

        $template_vars = array(
            '{lastname}' => $customer->lastname,
            '{firstname}' => $customer->firstname,
            '{id_order}' => $order->id,
            '{order_reference}' => $order->reference,
            '{total_payment}' => Tools::displayPrice($order->total_paid, $currency, false),
            '{url_payment}' => $url_payment,
        );

        Mail::Send(
            $id_lang,
            $templates,
            $subject_mail,
            $template_vars,
            $email,
            $to_name,
            null,
            null,
            null,
            null,
            dirname(__FILE__).'/mails/'
        );

        return true;
    }


    // Zasln mailu obchodnkovi
    public function sendMailConfirmationPaymentEmployees($id_order)
    {
        if(Configuration::get('DM_COMGATE_EMPLOYEES_MAIL_ACTIVE') != 1)
        {
            return true;
        }

        $order = new Order((int)$id_order);
        $currency = new Currency($order->id_currency);
        $id_lang = $order->id_lang;
        $templates = 'dm_comgate_mail_employees';
        $subject_mail = $this->l('Platba byla spn potvrzena');
        $email = Configuration::get('DM_COMGATE_EMPLOYEES_MAIL_RECIPIENT');
        $to_name = $email;

        $template_vars = array(
            '{id_order}' => $order->id,
            '{order_reference}' => $order->reference,
            '{total_payment}' => Tools::displayPrice($order->total_paid, $currency, false),
        );

        Mail::Send(
            $id_lang,
            $templates,
            $subject_mail,
            $template_vars,
            $email,
            $to_name,
            null,
            null,
            null,
            null,
            dirname(__FILE__).'/mails/'
        );

        return true;
    }

    public function hookActionGetExtraMailTemplateVars($params)
    {
        if (isset($params['template_vars']['{id_order}']))
        {
            $id_order = (int)$params['template_vars']['{id_order}'];

            if($id_order != 0)
            {
                $order = new Order((int)$id_order);

                if($order->module == 'dm_comgate')
                {
                    $context = Context::getContext();

                    $type_url_link = (string)Configuration::get('DM_COMGATE_TYPE_CUSTOMER_MAIL');

                    if($type_url_link == 'original')
                    {
                        $payment_number = Db::getInstance()->getValue('
                            SELECT `id_comgate`
                            FROM `'._DB_PREFIX_.'dm_comgate_payment`
                            WHERE `id_order` = '.(int)$id_order.'
                        ');

                        // Pokud nen platba, pouiji odkaz na eshop / kontroler, platba se zalo po navtven odkazu
                        if(!$payment_number)
                        {
                            $url_payment = $context->link->getModuleLink(
                                'dm_comgate',
                                'cgpayment',
                                array(
                                    'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                                    'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference),
                                ),
                                true,
                                (int)$order->id_lang,
                                (int)$order->id_shop
                            );
                        }

                        $url_payment = 'https://payments.comgate.cz/'.$payment_number.'';
                    }
                    else
                    {
                        $url_payment = $context->link->getModuleLink(
                            'dm_comgate',
                            'cgpayment',
                            array(
                                'token' => substr(Tools::encrypt('dm_comgate/cgpayment'), 0, 10), 
                                'refId' => (Configuration::get('DM_COMGATE_IDENT_PAYMENT') == 1 ? (int)$order->id : (string)$order->reference),
                            ),
                            true,
                            (int)$order->id_lang,
                            (int)$order->id_shop
                        );
                    }

                    $params['extra_template_vars']['{dm_comgate_url_payment}'] = $url_payment;
                }
            }
        }

        return true;
    }

    public function isDisallowedProductInCart($cart)
    {
        // vylouen ID produkt
        $excluded_ids = $this->getExcludedIdProduct();
        $product_in_cart = $cart->getProducts();

        foreach ($product_in_cart AS $product)
        {
            if(in_array((int)$product['id_product'], $excluded_ids))
            {
                return true;
            }
        }

        return false;
    }

    // Vrac ID produkt na kter nelze platit kartou
    public function getExcludedIdProduct()
    {
        $ids_products = Db::getInstance()->executeS('
            SELECT ps.`id_product`
            FROM `'._DB_PREFIX_.'product_shop` ps
            WHERE ps.`dm_comgate_active` = 0
                AND ps.`id_shop` = '.(int)$this->context->shop->id.'
        ');

        $return = array();

        foreach($ids_products as $item_id)
        {
            $return[] = (int)$item_id['id_product'];
        }

        return $return;
    }
}

Function Calls

None

Variables

None

Stats

MD5 a36878f04e8ecdd2895c43673ad103f0
Eval Count 0
Decode Time 264 ms