Issues (1092)

Helper/Database.php (7 issues)

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;
0 ignored issues
show
The type Magento\Framework\DB\Adapter\AdapterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Magento\Quote\Api\Data\AddressInterface;
0 ignored issues
show
The type Magento\Quote\Api\Data\AddressInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Magento\Store\Model\ScopeInterface;
0 ignored issues
show
The type Magento\Store\Model\ScopeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use Payone\Core\Model\PayoneConfig;
33
34
/**
35
 * Helper class for everything that has to do with database connections
36
 */
37
class Database extends \Payone\Core\Helper\Base
38
{
39
    /**
40
     * Database connection resource
41
     *
42
     * @var \Magento\Framework\App\ResourceConnection
0 ignored issues
show
The type Magento\Framework\App\ResourceConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    protected $databaseResource;
45
46
    /**
47
     * Object of CheckedAddresses resource
48
     *
49
     * @var \Payone\Core\Model\ResourceModel\CheckedAddresses
50
     */
51
    protected $addressesChecked;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param \Magento\Framework\App\Helper\Context             $context
57
     * @param \Magento\Store\Model\StoreManagerInterface        $storeManager
58
     * @param \Payone\Core\Helper\Shop                          $shopHelper
59
     * @param \Magento\Framework\App\State                      $state
60
     * @param \Magento\Framework\App\ResourceConnection         $resource
61
     * @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
62
     */
63
    public function __construct(
64
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
The type Magento\Framework\App\Helper\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
The type Magento\Store\Model\StoreManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
        \Payone\Core\Helper\Shop $shopHelper,
67
        \Magento\Framework\App\State $state,
0 ignored issues
show
The type Magento\Framework\App\State was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
        \Magento\Framework\App\ResourceConnection $resource,
69
        \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
70
    ) {
71
        parent::__construct($context, $storeManager, $shopHelper, $state);
72
        $this->databaseResource = $resource;
73
        $this->addressesChecked = $addressesChecked;
74
    }
75
76
    /**
77
     * Return database connection
78
     *
79
     * @return AdapterInterface
80
     */
81
    protected function getDb()
82
    {
83
        return $this->databaseResource->getConnection();
84
    }
85
86
    /**
87
     * Get the state for a given status from the database
88
     *
89
     * @param  string $sStatus
90
     * @return string
91
     */
92
    public function getStateByStatus($sStatus)
93
    {
94
        $oSelect = $this->getDb()
95
            ->select()
96
            ->from($this->databaseResource->getTableName('sales_order_status_state'), ['state'])
97
            ->where("status = :status")
98
            ->limit(1);
99
        return $this->getDb()->fetchOne($oSelect, ['status' => $sStatus]);
100
    }
101
102
    /**
103
     * Get the order increment id by the given TransactionStatus txid
104
     *
105
     * @param  string $sTxid
106
     * @return string
107
     */
108
    public function getOrderIncrementIdByTxid($sTxid)
109
    {
110
        $oSelect = $this->getDb()
111
            ->select()
112
            ->from($this->databaseResource->getTableName('payone_protocol_api'), ['order_id'])
113
            ->where("txid = :txid")
114
            ->limit(1);
115
        return $this->getDb()->fetchOne($oSelect, ['txid' => $sTxid]);
116
    }
117
118
    /**
119
     * Get module info from db
120
     *
121
     * @return array
122
     */
123
    public function getModuleInfo()
124
    {
125
        $oSelect = $this->getDb()
126
            ->select()
127
            ->from($this->databaseResource->getTableName('setup_module'), ['module', 'schema_version'])
128
            ->where("module NOT LIKE 'Magento_%'");
129
        return $this->getDb()->fetchAll($oSelect);
130
    }
131
132
    /**
133
     * Get increment_id from order by given order id
134
     *
135
     * @param  string $sOrderId
136
     * @return string|bool
137
     */
138
    public function getIncrementIdByOrderId($sOrderId)
139
    {
140
        $oSelect = $this->getDb()
141
            ->select()
142
            ->from($this->databaseResource->getTableName('sales_order'), ['increment_id'])
143
            ->where("entity_id = :entity_id")
144
            ->limit(1);
145
        return $this->getDb()->fetchOne($oSelect, ['entity_id' => $sOrderId]);
146
    }
147
148
    /**
149
     * Get payone user id by given customer nr
150
     *
151
     * @param  string $sCustNr
152
     * @return string
153
     */
154
    public function getPayoneUserIdByCustNr($sCustNr)
155
    {
156
        $oSelect = $this->getDb()
157
            ->select()
158
            ->from($this->databaseResource->getTableName('payone_protocol_transactionstatus'), ['userid'])
159
            ->where("customerid = :customerid")
160
            ->limit(1);
161
        return $this->getDb()->fetchOne($oSelect, ['customerid' => $sCustNr]);
162
    }
163
164
    /**
165
     * Get the next sequencenumber for this transaction
166
     *
167
     * @param  int $iTxid
168
     * @return int
169
     */
170
    public function getSequenceNumber($iTxid)
171
    {
172
        $oSelect = $this->getDb()
173
            ->select()
174
            ->from($this->databaseResource->getTableName('payone_protocol_transactionstatus'), ['MAX(sequencenumber)'])
175
            ->where("txid = :txid");
176
        $iCount = $this->getDb()->fetchOne($oSelect, ['txid' => $iTxid]);
177
        if ($iCount === null) {
178
            return 0;
179
        }
180
        return $iCount + 1;
181
    }
182
183
    /**
184
     * Helper method to get parameter from the config directly from db without cache
185
     *
186
     * @param  string $sKey
187
     * @param  string $sGroup
188
     * @param  string $sSection
189
     * @param  string $sScopeId
190
     * @return string
191
     */
192
    public function getConfigParamWithoutCache($sKey, $sGroup = 'global', $sSection = 'payone_general', $sScopeId = null)
193
    {
194
        $oSelect = $this->getDb()
195
            ->select()
196
            ->from($this->databaseResource->getTableName('core_config_data'), ['value'])
197
            ->where("path = :path")
198
            ->where("scope = :scope")
199
            ->where("scope_id = :scope_id");
200
        $sPath = $sSection."/".$sGroup."/".$sKey;
201
        $sScope = ScopeInterface::SCOPE_STORES;
202
        if (!$sScopeId) {
203
            $sScopeId = $this->storeManager->getStore()->getId();
204
        }
205
        return $this->getDb()->fetchOne($oSelect, ['path' => $sPath, 'scope' => $sScope, 'scope_id' => $sScopeId]);
206
    }
207
208
    /**
209
     * Get the address status from a previous order address
210
     *
211
     * @param  AddressInterface $oAddress
212
     * @param  bool             $blIsCreditrating
213
     * @return string
214
     */
215
    protected function getStatusFromPreviousQuoteAddress(AddressInterface $oAddress, $blIsCreditrating = true)
216
    {
217
        $sSelectField = 'payone_protect_score';
218
        if ($blIsCreditrating === false) {
219
            $sSelectField = 'payone_addresscheck_score';
220
        }
221
222
        $aParams = [
223
            'firstname' => $oAddress->getFirstname(),
224
            'lastname' => $oAddress->getLastname(),
225
            'street' => $oAddress->getStreet()[0],
226
            'city' => $oAddress->getCity(),
227
            'region' => $oAddress->getRegion(),
228
            'postcode' => $oAddress->getPostcode(),
229
            'country_id' => $oAddress->getCountryId(),
230
        ];
231
232
        $oSelect = $this->getDb()
233
            ->select()
234
            ->from($this->databaseResource->getTableName('quote_address'), [$sSelectField])
235
            ->where("firstname = :firstname")
236
            ->where("lastname = :lastname")
237
            ->where("street = :street")
238
            ->where("city = :city")
239
            ->where("region = :region")
240
            ->where("postcode = :postcode")
241
            ->where("country_id = :country_id")
242
            ->where($sSelectField." != ''")
243
            ->order('address_id DESC')
244
            ->limit(1);
245
246
        if (!empty($oAddress->getId())) {
247
            $oSelect->where("address_id != :curr_id");
248
            $aParams['curr_id'] = $oAddress->getId();
249
        }
250
        if (!empty($oAddress->getCustomerId())) {
251
            $oSelect->where("customer_id = :cust_id");
252
            $aParams['cust_id'] = $oAddress->getCustomerId();
253
        }
254
        if (!empty($oAddress->getAddressType())) {
255
            $oSelect->where("address_type = :addr_type");
256
            $aParams['addr_type'] = $oAddress->getAddressType();
257
        }
258
        return $this->getDb()->fetchOne($oSelect, $aParams);
259
    }
260
261
    /**
262
     * Returns last check score for given address
263
     *
264
     * @param  AddressInterface $oAddress
265
     * @return string
266
     * @throws \Magento\Framework\Exception\LocalizedException
267
     */
268
    protected function getScoreFromCheckedAddresses(AddressInterface $oAddress)
269
    {
270
        return $this->addressesChecked->getLatestScoreForAddress($oAddress, true);
271
    }
272
273
    /**
274
     * Get the address status from a previous order address
275
     *
276
     * @param  AddressInterface $oAddress
277
     * @param  bool             $blIsCreditrating
278
     * @return string
279
     */
280
    public function getOldAddressStatus(AddressInterface $oAddress, $blIsCreditrating = true)
281
    {
282
        $sStatus = $this->getStatusFromPreviousQuoteAddress($oAddress, $blIsCreditrating);
283
        if(empty($sStatus) && $blIsCreditrating === true && $this->getConfigParam('integration_event', 'creditrating', 'payone_protect') == 'after_payment') {
284
            $sStatus = $this->getScoreFromCheckedAddresses($oAddress);
285
        }
286
        return $sStatus;
287
    }
288
289
    /**
290
     * Relabel sales payment transaction to substitute order
291
     *
292
     * @param  string $sOldOrderId
293
     * @param  string $sNewOrderId
294
     * @param  string $sNewPaymentId
295
     * @return int
296
     */
297
    public function relabelTransaction($sOldOrderId, $sNewOrderId, $sNewPaymentId)
298
    {
299
        $table = $this->databaseResource->getTableName('sales_payment_transaction');
300
        $data = [
301
            'order_id' => $sNewOrderId,
302
            'payment_id' => $sNewPaymentId
303
        ];
304
        $where = ['order_id = ?' => $sOldOrderId];
305
        return $this->getDb()->update($table, $data, $where);
306
    }
307
308
    /**
309
     * Relabel payone protocol api to substitute order
310
     *
311
     * @param  string $sOldIncrementId
312
     * @param  string $sNewIncrementId
313
     * @return int
314
     */
315
    public function relabelApiProtocol($sOldIncrementId, $sNewIncrementId)
316
    {
317
        $table = $this->databaseResource->getTableName('payone_protocol_api');
318
        $data = ['order_id' => $sNewIncrementId];
319
        $where = ['order_id = ?' => $sOldIncrementId];
320
        return $this->getDb()->update($table, $data, $where);
321
    }
322
323
    /**
324
     * Relabel sales order payment to substitute order
325
     *
326
     * @param  string $sOldIncrementId
327
     * @param  string $sNewOrderId
328
     * @return int
329
     */
330
    public function relabelOrderPayment($sOldIncrementId, $sNewOrderId)
331
    {
332
        $oSelect = $this->getDb()
333
            ->select()
334
            ->from(['a' => $this->databaseResource->getTableName('sales_order_payment')], ['last_trans_id'])
335
            ->joinInner(['b' => $this->databaseResource->getTableName('sales_order')], 'a.parent_id = b.entity_id')
336
            ->where("b.increment_id = :incrementId")
337
            ->limit(1);
338
        $sLastTransId = $this->getDb()->fetchOne($oSelect, ['incrementId' => $sOldIncrementId]);
339
340
        $table = $this->databaseResource->getTableName('sales_order_payment');
341
        $data = ['last_trans_id' => $sLastTransId];
342
        $where = ['parent_id = ?' => $sNewOrderId];
343
        return $this->getDb()->update($table, $data, $where);
344
    }
345
346
    /**
347
     * Return not handled transactions by order id
348
     *
349
     * @param  string $sOrderId
350
     * @return array
351
     */
352
    public function getNotHandledTransactionsByOrderId($sOrderId)
353
    {
354
        $oSelect = $this->getDb()
355
            ->select()
356
            ->from($this->databaseResource->getTableName('payone_protocol_transactionstatus'), ['id'])
357
            ->where("order_id = :orderId")
358
            ->where("has_been_handled = 0")
359
            ->order('id ASC');
360
        return $this->getDb()->fetchAll($oSelect, ['orderId' => $sOrderId]);
361
    }
362
363
    /**
364
     * Will search the DB for the incrementId of a substitute order by the given incrementId of the canceled order
365
     *
366
     * @param  string $sIncrementId
367
     * @return string
368
     */
369
    public function getSubstituteOrderIncrementId($sIncrementId)
370
    {
371
        $oSelect = $this->getDb()
372
            ->select()
373
            ->from($this->databaseResource->getTableName('sales_order'), ['increment_id'])
374
            ->where("payone_cancel_substitute_increment_id = :incrementId");
375
        return $this->getDb()->fetchOne($oSelect, ['incrementId' => $sIncrementId]);
376
    }
377
}
378