This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatus.php
executable file
·285 lines (264 loc) · 8.23 KB
/
status.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* PAYONE Prestashop Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAYONE Prestashop Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PAYONE Prestashop Connector. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @author patworx multimedia GmbH <[email protected]>
* @copyright 2003 - 2020 BS PAYONE GmbH
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
* @link http://www.payone.de
*/
namespace Payone;
$sFcPayoneBaseDir = dirname(__FILE__) . '../../../';
require_once($sFcPayoneBaseDir . 'config/config.inc.php');
if (!defined('_PS_VERSION_')) {
exit;
}
require_once($sFcPayoneBaseDir . 'init.php');
require_once(dirname(__FILE__) . '/fcpayone.php');
class Status
{
/**
* Status constructor.
*
* Execute security checks
*/
public function __construct()
{
$this->isIPValid();
$this->isKeyValid();
}
/**
* Executes
* - transaction saving
* - order state handling
* - status forwarding
*/
public function process()
{
echo 'TSOK';
$this->saveTransaction();
$this->handleOrderState();
$this->handleForwarding();
exit;
}
/**
* Check if remote ip is valid
* only payone ips are allowed
* whitelist set in \Payone\Validation\Validation
*/
private function isIPValid()
{
$sRemoteIp = \Tools::getRemoteAddr();
$oValidation = new \Payone\Validation\Validation();
if (!$oValidation->isValidPayoneIp($sRemoteIp)) {
\Payone\Base\Registry::getLog()->log('transaction status update failed: ip not valid - ' . $sRemoteIp, 4);
echo 'IP INVALID';
exit;
}
}
/**
* Returns request data
* remove not needed params
*
* @return array
*/
private function getRequestData()
{
$aRequest = \Tools::getAllValues();
$aRemoveFromRequest = array('isolang', 'id_lang', 'module', 'controller', 'fc');
foreach ($aRemoveFromRequest as $sParam) {
unset($aRequest[$sParam]);
}
return $aRequest;
}
/**
* Save transaction data
*/
private function saveTransaction()
{
$oTransaction = new \Payone\Base\Transaction();
$oTransaction->setData($this->getRequestData());
$oTransaction->save();
}
/**
* Handles order state update
* and updates transaction
*/
private function handleOrderState()
{
$oPayoneOrder = new \Payone\Base\Order();
$aOrderData = $oPayoneOrder->getOrderDataByTxId($this->getTxId());
if ($aOrderData) {
$this->updateOrderState($aOrderData);
$oTransaction = new \Payone\Base\Transaction();
$oTransaction->updateOrderId($this->getTxId(), (int)$aOrderData['id_order']);
$oTransaction->updateReference($this->getTxId(), $aOrderData['reference']);
}
}
/**
* Updates order state
*
* @param $aOrderData
*/
private function updateOrderState($aOrderData)
{
$iOrderState = $this->getOrderStateId($aOrderData);
if ($iOrderState && $iOrderState > 0) {
$oOrderHistory = new \OrderHistory();
$oOrderHistory->id_order = (int)$aOrderData['id_order'];
$oOrderHistory->changeIdOrderState($iOrderState, $oOrderHistory->id_order);
$oOrderHistory->addWithemail();
$oOrderHistory->save();
}
}
/**
* Returns order state based on transaction configuration
* First: will try to get payment method specific status, otherwise will try to get global config
*
* @param $aOrderData
* @return mixed
*/
private function getOrderStateId($aOrderData)
{
$aStates = \Payone\Base\Transaction::getStates();
$sAction = $this->getAction();
$sOrderStateIdent = null;
$sOrderStateIdentGlobal = null;
foreach ($aStates as $sState) {
if (\Tools::strtolower($sAction) == \Tools::strtolower($sState)) {
$sOrderStateIdent = 'FC_PAYONE_PAYMENT_TRANSACTION_MAPPING_'
. \Tools::strtoupper($aOrderData['paymentid']) . '_'
. \Tools::strtoupper($sState);
$sOrderStateIdentGlobal = 'FC_PAYONE_PAYMENT_TRANSACTION_MAPPING_TRANSACTIONSTATEMAPPING_'
. \Tools::strtoupper($sState);
break;
}
}
$iOrderState = \Configuration::get($sOrderStateIdent);
if ($iOrderState) {
if ($iOrderState > 0) {
return $iOrderState;
}
}
$iOrderStateGlobal = \Configuration::get($sOrderStateIdentGlobal);
if ($iOrderStateGlobal) {
return $iOrderStateGlobal;
}
}
/**
* Returns txid
*
* @return mixed
*/
private function getTxId()
{
return \Tools::getValue('txid');
}
/**
* Check if key is valid
*
* @return bool
*/
private function isKeyValid()
{
$sKey = \Tools::getValue('key');
$sConfigurationKey = \Configuration::get('FC_PAYONE_CONNECTION_PORTALKEY');
if ($sKey != md5($sConfigurationKey)) {
echo 'Key wrong or missing!';
\Payone\Base\Registry::getLog()->log('transaction status update failed: key wrong or missing', 4);
exit;
}
}
/**
* Returns transaction action
*
* @return mixed
*/
private function getAction()
{
return \Tools::getValue('txaction');
}
/**
* Returns forwarding configuration for given action
*
* @param string $sAction
*
* @return string
*/
private function getForwardingConfiguration($sAction)
{
return trim(\Configuration::get('FC_PAYONE_TRANSACTION_FORWARDING_' . \Tools::strtoupper($sAction)));
}
/**
* Returns array with forwardings
*
* @return array
*/
private function getForwardingList()
{
$sAction = $this->getAction();
if (!($sForwardingConfig = $this->getForwardingConfiguration($sAction))) {
return array();
}
$aForwardings = array();
$aConfig = explode("\n", str_replace("\r", '', $sForwardingConfig));
foreach ($aConfig as $sForwarding) {
$aForwarding = $this->getForwardingConfigFromString($sForwarding);
if (count($aForwarding) > 0) {
$aForwardings[] = $aForwarding;
}
}
return $aForwardings;
}
/**
* Returns forwarding config array for config line
*
* @param $sForwarding
* @return array
*/
private function getForwardingConfigFromString($sForwarding)
{
$aForwarding = explode(';', $sForwarding);
if (count($aForwarding) > 0 && isset($aForwarding[0])) {
if (isset($aForwarding[1])) {
$iTimeout = (int)trim($aForwarding[1]);
} else {
$iTimeout = 20;
}
return array(
'url' => trim($aForwarding[0]),
'timeout' => $iTimeout,
);
}
}
/**
* Handles request forwarding
*/
private function handleForwarding()
{
$aForwardings = $this->getForwardingList();
if (count($aForwardings) > 0) {
foreach ($aForwardings as $aForwarding) {
$oDispatcher = new \Payone\Request\Dispatcher();
$oDispatcher->setCertificationUsage(false);
$oDispatcher->setTimeout($aForwarding['timeout']);
$oDispatcher->dispatchRequest(\Tools::getAllValues(), false, false, $aForwarding['url']);
}
}
}
}
$oScript = new \Payone\Status();
$oScript->process();