Passed
Pull Request — master (#34)
by Raúl
02:30
created

Pagantis   F

Complexity

Total Complexity 82

Size/Duplication

Total Lines 734
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 397
c 1
b 0
f 0
dl 0
loc 734
rs 2
wmc 82

23 Methods

Rating   Name   Duplication   Size   Complexity  
A isPaymentMethodAvailable() 0 14 4
A uninstall() 0 5 1
B migrate() 0 35 10
A hookHeader() 0 12 2
A loadEnvVariables() 0 27 5
A __construct() 0 25 1
A loadSQLFile() 0 14 3
A checkHooks() 0 19 3
A getButtonTemplateVars() 0 8 1
C install() 0 34 14
B getConfigForm() 0 72 3
A checkLogoExists() 0 7 3
A hookDisplayRightColumnProduct() 0 3 1
A hookDisplayLeftColumn() 0 3 1
A renderForm() 0 21 1
A productPageSimulatorDisplay() 0 41 5
B getContent() 0 64 9
A hookDisplayProductButtons() 0 3 1
A hookDisplayLeftColumnProduct() 0 3 1
A hookDisplayOrderConfirmation() 0 9 3
B hookPayment() 0 49 6
A hookPaymentOptions() 0 54 3
A hookDisplayRightColumn() 0 4 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 * This file is part of the official Pagantis module for PrestaShop.
4
 *
5
 * @author    Pagantis <[email protected]>
6
 * @copyright 2015-2016 Pagantis
7
 * @license   proprietary
8
 */
9
10
if (!defined('_PS_VERSION_')) {
11
    exit;
12
}
13
14
define('_PS_PAYLATER_DIR', _PS_MODULE_DIR_. '/pagantis');
0 ignored issues
show
Bug introduced by
The constant _PS_MODULE_DIR_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
16
require _PS_PAYLATER_DIR.'/vendor/autoload.php';
17
18
/**
19
 * Class Pagantis
20
 */
21
class Pagantis extends PaymentModule
0 ignored issues
show
Bug introduced by
The type PaymentModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
{
23
    /**
24
     * @var string
25
     */
26
    public $url = 'https://pagantis.com';
27
28
    /**
29
     * @var bool
30
     */
31
    public $bootstrap = true;
32
33
    /**
34
     * @var array
35
     */
36
    public $installErrors = array();
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' => 'pmtSDK.simulator.types.SIMPLE',
46
        'PAGANTIS_SIMULATOR_DISPLAY_SKIN' => 'pmtSDK.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' => 'pmtSDK.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
    );
58
59
    /**
60
     * Pagantis constructor.
61
     *
62
     * Define the module main properties so that prestashop understands what are the module requirements
63
     * and how to manage the module.
64
     *
65
     */
66
    public function __construct()
67
    {
68
        $this->name = 'pagantis';
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69
        $this->tab = 'payments_gateways';
0 ignored issues
show
Bug Best Practice introduced by
The property tab does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        $this->version = '8.0.0';
0 ignored issues
show
Bug Best Practice introduced by
The property version does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
        $this->author = 'Pagantis';
0 ignored issues
show
Bug Best Practice introduced by
The property author does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
        $this->currencies = true;
0 ignored issues
show
Bug Best Practice introduced by
The property currencies does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
        $this->currencies_mode = 'checkbox';
0 ignored issues
show
Bug Best Practice introduced by
The property currencies_mode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
        $this->module_key = '2b9bc901b4d834bb7069e7ea6510438f';
0 ignored issues
show
Bug Best Practice introduced by
The property module_key does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        $this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_);
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property ps_versions_compliancy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
        $this->displayName = $this->l('Pagantis');
0 ignored issues
show
Bug Best Practice introduced by
The property displayName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
        $this->description = $this->l(
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
            'Instant, easy and effective financial tool for your customers'
79
        );
80
81
        $sql_file = dirname(__FILE__).'/sql/install.sql';
82
        $this->loadSQLFile($sql_file);
83
84
        $this->loadEnvVariables();
85
86
        $this->migrate();
87
88
        $this->checkHooks();
89
90
        parent::__construct();
91
    }
92
93
    /**
94
     * Configure the variables for Pagantis payment method.
95
     *
96
     * @return bool
97
     */
98
    public function install()
99
    {
100
        if (!extension_loaded('curl')) {
101
            $this->installErrors[] =
102
                $this->l('You have to enable the cURL extension on your server to install this module');
103
            return false;
104
        }
105
        if (!version_compare(phpversion(), '5.3.0', '>=')) {
106
            $this->installErrors[] = $this->l('The PHP version bellow 5.3.0 is not supported');
107
            return false;
108
        }
109
        $curl_info = curl_version();
110
        $curl_version = $curl_info['version'];
111
        if (!version_compare($curl_version, '7.34.0', '>=')) {
112
            $this->installErrors[] = $this->l('Curl Version is lower than 7.34.0 and does not support TLS 1.2');
113
            return false;
114
        }
115
116
        Configuration::updateValue('pagantis_is_enabled', 1);
0 ignored issues
show
Bug introduced by
The type Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
117
        Configuration::updateValue('pagantis_simulator_is_enabled', 1);
118
        Configuration::updateValue('pagantis_public_key', '');
119
        Configuration::updateValue('pagantis_private_key', '');
120
121
        return (parent::install()
122
            && $this->registerHook('displayShoppingCart')
123
            && $this->registerHook('payment')
124
            && $this->registerHook('paymentOptions')
125
            && $this->registerHook('displayRightColumn')
126
            && $this->registerHook('displayLeftColumn')
127
            && $this->registerHook('displayRightColumnProduct')
128
            && $this->registerHook('displayLeftColumnProduct')
129
            && $this->registerHook('displayProductButtons')
130
            && $this->registerHook('displayOrderConfirmation')
131
            && $this->registerHook('header')
132
        );
133
    }
134
135
    /**
136
     * Remove the production private api key and remove the files
137
     *
138
     * @return bool
139
     */
140
    public function uninstall()
141
    {
142
        Configuration::deleteByName('PAYLATER_PRIVATE_KEY_PROD');
143
144
        return parent::uninstall();
145
    }
146
147
    /**
148
     * Migrate the configs of older versions < 7x to new configurations
149
     */
150
    public function migrate()
151
    {
152
        if (Configuration::get('PAYLATER_MIN_AMOUNT')) {
153
            Db::getInstance()->update(
0 ignored issues
show
Bug introduced by
The type Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
154
                'pagantis_config',
155
                array('value' => Configuration::get('PAYLATER_MIN_AMOUNT')),
156
                'config = \'PAGANTIS_DISPLAY_MIN_AMOUNT\''
157
            );
158
            Configuration::updateValue('PAYLATER_MIN_AMOUNT', false);
159
            Configuration::updateValue('pagantis_is_enabled', 1);
160
            Configuration::updateValue('pagantis_simulator_is_enabled', 1);
161
162
            // migrating pk/tk from previous version
163
            if (Configuration::get('pagantis_public_key') === false
164
                && Configuration::get('PAYLATER_PUBLIC_KEY_PROD')
165
            ) {
166
                Configuration::updateValue('pagantis_public_key', Configuration::get('PAYLATER_PUBLIC_KEY_PROD'));
167
                Configuration::updateValue('PAYLATER_PUBLIC_KEY_PROD', false);
168
            } elseif (Configuration::get('pagantis_public_key') === false
169
                && Configuration::get('PAYLATER_PUBLIC_KEY_TEST')
170
            ) {
171
                Configuration::updateValue('pagantis_public_key', Configuration::get('PAYLATER_PUBLIC_KEY_TEST'));
172
                Configuration::updateValue('PAYLATER_PUBLIC_KEY_TEST', false);
173
            }
174
175
            if (Configuration::get('pagantis_private_key') === false
176
                && Configuration::get('PAYLATER_PRIVATE_KEY_PROD')
177
            ) {
178
                Configuration::updateValue('pagantis_private_key', Configuration::get('PAYLATER_PRIVATE_KEY_PROD'));
179
                Configuration::updateValue('PAYLATER_PRIVATE_KEY_PROD', false);
180
            } elseif (Configuration::get('pagantis_private_key') === false
181
                && Configuration::get('PAYLATER_PRIVATE_KEY_TEST')
182
            ) {
183
                Configuration::updateValue('pagantis_private_key', Configuration::get('PAYLATER_PRIVATE_KEY_TEST'));
184
                Configuration::updateValue('PAYLATER_PRIVATE_KEY_TEST', false);
185
            }
186
        }
187
    }
188
189
    /**
190
     * Check if new hooks used in new 7x versions are enabled and activate them if needed
191
     *
192
     * @throws PrestaShopDatabaseException
193
     */
194
    public function checkHooks()
195
    {
196
        try {
197
            $sql_content = 'select * from ' . _DB_PREFIX_. 'hook_module where 
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
198
            id_module = \'' . Module::getModuleIdByName($this->name) . '\' and 
0 ignored issues
show
Bug introduced by
The type Module was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
199
            id_shop = \'' . Shop::getContextShopID() . '\' and 
0 ignored issues
show
Bug introduced by
The type Shop was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
200
            id_hook = \'' . Hook::getIdByName('header') . '\'';
0 ignored issues
show
Bug introduced by
The type Hook was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
201
            $hook_exists = Db::getInstance()->ExecuteS($sql_content);
202
            if (empty($hook_exists)) {
203
                $sql_insert = 'insert into ' . _DB_PREFIX_.  'hook_module 
204
            (id_module, id_shop, id_hook, position)
205
            values
206
            (\''. Module::getModuleIdByName($this->name) . '\',
207
            \''. Shop::getContextShopID() . '\',
208
            \''. Hook::getIdByName('header') . '\',
209
            150)';
210
                Db::getInstance()->execute($sql_insert);
211
            }
212
        } catch (\Exception $exception) {
213
            // continue without errors
214
        }
215
    }
216
217
    public function loadEnvVariables()
218
    {
219
        $sql_content = 'select * from ' . _DB_PREFIX_. 'pagantis_config';
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
220
        $dbConfigs = Db::getInstance()->executeS($sql_content);
221
222
        // Convert a multimple dimension array for SQL insert statements into a simple key/value
223
        $simpleDbConfigs = array();
224
        foreach ($dbConfigs as $config) {
225
            $simpleDbConfigs[$config['config']] = $config['value'];
226
        }
227
        $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs);
228
        if (!empty($newConfigs)) {
229
            $data = array();
230
            foreach ($newConfigs as $key => $value) {
231
                $data[] = array(
232
                    'config' => $key,
233
                    'value' => $value,
234
                );
235
            }
236
            Db::getInstance()->insert('pagantis_config', $data);
237
        }
238
239
        foreach (array_merge($this->defaultConfigs, $simpleDbConfigs) as $key => $value) {
240
            putenv($key . '=' . $value);
241
        }
242
243
        putenv("PAGANTIS_DEFAULT_CONFIGS" . '=' . json_encode($this->defaultConfigs));
244
    }
245
246
    /**
247
     * @param $sql_file
248
     * @return bool
249
     */
250
    public function loadSQLFile($sql_file)
251
    {
252
        $sql_content = Tools::file_get_contents($sql_file);
0 ignored issues
show
Bug introduced by
The type Tools was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
253
        $sql_content = str_replace('PREFIX_', _DB_PREFIX_, $sql_content);
0 ignored issues
show
Bug introduced by
The constant _DB_PREFIX_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
254
        $sql_requests = preg_split("/;\s*[\r\n]+/", $sql_content);
255
256
        $result = true;
257
        foreach ($sql_requests as $request) {
258
            if (!empty($request)) {
259
                $result &= Db::getInstance()->execute(trim($request));
260
            }
261
        }
262
263
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
264
    }
265
266
    /**
267
     * Check amount of order > minAmount
268
     * Check valid currency
269
     * Check API variables are set
270
     *
271
     * @return bool
272
     * @throws PrestaShopDatabaseException
273
     * @throws PrestaShopException
274
     */
275
    public function isPaymentMethodAvailable()
276
    {
277
        $cart                       = $this->context->cart;
278
        $currency                   = new Currency($cart->id_currency);
0 ignored issues
show
Bug introduced by
The type Currency was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
279
        $availableCurrencies        = array('EUR');
280
        $pagantisDisplayMinAmount        = getenv('PAGANTIS_DISPLAY_MIN_AMOUNT');
281
        $pagantisPublicKey               = Configuration::get('pagantis_public_key');
282
        $pagantisPrivateKey              = Configuration::get('pagantis_private_key');
283
284
        return (
285
            $cart->getOrderTotal() >= $pagantisDisplayMinAmount &&
286
            in_array($currency->iso_code, $availableCurrencies) &&
287
            $pagantisPublicKey &&
288
            $pagantisPrivateKey
289
        );
290
    }
291
292
    /**
293
     * @param Cart $cart
294
     *
295
     * @return array
296
     * @throws Exception
297
     */
298
    private function getButtonTemplateVars(Cart $cart)
0 ignored issues
show
Bug introduced by
The type Cart was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
299
    {
300
        $currency = new Currency(($cart->id_currency));
301
302
        return array(
303
            'pagantis_button' => '#pagantis_payment_button',
304
            'pagantis_currency_iso' => $currency->iso_code,
305
            'pagantis_cart_total' => $cart->getOrderTotal(),
306
        );
307
    }
308
309
    /**
310
     * Header hook
311
     */
312
    public function hookHeader()
313
    {
314
        if (_PS_VERSION_ >= "1.7") {
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
315
            $this->context->controller->registerJavascript(
316
                sha1(mt_rand(1, 90000)),
317
                'https://cdn.pagamastarde.com/js/pmt-v2/sdk.js',
318
                array('server' => 'remote')
319
            );
320
        } else {
321
            $this->context->controller->addJS('https://cdn.pagamastarde.com/js/pmt-v2/sdk.js');
322
        }
323
        $this->context->controller->addJS($this->getPathUri(). 'views/js/simulator.js');
324
    }
325
326
    /**
327
     * @return array
328
     * @throws Exception
329
     */
330
    public function hookPaymentOptions()
331
    {
332
        if (!$this->isPaymentMethodAvailable()) {
333
            return array();
334
        }
335
336
        /** @var Cart $cart */
337
        $cart                            = $this->context->cart;
338
        $orderTotal                      = $cart->getOrderTotal();
339
        $link                            = $this->context->link;
340
        $pagantisPublicKey               = Configuration::get('pagantis_public_key');
341
        $pagantisSimulatorIsEnabled      = Configuration::get('pagantis_simulator_is_enabled');
342
        $pagantisIsEnabled               = Configuration::get('pagantis_is_enabled');
343
        $pagantisSimulatorType           = getenv('PAGANTIS_SIMULATOR_DISPLAY_TYPE');
344
        $pagantisSimulatorCSSSelector    = getenv('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR');
345
        $pagantisSimulatorPriceSelector  = getenv('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR');
346
        $pagantisSimulatorQuotesStart    = getenv('PAGANTIS_SIMULATOR_START_INSTALLMENTS');
347
        $pagantisSimulatorSkin           = getenv('PAGANTIS_SIMULATOR_DISPLAY_SKIN');
348
        $pagantisSimulatorPosition       = getenv('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION');
349
        $pagantisTitle                   = $this->l(getenv('PAGANTIS_TITLE'));
350
351
        $this->context->smarty->assign($this->getButtonTemplateVars($cart));
352
        $this->context->smarty->assign(array(
353
            'amount'                     => $orderTotal,
354
            'pagantisPublicKey'          => $pagantisPublicKey,
355
            'pagantisCSSSelector'        => $pagantisSimulatorCSSSelector,
356
            'pagantisPriceSelector'      => $pagantisSimulatorPriceSelector,
357
            'pagantisQuotesStart'        => $pagantisSimulatorQuotesStart,
358
            'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled,
359
            'pagantisSimulatorType'      => $pagantisSimulatorType,
360
            'pagantisSimulatorSkin'      => $pagantisSimulatorSkin,
361
            'pagantisSimulatorPosition'  => $pagantisSimulatorPosition,
362
            'pagantisIsEnabled'          => $pagantisIsEnabled,
363
            'pagantisTitle'              => $pagantisTitle,
364
            'paymentUrl'                 => $link->getModuleLink('pagantis', 'payment'),
365
            'ps_version'                 => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)),
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
366
        ));
367
368
        $paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
0 ignored issues
show
Bug introduced by
The type PrestaShop\PrestaShop\Core\Payment\PaymentOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
369
        $paymentOption
370
            ->setCallToActionText($pagantisTitle)
371
            ->setAction($link->getModuleLink('pagantis', 'payment'))
372
            ->setLogo($this->getPathUri(). 'logo.gif')
373
            ->setModuleName(__CLASS__)
374
        ;
375
376
377
        if (_PS_VERSION_ < 1.7) {
378
            $paymentOption->setAdditionalInformation(
379
                $this->fetch('module:pagantis/views/templates/hook/checkout-15.tpl')
380
            );
381
        }
382
383
        return array($paymentOption);
384
    }
385
386
    /**
387
     * Get the form for editing the BackOffice options of the module
388
     *
389
     * @return array
390
     */
391
    private function getConfigForm()
392
    {
393
        return array(
394
            'form' => array(
395
                'legend' => array(
396
                    'title' => $this->l('Basic Settings'),
397
                    'icon' => 'icon-cogs',
398
                ),
399
                'input' => array(
400
                    array(
401
                        'name' => 'pagantis_is_enabled',
402
                        'type' =>  (version_compare(_PS_VERSION_, '1.6')<0) ?'radio' :'switch',
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
403
                        'label' => $this->l('Module is enabled'),
404
                        'prefix' => '<i class="icon icon-key"></i>',
405
                        'class' => 't',
406
                        'required' => true,
407
                        'values'=> array(
408
                            array(
409
                                'id' => 'pagantis_is_enabled_true',
410
                                'value' => 1,
411
                                'label' => $this->l('Yes', get_class($this), null, false),
412
                            ),
413
                            array(
414
                                'id' => 'pagantis_is_enabled_false',
415
                                'value' => 0,
416
                                'label' => $this->l('No', get_class($this), null, false),
417
                            ),
418
                        )
419
                    ),
420
                    array(
421
                        'name' => 'pagantis_public_key',
422
                        'suffix' => $this->l('ex: pk_fd53cd467ba49022e4gf215e'),
423
                        'type' => 'text',
424
                        'size' => 60,
425
                        'label' => $this->l('Public Key'),
426
                        'prefix' => '<i class="icon icon-key"></i>',
427
                        'col' => 6,
428
                        'required' => true,
429
                    ),
430
                    array(
431
                        'name' => 'pagantis_private_key',
432
                        'suffix' => $this->l('ex: 21e5723a97459f6a'),
433
                        'type' => 'text',
434
                        'size' => 60,
435
                        'label' => $this->l('Secret Key'),
436
                        'prefix' => '<i class="icon icon-key"></i>',
437
                        'col' => 6,
438
                        'required' => true,
439
                    ),
440
                    array(
441
                        'name' => 'pagantis_simulator_is_enabled',
442
                        'type' => (version_compare(_PS_VERSION_, '1.6')<0) ?'radio' :'switch',
443
                        'label' => $this->l('Simulator is enabled'),
444
                        'prefix' => '<i class="icon icon-key"></i>',
445
                        'class' => 't',
446
                        'required' => true,
447
                        'values'=> array(
448
                            array(
449
                                'id' => 'pagantis_simulator_is_enabled_on',
450
                                'value' => 1,
451
                                'label' => $this->l('Yes'),
452
                            ),
453
                            array(
454
                                'id' => 'pagantis_simulator_is_enabled_off',
455
                                'value' => 0,
456
                                'label' => $this->l('No'),
457
                            ),
458
                        )
459
                    ),
460
                ),
461
                'submit' => array(
462
                    'title' => $this->l('Save'),
463
                ),
464
            ),
465
        );
466
    }
467
468
    /**
469
     * Form configuration function
470
     *
471
     * @param array $settings
472
     *
473
     * @return string
474
     */
475
    private function renderForm(array $settings)
476
    {
477
        $helper = new HelperForm();
0 ignored issues
show
Bug introduced by
The type HelperForm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
478
        $helper->show_toolbar = false;
479
        $helper->table = $this->table;
480
        $helper->module = $this;
481
        $helper->default_form_language = $this->context->language->id;
482
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
483
        $helper->identifier = $this->identifier;
484
        $helper->submit_action = 'submit'.$this->name;
485
        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
0 ignored issues
show
Bug introduced by
The type AdminController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
486
        $helper->token = Tools::getAdminTokenLite('AdminModules');
487
        $helper->tpl_vars = array(
488
            'fields_value' => $settings,
489
            'languages' => $this->context->controller->getLanguages(),
490
            'id_language' => $this->context->language->id,
491
        );
492
493
        $helper->fields_value['pagantis_url_ok'] = Configuration::get('pagantis_url_ok');
494
495
        return $helper->generateForm(array($this->getConfigForm()));
496
    }
497
498
    /**
499
     * Function to update the variables of Pagantis Module in the backoffice of prestashop
500
     *
501
     * @return string
502
     * @throws SmartyException
503
     */
504
    public function getContent()
505
    {
506
        $error = '';
507
        $message = '';
508
        $settings = array();
509
        $settings['pagantis_public_key'] = Configuration::get('pagantis_public_key');
510
        $settings['pagantis_private_key'] = Configuration::get('pagantis_private_key');
511
        $settingsKeys = array(
512
            'pagantis_is_enabled',
513
            'pagantis_public_key',
514
            'pagantis_private_key',
515
            'pagantis_simulator_is_enabled',
516
        );
517
518
        //Different Behavior depending on 1.6 or earlier
519
        if (Tools::isSubmit('submit'.$this->name)) {
520
            foreach ($settingsKeys as $key) {
521
                switch ($key) {
522
                    case 'pagantis_public_key':
523
                        $value = Tools::getValue($key);
524
                        if (!$value) {
525
                            $error = $this->l('Please add a Pagantis API Public Key');
526
                            break;
527
                        }
528
                        Configuration::updateValue($key, $value);
529
                        $settings[$key] = $value;
530
                        break;
531
                    case 'pagantis_private_key':
532
                        $value = Tools::getValue($key);
533
                        if (!$value) {
534
                            $error = $this->l('Please add a Pagantis API Private Key');
535
                            break;
536
                        }
537
                        Configuration::updateValue($key, $value);
538
                        $settings[$key] = $value;
539
                        break;
540
                    default:
541
                        $value = Tools::getValue($key);
542
                        Configuration::updateValue($key, $value);
543
                        $settings[$key] = $value;
544
                        break;
545
                }
546
                $message = $this->displayConfirmation($this->l('All changes have been saved'));
547
            }
548
        } else {
549
            foreach ($settingsKeys as $key) {
550
                    $settings[$key] = Configuration::get($key);
551
            }
552
        }
553
554
        if ($error) {
555
            $message = $this->displayError($error);
556
        }
557
558
        $logo = $this->getPathUri(). 'views/img/logo_pagantis.png';
559
        $tpl = $this->local_path.'views/templates/admin/config-info.tpl';
560
        $this->context->smarty->assign(array(
561
            'logo' => $logo,
562
            'form' => $this->renderForm($settings),
563
            'message' => $message,
564
            'version' => 'v'.$this->version,
565
        ));
566
567
        return $this->context->smarty->fetch($tpl);
568
    }
569
570
    /**
571
     * Hook to show payment method, this only applies on prestashop <= 1.6
572
     *
573
     * @param $params
574
     * @return bool | string
575
     * @throws Exception
576
     */
577
    public function hookPayment($params)
578
    {
579
        if (!$this->isPaymentMethodAvailable()) {
580
            return false;
581
        }
582
583
        /** @var Cart $cart */
584
        $cart                            = $params['cart'];
585
        $orderTotal                      = $cart->getOrderTotal();
586
        $link                            = $this->context->link;
587
        $pagantisPublicKey               = Configuration::get('pagantis_public_key');
588
        $pagantisSimulatorIsEnabled      = Configuration::get('pagantis_simulator_is_enabled');
589
        $pagantisIsEnabled               = Configuration::get('pagantis_is_enabled');
590
        $pagantisSimulatorType           = getenv('PAGANTIS_SIMULATOR_DISPLAY_TYPE');
591
        $pagantisSimulatorCSSSelector    = getenv('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR');
592
        $pagantisSimulatorPriceSelector  = getenv('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR');
593
        $pagantisSimulatorQuotesStart    = getenv('PAGANTIS_SIMULATOR_START_INSTALLMENTS');
594
        $pagantisSimulatorSkin           = getenv('PAGANTIS_SIMULATOR_DISPLAY_SKIN');
595
        $pagantisSimulatorPosition       = getenv('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION');
596
        $pagantisTitle                   = $this->l(getenv('PAGANTIS_TITLE'));
597
        $this->context->smarty->assign($this->getButtonTemplateVars($cart));
598
        $this->context->smarty->assign(array(
599
            'amount'                     => $orderTotal,
600
            'pagantisPublicKey'          => $pagantisPublicKey,
601
            'pagantisCSSSelector'        => $pagantisSimulatorCSSSelector,
602
            'pagantisPriceSelector'      => $pagantisSimulatorPriceSelector,
603
            'pagantisQuotesStart'        => $pagantisSimulatorQuotesStart,
604
            'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled,
605
            'pagantisSimulatorType'      => $pagantisSimulatorType,
606
            'pagantisSimulatorSkin'      => $pagantisSimulatorSkin,
607
            'pagantisSimulatorPosition'  => $pagantisSimulatorPosition,
608
            'pagantisIsEnabled'          => $pagantisIsEnabled,
609
            'pagantisTitle'              => $pagantisTitle,
610
            'paymentUrl'                 => $link->getModuleLink('pagantis', 'payment'),
611
            'ps_version'                 => str_replace('.', '-', Tools::substr(_PS_VERSION_, 0, 3)),
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
612
        ));
613
614
        $supercheckout_enabled = Module::isEnabled('supercheckout');
615
        $onepagecheckoutps_enabled = Module::isEnabled('onepagecheckoutps');
616
        $onepagecheckout_enabled = Module::isEnabled('onepagecheckout');
617
618
        $return = true;
619
        if ($supercheckout_enabled || $onepagecheckout_enabled || $onepagecheckoutps_enabled) {
620
            $this->checkLogoExists();
621
            $return = $this->display(__FILE__, 'views/templates/hook/onepagecheckout.tpl');
622
        } elseif (_PS_VERSION_ < 1.7) {
623
            $return = $this->display(__FILE__, 'views/templates/hook/checkout-15.tpl');
624
        }
625
        return $return;
626
    }
627
628
    /**
629
     * @param string $functionName
630
     *:
631
     * @return string
632
     * @throws PrestaShopDatabaseException
633
     * @throws PrestaShopException
634
     */
635
    public function productPageSimulatorDisplay($functionName)
636
    {
637
        $productConfiguration = getenv('PAGANTIS_SIMULATOR_DISPLAY_POSITION');
638
        /** @var ProductCore $product */
639
        $product = new Product(Tools::getValue('id_product'));
0 ignored issues
show
Bug introduced by
The type Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
640
        $amount = $product->getPublicPrice();
641
        $pagantisPublicKey                 = Configuration::get('pagantis_public_key');
642
        $pagantisSimulatorIsEnabled        = Configuration::get('pagantis_simulator_is_enabled');
643
        $pagantisIsEnabled                 = Configuration::get('pagantis_is_enabled');
644
        $pagantisSimulatorType             = getenv('PAGANTIS_SIMULATOR_DISPLAY_TYPE');
645
        $pagantisSimulatorCSSSelector      = getenv('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR');
646
        $pagantisSimulatorPriceSelector    = getenv('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR');
647
        $pagantisSimulatorQuantitySelector = getenv('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR');
648
        $pagantisSimulatorQuotesStart      = getenv('PAGANTIS_SIMULATOR_START_INSTALLMENTS');
649
        $pagantisSimulatorSkin             = getenv('PAGANTIS_SIMULATOR_DISPLAY_SKIN');
650
        $pagantisSimulatorPosition         = getenv('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION');
651
        $pagantisDisplayMinAmount          = getenv('PAGANTIS_DISPLAY_MIN_AMOUNT');
652
653
        if ($functionName != $productConfiguration ||
654
            $amount <= 0 ||
655
            $amount < $pagantisDisplayMinAmount ||
656
            !$pagantisSimulatorType
657
        ) {
658
            return null;
659
        }
660
661
        $this->context->smarty->assign(array(
662
            'amount'                     => $amount,
663
            'pagantisPublicKey'          => $pagantisPublicKey,
664
            'pagantisCSSSelector'        => $pagantisSimulatorCSSSelector,
665
            'pagantisPriceSelector'      => $pagantisSimulatorPriceSelector,
666
            'pagantisQuantitySelector'   => $pagantisSimulatorQuantitySelector,
667
            'pagantisSimulatorIsEnabled' => $pagantisSimulatorIsEnabled,
668
            'pagantisIsEnabled'          => $pagantisIsEnabled,
669
            'pagantisSimulatorType'      => $pagantisSimulatorType,
670
            'pagantisSimulatorSkin'      => $pagantisSimulatorSkin,
671
            'pagantisSimulatorPosition'  => $pagantisSimulatorPosition,
672
            'pagantisQuotesStart'        => $pagantisSimulatorQuotesStart,
673
        ));
674
675
        return $this->display(__FILE__, 'views/templates/hook/product-simulator.tpl');
676
    }
677
678
    /**
679
     * @return string
680
     * @throws PrestaShopDatabaseException
681
     * @throws PrestaShopException
682
     */
683
    public function hookDisplayRightColumn()
684
    {
685
686
        return $this->productPageSimulatorDisplay(__FUNCTION__);
687
    }
688
689
    /**
690
     * @return string
691
     * @throws PrestaShopDatabaseException
692
     * @throws PrestaShopException
693
     */
694
    public function hookDisplayLeftColumn()
695
    {
696
        return $this->productPageSimulatorDisplay(__FUNCTION__);
697
    }
698
699
    /**
700
     * @return string
701
     * @throws PrestaShopDatabaseException
702
     * @throws PrestaShopException
703
     */
704
    public function hookDisplayRightColumnProduct()
705
    {
706
        return $this->productPageSimulatorDisplay(__FUNCTION__);
707
    }
708
709
    /**
710
     * @return string
711
     * @throws PrestaShopDatabaseException
712
     * @throws PrestaShopException
713
     */
714
    public function hookDisplayLeftColumnProduct()
715
    {
716
        return $this->productPageSimulatorDisplay(__FUNCTION__);
717
    }
718
719
    /**
720
     * @return string
721
     * @throws PrestaShopDatabaseException
722
     * @throws PrestaShopException
723
     */
724
    public function hookDisplayProductButtons()
725
    {
726
        return $this->productPageSimulatorDisplay(__FUNCTION__);
727
    }
728
729
    /**
730
     * @param array $params
731
     *
732
     * @return string
733
     */
734
    public function hookDisplayOrderConfirmation($params)
735
    {
736
        $paymentMethod = (_PS_VERSION_ < 1.7) ? ($params["objOrder"]->payment) : ($params["order"]->payment);
0 ignored issues
show
Bug introduced by
The constant _PS_VERSION_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
737
738
        if ($paymentMethod == $this->displayName) {
739
            return $this->display(__FILE__, 'views/templates/hook/payment-return.tpl');
740
        }
741
742
        return null;
743
    }
744
745
    /**
746
     * Check logo exists in OPC module
747
     */
748
    public function checkLogoExists()
749
    {
750
        $logo = _PS_MODULE_DIR_ . '/onepagecheckoutps/views/img/payments/'. Tools::strtolower(__CLASS__). '.png';
0 ignored issues
show
Bug introduced by
The constant _PS_MODULE_DIR_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
751
        if (!file_exists($logo) && is_dir(_PS_MODULE_DIR_ . '/onepagecheckoutps/views/img/payments')) {
752
            copy(
753
                _PS_PAYLATER_DIR . '/views/img/logo-64x64.png',
754
                $logo
755
            );
756
        }
757
    }
758
}
759