| Total Complexity | 105 |
| Total Lines | 838 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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 | /** |
||
| 36 | * Default module advanced configuration values |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | public $defaultConfigs = array( |
||
| 41 | 'PAGANTIS_TITLE' => 'Instant Financing', |
||
| 42 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE' => 'sdk.simulator.types.SIMPLE', |
||
| 43 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN' => 'sdk.simulator.skins.BLUE', |
||
| 44 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION' => 'hookDisplayProductButtons', |
||
| 45 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS' => '3', |
||
| 46 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR' => 'default', |
||
| 47 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION' => 'sdk.simulator.positions.INNER', |
||
| 48 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR' => 'default', |
||
| 49 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR' => 'default', |
||
| 50 | 'PAGANTIS_FORM_DISPLAY_TYPE' => '0', |
||
| 51 | 'PAGANTIS_DISPLAY_MIN_AMOUNT' => '1', |
||
| 52 | 'PAGANTIS_URL_OK' => '', |
||
| 53 | 'PAGANTIS_URL_KO' => '', |
||
| 54 | 'PAGANTIS_ALLOWED_COUNTRIES' => 'a:2:{i:0;s:2:"es";i:1;s:2:"it";}', |
||
| 55 | 'PAGANTIS_PROMOTION_EXTRA' => 'Finance this product without interest! - 0% TAE', |
||
| 56 | 'PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR' => '.', |
||
| 57 | 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR' => ',', |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Pagantis constructor. |
||
| 62 | * |
||
| 63 | * Define the module main properties so that prestashop understands what are the module requirements |
||
| 64 | * and how to manage the module. |
||
| 65 | * |
||
| 66 | */ |
||
| 67 | public function __construct() |
||
| 68 | { |
||
| 69 | $this->name = 'pagantis'; |
||
| 70 | $this->tab = 'payments_gateways'; |
||
| 71 | $this->version = '8.2.0'; |
||
| 72 | $this->author = 'Pagantis'; |
||
| 73 | $this->currencies = true; |
||
| 74 | $this->currencies_mode = 'checkbox'; |
||
| 75 | $this->module_key = '2b9bc901b4d834bb7069e7ea6510438f'; |
||
| 76 | $this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_); |
||
| 77 | $this->displayName = $this->l('Pagantis'); |
||
| 78 | $this->description = $this->l( |
||
| 79 | 'Instant, easy and effective financial tool for your customers' |
||
| 80 | ); |
||
| 81 | |||
| 82 | $sql_file = dirname(__FILE__).'/sql/install.sql'; |
||
| 83 | $this->loadSQLFile($sql_file); |
||
| 84 | |||
| 85 | $this->checkEnvVariables(); |
||
| 86 | |||
| 87 | $this->migrate(); |
||
| 88 | |||
| 89 | $this->checkHooks(); |
||
| 90 | |||
| 91 | $this->checkPromotionCategory(); |
||
| 92 | |||
| 93 | parent::__construct(); |
||
| 94 | |||
| 95 | $lang = Language::getLanguage($this->context->language->id); |
||
| 96 | $langArray = explode("-", $lang['language_code']); |
||
| 97 | if (count($langArray) != 2 && isset($lang['locale'])) { |
||
| 98 | $langArray = explode("-", $lang['locale']); |
||
| 99 | } |
||
| 100 | $this->language = Tools::strtoupper($langArray[count($langArray)-1]); |
||
| 101 | |||
| 102 | // Prevent null language detection |
||
| 103 | $this->language = ($this->language) ? $this->language : 'ES'; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Configure the variables for Pagantis payment method. |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function install() |
||
| 112 | { |
||
| 113 | if (!extension_loaded('curl')) { |
||
| 114 | $this->_errors[] = |
||
| 115 | $this->l('You have to enable the cURL extension on your server to install this module'); |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | if (!version_compare(phpversion(), '5.3.0', '>=')) { |
||
| 119 | $this->_errors[] = $this->l('The PHP version bellow 5.3.0 is not supported'); |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | Configuration::updateValue('pagantis_is_enabled', 1); |
||
| 124 | Configuration::updateValue('pagantis_simulator_is_enabled', 1); |
||
| 125 | Configuration::updateValue('pagantis_public_key', ''); |
||
| 126 | Configuration::updateValue('pagantis_private_key', ''); |
||
| 127 | |||
| 128 | return (parent::install() |
||
| 129 | && $this->registerHook('displayShoppingCart') |
||
| 130 | && $this->registerHook('payment') |
||
| 131 | && $this->registerHook('paymentOptions') |
||
| 132 | && $this->registerHook('displayRightColumn') |
||
| 133 | && $this->registerHook('displayLeftColumn') |
||
| 134 | && $this->registerHook('displayRightColumnProduct') |
||
| 135 | && $this->registerHook('displayLeftColumnProduct') |
||
| 136 | && $this->registerHook('displayProductButtons') |
||
| 137 | && $this->registerHook('displayOrderConfirmation') |
||
| 138 | && $this->registerHook('header') |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Remove the production private api key and remove the files |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function uninstall() |
||
| 148 | { |
||
| 149 | Configuration::deleteByName('pagantis_public_key'); |
||
| 150 | Configuration::deleteByName('pagantis_private_key'); |
||
| 151 | |||
| 152 | return parent::uninstall(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Migrate the configs of older versions < 7x to new configurations |
||
| 157 | */ |
||
| 158 | public function migrate() |
||
| 159 | { |
||
| 160 | if (Configuration::get('PAGANTIS_MIN_AMOUNT')) { |
||
| 161 | Db::getInstance()->update( |
||
| 162 | 'pagantis_config', |
||
| 163 | array('value' => Configuration::get('PAGANTIS_MIN_AMOUNT')), |
||
| 164 | 'config = \'PAGANTIS_DISPLAY_MIN_AMOUNT\'' |
||
| 165 | ); |
||
| 166 | Configuration::updateValue('PAGANTIS_MIN_AMOUNT', false); |
||
| 167 | Configuration::updateValue('pagantis_is_enabled', 1); |
||
| 168 | Configuration::updateValue('pagantis_simulator_is_enabled', 1); |
||
| 169 | |||
| 170 | // migrating pk/tk from previous version |
||
| 171 | if (Configuration::get('pagantis_public_key') === false |
||
| 172 | && Configuration::get('PAGANTIS_PUBLIC_KEY_PROD') |
||
| 173 | ) { |
||
| 174 | Configuration::updateValue('pagantis_public_key', Configuration::get('PAGANTIS_PUBLIC_KEY_PROD')); |
||
| 175 | Configuration::updateValue('PAGANTIS_PUBLIC_KEY_PROD', false); |
||
| 176 | } elseif (Configuration::get('pagantis_public_key') === false |
||
| 177 | && Configuration::get('PAGANTIS_PUBLIC_KEY_TEST') |
||
| 178 | ) { |
||
| 179 | Configuration::updateValue('pagantis_public_key', Configuration::get('PAGANTIS_PUBLIC_KEY_TEST')); |
||
| 180 | Configuration::updateValue('PAGANTIS_PUBLIC_KEY_TEST', false); |
||
| 181 | } |
||
| 182 | |||
| 183 | if (Configuration::get('pagantis_private_key') === false |
||
| 184 | && Configuration::get('PAGANTIS_PRIVATE_KEY_PROD') |
||
| 185 | ) { |
||
| 186 | Configuration::updateValue('pagantis_private_key', Configuration::get('PAGANTIS_PRIVATE_KEY_PROD')); |
||
| 187 | Configuration::updateValue('PAGANTIS_PRIVATE_KEY_PROD', false); |
||
| 188 | } elseif (Configuration::get('pagantis_private_key') === false |
||
| 189 | && Configuration::get('PAGANTIS_PRIVATE_KEY_TEST') |
||
| 190 | ) { |
||
| 191 | Configuration::updateValue('pagantis_private_key', Configuration::get('PAGANTIS_PRIVATE_KEY_TEST')); |
||
| 192 | Configuration::updateValue('PAGANTIS_PRIVATE_KEY_TEST', false); |
||
| 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() |
||
| 229 | { |
||
| 230 | $sql_content = 'select * from ' . _DB_PREFIX_. 'pagantis_config'; |
||
| 231 | $dbConfigs = Db::getInstance()->executeS($sql_content); |
||
| 232 | |||
| 233 | // Convert a multimple dimension array for SQL insert statements into a simple key/value |
||
| 234 | $simpleDbConfigs = array(); |
||
| 235 | foreach ($dbConfigs as $config) { |
||
| 236 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
| 237 | } |
||
| 238 | $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
||
| 239 | if (!empty($newConfigs)) { |
||
| 240 | $data = array(); |
||
| 241 | foreach ($newConfigs as $key => $value) { |
||
| 242 | $data[] = array( |
||
| 243 | 'config' => $key, |
||
| 244 | 'value' => $value, |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | Db::getInstance()->insert('pagantis_config', $data); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param $sql_file |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function loadSQLFile($sql_file) |
||
| 256 | { |
||
| 257 | $sql_content = Tools::file_get_contents($sql_file); |
||
| 258 | $sql_content = str_replace('PREFIX_', _DB_PREFIX_, $sql_content); |
||
| 259 | $sql_requests = preg_split("/;\s*[\r\n]+/", $sql_content); |
||
| 260 | |||
| 261 | $result = true; |
||
| 262 | foreach ($sql_requests as $request) { |
||
| 263 | if (!empty($request)) { |
||
| 264 | $result &= Db::getInstance()->execute(trim($request)); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | return $result; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Check amount of order > minAmount |
||
| 273 | * Check valid currency |
||
| 274 | * Check API variables are set |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | * @throws PrestaShopDatabaseException |
||
| 278 | * @throws PrestaShopException |
||
| 279 | */ |
||
| 280 | public function isPaymentMethodAvailable() |
||
| 281 | { |
||
| 282 | $cart = $this->context->cart; |
||
| 283 | $currency = new Currency($cart->id_currency); |
||
| 284 | $availableCurrencies = array('EUR'); |
||
| 285 | $pagantisDisplayMinAmount = Pagantis::getExtraConfig('PAGANTIS_DISPLAY_MIN_AMOUNT'); |
||
| 286 | $pagantisPublicKey = Configuration::get('pagantis_public_key'); |
||
| 287 | $pagantisPrivateKey = Configuration::get('pagantis_private_key'); |
||
| 288 | $allowedCountries = unserialize(Pagantis::getExtraConfig('PAGANTIS_ALLOWED_COUNTRIES')); |
||
| 289 | return ( |
||
| 290 | $cart->getOrderTotal() >= $pagantisDisplayMinAmount && |
||
| 291 | in_array($currency->iso_code, $availableCurrencies) && |
||
| 292 | in_array(Tools::strtolower($this->language), $allowedCountries) && |
||
| 293 | $pagantisPublicKey && |
||
| 294 | $pagantisPrivateKey |
||
| 295 | ); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param Cart $cart |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | * @throws Exception |
||
| 303 | */ |
||
| 304 | private function getButtonTemplateVars(Cart $cart) |
||
| 305 | { |
||
| 306 | $currency = new Currency(($cart->id_currency)); |
||
| 307 | |||
| 308 | return array( |
||
| 309 | 'pagantis_button' => '#pagantis_payment_button', |
||
| 310 | 'pagantis_currency_iso' => $currency->iso_code, |
||
| 311 | 'pagantis_cart_total' => $cart->getOrderTotal(), |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Header hook |
||
| 317 | */ |
||
| 318 | public function hookHeader() |
||
| 319 | { |
||
| 320 | $url = 'https://cdn.pagantis.com/js/pg-v2/sdk.js'; |
||
| 321 | if ($this->language == 'ES' || $this->language == null) { |
||
| 322 | $url = 'https://cdn.pagantis.com/js/pmt-v2/sdk.js'; |
||
| 323 | } |
||
| 324 | if (_PS_VERSION_ >= "1.7") { |
||
| 325 | $this->context->controller->registerJavascript( |
||
| 326 | sha1(mt_rand(1, 90000)), |
||
| 327 | $url, |
||
| 328 | array('server' => 'remote') |
||
| 329 | ); |
||
| 330 | } else { |
||
| 331 | $this->context->controller->addJS($url); |
||
| 332 | } |
||
| 333 | $this->context->controller->addJS($this->getPathUri(). 'views/js/simulator.js'); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return array |
||
| 338 | * @throws Exception |
||
| 339 | */ |
||
| 340 | public function hookPaymentOptions() |
||
| 341 | { |
||
| 342 | if (!$this->isPaymentMethodAvailable()) { |
||
| 343 | return array(); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** @var Cart $cart */ |
||
| 347 | $cart = $this->context->cart; |
||
| 348 | $orderTotal = $cart->getOrderTotal(); |
||
| 349 | $promotedAmount = 0; |
||
| 350 | $link = $this->context->link; |
||
| 351 | $pagantisPublicKey = Configuration::get('pagantis_public_key'); |
||
| 352 | $pagantisSimulatorIsEnabled = Configuration::get('pagantis_simulator_is_enabled'); |
||
| 353 | $pagantisIsEnabled = Configuration::get('pagantis_is_enabled'); |
||
| 354 | $pagantisSimulatorType = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); |
||
| 355 | $pagantisSimulatorCSSSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); |
||
| 356 | $pagantisSimulatorPriceSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); |
||
| 357 | $pagantisSimulatorQuotesStart = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); |
||
| 358 | $pagantisSimulatorSkin = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); |
||
| 359 | $pagantisSimulatorPosition = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); |
||
| 360 | $pagantisTitle = $this->l(Pagantis::getExtraConfig('PAGANTIS_TITLE')); |
||
| 361 | $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); |
||
| 362 | $pagantisSimulatorDecimalSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); |
||
| 363 | |||
| 364 | $items = $cart->getProducts(true); |
||
| 365 | foreach ($items as $key => $item) { |
||
| 366 | $itemCategories = ProductCore::getProductCategoriesFull($item['id_product']); |
||
| 367 | if (in_array(PROMOTIONS_CATEGORY_NAME, array_column($itemCategories, 'name')) !== false) { |
||
| 368 | $promotedAmount += Product::getPriceStatic($item['id_product']); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | $this->context->smarty->assign($this->getButtonTemplateVars($cart)); |
||
| 373 | $this->context->smarty->assign(array( |
||
| 374 | 'amount' => $orderTotal, |
||
| 375 | 'locale' => $this->language, |
||
| 376 | 'pagantisPublicKey' => $pagantisPublicKey, |
||
| 377 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, |
||
| 378 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, |
||
| 379 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, |
||
| 380 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, |
||
| 381 | 'pagantisSimulatorType' => $pagantisSimulatorType, |
||
| 382 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, |
||
| 383 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, |
||
| 384 | 'pagantisIsEnabled' => $pagantisIsEnabled, |
||
| 385 | 'pagantisTitle' => $pagantisTitle, |
||
| 386 | 'paymentUrl' => $link->getModuleLink('pagantis', 'payment'), |
||
| 387 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, |
||
| 388 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, |
||
| 389 | 'ps_version' => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), |
||
| 390 | )); |
||
| 391 | |||
| 392 | $logo = ($this->language == 'ES' || $this->language == null) ? 'logo_pagamastarde.png' : 'logo_pagantis.png'; |
||
| 393 | $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); |
||
| 394 | $paymentOption |
||
| 395 | ->setCallToActionText($pagantisTitle) |
||
| 396 | ->setAction($link->getModuleLink('pagantis', 'payment')) |
||
| 397 | ->setLogo($this->getPathUri(). 'views/img/' . $logo) |
||
| 398 | ->setModuleName(__CLASS__) |
||
| 399 | ; |
||
| 400 | |||
| 401 | $paymentOption->setAdditionalInformation( |
||
| 402 | $this->fetch('module:pagantis/views/templates/hook/checkout.tpl') |
||
| 403 | ); |
||
| 404 | |||
| 405 | return array($paymentOption); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get the form for editing the BackOffice options of the module |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | private function getConfigForm() |
||
| 414 | { |
||
| 415 | return array( |
||
| 416 | 'form' => array( |
||
| 417 | 'legend' => array( |
||
| 418 | 'title' => $this->l('Basic Settings'), |
||
| 419 | 'icon' => 'icon-cogs', |
||
| 420 | ), |
||
| 421 | 'input' => array( |
||
| 422 | array( |
||
| 423 | 'name' => 'pagantis_is_enabled', |
||
| 424 | 'type' => (version_compare(_PS_VERSION_, '1.6')<0) ?'radio' :'switch', |
||
| 425 | 'label' => $this->l('Module is enabled'), |
||
| 426 | 'prefix' => '<i class="icon icon-key"></i>', |
||
| 427 | 'class' => 't', |
||
| 428 | 'required' => true, |
||
| 429 | 'values'=> array( |
||
| 430 | array( |
||
| 431 | 'id' => 'pagantis_is_enabled_true', |
||
| 432 | 'value' => 1, |
||
| 433 | 'label' => $this->l('Yes', get_class($this), null, false), |
||
| 434 | ), |
||
| 435 | array( |
||
| 436 | 'id' => 'pagantis_is_enabled_false', |
||
| 437 | 'value' => 0, |
||
| 438 | 'label' => $this->l('No', get_class($this), null, false), |
||
| 439 | ), |
||
| 440 | ) |
||
| 441 | ), |
||
| 442 | array( |
||
| 443 | 'name' => 'pagantis_public_key', |
||
| 444 | 'suffix' => $this->l('ex: pk_fd53cd467ba49022e4gf215e'), |
||
| 445 | 'type' => 'text', |
||
| 446 | 'size' => 60, |
||
| 447 | 'label' => $this->l('Public Key'), |
||
| 448 | 'prefix' => '<i class="icon icon-key"></i>', |
||
| 449 | 'col' => 6, |
||
| 450 | 'required' => true, |
||
| 451 | ), |
||
| 452 | array( |
||
| 453 | 'name' => 'pagantis_private_key', |
||
| 454 | 'suffix' => $this->l('ex: 21e5723a97459f6a'), |
||
| 455 | 'type' => 'text', |
||
| 456 | 'size' => 60, |
||
| 457 | 'label' => $this->l('Secret Key'), |
||
| 458 | 'prefix' => '<i class="icon icon-key"></i>', |
||
| 459 | 'col' => 6, |
||
| 460 | 'required' => true, |
||
| 461 | ), |
||
| 462 | array( |
||
| 463 | 'name' => 'pagantis_simulator_is_enabled', |
||
| 464 | 'type' => (version_compare(_PS_VERSION_, '1.6')<0) ?'radio' :'switch', |
||
| 465 | 'label' => $this->l('Simulator is enabled'), |
||
| 466 | 'prefix' => '<i class="icon icon-key"></i>', |
||
| 467 | 'class' => 't', |
||
| 468 | 'required' => true, |
||
| 469 | 'values'=> array( |
||
| 470 | array( |
||
| 471 | 'id' => 'pagantis_simulator_is_enabled_on', |
||
| 472 | 'value' => 1, |
||
| 473 | 'label' => $this->l('Yes'), |
||
| 474 | ), |
||
| 475 | array( |
||
| 476 | 'id' => 'pagantis_simulator_is_enabled_off', |
||
| 477 | 'value' => 0, |
||
| 478 | 'label' => $this->l('No'), |
||
| 479 | ), |
||
| 480 | ) |
||
| 481 | ), |
||
| 482 | ), |
||
| 483 | 'submit' => array( |
||
| 484 | 'title' => $this->l('Save'), |
||
| 485 | ), |
||
| 486 | ), |
||
| 487 | ); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Form configuration function |
||
| 492 | * |
||
| 493 | * @param array $settings |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | private function renderForm(array $settings) |
||
| 498 | { |
||
| 499 | $helper = new HelperForm(); |
||
| 500 | $helper->show_toolbar = false; |
||
| 501 | $helper->table = $this->table; |
||
| 502 | $helper->module = $this; |
||
| 503 | $helper->default_form_language = $this->context->language->id; |
||
| 504 | $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); |
||
| 505 | $helper->identifier = $this->identifier; |
||
| 506 | $helper->submit_action = 'submit'.$this->name; |
||
| 507 | $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; |
||
| 508 | $helper->token = Tools::getAdminTokenLite('AdminModules'); |
||
| 509 | $helper->tpl_vars = array( |
||
| 510 | 'fields_value' => $settings, |
||
| 511 | 'languages' => $this->context->controller->getLanguages(), |
||
| 512 | 'id_language' => $this->context->language->id, |
||
| 513 | ); |
||
| 514 | |||
| 515 | $helper->fields_value['pagantis_url_ok'] = Configuration::get('pagantis_url_ok'); |
||
| 516 | |||
| 517 | return $helper->generateForm(array($this->getConfigForm())); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Function to update the variables of Pagantis Module in the backoffice of prestashop |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | * @throws SmartyException |
||
| 525 | */ |
||
| 526 | public function getContent() |
||
| 527 | { |
||
| 528 | $error = ''; |
||
| 529 | $message = ''; |
||
| 530 | $settings = array(); |
||
| 531 | $settings['pagantis_public_key'] = Configuration::get('pagantis_public_key'); |
||
| 532 | $settings['pagantis_private_key'] = Configuration::get('pagantis_private_key'); |
||
| 533 | $settingsKeys = array( |
||
| 534 | 'pagantis_is_enabled', |
||
| 535 | 'pagantis_public_key', |
||
| 536 | 'pagantis_private_key', |
||
| 537 | 'pagantis_simulator_is_enabled', |
||
| 538 | ); |
||
| 539 | |||
| 540 | //Different Behavior depending on 1.6 or earlier |
||
| 541 | if (Tools::isSubmit('submit'.$this->name)) { |
||
| 542 | foreach ($settingsKeys as $key) { |
||
| 543 | switch ($key) { |
||
| 544 | case 'pagantis_public_key': |
||
| 545 | $value = Tools::getValue($key); |
||
| 546 | if (!$value) { |
||
| 547 | $error = $this->l('Please add a Pagantis API Public Key'); |
||
| 548 | break; |
||
| 549 | } |
||
| 550 | Configuration::updateValue($key, $value); |
||
| 551 | $settings[$key] = $value; |
||
| 552 | break; |
||
| 553 | case 'pagantis_private_key': |
||
| 554 | $value = Tools::getValue($key); |
||
| 555 | if (!$value) { |
||
| 556 | $error = $this->l('Please add a Pagantis API Private Key'); |
||
| 557 | break; |
||
| 558 | } |
||
| 559 | Configuration::updateValue($key, $value); |
||
| 560 | $settings[$key] = $value; |
||
| 561 | break; |
||
| 562 | default: |
||
| 563 | $value = Tools::getValue($key); |
||
| 564 | Configuration::updateValue($key, $value); |
||
| 565 | $settings[$key] = $value; |
||
| 566 | break; |
||
| 567 | } |
||
| 568 | $message = $this->displayConfirmation($this->l('All changes have been saved')); |
||
| 569 | } |
||
| 570 | } else { |
||
| 571 | foreach ($settingsKeys as $key) { |
||
| 572 | $settings[$key] = Configuration::get($key); |
||
| 573 | } |
||
| 574 | } |
||
| 575 | |||
| 576 | if ($error) { |
||
| 577 | $message = $this->displayError($error); |
||
| 578 | } |
||
| 579 | |||
| 580 | $logo = $this->getPathUri(). 'views/img/logo_pagantis.png'; |
||
| 581 | if ($this->language == 'ES' || $this->language == null) { |
||
| 582 | $logo = $this->getPathUri(). 'views/img/logo_pagamastarde.png'; |
||
| 583 | } |
||
| 584 | $tpl = $this->local_path.'views/templates/admin/config-info.tpl'; |
||
| 585 | $this->context->smarty->assign(array( |
||
| 586 | 'logo' => $logo, |
||
| 587 | 'form' => $this->renderForm($settings), |
||
| 588 | 'message' => $message, |
||
| 589 | 'version' => 'v'.$this->version, |
||
| 590 | )); |
||
| 591 | |||
| 592 | return $this->context->smarty->fetch($tpl); |
||
| 593 | } |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Hook to show payment method, this only applies on prestashop <= 1.6 |
||
| 597 | * |
||
| 598 | * @param $params |
||
| 599 | * @return bool | string |
||
| 600 | * @throws Exception |
||
| 601 | */ |
||
| 602 | public function hookPayment($params) |
||
| 603 | { |
||
| 604 | if (!$this->isPaymentMethodAvailable()) { |
||
| 605 | return false; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** @var Cart $cart */ |
||
| 609 | |||
| 610 | $cart = $params['cart']; |
||
| 611 | $orderTotal = $cart->getOrderTotal(); |
||
| 612 | $promotedAmount = 0; |
||
| 613 | $link = $this->context->link; |
||
| 614 | $pagantisPublicKey = Configuration::get('pagantis_public_key'); |
||
| 615 | $pagantisSimulatorIsEnabled = Configuration::get('pagantis_simulator_is_enabled'); |
||
| 616 | $pagantisIsEnabled = Configuration::get('pagantis_is_enabled'); |
||
| 617 | $pagantisSimulatorType = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); |
||
| 618 | $pagantisSimulatorCSSSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); |
||
| 619 | $pagantisSimulatorPriceSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); |
||
| 620 | $pagantisSimulatorQuotesStart = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); |
||
| 621 | $pagantisSimulatorSkin = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); |
||
| 622 | $pagantisSimulatorPosition = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); |
||
| 623 | $pagantisTitle = $this->l(Pagantis::getExtraConfig('PAGANTIS_TITLE')); |
||
| 624 | $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); |
||
| 625 | $pagantisSimulatorDecimalSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); |
||
| 626 | |||
| 627 | $items = $cart->getProducts(true); |
||
| 628 | foreach ($items as $key => $item) { |
||
| 629 | $itemCategories = ProductCore::getProductCategoriesFull($item['id_product']); |
||
| 630 | if (in_array(PROMOTIONS_CATEGORY_NAME, array_column($itemCategories, 'name')) !== false) { |
||
| 631 | $promotedAmount += Product::getPriceStatic($item['id_product']); |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | $this->context->smarty->assign($this->getButtonTemplateVars($cart)); |
||
| 636 | $this->context->smarty->assign(array( |
||
| 637 | 'amount' => $orderTotal, |
||
| 638 | 'promotedAmount' => $promotedAmount, |
||
| 639 | 'locale' => $this->language, |
||
| 640 | 'logo' => ($this->language == 'ES' || $this->language == null) ? 'pagamastarde.png' : 'pagantis.png', |
||
| 641 | 'pagantisPublicKey' => $pagantisPublicKey, |
||
| 642 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, |
||
| 643 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, |
||
| 644 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, |
||
| 645 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, |
||
| 646 | 'pagantisSimulatorType' => $pagantisSimulatorType, |
||
| 647 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, |
||
| 648 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, |
||
| 649 | 'pagantisIsEnabled' => $pagantisIsEnabled, |
||
| 650 | 'pagantisTitle' => $pagantisTitle, |
||
| 651 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, |
||
| 652 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, |
||
| 653 | 'paymentUrl' => $link->getModuleLink('pagantis', 'payment'), |
||
| 654 | 'ps_version' => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), |
||
| 655 | )); |
||
| 656 | |||
| 657 | $supercheckout_enabled = Module::isEnabled('supercheckout'); |
||
| 658 | $onepagecheckoutps_enabled = Module::isEnabled('onepagecheckoutps'); |
||
| 659 | $onepagecheckout_enabled = Module::isEnabled('onepagecheckout'); |
||
| 660 | |||
| 661 | $return = true; |
||
| 662 | if ($supercheckout_enabled || $onepagecheckout_enabled || $onepagecheckoutps_enabled) { |
||
| 663 | $this->checkLogoExists(); |
||
| 664 | $return = $this->display(__FILE__, 'views/templates/hook/onepagecheckout.tpl'); |
||
| 665 | } elseif (_PS_VERSION_ < 1.7) { |
||
| 666 | $return = $this->display(__FILE__, 'views/templates/hook/checkout.tpl'); |
||
| 667 | } |
||
| 668 | return $return; |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @param string $functionName |
||
| 673 | *: |
||
| 674 | * @return string |
||
| 675 | * @throws PrestaShopDatabaseException |
||
| 676 | * @throws PrestaShopException |
||
| 677 | */ |
||
| 678 | public function productPageSimulatorDisplay($functionName) |
||
| 679 | { |
||
| 680 | $productConfiguration = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_POSITION'); |
||
| 681 | /** @var ProductCore $product */ |
||
| 682 | $product = new Product(Tools::getValue('id_product')); |
||
| 683 | $amount = $product->getPublicPrice(); |
||
| 684 | |||
| 685 | $itemCategoriesNames = array_column(Product::getProductCategoriesFull($product->id), 'name'); |
||
| 686 | $isPromotedProduct = in_array(PROMOTIONS_CATEGORY_NAME, $itemCategoriesNames); |
||
| 687 | |||
| 688 | $pagantisPublicKey = Configuration::get('pagantis_public_key'); |
||
| 689 | $pagantisSimulatorIsEnabled = Configuration::get('pagantis_simulator_is_enabled'); |
||
| 690 | $pagantisIsEnabled = Configuration::get('pagantis_is_enabled'); |
||
| 691 | $pagantisSimulatorType = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); |
||
| 692 | $pagantisSimulatorCSSSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); |
||
| 693 | $pagantisSimulatorPriceSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); |
||
| 694 | $pagantisSimulatorQuantitySelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'); |
||
| 695 | $pagantisSimulatorQuotesStart = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); |
||
| 696 | $pagantisSimulatorSkin = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); |
||
| 697 | $pagantisSimulatorPosition = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); |
||
| 698 | $pagantisDisplayMinAmount = Pagantis::getExtraConfig('PAGANTIS_DISPLAY_MIN_AMOUNT'); |
||
| 699 | $pagantisPromotionExtra = Pagantis::getExtraConfig('PAGANTIS_PROMOTION_EXTRA'); |
||
| 700 | $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); |
||
| 701 | $pagantisSimulatorDecimalSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); |
||
| 702 | |||
| 703 | if ($functionName != $productConfiguration || |
||
| 704 | $amount <= 0 || |
||
| 705 | $amount < $pagantisDisplayMinAmount || |
||
| 706 | !$pagantisSimulatorType |
||
| 707 | ) { |
||
| 708 | return null; |
||
| 709 | } |
||
| 710 | |||
| 711 | $this->context->smarty->assign(array( |
||
| 712 | 'amount' => $amount, |
||
| 713 | 'locale' => $this->language, |
||
| 714 | 'pagantisPublicKey' => $pagantisPublicKey, |
||
| 715 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, |
||
| 716 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, |
||
| 717 | 'pagantisQuantitySelector' => $pagantisSimulatorQuantitySelector, |
||
| 718 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, |
||
| 719 | 'pagantisIsEnabled' => $pagantisIsEnabled, |
||
| 720 | 'pagantisSimulatorType' => $pagantisSimulatorType, |
||
| 721 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, |
||
| 722 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, |
||
| 723 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, |
||
| 724 | 'isPromotedProduct' => $isPromotedProduct, |
||
| 725 | 'pagantisPromotionExtra' => $this->l($pagantisPromotionExtra), |
||
| 726 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, |
||
| 727 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, |
||
| 728 | 'ps_version' => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), |
||
| 729 | )); |
||
| 730 | |||
| 731 | return $this->display(__FILE__, 'views/templates/hook/product-simulator.tpl'); |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @return string |
||
| 736 | * @throws PrestaShopDatabaseException |
||
| 737 | * @throws PrestaShopException |
||
| 738 | */ |
||
| 739 | public function hookDisplayRightColumn() |
||
| 740 | { |
||
| 741 | |||
| 742 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @return string |
||
| 747 | * @throws PrestaShopDatabaseException |
||
| 748 | * @throws PrestaShopException |
||
| 749 | */ |
||
| 750 | public function hookDisplayLeftColumn() |
||
| 751 | { |
||
| 752 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
| 753 | } |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @return string |
||
| 757 | * @throws PrestaShopDatabaseException |
||
| 758 | * @throws PrestaShopException |
||
| 759 | */ |
||
| 760 | public function hookDisplayRightColumnProduct() |
||
| 761 | { |
||
| 762 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @return string |
||
| 767 | * @throws PrestaShopDatabaseException |
||
| 768 | * @throws PrestaShopException |
||
| 769 | */ |
||
| 770 | public function hookDisplayLeftColumnProduct() |
||
| 771 | { |
||
| 772 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @return string |
||
| 777 | * @throws PrestaShopDatabaseException |
||
| 778 | * @throws PrestaShopException |
||
| 779 | */ |
||
| 780 | public function hookDisplayProductButtons() |
||
| 781 | { |
||
| 782 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @param array $params |
||
| 787 | * |
||
| 788 | * @return string |
||
| 789 | */ |
||
| 790 | public function hookDisplayOrderConfirmation($params) |
||
| 791 | { |
||
| 792 | $paymentMethod = (_PS_VERSION_ < 1.7) ? ($params["objOrder"]->payment) : ($params["order"]->payment); |
||
| 793 | |||
| 794 | if ($paymentMethod == $this->displayName) { |
||
| 795 | return $this->display(__FILE__, 'views/templates/hook/payment-return.tpl'); |
||
| 796 | } |
||
| 797 | |||
| 798 | return null; |
||
| 799 | } |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Check logo exists in OPC module |
||
| 803 | */ |
||
| 804 | public function checkLogoExists() |
||
| 818 | ); |
||
| 819 | } |
||
| 820 | } |
||
| 821 | |||
| 822 | /** |
||
| 823 | * checkPromotionCategory |
||
| 824 | */ |
||
| 825 | public function checkPromotionCategory() |
||
| 826 | { |
||
| 827 | $categories = array_column(Category::getCategories(null, false, false), 'name'); |
||
| 828 | if (!in_array(PROMOTIONS_CATEGORY_NAME, $categories)) { |
||
| 829 | /** @var CategoryCore $category */ |
||
| 830 | $category = new Category(); |
||
| 831 | $category->is_root_category = false; |
||
| 832 | $category->link_rewrite = array( 1=> PROMOTIONS_CATEGORY ); |
||
| 833 | $category->meta_description = array( 1=> PROMOTIONS_CATEGORY ); |
||
| 834 | $category->meta_keywords = array( 1=> PROMOTIONS_CATEGORY ); |
||
| 835 | $category->meta_title = array( 1=> PROMOTIONS_CATEGORY ); |
||
| 836 | $category->name = array( 1=> PROMOTIONS_CATEGORY_NAME ); |
||
| 837 | $category->id_parent = Configuration::get('PS_HOME_CATEGORY'); |
||
| 838 | $category->active=0; |
||
| 839 | $description = 'Pagantis: Products with this category have free financing assumed by the merchant. ' . |
||
| 840 | 'Use it to promote your products or brands.'; |
||
| 841 | $category->description = $this->l($description); |
||
| 842 | $category->save(); |
||
| 843 | } |
||
| 844 | } |
||
| 845 | |||
| 846 | |||
| 847 | public static function getExtraConfig($config = null, $default = '') |
||
| 861 | } |
||
| 862 | } |
||
| 863 |