Issues (112)

Controller/Payment/ConfigV2.php (3 issues)

1
<?php
2
namespace Pagantis\Pagantis\Controller\Payment;
3
4
use Magento\Framework\App\Action\Action;
5
use Magento\Framework\App\Action\Context;
6
use Magento\Framework\App\ResourceConnection;
7
use Magento\Framework\App\RequestInterface;
8
use Magento\Framework\App\Request\InvalidRequestException;
9
use Pagantis\Pagantis\Helper\Config;
10
11
class ConfigV2 extends Action
12
{
13
    /** Config tablename */
14
    const CONFIG_TABLE = 'Pagantis_config';
15
16
    /** @var mixed $config */
17
    protected $config;
18
19
    /** @var ResourceConnection $dbObject */
20
    protected $dbObject;
21
22
    /**
23
     * Variable which contains extra configuration.
24
     * @var array $defaultConfigs
25
     */
26
    public $defaultConfigs = array('PAGANTIS_TITLE'=>'Paga en cuotas',
27
                                   'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE',
28
                                   'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE',
29
                                   'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
30
                                   'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3,
31
                                   'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12,
32
                                   'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
33
                                   'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER',
34
                                   'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default',
35
                                   'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default',
36
                                   'PAGANTIS_FORM_DISPLAY_TYPE'=>0,
37
                                   'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1,
38
                                   'PAGANTIS_DISPLAY_MAX_AMOUNT'=>0,
39
                                   'PAGANTIS_URL_OK'=>'',
40
                                   'PAGANTIS_URL_KO'=>'',
41
                                   'PAGANTIS_TITLE_EXTRA' => 'Pay up to 12 comfortable installments with Pagantis. Completely online and sympathetic request, and the answer is immediate!'
42
    );
43
44
    /**
45
     * ConfigV2 constructor.
46
     *
47
     * @param Context            $context
48
     * @param Config             $pagantisConfig
49
     * @param ResourceConnection $dbObject
50
     * @param RequestInterface   $request
51
     */
52
    public function __construct(
53
        Context $context,
54
        Config $pagantisConfig,
55
        ResourceConnection $dbObject,
56
        RequestInterface $request
57
    ) {
58
        $this->config = $pagantisConfig->getConfig();
59
        $this->dbObject = $dbObject;
60
61
        // CsrfAwareAction Magento2.3 compatibility
62
        if (interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) {
63
            if (isset($request) && $request->isPost() && empty($request->getParam('form_key'))) {
64
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
65
                $formKey = $objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
66
                $request->setParam('form_key', $formKey->getFormKey());
67
            }
68
        }
69
70
        return parent::__construct($context);
0 ignored issues
show
Are you sure the usage of parent::__construct($context) targeting Magento\Framework\App\Action\Action::__construct() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
    }
72
73
    /**
74
     * Main function
75
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
76
     */
77
    public function execute()
78
    {
79
        try {
80
            $response = array('status'=>null);
81
            $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE);
82
            $secretKey = $this->_request->getParam('secret');
83
            $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null;
84
85
            /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */
86
            $dbConnection = $this->dbObject->getConnection();
87
            if ($privateKey != $secretKey) {
88
                $response['status'] = 401;
89
                $response['result'] = 'Unauthorized';
90
            } elseif ($this->_request->isPost()) {
91
                if (count($_POST)) {
92
                    foreach ($_POST as $config => $value) {
93
                        if (isset($this->defaultConfigs[$config]) && $response['status']==null) {
94
                            $dbConnection->update(
95
                                $tableName,
96
                                array('value' => $value),
97
                                "config='$config'"
98
                            );
99
                        } else {
100
                            $response['status'] = 400;
101
                            $response['result'] = 'Bad request';
102
                        }
103
                    }
104
                } else {
105
                    $response['status'] = 422;
106
                    $response['result'] = 'Empty data';
107
                }
108
            }
109
110
            $formattedResult = array();
111
            if ($response['status']==null) {
112
                $dbResult = $dbConnection->fetchAll("select * from $tableName");
113
                foreach ($dbResult as $value) {
114
                    $formattedResult[$value['config']] = $value['value'];
115
                }
116
                $response['result'] = $formattedResult;
117
            }
118
            $result = json_encode($response['result']);
119
            header("HTTP/1.1 ".$response['status'], true, $response['status']);
120
            header('Content-Type: application/json', true);
121
            header('Content-Length: '.strlen($result));
122
            echo($result);
123
            exit();
124
        } catch (\Exception $e) {
125
            die($e->getMessage());
126
        }
127
    }
128
129
    /**
130
     * @param RequestInterface $request
131
     *
132
     * @return InvalidRequestException|null
133
     */
134
    public function createCsrfValidationException(RequestInterface $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

134
    public function createCsrfValidationException(/** @scrutinizer ignore-unused */ RequestInterface $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        return null;
137
    }
138
139
    /**
140
     * @param RequestInterface $request
141
     *
142
     * @return bool|null
143
     */
144
    public function validateForCsrf(RequestInterface $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

144
    public function validateForCsrf(/** @scrutinizer ignore-unused */ RequestInterface $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        return true;
147
    }
148
}
149