-
Notifications
You must be signed in to change notification settings - Fork 27
/
statusbase.php
331 lines (290 loc) · 8.75 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
<?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
*/
class fcPayOneTransactionStatusBase extends oxBase {
protected $_aShopList = null;
protected $_sLogFile = 'log/fcpo_message_forwarding.log';
protected $_sExceptionLog = 'log/fcpo_statusmessage_exception.log';
protected $_oFcOrder = null;
protected $_oUtilsObject = null;
/**
* Returns instance of oxUtilsObject
*
* @param void
* @return |null
* @throws Exception
*/
protected function _getUtilsObject()
{
if ($this->_oUtilsObject === null) {
try {
$this->_oUtilsObject = oxNew('oxUtilsObject');
} catch (Exception $e) {
throw $e;
}
}
return $this->_oUtilsObject;
}
/**
* Check if logging is activated by configuration
*
* @param void
* @return bool
*/
protected function _fcCheckLoggingAllowed()
{
$oConfig = $this->getConfig();
$sLogMethod =
$oConfig->getConfigParam('sTransactionRedirectLogging');
$blLoggingAllowed = $sLogMethod == 'all';
return $blLoggingAllowed;
}
/**
* Check and return post parameter
*
* @param string $sKey
* @return string
*/
public function fcGetPostParam($sKey)
{
$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 ) {
if($this->getConfig()->isUtf() ) {
$mValue = utf8_encode($mValue);
}
$sReturn = $mValue;
}
return $sReturn;
}
protected function _getShopList()
{
if($this->_aShopList === null) {
$aShops = array();
$sQuery = "SELECT oxid FROM oxshops";
$aRows = oxDb::getDb()->getAll($sQuery);
foreach ($aRows as $aRow) {
$aShops[] = $aRow[0];
}
$this->_aShopList = $aShops;
}
return $this->_aShopList;
}
protected function _getConfigParams($sParam)
{
$aShops = $this->_getShopList();
$aParams = array();
foreach ($aShops as $sShop) {
$mValue = $this->getConfig()->getShopConfVar($sParam, $sShop);
if($mValue) {
$aParams[$sShop] = $mValue;
}
}
return $aParams;
}
/**
* Check if key is available and valid. Throw exception if not
*
* @param void
* @return void
* @throws Exception
*/
protected function _isKeyValid()
{
if(defined('STDIN')) {
return;
}
$sKey = $this->fcGetPostParam('key');
if (!$sKey) {
throw new Exception('Key missing!');
}
$aKeys = array_merge(
array_values($this->_getConfigParams('sFCPOPortalKey')),
array_values($this->_getConfigParams('sFCPOSecinvoicePortalKey')),
array_values($this->_getConfigParams('sFCPOPLPortalKey'))
);
$blValid = false;
foreach ($aKeys as $i => $sConfigKey) {
if(md5($sConfigKey) != $sKey) {
continue;
}
$blValid = true;
break;
}
if(!$blValid) {
throw new Exception('Invalid key!');
}
}
/**
* Logs exception for later analysis
*
* @param $sMessage
* @return void
*/
protected function _logException($sMessage)
{
$sBasePath = dirname(__FILE__) . "/../../../";
$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);
}
/**
* Logs given message if logging is activated
*
* @param $sMessage
* @return void
*/
protected function _logForwardMessage($sMessage)
{
$blLoggingAllowed = $this->_fcCheckLoggingAllowed();
if (!$blLoggingAllowed) return;
$sBasePath = dirname(__FILE__) . "/../../../";
$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);
}
/**
* Adding param
*
* @param $sKey
* @param $mValue
* @return string
*/
protected function _addParam($sKey, $mValue)
{
$sParams = '';
if(is_array($mValue)) {
foreach ($mValue as $sKey2 => $mValue2) {
$sParams .= $this->_addParam($sKey.'['.$sKey2.']', $mValue2);
}
} else {
$sParams .= "&".$sKey."=".urlencode($mValue);
}
return $sParams;
}
/**
* Method collects redirect targets and add them to statusforward queue
*
* @param $sStatusmessageId
* @param $sPayoneStatus
* @return void
* @throws
*/
protected function _addQueueEntries($sStatusmessageId, $sPayoneStatus=null)
{
try {
if ($sPayoneStatus === null) {
$sPayoneStatus = $this->fcGetPostParam('txaction');
}
$sQuery = "
SELECT
OXID
FROM
fcpostatusforwarding
WHERE
fcpo_payonestatus = '{$sPayoneStatus}'";
$aRows = oxDb::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);
}
} catch (Exception $e) {
throw $e;
}
}
/**
* Add certain combination of transaction and forward configuration
* to queue
*
* @param $sStatusmessageId
* @param $sForwardId
* @throws Exception
*/
protected function _addToQueue($sStatusmessageId, $sForwardId)
{
try {
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',
'',
''
)
";
oxDb::getDb()->Execute($sQuery);
} catch (Exception $e) {
throw $e;
}
}
/**
* Checks if a certain combination of statusmessageid already
* exists
*
* @param $sStatusmessageId
* @param $sForwardId
* @return bool
*/
protected function _queueEntryExists($sStatusmessageId, $sForwardId)
{
$sQuery = "
SELECT COUNT(*)
FROM fcpostatusforwardqueue
WHERE
FCSTATUSMESSAGEID='{$sStatusmessageId}' AND
FCSTATUSFORWARDID='{$sForwardId}'
";
$iRows = (int) oxDb::getDb()->getOne($sQuery);
return (bool) ($iRows > 0);
}
}