| Total Complexity | 119 | 
| Total Lines | 912 | 
| Duplicated Lines | 0 % | 
| Changes | 10 | ||
| Bugs | 1 | Features | 0 | 
Complex classes like Pagantis often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pagantis, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 23 | class Pagantis extends PaymentModule | ||
| 24 | { | ||
| 25 | /** | ||
| 26 | * @var string | ||
| 27 | */ | ||
| 28 | public $url = 'https://pagantis.com'; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var bool | ||
| 32 | */ | ||
| 33 | public $bootstrap = true; | ||
| 34 | |||
| 35 | /** @var string $language */ | ||
| 36 | public $language; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Default module advanced configuration values | ||
| 40 | * | ||
| 41 | * @var array | ||
| 42 | */ | ||
| 43 | public $defaultConfigs = array( | ||
| 44 | 'PAGANTIS_TITLE' => 'Instant Financing', | ||
| 45 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE' => 'sdk.simulator.types.SIMPLE', | ||
| 46 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN' => 'sdk.simulator.skins.BLUE', | ||
| 47 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION' => 'hookDisplayProductButtons', | ||
| 48 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS' => '3', | ||
| 49 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR' => 'default', | ||
| 50 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION' => 'sdk.simulator.positions.INNER', | ||
| 51 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR' => 'default', | ||
| 52 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR' => 'default', | ||
| 53 | 'PAGANTIS_FORM_DISPLAY_TYPE' => '0', | ||
| 54 | 'PAGANTIS_DISPLAY_MIN_AMOUNT' => '1', | ||
| 55 | 'PAGANTIS_URL_OK' => '', | ||
| 56 | 'PAGANTIS_URL_KO' => '', | ||
| 57 |         'PAGANTIS_ALLOWED_COUNTRIES' => 'a:3:{i:0;s:2:"es";i:1;s:2:"it";i:2;s:2:"fr";}', | ||
| 58 | 'PAGANTIS_PROMOTION_EXTRA' => 'Finance this product <span class="pmt-no-interest">without interest!</span>', | ||
| 59 | 'PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR' => '.', | ||
| 60 | 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR' => ',', | ||
| 61 | ); | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Pagantis constructor. | ||
| 65 | * | ||
| 66 | * Define the module main properties so that prestashop understands what are the module requirements | ||
| 67 | * and how to manage the module. | ||
| 68 | * | ||
| 69 | */ | ||
| 70 | public function __construct() | ||
| 71 |     { | ||
| 72 | $this->name = 'pagantis'; | ||
| 73 | $this->tab = 'payments_gateways'; | ||
| 74 | $this->version = '8.2.13'; | ||
| 75 | $this->author = 'Pagantis'; | ||
| 76 | $this->currencies = true; | ||
| 77 | $this->currencies_mode = 'checkbox'; | ||
| 78 | $this->module_key = '2b9bc901b4d834bb7069e7ea6510438f'; | ||
| 79 |         $this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_); | ||
| 80 |         $this->displayName = $this->l('Pagantis'); | ||
| 81 | $this->description = $this->l( | ||
| 82 | 'Instant, easy and effective financial tool for your customers' | ||
| 83 | ); | ||
| 84 | |||
| 85 | $sql_file = dirname(__FILE__).'/sql/install.sql'; | ||
| 86 | $this->loadSQLFile($sql_file); | ||
| 87 | |||
| 88 | $this->checkEnvVariables(); | ||
| 89 | |||
| 90 | $this->migrate(); | ||
| 91 | |||
| 92 | $this->checkHooks(); | ||
| 93 | |||
| 94 | $this->checkPromotionCategory(); | ||
| 95 | |||
| 96 | parent::__construct(); | ||
| 97 | |||
| 98 | $this->getUserLanguage(); | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Configure the variables for Pagantis payment method. | ||
| 103 | * | ||
| 104 | * @return bool | ||
| 105 | */ | ||
| 106 | public function install() | ||
| 107 |     { | ||
| 108 |         if (!extension_loaded('curl')) { | ||
| 109 | $this->_errors[] = | ||
| 110 |                 $this->l('You have to enable the cURL extension on your server to install this module'); | ||
| 111 | return false; | ||
| 112 | } | ||
| 113 |         if (!version_compare(phpversion(), '5.3.0', '>=')) { | ||
| 114 |             $this->_errors[] = $this->l('The PHP version bellow 5.3.0 is not supported'); | ||
| 115 | return false; | ||
| 116 | } | ||
| 117 | |||
| 118 |         Configuration::updateValue('pagantis_is_enabled', 1); | ||
| 119 |         Configuration::updateValue('pagantis_simulator_is_enabled', 1); | ||
| 120 |         Configuration::updateValue('pagantis_public_key', ''); | ||
| 121 |         Configuration::updateValue('pagantis_private_key', ''); | ||
| 122 | |||
| 123 | $return = (parent::install() | ||
| 124 |             && $this->registerHook('displayShoppingCart') | ||
| 125 |             && $this->registerHook('paymentOptions') | ||
| 126 |             && $this->registerHook('displayRightColumn') | ||
| 127 |             && $this->registerHook('displayLeftColumn') | ||
| 128 |             && $this->registerHook('displayRightColumnProduct') | ||
| 129 |             && $this->registerHook('displayLeftColumnProduct') | ||
| 130 |             && $this->registerHook('displayProductButtons') | ||
| 131 |             && $this->registerHook('displayOrderConfirmation') | ||
| 132 |             && $this->registerHook('header') | ||
| 133 | ); | ||
| 134 | |||
| 135 |         if ($return && _PS_VERSION_ < "1.7") { | ||
| 136 |             $this->registerHook('payment'); | ||
| 137 | } | ||
| 138 | |||
| 139 | return $return; | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Remove the production private api key and remove the files | ||
| 144 | * | ||
| 145 | * @return bool | ||
| 146 | */ | ||
| 147 | public function uninstall() | ||
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Migrate the configs of older versions < 7x to new configurations | ||
| 157 | */ | ||
| 158 | public function migrate() | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | /** | ||
| 198 | * Check if new hooks used in new 7x versions are enabled and activate them if needed | ||
| 199 | * | ||
| 200 | * @throws PrestaShopDatabaseException | ||
| 201 | */ | ||
| 202 | public function checkHooks() | ||
| 203 |     { | ||
| 204 |         try { | ||
| 205 | $sql_content = 'select * from ' . _DB_PREFIX_. 'hook_module where | ||
| 206 | id_module = \'' . Module::getModuleIdByName($this->name) . '\' and | ||
| 207 | id_shop = \'' . Shop::getContextShopID() . '\' and | ||
| 208 |             id_hook = \'' . Hook::getIdByName('header') . '\''; | ||
| 209 | $hook_exists = Db::getInstance()->ExecuteS($sql_content); | ||
| 210 |             if (empty($hook_exists)) { | ||
| 211 | $sql_insert = 'insert into ' . _DB_PREFIX_. 'hook_module | ||
| 212 | (id_module, id_shop, id_hook, position) | ||
| 213 | values | ||
| 214 | (\''. Module::getModuleIdByName($this->name) . '\', | ||
| 215 | \''. Shop::getContextShopID() . '\', | ||
| 216 |             \''. Hook::getIdByName('header') . '\', | ||
| 217 | 150)'; | ||
| 218 | Db::getInstance()->execute($sql_insert); | ||
| 219 | } | ||
| 220 |         } catch (\Exception $exception) { | ||
| 221 | // continue without errors | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | /** | ||
| 226 | * @throws PrestaShopDatabaseException | ||
| 227 | */ | ||
| 228 | public function checkEnvVariables() | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | /** | ||
| 252 | * @param $sql_file | ||
| 253 | * @return bool | ||
| 254 | */ | ||
| 255 | public function loadSQLFile($sql_file) | ||
| 256 |     { | ||
| 257 |         try { | ||
| 258 | $tableName = _DB_PREFIX_.'pagantis_order'; | ||
| 259 |             $sql = ("SHOW TABLES LIKE '$tableName'"); | ||
| 260 | $results = Db::getInstance()->ExecuteS($sql); | ||
| 261 |             if (is_array($results) && count($results) === 1) { | ||
| 262 | $query = "select COLUMN_TYPE FROM information_schema.COLUMNS where TABLE_NAME='$tableName' AND COLUMN_NAME='ps_order_id'"; | ||
| 263 | $results = $results = Db::getInstance()->ExecuteS($query); | ||
| 264 |                 if (is_array($results) && count($results) === 0) { | ||
| 265 | $sql = "ALTER TABLE $tableName ADD COLUMN ps_order_id VARCHAR(60) AFTER order_id"; | ||
| 266 | Db::getInstance()->Execute($sql); | ||
| 267 | } | ||
| 268 | return false; | ||
| 269 | } | ||
| 270 |         } catch (\Exception $exception) { | ||
| 271 | // do nothing | ||
| 272 | } | ||
| 273 | |||
| 274 | $sql_content = Tools::file_get_contents($sql_file); | ||
| 275 |         $sql_content = str_replace('PREFIX_', _DB_PREFIX_, $sql_content); | ||
| 276 |         $sql_requests = preg_split("/;\s*[\r\n]+/", $sql_content); | ||
| 277 | |||
| 278 | $result = true; | ||
| 279 |         foreach ($sql_requests as $request) { | ||
| 280 |             if (!empty($request)) { | ||
| 281 | $result &= Db::getInstance()->execute(trim($request)); | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | return $result; | ||
| 286 | } | ||
| 287 | |||
| 288 | /** | ||
| 289 | * Check amount of order > minAmount | ||
| 290 | * Check valid currency | ||
| 291 | * Check API variables are set | ||
| 292 | * | ||
| 293 | * @return bool | ||
| 294 | * @throws PrestaShopDatabaseException | ||
| 295 | * @throws PrestaShopException | ||
| 296 | */ | ||
| 297 | public function isPaymentMethodAvailable() | ||
| 298 |     { | ||
| 299 | $cart = $this->context->cart; | ||
| 300 | $currency = new Currency($cart->id_currency); | ||
| 301 |         $availableCurrencies       = array('EUR'); | ||
| 302 |         $pagantisDisplayMinAmount  = Pagantis::getExtraConfig('PAGANTIS_DISPLAY_MIN_AMOUNT'); | ||
| 303 |         $pagantisPublicKey         = Configuration::get('pagantis_public_key'); | ||
| 304 |         $pagantisPrivateKey        = Configuration::get('pagantis_private_key'); | ||
| 305 |         $allowedCountries          = unserialize(Pagantis::getExtraConfig('PAGANTIS_ALLOWED_COUNTRIES')); | ||
| 306 | return ( | ||
| 307 | $cart->getOrderTotal() >= $pagantisDisplayMinAmount && | ||
| 308 | in_array($currency->iso_code, $availableCurrencies) && | ||
| 309 | in_array(Tools::strtolower($this->language), $allowedCountries) && | ||
| 310 | $pagantisPublicKey && | ||
| 311 | $pagantisPrivateKey | ||
| 312 | ); | ||
| 313 | } | ||
| 314 | |||
| 315 | /** | ||
| 316 | * @param Cart $cart | ||
| 317 | * | ||
| 318 | * @return array | ||
| 319 | * @throws Exception | ||
| 320 | */ | ||
| 321 | private function getButtonTemplateVars(Cart $cart) | ||
| 322 |     { | ||
| 323 | $currency = new Currency(($cart->id_currency)); | ||
| 324 | |||
| 325 | return array( | ||
| 326 | 'pagantis_button' => '#pagantis_payment_button', | ||
| 327 | 'pagantis_currency_iso' => $currency->iso_code, | ||
| 328 | 'pagantis_cart_total' => $cart->getOrderTotal(), | ||
| 329 | ); | ||
| 330 | } | ||
| 331 | |||
| 332 | /** | ||
| 333 | * Header hook | ||
| 334 | */ | ||
| 335 | public function hookHeader() | ||
| 336 |     { | ||
| 337 | $url = 'https://cdn.pagantis.com/js/pg-v2/sdk.js'; | ||
| 338 |         if ($this->language == 'ES' || $this->language == null) { | ||
| 339 | $url = 'https://cdn.pagantis.com/js/pmt-v2/sdk.js'; | ||
| 340 | } | ||
| 341 |         if (_PS_VERSION_ >= "1.7") { | ||
| 342 | $this->context->controller->registerJavascript( | ||
| 343 | sha1(mt_rand(1, 90000)), | ||
| 344 | $url, | ||
| 345 |                 array('server' => 'remote') | ||
| 346 | ); | ||
| 347 |         } else { | ||
| 348 | $this->context->controller->addJS($url); | ||
| 349 | } | ||
| 350 | $this->context->controller->addJS($this->getPathUri(). 'views/js/simulator.js'); | ||
| 351 | } | ||
| 352 | |||
| 353 | /** | ||
| 354 | * @return array | ||
| 355 | * @throws Exception | ||
| 356 | */ | ||
| 357 | public function hookPaymentOptions() | ||
| 358 |     { | ||
| 359 |         if (!$this->isPaymentMethodAvailable()) { | ||
| 360 | return array(); | ||
| 361 | } | ||
| 362 | |||
| 363 | /** @var Cart $cart */ | ||
| 364 | $cart = $this->context->cart; | ||
| 365 | $orderTotal = $cart->getOrderTotal(); | ||
| 366 | $promotedAmount = 0; | ||
| 367 | $link = $this->context->link; | ||
| 368 |         $pagantisPublicKey                  = Configuration::get('pagantis_public_key'); | ||
| 369 |         $pagantisSimulatorIsEnabled         = Configuration::get('pagantis_simulator_is_enabled'); | ||
| 370 |         $pagantisIsEnabled                  = Configuration::get('pagantis_is_enabled'); | ||
| 371 |         $pagantisSimulatorType              = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); | ||
| 372 |         $pagantisSimulatorCSSSelector       = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); | ||
| 373 |         $pagantisSimulatorPriceSelector     = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); | ||
| 374 |         $pagantisSimulatorQuotesStart       = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); | ||
| 375 |         $pagantisSimulatorSkin              = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); | ||
| 376 |         $pagantisSimulatorPosition          = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); | ||
| 377 |         $pagantisTitle                      = $this->l(Pagantis::getExtraConfig('PAGANTIS_TITLE')); | ||
| 378 |         $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); | ||
| 379 |         $pagantisSimulatorDecimalSeparator  = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); | ||
| 380 | |||
| 381 | $items = $cart->getProducts(true); | ||
| 382 |         foreach ($items as $key => $item) { | ||
| 383 | $itemCategories = ProductCore::getProductCategoriesFull($item['id_product']); | ||
| 384 |             if (in_array(PROMOTIONS_CATEGORY_NAME, $this->arrayColumn($itemCategories, 'name')) !== false) { | ||
| 385 | $productId = $item['id_product']; | ||
| 386 | $promotedAmount += Product::getPriceStatic($productId); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | $this->context->smarty->assign($this->getButtonTemplateVars($cart)); | ||
| 391 | $this->context->smarty->assign(array( | ||
| 392 | 'amount' => $orderTotal, | ||
| 393 | 'locale' => $this->language, | ||
| 394 | 'country' => $this->language, | ||
| 395 | 'pagantisPublicKey' => $pagantisPublicKey, | ||
| 396 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, | ||
| 397 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, | ||
| 398 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, | ||
| 399 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, | ||
| 400 | 'pagantisSimulatorType' => $pagantisSimulatorType, | ||
| 401 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, | ||
| 402 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, | ||
| 403 | 'pagantisIsEnabled' => $pagantisIsEnabled, | ||
| 404 | 'pagantisTitle' => $pagantisTitle, | ||
| 405 |             'paymentUrl'                         => $link->getModuleLink('pagantis', 'payment'), | ||
| 406 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, | ||
| 407 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, | ||
| 408 | 'promotedAmount' => $promotedAmount, | ||
| 409 |             'ps_version'                         => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), | ||
| 410 | )); | ||
| 411 | |||
| 412 | $logo = ($this->language == 'ES' || $this->language == null) ? 'logo_pagamastarde.png' : 'logo_pagantis.png'; | ||
| 413 | $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); | ||
| 414 | $paymentOption | ||
| 415 | ->setCallToActionText($pagantisTitle) | ||
| 416 |             ->setAction($link->getModuleLink('pagantis', 'payment')) | ||
| 417 | ->setLogo($this->getPathUri(). 'views/img/' . $logo) | ||
| 418 | ->setModuleName(__CLASS__) | ||
| 419 | ; | ||
| 420 | |||
| 421 | $paymentOption->setAdditionalInformation( | ||
| 422 |             $this->fetch('module:pagantis/views/templates/hook/checkout.tpl') | ||
| 423 | ); | ||
| 424 | |||
| 425 | return array($paymentOption); | ||
| 426 | } | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Get the form for editing the BackOffice options of the module | ||
| 430 | * | ||
| 431 | * @return array | ||
| 432 | */ | ||
| 433 | private function getConfigForm() | ||
| 434 |     { | ||
| 435 | return array( | ||
| 436 | 'form' => array( | ||
| 437 | 'legend' => array( | ||
| 438 |                     'title' => $this->l('Basic Settings'), | ||
| 439 | 'icon' => 'icon-cogs', | ||
| 440 | ), | ||
| 441 | 'input' => array( | ||
| 442 | array( | ||
| 443 | 'name' => 'pagantis_is_enabled', | ||
| 444 | 'type' => (version_compare(_PS_VERSION_, '1.6')<0) ?'radio' :'switch', | ||
| 445 |                         'label' => $this->l('Module is enabled'), | ||
| 446 | 'prefix' => '<i class="icon icon-key"></i>', | ||
| 447 | 'class' => 't', | ||
| 448 | 'required' => true, | ||
| 449 | 'values'=> array( | ||
| 450 | array( | ||
| 451 | 'id' => 'pagantis_is_enabled_true', | ||
| 452 | 'value' => 1, | ||
| 453 |                                 'label' => $this->l('Yes', get_class($this), null, false), | ||
| 454 | ), | ||
| 455 | array( | ||
| 456 | 'id' => 'pagantis_is_enabled_false', | ||
| 457 | 'value' => 0, | ||
| 458 |                                 'label' => $this->l('No', get_class($this), null, false), | ||
| 459 | ), | ||
| 460 | ) | ||
| 461 | ), | ||
| 462 | array( | ||
| 463 | 'name' => 'pagantis_public_key', | ||
| 464 |                         'suffix' => $this->l('ex: pk_fd53cd467ba49022e4gf215e'), | ||
| 465 | 'type' => 'text', | ||
| 466 | 'size' => 60, | ||
| 467 |                         'label' => $this->l('Public Key'), | ||
| 468 | 'prefix' => '<i class="icon icon-key"></i>', | ||
| 469 | 'col' => 6, | ||
| 470 | 'required' => true, | ||
| 471 | ), | ||
| 472 | array( | ||
| 473 | 'name' => 'pagantis_private_key', | ||
| 474 |                         'suffix' => $this->l('ex: 21e5723a97459f6a'), | ||
| 475 | 'type' => 'text', | ||
| 476 | 'size' => 60, | ||
| 477 |                         'label' => $this->l('Secret Key'), | ||
| 478 | 'prefix' => '<i class="icon icon-key"></i>', | ||
| 479 | 'col' => 6, | ||
| 480 | 'required' => true, | ||
| 481 | ), | ||
| 482 | array( | ||
| 483 | 'name' => 'pagantis_simulator_is_enabled', | ||
| 484 | 'type' => (version_compare(_PS_VERSION_, '1.6')<0) ?'radio' :'switch', | ||
| 485 |                         'label' => $this->l('Simulator is enabled'), | ||
| 486 | 'prefix' => '<i class="icon icon-key"></i>', | ||
| 487 | 'class' => 't', | ||
| 488 | 'required' => true, | ||
| 489 | 'values'=> array( | ||
| 490 | array( | ||
| 491 | 'id' => 'pagantis_simulator_is_enabled_on', | ||
| 492 | 'value' => 1, | ||
| 493 |                                 'label' => $this->l('Yes'), | ||
| 494 | ), | ||
| 495 | array( | ||
| 496 | 'id' => 'pagantis_simulator_is_enabled_off', | ||
| 497 | 'value' => 0, | ||
| 498 |                                 'label' => $this->l('No'), | ||
| 499 | ), | ||
| 500 | ) | ||
| 501 | ), | ||
| 502 | ), | ||
| 503 | 'submit' => array( | ||
| 504 |                     'title' => $this->l('Save'), | ||
| 505 | ), | ||
| 506 | ), | ||
| 507 | ); | ||
| 508 | } | ||
| 509 | |||
| 510 | /** | ||
| 511 | * Form configuration function | ||
| 512 | * | ||
| 513 | * @param array $settings | ||
| 514 | * | ||
| 515 | * @return string | ||
| 516 | */ | ||
| 517 | private function renderForm(array $settings) | ||
| 518 |     { | ||
| 519 | $helper = new HelperForm(); | ||
| 520 | $helper->show_toolbar = false; | ||
| 521 | $helper->table = $this->table; | ||
| 522 | $helper->module = $this; | ||
| 523 | $helper->default_form_language = $this->context->language->id; | ||
| 524 |         $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); | ||
| 525 | $helper->identifier = $this->identifier; | ||
| 526 | $helper->submit_action = 'submit'.$this->name; | ||
| 527 | $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; | ||
| 528 |         $helper->token = Tools::getAdminTokenLite('AdminModules'); | ||
| 529 | $helper->tpl_vars = array( | ||
| 530 | 'fields_value' => $settings, | ||
| 531 | 'languages' => $this->context->controller->getLanguages(), | ||
| 532 | 'id_language' => $this->context->language->id, | ||
| 533 | ); | ||
| 534 | |||
| 535 |         $helper->fields_value['pagantis_url_ok'] = Configuration::get('pagantis_url_ok'); | ||
| 536 | |||
| 537 | return $helper->generateForm(array($this->getConfigForm())); | ||
| 538 | } | ||
| 539 | |||
| 540 | /** | ||
| 541 | * Function to update the variables of Pagantis Module in the backoffice of prestashop | ||
| 542 | * | ||
| 543 | * @return string | ||
| 544 | * @throws SmartyException | ||
| 545 | */ | ||
| 546 | public function getContent() | ||
| 547 |     { | ||
| 548 | $error = ''; | ||
| 549 | $message = ''; | ||
| 550 | $settings = array(); | ||
| 551 |         $settings['pagantis_public_key'] = Configuration::get('pagantis_public_key'); | ||
| 552 |         $settings['pagantis_private_key'] = Configuration::get('pagantis_private_key'); | ||
| 553 | $settingsKeys = array( | ||
| 554 | 'pagantis_is_enabled', | ||
| 555 | 'pagantis_public_key', | ||
| 556 | 'pagantis_private_key', | ||
| 557 | 'pagantis_simulator_is_enabled', | ||
| 558 | ); | ||
| 559 | |||
| 560 | //Different Behavior depending on 1.6 or earlier | ||
| 561 |         if (Tools::isSubmit('submit'.$this->name)) { | ||
| 562 |             foreach ($settingsKeys as $key) { | ||
| 563 |                 switch ($key) { | ||
| 564 | case 'pagantis_public_key': | ||
| 565 | $value = Tools::getValue($key); | ||
| 566 |                         if (!$value) { | ||
| 567 |                             $error = $this->l('Please add a Pagantis API Public Key'); | ||
| 568 | break; | ||
| 569 | } | ||
| 570 | Configuration::updateValue($key, $value); | ||
| 571 | $settings[$key] = $value; | ||
| 572 | break; | ||
| 573 | case 'pagantis_private_key': | ||
| 574 | $value = Tools::getValue($key); | ||
| 575 |                         if (!$value) { | ||
| 576 |                             $error = $this->l('Please add a Pagantis API Private Key'); | ||
| 577 | break; | ||
| 578 | } | ||
| 579 | Configuration::updateValue($key, $value); | ||
| 580 | $settings[$key] = $value; | ||
| 581 | break; | ||
| 582 | default: | ||
| 583 | $value = Tools::getValue($key); | ||
| 584 | Configuration::updateValue($key, $value); | ||
| 585 | $settings[$key] = $value; | ||
| 586 | break; | ||
| 587 | } | ||
| 588 |                 $message = $this->displayConfirmation($this->l('All changes have been saved')); | ||
| 589 | } | ||
| 590 |         } else { | ||
| 591 |             foreach ($settingsKeys as $key) { | ||
| 592 | $settings[$key] = Configuration::get($key); | ||
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 |         if ($error) { | ||
| 597 | $message = $this->displayError($error); | ||
| 598 | } | ||
| 599 | |||
| 600 | $logo = $this->getPathUri(). 'views/img/logo_pagantis.png'; | ||
| 601 |         if ($this->language == 'ES' || $this->language == null) { | ||
| 602 | $logo = $this->getPathUri(). 'views/img/logo_pagamastarde.png'; | ||
| 603 | } | ||
| 604 | $tpl = $this->local_path.'views/templates/admin/config-info.tpl'; | ||
| 605 | $this->context->smarty->assign(array( | ||
| 606 | 'logo' => $logo, | ||
| 607 | 'form' => $this->renderForm($settings), | ||
| 608 | 'message' => $message, | ||
| 609 | 'version' => 'v'.$this->version, | ||
| 610 | )); | ||
| 611 | |||
| 612 | return $this->context->smarty->fetch($tpl); | ||
| 613 | } | ||
| 614 | |||
| 615 | /** | ||
| 616 | * Hook to show payment method, this only applies on prestashop <= 1.6 | ||
| 617 | * | ||
| 618 | * @param $params | ||
| 619 | * @return bool | string | ||
| 620 | * @throws Exception | ||
| 621 | */ | ||
| 622 | public function hookPayment($params) | ||
| 623 |     { | ||
| 624 |         if (!$this->isPaymentMethodAvailable()) { | ||
| 625 | return false; | ||
| 626 | } | ||
| 627 | |||
| 628 | /** @var Cart $cart */ | ||
| 629 | |||
| 630 | $cart = $params['cart']; | ||
| 631 | $orderTotal = $cart->getOrderTotal(); | ||
| 632 | $promotedAmount = 0; | ||
| 633 | $link = $this->context->link; | ||
| 634 |         $pagantisPublicKey                  = Configuration::get('pagantis_public_key'); | ||
| 635 |         $pagantisSimulatorIsEnabled         = Configuration::get('pagantis_simulator_is_enabled'); | ||
| 636 |         $pagantisIsEnabled                  = Configuration::get('pagantis_is_enabled'); | ||
| 637 |         $pagantisSimulatorType              = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); | ||
| 638 |         $pagantisSimulatorCSSSelector       = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); | ||
| 639 |         $pagantisSimulatorPriceSelector     = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); | ||
| 640 |         $pagantisSimulatorQuotesStart       = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); | ||
| 641 |         $pagantisSimulatorSkin              = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); | ||
| 642 |         $pagantisSimulatorPosition          = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); | ||
| 643 |         $pagantisTitle                      = $this->l(Pagantis::getExtraConfig('PAGANTIS_TITLE')); | ||
| 644 |         $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); | ||
| 645 |         $pagantisSimulatorDecimalSeparator  = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); | ||
| 646 | |||
| 647 | $items = $cart->getProducts(true); | ||
| 648 |         foreach ($items as $key => $item) { | ||
| 649 | $itemCategories = ProductCore::getProductCategoriesFull($item['id_product']); | ||
| 650 |             if (in_array(PROMOTIONS_CATEGORY_NAME, $this->arrayColumn($itemCategories, 'name')) !== false) { | ||
| 651 | $productId = $item['id_product']; | ||
| 652 | $promotedAmount += Product::getPriceStatic($productId); | ||
| 653 | } | ||
| 654 | } | ||
| 655 | |||
| 656 | $this->context->smarty->assign($this->getButtonTemplateVars($cart)); | ||
| 657 | $this->context->smarty->assign(array( | ||
| 658 | 'amount' => $orderTotal, | ||
| 659 | 'promotedAmount' => $promotedAmount, | ||
| 660 | 'locale' => $this->language, | ||
| 661 | 'country' => $this->language, | ||
| 662 | 'logo' => ($this->language == 'ES' || $this->language == null) ? 'pagamastarde.png' : 'pagantis.png', | ||
| 663 | 'pagantisPublicKey' => $pagantisPublicKey, | ||
| 664 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, | ||
| 665 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, | ||
| 666 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, | ||
| 667 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, | ||
| 668 | 'pagantisSimulatorType' => $pagantisSimulatorType, | ||
| 669 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, | ||
| 670 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, | ||
| 671 | 'pagantisIsEnabled' => $pagantisIsEnabled, | ||
| 672 | 'pagantisTitle' => $pagantisTitle, | ||
| 673 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, | ||
| 674 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, | ||
| 675 |             'paymentUrl'                         => $link->getModuleLink('pagantis', 'payment'), | ||
| 676 |             'ps_version'                         => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), | ||
| 677 | )); | ||
| 678 | |||
| 679 |         $supercheckout_enabled = Module::isEnabled('supercheckout'); | ||
| 680 |         $onepagecheckoutps_enabled = Module::isEnabled('onepagecheckoutps'); | ||
| 681 |         $onepagecheckout_enabled = Module::isEnabled('onepagecheckout'); | ||
| 682 | |||
| 683 | $return = true; | ||
| 684 |         if ($supercheckout_enabled || $onepagecheckout_enabled || $onepagecheckoutps_enabled) { | ||
| 685 | $this->checkLogoExists(); | ||
| 686 | $return = $this->display(__FILE__, 'views/templates/hook/onepagecheckout.tpl'); | ||
| 687 |         } elseif (_PS_VERSION_ < 1.7) { | ||
| 688 | $return = $this->display(__FILE__, 'views/templates/hook/checkout.tpl'); | ||
| 689 | } | ||
| 690 | return $return; | ||
| 691 | } | ||
| 692 | |||
| 693 | /** | ||
| 694 | * @param string $functionName | ||
| 695 | *: | ||
| 696 | * @return string | ||
| 697 | * @throws PrestaShopDatabaseException | ||
| 698 | * @throws PrestaShopException | ||
| 699 | */ | ||
| 700 | public function productPageSimulatorDisplay($functionName) | ||
| 701 |     { | ||
| 702 |         $productConfiguration = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_POSITION'); | ||
| 703 |         $productId = Tools::getValue('id_product'); | ||
| 704 |         if (!$productId) { | ||
| 705 | return false; | ||
| 706 | } | ||
| 707 | //Resolves bug of reference passtrow | ||
| 708 | $amount = Product::getPriceStatic($productId); | ||
| 709 | |||
| 710 | $itemCategoriesNames = $this->arrayColumn(Product::getProductCategoriesFull($productId), 'name'); | ||
| 711 | $isPromotedProduct = in_array(PROMOTIONS_CATEGORY_NAME, $itemCategoriesNames); | ||
| 712 | |||
| 713 |         $pagantisPublicKey                  = Configuration::get('pagantis_public_key'); | ||
| 714 |         $pagantisSimulatorIsEnabled         = Configuration::get('pagantis_simulator_is_enabled'); | ||
| 715 |         $pagantisIsEnabled                  = Configuration::get('pagantis_is_enabled'); | ||
| 716 |         $pagantisSimulatorType              = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); | ||
| 717 |         $pagantisSimulatorCSSSelector       = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); | ||
| 718 |         $pagantisSimulatorPriceSelector     = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); | ||
| 719 |         $pagantisSimulatorQuantitySelector  = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'); | ||
| 720 |         $pagantisSimulatorQuotesStart       = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); | ||
| 721 |         $pagantisSimulatorSkin              = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); | ||
| 722 |         $pagantisSimulatorPosition          = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); | ||
| 723 |         $pagantisDisplayMinAmount           = Pagantis::getExtraConfig('PAGANTIS_DISPLAY_MIN_AMOUNT'); | ||
| 724 |         $pagantisPromotionExtra             = Pagantis::getExtraConfig('PAGANTIS_PROMOTION_EXTRA'); | ||
| 725 |         $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); | ||
| 726 |         $pagantisSimulatorDecimalSeparator  = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); | ||
| 727 | |||
| 728 | if ($functionName != $productConfiguration || | ||
| 729 | $amount <= 0 || | ||
| 730 | $amount < $pagantisDisplayMinAmount || | ||
| 731 | !$pagantisSimulatorType | ||
| 732 |         ) { | ||
| 733 | return null; | ||
| 734 | } | ||
| 735 | |||
| 736 | $this->context->smarty->assign(array( | ||
| 737 | 'amount' => $amount, | ||
| 738 | 'locale' => $this->language, | ||
| 739 | 'country' => $this->language, | ||
| 740 | 'pagantisPublicKey' => $pagantisPublicKey, | ||
| 741 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, | ||
| 742 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, | ||
| 743 | 'pagantisQuantitySelector' => $pagantisSimulatorQuantitySelector, | ||
| 744 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, | ||
| 745 | 'pagantisIsEnabled' => $pagantisIsEnabled, | ||
| 746 | 'pagantisSimulatorType' => $pagantisSimulatorType, | ||
| 747 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, | ||
| 748 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, | ||
| 749 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, | ||
| 750 | 'isPromotedProduct' => $isPromotedProduct, | ||
| 751 | 'pagantisPromotionExtra' => Tools::htmlentitiesDecodeUTF8($this->l($pagantisPromotionExtra)), | ||
| 752 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, | ||
| 753 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, | ||
| 754 |             'ps_version'                         => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), | ||
| 755 | )); | ||
| 756 | |||
| 757 | return $this->display(__FILE__, 'views/templates/hook/product-simulator.tpl'); | ||
| 758 | } | ||
| 759 | |||
| 760 | /** | ||
| 761 | * @return string | ||
| 762 | * @throws PrestaShopDatabaseException | ||
| 763 | * @throws PrestaShopException | ||
| 764 | */ | ||
| 765 | public function hookDisplayRightColumn() | ||
| 766 |     { | ||
| 767 | |||
| 768 | return $this->productPageSimulatorDisplay(__FUNCTION__); | ||
| 769 | } | ||
| 770 | |||
| 771 | /** | ||
| 772 | * @return string | ||
| 773 | * @throws PrestaShopDatabaseException | ||
| 774 | * @throws PrestaShopException | ||
| 775 | */ | ||
| 776 | public function hookDisplayLeftColumn() | ||
| 777 |     { | ||
| 778 | return $this->productPageSimulatorDisplay(__FUNCTION__); | ||
| 779 | } | ||
| 780 | |||
| 781 | /** | ||
| 782 | * @return string | ||
| 783 | * @throws PrestaShopDatabaseException | ||
| 784 | * @throws PrestaShopException | ||
| 785 | */ | ||
| 786 | public function hookDisplayRightColumnProduct() | ||
| 787 |     { | ||
| 788 | return $this->productPageSimulatorDisplay(__FUNCTION__); | ||
| 789 | } | ||
| 790 | |||
| 791 | /** | ||
| 792 | * @return string | ||
| 793 | * @throws PrestaShopDatabaseException | ||
| 794 | * @throws PrestaShopException | ||
| 795 | */ | ||
| 796 | public function hookDisplayLeftColumnProduct() | ||
| 797 |     { | ||
| 798 | return $this->productPageSimulatorDisplay(__FUNCTION__); | ||
| 799 | } | ||
| 800 | |||
| 801 | /** | ||
| 802 | * @return string | ||
| 803 | * @throws PrestaShopDatabaseException | ||
| 804 | * @throws PrestaShopException | ||
| 805 | */ | ||
| 806 | public function hookDisplayProductButtons() | ||
| 807 |     { | ||
| 808 | return $this->productPageSimulatorDisplay(__FUNCTION__); | ||
| 809 | } | ||
| 810 | |||
| 811 | /** | ||
| 812 | * @param array $params | ||
| 813 | * | ||
| 814 | * @return string | ||
| 815 | */ | ||
| 816 | public function hookDisplayOrderConfirmation($params) | ||
| 817 |     { | ||
| 818 | $paymentMethod = (_PS_VERSION_ < 1.7) ? ($params["objOrder"]->payment) : ($params["order"]->payment); | ||
| 819 | |||
| 820 |         if ($paymentMethod == $this->displayName) { | ||
| 821 | return $this->display(__FILE__, 'views/templates/hook/payment-return.tpl'); | ||
| 822 | } | ||
| 823 | |||
| 824 | return null; | ||
| 825 | } | ||
| 826 | |||
| 827 | /** | ||
| 828 | * Check logo exists in OPC module | ||
| 829 | */ | ||
| 830 | public function checkLogoExists() | ||
| 844 | ); | ||
| 845 | } | ||
| 846 | } | ||
| 847 | |||
| 848 | /** | ||
| 849 | * checkPromotionCategory | ||
| 850 | */ | ||
| 851 | public function checkPromotionCategory() | ||
| 852 |     { | ||
| 853 | $categories = $this->arrayColumn(Category::getCategories(null, false, false), 'name'); | ||
| 854 |         if (!in_array(PROMOTIONS_CATEGORY_NAME, $categories)) { | ||
| 855 | /** @var CategoryCore $category */ | ||
| 856 | $category = new Category(); | ||
| 857 |             $categoryArray = array((int)Configuration::get('PS_LANG_DEFAULT')=> PROMOTIONS_CATEGORY ); | ||
| 858 | $category->is_root_category = false; | ||
| 859 | $category->link_rewrite = $categoryArray; | ||
| 860 | $category->meta_description = $categoryArray; | ||
| 861 | $category->meta_keywords = $categoryArray; | ||
| 862 | $category->meta_title = $categoryArray; | ||
| 863 |             $category->name = array((int)Configuration::get('PS_LANG_DEFAULT')=> PROMOTIONS_CATEGORY_NAME); | ||
| 864 |             $category->id_parent = Configuration::get('PS_HOME_CATEGORY'); | ||
| 865 | $category->active=0; | ||
| 866 | $description = 'Pagantis: Products with this category have free financing assumed by the merchant. ' . | ||
| 867 | 'Use it to promote your products or brands.'; | ||
| 868 | $category->description = $this->l($description); | ||
| 869 | $category->save(); | ||
| 870 | } | ||
| 871 | } | ||
| 872 | |||
| 873 | |||
| 874 | public static function getExtraConfig($config = null, $default = '') | ||
| 875 |     { | ||
| 876 |         if (is_null($config)) { | ||
| 877 | return ''; | ||
| 878 | } | ||
| 879 | |||
| 880 | $sql = 'SELECT value FROM '._DB_PREFIX_.'pagantis_config where config = \'' . pSQL($config) . '\' limit 1'; | ||
| 881 |         if ($results = Db::getInstance()->ExecuteS($sql)) { | ||
| 882 |             if (is_array($results) && count($results) === 1 && isset($results[0]['value'])) { | ||
| 883 | return $results[0]['value']; | ||
| 884 | } | ||
| 885 | } | ||
| 886 | |||
| 887 | return $default; | ||
| 888 | } | ||
| 889 | |||
| 890 | /** | ||
| 891 | * Get user language | ||
| 892 | */ | ||
| 893 | private function getUserLanguage() | ||
| 903 | } | ||
| 904 | |||
| 905 | /** | ||
| 906 | * @param array $input | ||
| 907 | * @param $columnKey | ||
| 908 | * @param null $indexKey | ||
| 909 | * | ||
| 910 | * @return array|bool | ||
| 911 | */ | ||
| 912 | private function arrayColumn(array $input, $columnKey, $indexKey = null) | ||
| 913 |     { | ||
| 914 | $array = array(); | ||
| 915 |         foreach ($input as $value) { | ||
| 916 |             if (!array_key_exists($columnKey, $value)) { | ||
| 917 |                 trigger_error("Key \"$columnKey\" does not exist in array"); | ||
| 918 | return false; | ||
| 919 | } | ||
| 920 |             if (is_null($indexKey)) { | ||
| 921 | $array[] = $value[$columnKey]; | ||
| 937 |