1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* PHP version 5 |
18
|
|
|
* |
19
|
|
|
* @category Payone |
20
|
|
|
* @package Payone_Magento2_Plugin |
21
|
|
|
* @author FATCHIP GmbH <[email protected]> |
22
|
|
|
* @copyright 2003 - 2016 Payone GmbH |
23
|
|
|
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
24
|
|
|
* @link http://www.payone.de |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Payone\Core\Helper; |
28
|
|
|
|
29
|
|
|
use Magento\Framework\DB\Adapter\AdapterInterface; |
30
|
|
|
use Magento\Quote\Api\Data\AddressInterface; |
31
|
|
|
use Magento\Store\Model\ScopeInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Helper class for everything that has to do with database connections |
35
|
|
|
*/ |
36
|
|
|
class Database extends \Payone\Core\Helper\Base |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Database connection resource |
40
|
|
|
* |
41
|
|
|
* @var \Magento\Framework\App\ResourceConnection |
42
|
|
|
*/ |
43
|
|
|
protected $databaseResource; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param \Magento\Framework\App\Helper\Context $context |
49
|
|
|
* @param \Magento\Store\Model\StoreManagerInterface $storeManager |
50
|
|
|
* @param \Magento\Framework\App\ResourceConnection $resource |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
\Magento\Framework\App\Helper\Context $context, |
54
|
|
|
\Magento\Store\Model\StoreManagerInterface $storeManager, |
55
|
|
|
\Magento\Framework\App\ResourceConnection $resource |
56
|
|
|
) { |
57
|
|
|
parent::__construct($context, $storeManager); |
58
|
|
|
$this->databaseResource = $resource; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return database connection |
63
|
|
|
* |
64
|
|
|
* @return AdapterInterface |
65
|
|
|
*/ |
66
|
|
|
protected function getDb() |
67
|
|
|
{ |
68
|
|
|
return $this->databaseResource->getConnection(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the state for a given status from the database |
73
|
|
|
* |
74
|
|
|
* @param string $sStatus |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function getStateByStatus($sStatus) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$sQuery = " SELECT |
80
|
|
|
state |
81
|
|
|
FROM |
82
|
|
|
".$this->databaseResource->getTableName('sales_order_status_state')." |
83
|
|
|
WHERE |
84
|
|
|
status = :status |
85
|
|
|
LIMIT 1"; |
86
|
|
|
$sState = $this->getDb()->fetchOne($sQuery, ['status' => $sStatus]); |
87
|
|
|
return $sState; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the order increment id by the given TransactionStatus txid |
92
|
|
|
* |
93
|
|
|
* @param string $sTxid |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function getOrderIncrementIdByTxid($sTxid) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$sQuery = " SELECT |
99
|
|
|
order_id |
100
|
|
|
FROM |
101
|
|
|
".$this->databaseResource->getTableName('payone_protocol_api')." |
102
|
|
|
WHERE |
103
|
|
|
txid = :txid |
104
|
|
|
LIMIT 1"; |
105
|
|
|
$sIncrementId = $this->getDb()->fetchOne($sQuery, ['txid' => $sTxid]); |
106
|
|
|
return $sIncrementId; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get module info from db |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getModuleInfo() |
115
|
|
|
{ |
116
|
|
|
$sQuery = " SELECT |
117
|
|
|
module, schema_version |
118
|
|
|
FROM |
119
|
|
|
".$this->databaseResource->getTableName('setup_module')." |
120
|
|
|
WHERE |
121
|
|
|
module NOT LIKE 'Magento_%'"; |
122
|
|
|
$aResult = $this->getDb()->fetchAll($sQuery); |
123
|
|
|
return $aResult; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get increment_id from order by given order id |
128
|
|
|
* |
129
|
|
|
* @param string $sOrderId |
130
|
|
|
* @return string|bool |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function getIncrementIdByOrderId($sOrderId) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$sQuery = " SELECT |
135
|
|
|
increment_id |
136
|
|
|
FROM |
137
|
|
|
".$this->databaseResource->getTableName('sales_order')." |
138
|
|
|
WHERE |
139
|
|
|
entity_id = :entity_id"; |
140
|
|
|
$sIncrementId = $this->getDb()->fetchOne($sQuery, ['entity_id' => $sOrderId]); |
141
|
|
|
return $sIncrementId; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get payone user id by given customer nr |
146
|
|
|
* |
147
|
|
|
* @param string $sCustNr |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function getPayoneUserIdByCustNr($sCustNr) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$sQuery = " SELECT |
153
|
|
|
userid |
154
|
|
|
FROM |
155
|
|
|
".$this->databaseResource->getTableName('payone_protocol_transactionstatus')." |
156
|
|
|
WHERE |
157
|
|
|
customerid = :customerid |
158
|
|
|
LIMIT 1"; |
159
|
|
|
$sUserId = $this->getDb()->fetchOne($sQuery, ['customerid' => $sCustNr]); |
160
|
|
|
return $sUserId; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the next sequencenumber for this transaction |
165
|
|
|
* |
166
|
|
|
* @param int $iTxid |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
|
|
public function getSequenceNumber($iTxid) |
170
|
|
|
{ |
171
|
|
|
$sQuery = " SELECT |
172
|
|
|
MAX(sequencenumber) |
173
|
|
|
FROM |
174
|
|
|
".$this->databaseResource->getTableName('payone_protocol_transactionstatus')." |
175
|
|
|
WHERE |
176
|
|
|
txid = :txid"; |
177
|
|
|
$iCount = $this->getDb()->fetchOne($sQuery, ['txid' => $iTxid]); |
178
|
|
|
if ($iCount === null) { |
179
|
|
|
return 0; |
180
|
|
|
} |
181
|
|
|
return $iCount + 1; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Helper method to get parameter from the config directly from db without cache |
186
|
|
|
* |
187
|
|
|
* @param string $sKey |
188
|
|
|
* @param string $sGroup |
189
|
|
|
* @param string $sSection |
190
|
|
|
* @param string $sScopeId |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getConfigParamWithoutCache($sKey, $sGroup = 'global', $sSection = 'payone_general', $sScopeId = null) |
194
|
|
|
{ |
195
|
|
|
$sQuery = " SELECT |
196
|
|
|
value |
197
|
|
|
FROM |
198
|
|
|
".$this->databaseResource->getTableName('core_config_data')." |
199
|
|
|
WHERE |
200
|
|
|
path = :path AND |
201
|
|
|
scope = :scope AND |
202
|
|
|
scope_id = :scope_id"; |
203
|
|
|
$sPath = $sSection."/".$sGroup."/".$sKey; |
204
|
|
|
$sScope = ScopeInterface::SCOPE_STORE; |
205
|
|
|
if (!$sScopeId) { |
|
|
|
|
206
|
|
|
$sScopeId = $this->storeManager->getStore()->getId(); |
207
|
|
|
} |
208
|
|
|
$sReturn = $this->getDb()->fetchOne($sQuery, ['path' => $sPath, 'scope' => $sScope, 'scope_id' => $sScopeId]); |
209
|
|
|
return $sReturn; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the address status from a previous order address |
214
|
|
|
* |
215
|
|
|
* @param AddressInterface $oAddress |
216
|
|
|
* @param bool $blIsCreditrating |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getOldAddressStatus(AddressInterface $oAddress, $blIsCreditrating = true) |
220
|
|
|
{ |
221
|
|
|
$sSelectField = 'payone_protect_score'; |
222
|
|
|
if ($blIsCreditrating === false) { |
223
|
|
|
$sSelectField = 'payone_addresscheck_score'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$aParams = [ |
227
|
|
|
'firstname' => $oAddress->getFirstname(), |
228
|
|
|
'lastname' => $oAddress->getLastname(), |
229
|
|
|
'street' => $oAddress->getStreet()[0], |
230
|
|
|
'city' => $oAddress->getCity(), |
231
|
|
|
'region' => $oAddress->getRegion(), |
232
|
|
|
'postcode' => $oAddress->getPostcode(), |
233
|
|
|
'country_id' => $oAddress->getCountryId(), |
234
|
|
|
]; |
235
|
|
|
$sQuery = " SELECT |
236
|
|
|
{$sSelectField} |
237
|
|
|
FROM |
238
|
|
|
{$this->databaseResource->getTableName('quote_address')} |
239
|
|
|
WHERE |
240
|
|
|
firstname = :firstname AND |
241
|
|
|
lastname = :lastname AND |
242
|
|
|
street = :street AND |
243
|
|
|
city = :city AND |
244
|
|
|
region = :region AND |
245
|
|
|
postcode = :postcode AND |
246
|
|
|
country_id = :country_id"; |
247
|
|
|
if (!empty($oAddress->getId())) { |
248
|
|
|
$sQuery .= " AND address_id != :curr_id"; |
249
|
|
|
$aParams['curr_id'] = $oAddress->getId(); |
250
|
|
|
} |
251
|
|
|
if (!empty($oAddress->getCustomerId())) { |
252
|
|
|
$sQuery .= " AND customer_id = :cust_id"; |
253
|
|
|
$aParams['cust_id'] = $oAddress->getCustomerId(); |
254
|
|
|
} |
255
|
|
|
if (!empty($oAddress->getAddressType())) { |
256
|
|
|
$sQuery .= " AND address_type = :addr_type"; |
257
|
|
|
$aParams['addr_type'] = $oAddress->getAddressType(); |
258
|
|
|
} |
259
|
|
|
if ($blIsCreditrating === true) { |
260
|
|
|
$sQuery .= " AND payone_protect_score != ''"; |
261
|
|
|
} else { |
262
|
|
|
$sQuery .= " AND payone_addresscheck_score != ''"; |
263
|
|
|
} |
264
|
|
|
$sQuery .= " ORDER BY address_id DESC LIMIT 1"; |
265
|
|
|
|
266
|
|
|
$sReturn = $this->getDb()->fetchOne($sQuery, $aParams); |
267
|
|
|
return $sReturn; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.