Issues (42)

b2binpay.php (25 issues)

1
<?php
2
/**
3
 * NOTICE OF LICENSE
4
 *
5
 * Copyright (c) 2019 B2BinPay
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 *
25
 *  @author    B2BINPAY <[email protected]>
26
 *  @copyright 2019 B2BinPay
27
 *  @license   https://github.com/b2binpay/prestashop/blob/master/LICENSE  The MIT License (MIT)
28
 */
29
30
if (!defined('_PS_VERSION_')) {
31
    exit;
32
}
33
34
if (defined('_PS_MODULE_DIR_')) {
35
    require_once _PS_MODULE_DIR_.'b2binpay/vendor/autoload.php';
0 ignored issues
show
The constant _PS_MODULE_DIR_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
}
37
38
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
0 ignored issues
show
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...
39
use B2Binpay\Currency as B2BinpayCurrency;
40
use B2Binpay\Provider;
41
use B2Binpay\Exception\ServerApiException;
42
43
class B2binpay extends PaymentModule
0 ignored issues
show
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...
44
{
45
    public $provider;
46
    public $b2binpay_currency;
47
48
    public function __construct()
49
    {
50
        $this->name = 'b2binpay';
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...
51
        $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...
52
        $this->version = '1.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...
53
        $this->is_eu_compatible = 1;
0 ignored issues
show
Bug Best Practice introduced by
The property is_eu_compatible does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
0 ignored issues
show
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...
55
        $this->module_key = 'c1ae0a3ef16e14e71d3ded99c5faf216';
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...
56
        $this->author = 'B2BinPay';
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...
57
        $this->controllers = array('redirect', 'callback');
0 ignored issues
show
Bug Best Practice introduced by
The property controllers does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
59
        $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...
60
        $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...
61
62
        $this->bootstrap = true;
0 ignored issues
show
Bug Best Practice introduced by
The property bootstrap does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
64
        parent::__construct();
65
66
        $this->provider = new Provider(
67
            Configuration::get('B2BINPAY_AUTH_KEY'),
0 ignored issues
show
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...
68
            Configuration::get('B2BINPAY_AUTH_SECRET'),
69
            Configuration::get('B2BINPAY_TEST_MODE')
70
        );
71
72
        $this->b2binpay_currency = new B2BinpayCurrency();
73
74
        $this->displayName = $this->l('B2BinPay Crypto Payment Gateway');
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...
75
        $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...
76
            'Accept Bitcoin, Bitcoin Cash, Litecoin, Ethereum, and other CryptoCurrencies via B2BinPay.'
77
        );
78
79
        $this->warning = Configuration::get('B2BINPAY_WARNING');
0 ignored issues
show
Bug Best Practice introduced by
The property warning does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
81
        if (!count(Currency::checkPaymentCurrencies($this->id))) {
0 ignored issues
show
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...
82
            $this->warning = $this->l('No currency has been set for this module.');
83
        }
84
85
        if (empty(Configuration::get('B2BINPAY_AUTH_KEY'))
86
            || empty(Configuration::get('B2BINPAY_AUTH_SECRET'))
87
        ) {
88
            $this->warning = $this->l(
89
                'B2BinPay Account details must be configured in order to use this module correctly.'
90
            );
91
        }
92
    }
93
94
    public function install()
95
    {
96
        if (!parent::install()
97
            || !$this->registerHook('paymentOptions')
98
            || !$this->registerHook('header')
99
            || !$this->registerHook('backOfficeHeader')
100
        ) {
101
            return false;
102
        }
103
104
        return true;
105
    }
106
107
    public function uninstall()
108
    {
109
        return (
110
            Configuration::deleteByName('B2BINPAY_TITLE') &&
111
            Configuration::deleteByName('B2BINPAY_TEST_MODE') &&
112
            Configuration::deleteByName('B2BINPAY_AUTH_KEY') &&
113
            Configuration::deleteByName('B2BINPAY_AUTH_SECRET') &&
114
            Configuration::deleteByName('B2BINPAY_WALLETS') &&
115
            Configuration::deleteByName('B2BINPAY_MARKUP') &&
116
            Configuration::deleteByName('B2BINPAY_LIFETIME') &&
117
            Configuration::deleteByName('B2BINPAY_WARNING') &&
118
            parent::uninstall()
119
        );
120
    }
121
122
    /**
123
     * Load the configuration form
124
     */
125
    public function getContent()
126
    {
127
        /**
128
         * If values have been submitted in the form, process.
129
         */
130
        if (((bool)Tools::isSubmit('submitB2binpayModule')) === true) {
0 ignored issues
show
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...
131
            $this->postProcess();
132
        }
133
134
        $this->context->smarty->assign('module_dir', $this->_path);
135
        $this->context->smarty->assign(
136
            'wallets',
137
            json_decode(
138
                Configuration::get('B2BINPAY_WALLETS')
139
            )
140
        );
141
        $this->context->smarty->assign('warning', $this->warning);
142
143
        $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/template.tpl');
144
145
        return $output.$this->renderForm();
146
    }
147
148
    /**
149
     * Create the form that will be displayed in the configuration of your module.
150
     */
151
    protected function renderForm()
152
    {
153
        $helper = new HelperForm();
0 ignored issues
show
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...
154
155
        $helper->show_toolbar = false;
156
        $helper->table = $this->table;
157
        $helper->module = $this;
158
        $helper->default_form_language = $this->context->language->id;
159
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
160
161
        $helper->identifier = $this->identifier;
162
        $helper->submit_action = 'submitB2binpayModule';
163
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
164
            .'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
165
        $helper->token = Tools::getAdminTokenLite('AdminModules');
166
167
        $helper->tpl_vars = array(
168
            'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
169
            'languages' => $this->context->controller->getLanguages(),
170
            'id_language' => $this->context->language->id,
171
        );
172
173
        return $helper->generateForm(array($this->getConfigForm()));
174
    }
175
176
    /**
177
     * Create the structure of your form.
178
     */
179
    protected function getConfigForm()
180
    {
181
        return array(
182
            'form' => array(
183
                'legend' => array(
184
                    'title' => $this->l('Settings'),
185
                    'icon' => 'icon-cogs',
186
                ),
187
                'input' => array(
188
                    array(
189
                        'type' => 'b2binpay-warning',
190
                        'name' => 'B2BINPAY_WARNING',
191
                    ),
192
                    array(
193
                        'col' => 2,
194
                        'type' => 'text',
195
                        'name' => 'B2BINPAY_TITLE',
196
                        'label' => $this->l('Title'),
197
                        'desc' => $this->l('The payment method title which a customer sees at the checkout'),
198
                        'required' => true,
199
                    ),
200
                    array(
201
                        'type' => 'switch',
202
                        'label' => $this->l('Test Mode (Sandbox)'),
203
                        'name' => 'B2BINPAY_TEST_MODE',
204
                        'is_bool' => true,
205
                        'desc' => $this->l(
206
                            'Use this module in test mode. Warning: Sandbox and main gateway has their own credentials!'
207
                        ),
208
                        'required' => true,
209
                        'values' => array(
210
                            array(
211
                                'id' => 'active_on',
212
                                'value' => true,
213
                                'label' => $this->l('Enabled'),
214
                            ),
215
                            array(
216
                                'id' => 'active_off',
217
                                'value' => false,
218
                                'label' => $this->l('Disabled'),
219
                            ),
220
                        ),
221
                    ),
222
                    array(
223
                        'col' => 2,
224
                        'type' => 'text',
225
                        'name' => 'B2BINPAY_AUTH_KEY',
226
                        'label' => $this->l('Auth Key'),
227
                        'desc' => $this->l('B2BinPay API Auth Key'),
228
                        'required' => true,
229
                    ),
230
                    array(
231
                        'col' => 2,
232
                        'type' => 'text',
233
                        'name' => 'B2BINPAY_AUTH_SECRET',
234
                        'label' => $this->l('Auth Secret'),
235
                        'desc' => $this->l('B2BinPay API Auth Secret'),
236
                        'required' => true,
237
                    ),
238
                    array(
239
                        'type' => 'b2binpay-wallets',
240
                        'name' => 'B2BINPAY_WALLETS',
241
                        'label' => $this->l('Wallets'),
242
                        'required' => true,
243
                    ),
244
                    array(
245
                        'col' => 2,
246
                        'type' => 'text',
247
                        'name' => 'B2BINPAY_MARKUP',
248
                        'label' => $this->l('Markup (%)'),
249
                        'desc' => $this->l('Markup percentage for each payment'),
250
                    ),
251
                    array(
252
                        'col' => 2,
253
                        'type' => 'text',
254
                        'name' => 'B2BINPAY_LIFETIME',
255
                        'label' => $this->l('Order lifetime (seconds)'),
256
                        'desc' => $this->l('Lifetime for your orders in seconds'),
257
                        'required' => true,
258
                    ),
259
                ),
260
                'submit' => array(
261
                    'title' => $this->l('Save'),
262
                ),
263
            ),
264
        );
265
    }
266
267
    /**
268
     * Set values for the inputs.
269
     */
270
    protected function getConfigFormValues()
271
    {
272
        return array(
273
            'B2BINPAY_TITLE' => Configuration::get('B2BINPAY_TITLE'),
274
            'B2BINPAY_TEST_MODE' => Configuration::get('B2BINPAY_TEST_MODE'),
275
            'B2BINPAY_AUTH_KEY' => Configuration::get('B2BINPAY_AUTH_KEY'),
276
            'B2BINPAY_AUTH_SECRET' => Configuration::get('B2BINPAY_AUTH_SECRET'),
277
            'B2BINPAY_WALLETS' => Configuration::get('B2BINPAY_WALLETS'),
278
            'B2BINPAY_MARKUP' => Configuration::get('B2BINPAY_MARKUP'),
279
            'B2BINPAY_LIFETIME' => Configuration::get('B2BINPAY_LIFETIME'),
280
        );
281
    }
282
283
    /**
284
     * Save form data.
285
     */
286
    protected function postProcess()
287
    {
288
        $form_values = $this->getConfigFormValues();
289
290
        foreach (array_keys($form_values) as $key) {
291
            Configuration::updateValue($key, Tools::getValue($key));
292
        }
293
294
        $this->warning = $this->checkConfig(
0 ignored issues
show
Bug Best Practice introduced by
The property warning does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
295
            Tools::getValue('B2BINPAY_AUTH_KEY'),
296
            Tools::getValue('B2BINPAY_AUTH_SECRET'),
297
            Tools::getValue('B2BINPAY_TEST_MODE'),
298
            Tools::getValue('B2BINPAY_WALLETS')
299
        );
300
301
        Configuration::updateValue('B2BINPAY_WARNING', $this->warning);
302
    }
303
304
    /**
305
     * Check B2BinPay authorization.
306
     * @param $key
307
     * @param $secret
308
     * @param $test
309
     * @param $wallets
310
     * @return string|null
311
     */
312
    protected function checkConfig($key, $secret, $test, $wallets)
313
    {
314
        if (empty($key) || empty($secret)) {
315
            return $this->l('You need to enter B2BinPay Auth Key/Secret.');
316
        }
317
318
        $this->provider = new Provider($key, $secret, $test);
319
320
        try {
321
            $this->provider->getAuthToken();
322
        } catch (ServerApiException $e) {
323
            return $this->l('Wrong B2BinPay Auth Key/Secret.');
324
        }
325
326
        if (empty(json_decode($wallets))) {
327
            return $this->l('You need to enter B2BinPay Wallet(s).');
328
        }
329
330
        $wallets_retrieved = $this->retrieveWallets($wallets);
331
        Configuration::updateValue('B2BINPAY_WALLETS', json_encode($wallets_retrieved));
332
333
        if (empty($wallets_retrieved)) {
334
            return $this->l('You entered wrong B2BinPay Wallet(s).');
335
        }
336
337
        return null;
338
    }
339
340
    /**
341
     * @param string $wallets_json
342
     * @return array
343
     */
344
    protected function retrieveWallets($wallets_json)
345
    {
346
        $wallets = json_decode($wallets_json);
347
        $wallets_updated = array();
348
349
        foreach ($wallets as $wallet) {
350
            if (empty($wallet->id)) {
351
                continue;
352
            }
353
354
            try {
355
                $b2binpay_wallet = $this->provider->getWallet((int)$wallet->id);
356
            } catch (ServerApiException $e) {
357
                continue;
358
            }
359
360
            $currency = (empty($wallet->currency))
361
                ? $this->b2binpay_currency->getName($b2binpay_wallet->currency->iso) : $wallet->currency;
362
363
            array_push(
364
                $wallets_updated,
365
                array(
366
                    'id' => $wallet->id,
367
                    'currency' => $currency,
368
                    'alpha' => $b2binpay_wallet->currency->alpha,
369
                    'iso' => $b2binpay_wallet->currency->iso,
370
                )
371
            );
372
        }
373
374
        return $wallets_updated;
375
    }
376
377
    /**
378
     * Add the CSS & JavaScript files you want to be loaded in the BO.
379
     */
380
    public function hookBackOfficeHeader()
381
    {
382
        $this->context->controller->addJS($this->_path.'views/js/back.js');
383
    }
384
385
    /**
386
     * Add the CSS & JavaScript files you want to be added on the FO.
387
     */
388
    public function hookHeader()
389
    {
390
        $this->context->controller->addCSS($this->_path.'/views/css/front.css');
391
    }
392
393
    public function hookPaymentOptions($params)
394
    {
395
        if (!$this->active || !empty($this->warning)) {
396
            return;
397
        }
398
399
        $paymentOption = new PaymentOption();
400
        $paymentOption->setCallToActionText(Configuration::get('B2BINPAY_TITLE'))
401
            ->setForm($this->generateForm())
402
            ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/logo.png'));
0 ignored issues
show
The constant _PS_MODULE_DIR_ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
The type Media 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...
403
404
        $payment_options = array($paymentOption);
405
406
        return $payment_options;
407
    }
408
409
    protected function generateForm()
410
    {
411
        $this->context->smarty->assign(
412
            array(
413
                'action' => $this->context->link->getModuleLink($this->name, 'redirect', array(), true),
414
                'crypto_list' => json_decode(Configuration::get('B2BINPAY_WALLETS')),
415
            )
416
        );
417
418
        return $this->context->smarty->fetch($this->local_path.'views/templates/front/payment_form.tpl');
419
    }
420
}
421