Total Complexity | 114 |
Total Lines | 939 |
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 | * @var null $shippingAddress |
||
40 | */ |
||
41 | protected $shippingAddress = null; |
||
42 | /** |
||
43 | * @var null $billingAddress |
||
44 | */ |
||
45 | protected $billingAddress = null; |
||
46 | /** |
||
47 | * @var array $allowedCountries |
||
48 | */ |
||
49 | protected $allowedCountries = []; |
||
50 | |||
51 | /** |
||
52 | * Default module advanced configuration values |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public $defaultConfigs = array( |
||
57 | 'PAGANTIS_TITLE' => 'Instant Financing', |
||
58 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE' => 'sdk.simulator.types.SELECTABLE_TEXT_CUSTOM', |
||
59 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN' => 'sdk.simulator.skins.BLUE', |
||
60 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION' => 'hookDisplayProductButtons', |
||
61 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS' => '3', |
||
62 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR' => 'default', |
||
63 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION' => 'sdk.simulator.positions.INNER', |
||
64 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR' => 'default', |
||
65 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR' => 'default', |
||
66 | 'PAGANTIS_FORM_DISPLAY_TYPE' => '0', |
||
67 | 'PAGANTIS_DISPLAY_MIN_AMOUNT' => '1', |
||
68 | 'PAGANTIS_URL_OK' => '', |
||
69 | 'PAGANTIS_URL_KO' => '', |
||
70 | 'PAGANTIS_ALLOWED_COUNTRIES' => 'a:3:{i:0;s:2:"es";i:1;s:2:"it";i:2;s:2:"fr";}', |
||
71 | 'PAGANTIS_PROMOTION_EXTRA' => 'Finance this product <span class="pg-no-interest">without interest!</span>', |
||
72 | 'PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR' => '.', |
||
73 | 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR' => ',', |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * Pagantis constructor. |
||
78 | * |
||
79 | * Define the module main properties so that prestashop understands what are the module requirements |
||
80 | * and how to manage the module. |
||
81 | * |
||
82 | */ |
||
83 | public function __construct() |
||
84 | { |
||
85 | $this->name = 'pagantis'; |
||
86 | $this->tab = 'payments_gateways'; |
||
87 | $this->version = '8.2.15'; |
||
88 | $this->author = 'Pagantis'; |
||
89 | $this->currencies = true; |
||
90 | $this->currencies_mode = 'checkbox'; |
||
91 | $this->module_key = '2b9bc901b4d834bb7069e7ea6510438f'; |
||
92 | $this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_); |
||
93 | $this->displayName = $this->l('Pagantis'); |
||
94 | $this->description = $this->l( |
||
95 | 'Instant, easy and effective financial tool for your customers' |
||
96 | ); |
||
97 | |||
98 | $sql_file = dirname(__FILE__).'/sql/install.sql'; |
||
99 | $this->loadSQLFile($sql_file); |
||
100 | |||
101 | $this->checkEnvVariables(); |
||
102 | |||
103 | $this->migrate(); |
||
104 | |||
105 | $this->checkHooks(); |
||
106 | |||
107 | $this->checkPromotionCategory(); |
||
108 | |||
109 | parent::__construct(); |
||
110 | |||
111 | $this->getUserLanguage(); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Configure the variables for Pagantis payment method. |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function install() |
||
120 | { |
||
121 | if (!extension_loaded('curl')) { |
||
122 | $this->_errors[] = |
||
123 | $this->l('You have to enable the cURL extension on your server to install this module'); |
||
124 | return false; |
||
125 | } |
||
126 | if (!version_compare(phpversion(), '5.3.0', '>=')) { |
||
127 | $this->_errors[] = $this->l('The PHP version bellow 5.3.0 is not supported'); |
||
128 | return false; |
||
129 | } |
||
130 | |||
131 | Configuration::updateValue('pagantis_is_enabled', 1); |
||
132 | Configuration::updateValue('pagantis_simulator_is_enabled', 1); |
||
133 | Configuration::updateValue('pagantis_public_key', ''); |
||
134 | Configuration::updateValue('pagantis_private_key', ''); |
||
135 | |||
136 | $return = (parent::install() |
||
137 | && $this->registerHook('displayShoppingCart') |
||
138 | && $this->registerHook('paymentOptions') |
||
139 | && $this->registerHook('displayRightColumn') |
||
140 | && $this->registerHook('displayLeftColumn') |
||
141 | && $this->registerHook('displayRightColumnProduct') |
||
142 | && $this->registerHook('displayLeftColumnProduct') |
||
143 | && $this->registerHook('displayProductButtons') |
||
144 | && $this->registerHook('displayOrderConfirmation') |
||
145 | && $this->registerHook('header') |
||
146 | ); |
||
147 | |||
148 | if ($return && _PS_VERSION_ < "1.7") { |
||
149 | $this->registerHook('payment'); |
||
150 | } |
||
151 | |||
152 | return $return; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Remove the production private api key and remove the files |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function uninstall() |
||
161 | { |
||
162 | Configuration::deleteByName('pagantis_public_key'); |
||
163 | Configuration::deleteByName('pagantis_private_key'); |
||
164 | |||
165 | return parent::uninstall(); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Migrate the configs of older versions < 7x to new configurations |
||
170 | */ |
||
171 | public function migrate() |
||
172 | { |
||
173 | if (Configuration::get('PAGANTIS_MIN_AMOUNT')) { |
||
174 | Db::getInstance()->update( |
||
175 | 'pagantis_config', |
||
176 | array('value' => Configuration::get('PAGANTIS_MIN_AMOUNT')), |
||
177 | 'config = \'PAGANTIS_DISPLAY_MIN_AMOUNT\'' |
||
178 | ); |
||
179 | Configuration::updateValue('PAGANTIS_MIN_AMOUNT', false); |
||
180 | Configuration::updateValue('pagantis_is_enabled', 1); |
||
181 | Configuration::updateValue('pagantis_simulator_is_enabled', 1); |
||
182 | |||
183 | // migrating pk/tk from previous version |
||
184 | if (Configuration::get('pagantis_public_key') === false |
||
185 | && Configuration::get('PAGANTIS_PUBLIC_KEY_PROD') |
||
186 | ) { |
||
187 | Configuration::updateValue('pagantis_public_key', Configuration::get('PAGANTIS_PUBLIC_KEY_PROD')); |
||
188 | Configuration::updateValue('PAGANTIS_PUBLIC_KEY_PROD', false); |
||
189 | } elseif (Configuration::get('pagantis_public_key') === false |
||
190 | && Configuration::get('PAGANTIS_PUBLIC_KEY_TEST') |
||
191 | ) { |
||
192 | Configuration::updateValue('pagantis_public_key', Configuration::get('PAGANTIS_PUBLIC_KEY_TEST')); |
||
193 | Configuration::updateValue('PAGANTIS_PUBLIC_KEY_TEST', false); |
||
194 | } |
||
195 | |||
196 | if (Configuration::get('pagantis_private_key') === false |
||
197 | && Configuration::get('PAGANTIS_PRIVATE_KEY_PROD') |
||
198 | ) { |
||
199 | Configuration::updateValue('pagantis_private_key', Configuration::get('PAGANTIS_PRIVATE_KEY_PROD')); |
||
200 | Configuration::updateValue('PAGANTIS_PRIVATE_KEY_PROD', false); |
||
201 | } elseif (Configuration::get('pagantis_private_key') === false |
||
202 | && Configuration::get('PAGANTIS_PRIVATE_KEY_TEST') |
||
203 | ) { |
||
204 | Configuration::updateValue('pagantis_private_key', Configuration::get('PAGANTIS_PRIVATE_KEY_TEST')); |
||
205 | Configuration::updateValue('PAGANTIS_PRIVATE_KEY_TEST', false); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Check if new hooks used in new 7x versions are enabled and activate them if needed |
||
212 | * |
||
213 | * @throws PrestaShopDatabaseException |
||
214 | */ |
||
215 | public function checkHooks() |
||
216 | { |
||
217 | try { |
||
218 | $sql_content = 'select * from ' . _DB_PREFIX_. 'hook_module where |
||
219 | id_module = \'' . Module::getModuleIdByName($this->name) . '\' and |
||
220 | id_shop = \'' . Shop::getContextShopID() . '\' and |
||
221 | id_hook = \'' . Hook::getIdByName('header') . '\''; |
||
222 | $hook_exists = Db::getInstance()->ExecuteS($sql_content); |
||
223 | if (empty($hook_exists)) { |
||
224 | $sql_insert = 'insert into ' . _DB_PREFIX_. 'hook_module |
||
225 | (id_module, id_shop, id_hook, position) |
||
226 | values |
||
227 | (\''. Module::getModuleIdByName($this->name) . '\', |
||
228 | \''. Shop::getContextShopID() . '\', |
||
229 | \''. Hook::getIdByName('header') . '\', |
||
230 | 150)'; |
||
231 | Db::getInstance()->execute($sql_insert); |
||
232 | } |
||
233 | } catch (\Exception $exception) { |
||
234 | // continue without errors |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @throws PrestaShopDatabaseException |
||
240 | */ |
||
241 | public function checkEnvVariables() |
||
242 | { |
||
243 | $sql_content = 'select * from ' . _DB_PREFIX_. 'pagantis_config'; |
||
244 | $dbConfigs = Db::getInstance()->executeS($sql_content); |
||
245 | |||
246 | // Convert a multimple dimension array for SQL insert statements into a simple key/value |
||
247 | $simpleDbConfigs = array(); |
||
248 | foreach ($dbConfigs as $config) { |
||
249 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
250 | } |
||
251 | $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
||
252 | if (!empty($newConfigs)) { |
||
253 | $data = array(); |
||
254 | foreach ($newConfigs as $key => $value) { |
||
255 | $data[] = array( |
||
256 | 'config' => $key, |
||
257 | 'value' => $value, |
||
258 | ); |
||
259 | } |
||
260 | Db::getInstance()->insert('pagantis_config', $data); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param $sql_file |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function loadSQLFile($sql_file) |
||
269 | { |
||
270 | try { |
||
271 | $tableName = _DB_PREFIX_.'pagantis_order'; |
||
272 | $sql = ("SHOW TABLES LIKE '$tableName'"); |
||
273 | $results = Db::getInstance()->ExecuteS($sql); |
||
274 | if (is_array($results) && count($results) === 1) { |
||
275 | $query = "select COLUMN_TYPE FROM information_schema.COLUMNS where TABLE_NAME='$tableName' AND COLUMN_NAME='ps_order_id'"; |
||
276 | $results = $results = Db::getInstance()->ExecuteS($query); |
||
277 | if (is_array($results) && count($results) === 0) { |
||
278 | $sql = "ALTER TABLE $tableName ADD COLUMN ps_order_id VARCHAR(60) AFTER order_id"; |
||
279 | Db::getInstance()->Execute($sql); |
||
280 | } |
||
281 | return false; |
||
282 | } |
||
283 | } catch (\Exception $exception) { |
||
284 | // do nothing |
||
285 | } |
||
286 | |||
287 | $sql_content = Tools::file_get_contents($sql_file); |
||
288 | $sql_content = str_replace('PREFIX_', _DB_PREFIX_, $sql_content); |
||
289 | $sql_requests = preg_split("/;\s*[\r\n]+/", $sql_content); |
||
290 | |||
291 | $result = true; |
||
292 | foreach ($sql_requests as $request) { |
||
293 | if (!empty($request)) { |
||
294 | $result &= Db::getInstance()->execute(trim($request)); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | return $result; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Check amount of order > minAmount |
||
303 | * Check valid currency |
||
304 | * Check API variables are set |
||
305 | * |
||
306 | * @return bool |
||
307 | * @throws PrestaShopDatabaseException |
||
308 | * @throws PrestaShopException |
||
309 | */ |
||
310 | public function isPaymentMethodAvailable() |
||
326 | ); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @param Cart $cart |
||
331 | * |
||
332 | * @return array |
||
333 | * @throws Exception |
||
334 | */ |
||
335 | private function getButtonTemplateVars(Cart $cart) |
||
336 | { |
||
337 | $currency = new Currency(($cart->id_currency)); |
||
338 | |||
339 | return array( |
||
340 | 'pagantis_button' => '#pagantis_payment_button', |
||
341 | 'pagantis_currency_iso' => $currency->iso_code, |
||
342 | 'pagantis_cart_total' => $cart->getOrderTotal(), |
||
343 | ); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Header hook |
||
348 | */ |
||
349 | public function hookHeader() |
||
350 | { |
||
351 | $url = 'https://cdn.pagantis.com/js/pg-v2/sdk.js'; |
||
352 | if (_PS_VERSION_ >= "1.7") { |
||
353 | $this->context->controller->registerJavascript( |
||
354 | sha1(mt_rand(1, 90000)), |
||
355 | $url, |
||
356 | array('server' => 'remote') |
||
357 | ); |
||
358 | } else { |
||
359 | $this->context->controller->addJS($url); |
||
360 | } |
||
361 | $this->context->controller->addJS($this->getPathUri(). 'views/js/simulator.js'); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @return array |
||
366 | * @throws Exception |
||
367 | */ |
||
368 | public function hookPaymentOptions() |
||
369 | { |
||
370 | /** @var Cart $cart */ |
||
371 | $cart = $this->context->cart; |
||
372 | $this->shippingAddress = new Address($cart->id_address_delivery); |
||
373 | $this->billingAddress = new Address($cart->id_address_invoice); |
||
374 | if (!$this->isPaymentMethodAvailable()) { |
||
375 | return array(); |
||
376 | } |
||
377 | |||
378 | /** @var Cart $cart */ |
||
379 | $cart = $this->context->cart; |
||
380 | $orderTotal = $cart->getOrderTotal(); |
||
381 | $promotedAmount = 0; |
||
382 | $link = $this->context->link; |
||
383 | $pagantisPublicKey = Configuration::get('pagantis_public_key'); |
||
384 | $pagantisSimulatorIsEnabled = Configuration::get('pagantis_simulator_is_enabled'); |
||
385 | $pagantisIsEnabled = Configuration::get('pagantis_is_enabled'); |
||
386 | $pagantisSimulatorType = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); |
||
387 | $pagantisSimulatorCSSSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); |
||
388 | $pagantisSimulatorPriceSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); |
||
389 | $pagantisSimulatorQuotesStart = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); |
||
390 | $pagantisSimulatorSkin = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); |
||
391 | $pagantisSimulatorPosition = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); |
||
392 | $pagantisTitle = $this->l(Pagantis::getExtraConfig('PAGANTIS_TITLE')); |
||
393 | $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); |
||
394 | $pagantisSimulatorDecimalSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); |
||
395 | |||
396 | $items = $cart->getProducts(true); |
||
397 | foreach ($items as $key => $item) { |
||
398 | $itemCategories = ProductCore::getProductCategoriesFull($item['id_product']); |
||
399 | if (in_array(PROMOTIONS_CATEGORY_NAME, $this->arrayColumn($itemCategories, 'name')) !== false) { |
||
400 | $productId = $item['id_product']; |
||
401 | $promotedAmount += Product::getPriceStatic($productId); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | $this->context->smarty->assign($this->getButtonTemplateVars($cart)); |
||
406 | $this->context->smarty->assign(array( |
||
407 | 'amount' => $orderTotal, |
||
408 | 'locale' => $this->language, |
||
409 | 'country' => $this->language, |
||
410 | 'pagantisPublicKey' => $pagantisPublicKey, |
||
411 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, |
||
412 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, |
||
413 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, |
||
414 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, |
||
415 | 'pagantisSimulatorType' => $pagantisSimulatorType, |
||
416 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, |
||
417 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, |
||
418 | 'pagantisIsEnabled' => $pagantisIsEnabled, |
||
419 | 'pagantisTitle' => $pagantisTitle, |
||
420 | 'paymentUrl' => $link->getModuleLink('pagantis', 'payment'), |
||
421 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, |
||
422 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, |
||
423 | 'promotedAmount' => $promotedAmount, |
||
424 | 'ps_version' => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), |
||
425 | )); |
||
426 | |||
427 | $logo = 'https://cdn.digitalorigin.com/assets/master/logos/pg-favicon.png'; |
||
428 | $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption(); |
||
429 | $paymentOption |
||
430 | ->setCallToActionText($pagantisTitle) |
||
431 | ->setAction($link->getModuleLink('pagantis', 'payment')) |
||
432 | ->setLogo($logo) |
||
433 | ->setModuleName(__CLASS__) |
||
434 | ; |
||
435 | |||
436 | $paymentOption->setAdditionalInformation( |
||
437 | $this->fetch('module:pagantis/views/templates/hook/checkout.tpl') |
||
438 | ); |
||
439 | |||
440 | return array($paymentOption); |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Get the form for editing the BackOffice options of the module |
||
445 | * |
||
446 | * @return array |
||
447 | */ |
||
448 | private function getConfigForm() |
||
520 | ), |
||
521 | ), |
||
522 | ); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Form configuration function |
||
527 | * |
||
528 | * @param array $settings |
||
529 | * |
||
530 | * @return string |
||
531 | */ |
||
532 | private function renderForm(array $settings) |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * Function to update the variables of Pagantis Module in the backoffice of prestashop |
||
557 | * |
||
558 | * @return string |
||
559 | * @throws SmartyException |
||
560 | */ |
||
561 | public function getContent() |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Hook to show payment method, this only applies on prestashop <= 1.6 |
||
629 | * |
||
630 | * @param $params |
||
631 | * @return bool | string |
||
632 | * @throws Exception |
||
633 | */ |
||
634 | public function hookPayment($params) |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * @param string $functionName |
||
711 | *: |
||
712 | * @return string |
||
713 | * @throws PrestaShopDatabaseException |
||
714 | * @throws PrestaShopException |
||
715 | */ |
||
716 | public function productPageSimulatorDisplay($functionName) |
||
717 | { |
||
718 | $productConfiguration = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_POSITION'); |
||
719 | $productId = Tools::getValue('id_product'); |
||
720 | if (!$productId) { |
||
721 | return false; |
||
722 | } |
||
723 | //Resolves bug of reference passtrow |
||
724 | $amount = Product::getPriceStatic($productId); |
||
725 | |||
726 | $itemCategoriesNames = $this->arrayColumn(Product::getProductCategoriesFull($productId), 'name'); |
||
727 | $isPromotedProduct = in_array(PROMOTIONS_CATEGORY_NAME, $itemCategoriesNames); |
||
728 | |||
729 | $pagantisPublicKey = Configuration::get('pagantis_public_key'); |
||
730 | $pagantisSimulatorIsEnabled = Configuration::get('pagantis_simulator_is_enabled'); |
||
731 | $pagantisIsEnabled = Configuration::get('pagantis_is_enabled'); |
||
732 | $pagantisSimulatorType = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_TYPE'); |
||
733 | $pagantisSimulatorCSSSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'); |
||
734 | $pagantisSimulatorPriceSelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'); |
||
735 | $pagantisSimulatorQuantitySelector = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'); |
||
736 | $pagantisSimulatorQuotesStart = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_START_INSTALLMENTS'); |
||
737 | $pagantisSimulatorSkin = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_SKIN'); |
||
738 | $pagantisSimulatorPosition = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'); |
||
739 | $pagantisDisplayMinAmount = Pagantis::getExtraConfig('PAGANTIS_DISPLAY_MIN_AMOUNT'); |
||
740 | $pagantisPromotionExtra = Pagantis::getExtraConfig('PAGANTIS_PROMOTION_EXTRA'); |
||
741 | $pagantisSimulatorThousandSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_THOUSAND_SEPARATOR'); |
||
742 | $pagantisSimulatorDecimalSeparator = Pagantis::getExtraConfig('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'); |
||
743 | $allowedCountries = unserialize(Pagantis::getExtraConfig('PAGANTIS_ALLOWED_COUNTRIES')); |
||
744 | |||
745 | if ($functionName != $productConfiguration || |
||
746 | $amount <= 0 || |
||
747 | $amount < $pagantisDisplayMinAmount || |
||
748 | !$pagantisSimulatorType || |
||
749 | !in_array(Tools::strtolower($this->language), $allowedCountries) |
||
750 | ) { |
||
751 | return null; |
||
752 | } |
||
753 | |||
754 | $this->context->smarty->assign(array( |
||
755 | 'amount' => $amount, |
||
756 | 'locale' => $this->language, |
||
757 | 'country' => $this->language, |
||
758 | 'pagantisPublicKey' => $pagantisPublicKey, |
||
759 | 'pagantisCSSSelector' => $pagantisSimulatorCSSSelector, |
||
760 | 'pagantisPriceSelector' => $pagantisSimulatorPriceSelector, |
||
761 | 'pagantisQuantitySelector' => $pagantisSimulatorQuantitySelector, |
||
762 | 'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled, |
||
763 | 'pagantisIsEnabled' => $pagantisIsEnabled, |
||
764 | 'pagantisSimulatorType' => $pagantisSimulatorType, |
||
765 | 'pagantisSimulatorSkin' => $pagantisSimulatorSkin, |
||
766 | 'pagantisSimulatorPosition' => $pagantisSimulatorPosition, |
||
767 | 'pagantisQuotesStart' => $pagantisSimulatorQuotesStart, |
||
768 | 'isPromotedProduct' => $isPromotedProduct, |
||
769 | 'pagantisPromotionExtra' => Tools::htmlentitiesDecodeUTF8($this->l($pagantisPromotionExtra)), |
||
770 | 'pagantisSimulatorThousandSeparator' => $pagantisSimulatorThousandSeparator, |
||
771 | 'pagantisSimulatorDecimalSeparator' => $pagantisSimulatorDecimalSeparator, |
||
772 | 'ps_version' => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)), |
||
773 | 'pagantisSimPreposition' => $this->l('or'), |
||
774 | )); |
||
775 | |||
776 | return $this->display(__FILE__, 'views/templates/hook/product-simulator.tpl'); |
||
777 | } |
||
778 | |||
779 | /** |
||
780 | * @return string |
||
781 | * @throws PrestaShopDatabaseException |
||
782 | * @throws PrestaShopException |
||
783 | */ |
||
784 | public function hookDisplayRightColumn() |
||
785 | { |
||
786 | |||
787 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * @return string |
||
792 | * @throws PrestaShopDatabaseException |
||
793 | * @throws PrestaShopException |
||
794 | */ |
||
795 | public function hookDisplayLeftColumn() |
||
796 | { |
||
797 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * @return string |
||
802 | * @throws PrestaShopDatabaseException |
||
803 | * @throws PrestaShopException |
||
804 | */ |
||
805 | public function hookDisplayRightColumnProduct() |
||
806 | { |
||
807 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * @return string |
||
812 | * @throws PrestaShopDatabaseException |
||
813 | * @throws PrestaShopException |
||
814 | */ |
||
815 | public function hookDisplayLeftColumnProduct() |
||
816 | { |
||
817 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
818 | } |
||
819 | |||
820 | /** |
||
821 | * @return string |
||
822 | * @throws PrestaShopDatabaseException |
||
823 | * @throws PrestaShopException |
||
824 | */ |
||
825 | public function hookDisplayProductButtons() |
||
826 | { |
||
827 | return $this->productPageSimulatorDisplay(__FUNCTION__); |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * @param array $params |
||
832 | * |
||
833 | * @return string |
||
834 | */ |
||
835 | public function hookDisplayOrderConfirmation($params) |
||
836 | { |
||
837 | $paymentMethod = (_PS_VERSION_ < 1.7) ? ($params["objOrder"]->payment) : ($params["order"]->payment); |
||
838 | |||
839 | if ($paymentMethod == $this->displayName) { |
||
840 | return $this->display(__FILE__, 'views/templates/hook/payment-return.tpl'); |
||
841 | } |
||
842 | |||
843 | return null; |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * checkPromotionCategory |
||
848 | */ |
||
849 | public function checkPromotionCategory() |
||
850 | { |
||
851 | $categories = $this->arrayColumn(Category::getCategories(null, false, false), 'name'); |
||
852 | if (!in_array(PROMOTIONS_CATEGORY_NAME, $categories)) { |
||
853 | /** @var CategoryCore $category */ |
||
854 | $category = new Category(); |
||
855 | $categoryArray = array((int)Configuration::get('PS_LANG_DEFAULT')=> PROMOTIONS_CATEGORY ); |
||
856 | $category->is_root_category = false; |
||
857 | $category->link_rewrite = $categoryArray; |
||
858 | $category->meta_description = $categoryArray; |
||
859 | $category->meta_keywords = $categoryArray; |
||
860 | $category->meta_title = $categoryArray; |
||
861 | $category->name = array((int)Configuration::get('PS_LANG_DEFAULT')=> PROMOTIONS_CATEGORY_NAME); |
||
862 | $category->id_parent = Configuration::get('PS_HOME_CATEGORY'); |
||
863 | $category->active=0; |
||
864 | $description = 'Pagantis: Products with this category have free financing assumed by the merchant. ' . |
||
865 | 'Use it to promote your products or brands.'; |
||
866 | $category->description = $this->l($description); |
||
867 | $category->save(); |
||
868 | } |
||
869 | } |
||
870 | |||
871 | |||
872 | public static function getExtraConfig($config = null, $default = '') |
||
873 | { |
||
874 | if (is_null($config)) { |
||
875 | return ''; |
||
876 | } |
||
877 | |||
878 | $sql = 'SELECT value FROM '._DB_PREFIX_.'pagantis_config where config = \'' . pSQL($config) . '\' limit 1'; |
||
879 | if ($results = Db::getInstance()->ExecuteS($sql)) { |
||
880 | if (is_array($results) && count($results) === 1 && isset($results[0]['value'])) { |
||
881 | return $results[0]['value']; |
||
882 | } |
||
883 | } |
||
884 | |||
885 | return $default; |
||
886 | } |
||
887 | |||
888 | /** |
||
889 | * Check logo exists in OPC module |
||
890 | */ |
||
891 | public function checkLogoExists() |
||
898 | ); |
||
899 | } |
||
900 | } |
||
901 | |||
902 | /** |
||
903 | * Get user language |
||
904 | */ |
||
905 | private function getUserLanguage() |
||
930 | } |
||
931 | |||
932 | /** |
||
933 | * @param array $input |
||
934 | * @param $columnKey |
||
935 | * @param null $indexKey |
||
936 | * |
||
937 | * @return array|bool |
||
938 | */ |
||
939 | private function arrayColumn(array $input, $columnKey, $indexKey = null) |
||
962 | } |
||
963 | } |
||
964 |