-
Notifications
You must be signed in to change notification settings - Fork 4
/
statusbase.php
333 lines (300 loc) · 9.32 KB
/
statusbase.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* PAYONE OXID 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 OXID 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 OXID Connector. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://www.payone.de
* @copyright (C) Payone GmbH
* @version OXID eShop CE
*/
namespace Fatchip\PayOne;
use Exception;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
use OxidEsales\Eshop\Core\Model\BaseModel;
use OxidEsales\Eshop\Core\UtilsObject;
use oxUtilsObject;
class FcPayOneTransactionStatusBase extends BaseModel
{
protected array $_aShopList;
protected string $_sLogFile = 'log/fcpo_message_forwarding.log';
protected string$_sExceptionLog = 'log/fcpo_statusmessage_exception.log';
protected Order $_oFcOrder;
protected UtilsObject $_oUtilsObject;
/** @var string */
private const S_QUERY = "SELECT oxid FROM oxshops";
/**
* Check if key is available and valid. Throw exception if not
*
*
* @return void
* @throws Exception
*/
protected function _isKeyValid(): void
{
if (defined('STDIN')) {
return;
}
$sKey = $this->fcGetPostParam('key');
if ($sKey === '' || $sKey === '0') {
throw new Exception('Key missing!');
}
$aKeys = [...array_values($this->_getConfigParams('sFCPOPortalKey')), ...array_values($this->_getConfigParams('sFCPOSecinvoicePortalKey'))];
$blValid = false;
foreach ($aKeys as $aKey) {
if (md5((string) $aKey) !== $sKey) {
continue;
}
$blValid = true;
break;
}
if (!$blValid) {
throw new Exception('Invalid key!');
}
}
/**
* Check and return post parameter
*
* @param string $sKey
* @return string
*/
public function fcGetPostParam(string $sKey): string
{
$sReturn = '';
$mValue = filter_input(INPUT_GET, $sKey, FILTER_SANITIZE_SPECIAL_CHARS);
if (!$mValue) {
$mValue = filter_input(INPUT_POST, $sKey, FILTER_SANITIZE_SPECIAL_CHARS);
}
if ($mValue) {
$sReturn = utf8_encode((string) $mValue);
}
return $sReturn;
}
/**
* @param string $sParam
* @return array
*/
protected function _getConfigParams(string $sParam): array
{
$aShops = $this->_getShopList();
$aParams = [];
foreach ($aShops as $aShop) {
$mValue = $this-> getConfig()->getShopConfVar($sParam, $aShop);
if ($mValue) {
$aParams[$aShop] = $mValue;
}
}
return $aParams;
}
/**
* @return array
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
*/
protected function _getShopList(): array
{
if ($this->_aShopList === null) {
$aShops = [];
$aRows = DatabaseProvider::getDb()->getAll(self::S_QUERY);
foreach ($aRows as $aRow) {
$aShops[] = $aRow[0];
}
$this->_aShopList = $aShops;
}
return $this->_aShopList;
}
/**
* Logs exception for later analysis
*
* @param $sMessage
* @return void
*/
protected function _logException(string $sMessage): void
{
$sBasePath = __DIR__ . "/../../../";
$sLogFilePath = $sBasePath . $this->_sExceptionLog;
$sPrefix = "[" . date('Y-m-d H:i:s') . "] ";
$sFullMessage = $sPrefix . $sMessage . "\n";
$oLogFile = fopen($sLogFilePath, 'a');
fwrite($oLogFile, $sFullMessage);
fclose($oLogFile);
}
/**
* Adding param
*
* @param string $sKey
* @param $mValue
* @return string
*/
protected function _addParam(string $sKey, $mValue): string
{
$sParams = '';
if (is_array($mValue)) {
foreach ($mValue as $sKey2 => $mValue2) {
$sParams .= $this->_addParam($sKey . '[' . $sKey2 . ']', $mValue2);
}
} else {
$sParams .= "&" . $sKey . "=" . urlencode((string) $mValue);
}
return $sParams;
}
/**
* Method collects redirect targets and add them to statusforward queue
*
* @param string $sStatusmessageId
* @param string|null $sPayoneStatus
* @return void
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
*/
protected function _addQueueEntries(string $sStatusmessageId, string $sPayoneStatus = null): void
{
$sQuery = null;
$aRows = null;
if ($sPayoneStatus === null) {
$sPayoneStatus = $this->fcGetPostParam('txaction');
}
$sQuery = "
SELECT
OXID
FROM
fcpostatusforwarding
WHERE
fcpo_payonestatus = '{$sPayoneStatus}'";
$aRows = DatabaseProvider::getDb()->getAll($sQuery);
$this->_logForwardMessage('Add fowardings to queue: ' . print_r($aRows, true));
foreach ($aRows as $aRow) {
$sForwardId = (string)$aRow[0];
$this->_addToQueue($sStatusmessageId, $sForwardId);
}
}
/**
* Logs given message if logging is activated
*
* @param string $sMessage
* @return void
*/
protected function _logForwardMessage(string $sMessage): void
{
$blLoggingAllowed = $this->_fcCheckLoggingAllowed();
if (!$blLoggingAllowed) {
return;
}
$sBasePath = __DIR__ . "/../../../";
$sLogFilePath = $sBasePath . $this->_sLogFile;
$sPrefix = "[" . date('Y-m-d H:i:s') . "] ";
$sFullMessage = $sPrefix . $sMessage . "\n";
$oLogFile = fopen($sLogFilePath, 'a');
fwrite($oLogFile, $sFullMessage);
fclose($oLogFile);
}
/**
* Check if logging is activated by configuration
*
*
* @return bool
*/
protected function _fcCheckLoggingAllowed(): bool
{
$oConfig = $this->getConfig();
$sLogMethod =
$oConfig->getConfigParam('sTransactionRedirectLogging');
return $sLogMethod == 'all';
}
/**
* Add certain combination of transaction and forward configuration
* to queue
*
* @param string $sStatusmessageId
* @param string $sForwardId
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
* @throws Exception
*/
protected function _addToQueue(string $sStatusmessageId, string $sForwardId)
{
$oUtilsObject = null;
$sOxid = null;
$sQuery = null;
if ($this->_queueEntryExists($sStatusmessageId, $sForwardId)) {
$this->_logForwardMessage(
'Entry already exitsts. Skipping. StatusmessageId: ' .
$sStatusmessageId .
', ForwardId: ' .
$sForwardId
);
return;
}
$oUtilsObject = $this->_getUtilsObject();
$sOxid = $oUtilsObject->generateUId();
$sQuery = "
INSERT INTO fcpostatusforwardqueue
(
OXID,
FCSTATUSMESSAGEID,
FCSTATUSFORWARDID,
FCTRIES,
FCLASTTRY,
FCLASTREQUEST,
FCLASTRESPONSE
)
VALUES
(
'{$sOxid}',
'{$sStatusmessageId}',
'{$sForwardId}',
'0',
'0000-00-00 00:00:00',
'',
''
)
";
DatabaseProvider::getDb()->Execute($sQuery);
}
/**
* Checks if a certain combination of statusmessageid already
* exists
*
* @param string $sStatusmessageId
* @param string $sForwardId
* @return bool
* @throws DatabaseConnectionException
*/
protected function _queueEntryExists(string $sStatusmessageId, string $sForwardId): bool
{
$sQuery = "
SELECT COUNT(*)
FROM fcpostatusforwardqueue
WHERE
FCSTATUSMESSAGEID='{$sStatusmessageId}' AND
FCSTATUSFORWARDID='{$sForwardId}'
";
$iRows = (int)DatabaseProvider::getDb()->getOne($sQuery);
return $iRows > 0;
}
/**
* Returns instance of oxUtilsObject
*
*
* @return UtilsObject
*/
protected function _getUtilsObject(): UtilsObject
{
if ($this->_oUtilsObject === null) {
$this->_oUtilsObject = oxNew(UtilsObject::class);
}
return $this->_oUtilsObject;
}
}