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); |
|
|
|
|
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) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param RequestInterface $request |
141
|
|
|
* |
142
|
|
|
* @return bool|null |
143
|
|
|
*/ |
144
|
|
|
public function validateForCsrf(RequestInterface $request) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: