MethodList::getQuote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Model\Plugins;
28
29
use Magento\Payment\Model\MethodList as OrigMethodList;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\MethodList 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\Payment\Model\MethodInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\MethodInterface 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\Quote\Api\Data\AddressInterface;
0 ignored issues
show
Bug introduced by
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...
32
use Payone\Core\Model\PayoneConfig;
33
use Payone\Core\Model\Source\CreditratingIntegrationEvent as Event;
34
use Magento\Quote\Model\Quote;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote 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...
35
use Payone\Core\Model\Source\PersonStatus;
36
37
/**
38
 * Plugin for Magentos MethodList class
39
 */
40
class MethodList
41
{
42
    /**
43
     * PAYONE consumerscore request model
44
     *
45
     * @var \Payone\Core\Model\Api\Request\Consumerscore
46
     */
47
    protected $consumerscore;
48
49
    /**
50
     * Consumerscore helper
51
     *
52
     * @var \Payone\Core\Helper\Consumerscore
53
     */
54
    protected $consumerscoreHelper;
55
56
    /**
57
     * Checkout session
58
     *
59
     * @var \Magento\Checkout\Model\Session
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Session 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...
60
     */
61
    protected $checkoutSession;
62
63
    /**
64
     * Payment ban entity
65
     *
66
     * @var \Payone\Core\Model\ResourceModel\PaymentBan
67
     */
68
    protected $paymentBan;
69
70
    /**
71
     * Addresscheck management object
72
     *
73
     * @var \Payone\Core\Model\Risk\Addresscheck
74
     */
75
    protected $addresscheck;
76
77
    /**
78
     * Payone customer helper
79
     *
80
     * @var \Payone\Core\Helper\Customer
81
     */
82
    protected $customerHelper;
83
84
    /**
85
     * Constructor
86
     *
87
     * @param \Payone\Core\Model\Api\Request\Consumerscore $consumerscore
88
     * @param \Payone\Core\Helper\Consumerscore            $consumerscoreHelper
89
     * @param \Magento\Checkout\Model\Session              $checkoutSession
90
     * @param \Payone\Core\Model\ResourceModel\PaymentBan  $paymentBan
91
     * @param \Payone\Core\Model\Risk\Addresscheck         $addresscheck
92
     * @param \Payone\Core\Helper\Customer                 $customerHelper
93
     */
94
    public function __construct(
95
        \Payone\Core\Model\Api\Request\Consumerscore $consumerscore,
96
        \Payone\Core\Helper\Consumerscore $consumerscoreHelper,
97
        \Magento\Checkout\Model\Session $checkoutSession,
98
        \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan,
99
        \Payone\Core\Model\Risk\Addresscheck $addresscheck,
100
        \Payone\Core\Helper\Customer $customerHelper
101
    ) {
102
        $this->consumerscore = $consumerscore;
103
        $this->consumerscoreHelper = $consumerscoreHelper;
104
        $this->checkoutSession = $checkoutSession;
105
        $this->paymentBan = $paymentBan;
106
        $this->addresscheck = $addresscheck;
107
        $this->customerHelper = $customerHelper;
108
    }
109
110
    /**
111
     * Filter methods by the worst score
112
     *
113
     * @param  MethodInterface[] $aPaymentMethods
114
     * @param  string            $sWorstScore
115
     * @return MethodInterface[]
116
     */
117
    protected function filterMethodsByScore($aPaymentMethods, $sWorstScore)
118
    {
119
        $aRedMethods = $this->consumerscoreHelper->getAllowedMethodsForScore('R');
120
        $aYellowMethods = array_merge($aRedMethods, $this->consumerscoreHelper->getAllowedMethodsForScore('Y'));
121
122
        $aReturnMethods = [];
123
        foreach ($aPaymentMethods as $oMethod) {
124
            if ($sWorstScore == 'Y' && array_search($oMethod->getCode(), $aYellowMethods) !== false) {
125
                $aReturnMethods[] = $oMethod;
126
            }
127
128
            if ($sWorstScore == 'R' && array_search($oMethod->getCode(), $aRedMethods) !== false) {
129
                $aReturnMethods[] = $oMethod;
130
            }
131
        }
132
        return $aReturnMethods;
133
    }
134
135
    /**
136
     * Execute a consumerscore request to PAYONE or load an old score if its lifetime is still active
137
     *
138
     * @param  AddressInterface $oShipping
139
     * @return string
140
     */
141
    protected function getScoreByCreditrating(AddressInterface $oShipping)
142
    {
143
        $aResponse = $this->consumerscore->sendRequest($oShipping, $this->customerHelper->getCustomerGender(), $this->customerHelper->getCustomerBirthday());
144
        if ($aResponse === true) {// creditrating not executed because of a previous check
145
            $this->consumerscoreHelper->copyOldStatusToNewAddress($oShipping);
146
        }
147
148
        if (isset($aResponse['score'])) {
149
            $oShipping->setPayoneProtectScore($aResponse['score'])->save();
150
        }
151
152
        $sScore = $oShipping->getPayoneProtectScore();
153
        if (isset($aResponse['personstatus']) && $aResponse['personstatus'] !== PersonStatus::NONE) {
154
            $aMapping = $this->addresscheck->getPersonstatusMapping();
155
            if (array_key_exists($aResponse['personstatus'], $aMapping)) {
156
                $sScore = $this->consumerscoreHelper->getWorstScore([$sScore, $aMapping[$aResponse['personstatus']]]);
157
            }
158
        }
159
        return $sScore;
160
    }
161
162
    /**
163
     * Get parameter from config
164
     *
165
     * @param  string $sParam
166
     * @param  bool   $blIsAddresscheck
167
     * @return string
168
     */
169
    protected function getConfigParam($sParam, $blIsAddresscheck = false)
170
    {
171
        $sGroup = 'creditrating';
172
        if ($blIsAddresscheck === true) {
173
            $sGroup = 'address_check';
174
        }
175
        return $this->consumerscoreHelper->getConfigParam($sParam, $sGroup, 'payone_protect');
176
    }
177
178
    /**
179
     * Get quote object from session
180
     *
181
     * @return Quote
182
     */
183
    protected function getQuote()
184
    {
185
        return $this->checkoutSession->getQuote();
186
    }
187
188
    /**
189
     * Return banned payment methods for the current user
190
     *
191
     * @param  Quote $oQuote
192
     * @return array
193
     */
194
    protected function getBannedPaymentMethods(Quote $oQuote)
195
    {
196
        $aBans = [];
197
        if (!empty($oQuote->getCustomerId())) {
198
            $aBans = $this->paymentBan->getPaymentBans($oQuote->getCustomerId());
199
        }
200
201
        $aSessionBans = $this->checkoutSession->getPayonePaymentBans();
202
        if (!empty($aSessionBans)) {
203
            $aBans = array_merge($aBans, $aSessionBans);
204
        }
205
        return $aBans;
206
    }
207
208
    /**
209
     * Remove banned paymenttypes
210
     *
211
     * @param  array $aPaymentMethods
212
     * @param  Quote $oQuote
213
     * @return array
214
     */
215
    protected function removeBannedPaymentMethods($aPaymentMethods, Quote $oQuote)
216
    {
217
        $aBannedMethods = $this->getBannedPaymentMethods($oQuote);
218
        foreach ($aPaymentMethods as $key => $aPaymentMethod) {
219
            $sCode = $aPaymentMethod->getCode();
220
            if (array_key_exists($sCode, $aBannedMethods) !== false) {
221
                $iBannedUntil = strtotime($aBannedMethods[$sCode]);
222
                if ($iBannedUntil > time()) {
223
                    unset($aPaymentMethods[$key]);
224
                }
225
            }
226
        }
227
        return $aPaymentMethods;
228
    }
229
230
    /**
231
     * Remove paymenttypes not on the session whitelist
232
     *
233
     * @param  array $aPaymentMethods
234
     * @return array
235
     */
236
    protected function removeNotWhitelistedPaymentMethods($aPaymentMethods)
237
    {
238
        $aWhitelist = $this->checkoutSession->getPayonePaymentWhitelist();
239
        if (!empty($aWhitelist)) {
240
            foreach ($aPaymentMethods as $key => $aPaymentMethod) {
241
                if (!in_array($aPaymentMethod->getCode(), $aWhitelist)) {
242
                    unset($aPaymentMethods[$key]);
243
                }
244
            }
245
        }
246
        return $aPaymentMethods;
247
    }
248
249
    /**
250
     * Remove Amazon Pay from payment method array
251
     *
252
     * @param  array $aPaymentMethods
253
     * @return array
254
     */
255
    public function removeAmazonPay($aPaymentMethods)
256
    {
257
        foreach ($aPaymentMethods as $key => $aPaymentMethod) {
258
            if ($aPaymentMethod->getCode() == PayoneConfig::METHOD_AMAZONPAY ||
259
                ($aPaymentMethod->getCode() == PayoneConfig::METHOD_AMAZONPAYV2 && (bool)$this->customerHelper->getConfigParam('apb_active', PayoneConfig::METHOD_AMAZONPAYV2, 'payone_payment') === false)) {
260
                unset($aPaymentMethods[$key]);
261
            }
262
        }
263
        return $aPaymentMethods;
264
    }
265
266
    /**
267
     * Removes Klarna base methode if there are no Klarna sub-types available
268
     *
269
     * @param  array $aPaymentMethods
270
     * @return array
271
     */
272
    public function checkKlarnaMethods($aPaymentMethods)
273
    {
274
        $iKeyKlarna = false;
275
        $blHasKlarnaSubtypes = false;
276
        $aKlarnaSubtypes = [
277
            PayoneConfig::METHOD_KLARNA_INVOICE,
278
            PayoneConfig::METHOD_KLARNA_DEBIT,
279
            PayoneConfig::METHOD_KLARNA_INSTALLMENT
280
        ];
281
        foreach ($aPaymentMethods as $key => $aPaymentMethod) {
282
            if ($aPaymentMethod->getCode() == PayoneConfig::METHOD_KLARNA_BASE) {
283
                $iKeyKlarna = $key;
284
            }
285
            if (in_array($aPaymentMethod->getCode(), $aKlarnaSubtypes) === true) {
286
                $blHasKlarnaSubtypes = true;
287
                break;
288
            }
289
        }
290
        if ($iKeyKlarna !== false && $blHasKlarnaSubtypes === false) {
291
            unset($aPaymentMethods[$iKeyKlarna]);
292
        }
293
        return $aPaymentMethods;
294
    }
295
296
    /**
297
     *
298
     * @param  OrigMethodList                        $subject
299
     * @param  MethodInterface[]                     $aPaymentMethods
300
     * @param  \Magento\Quote\Api\Data\CartInterface $quote
301
     * @return MethodInterface[]
302
     */
303
    public function afterGetAvailableMethods(OrigMethodList $subject, $aPaymentMethods, \Magento\Quote\Api\Data\CartInterface $quote)
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

303
    public function afterGetAvailableMethods(/** @scrutinizer ignore-unused */ OrigMethodList $subject, $aPaymentMethods, \Magento\Quote\Api\Data\CartInterface $quote)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $quote is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

303
    public function afterGetAvailableMethods(OrigMethodList $subject, $aPaymentMethods, /** @scrutinizer ignore-unused */ \Magento\Quote\Api\Data\CartInterface $quote)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Bug introduced by
The type Magento\Quote\Api\Data\CartInterface 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...
304
    {
305
        $oQuote = $this->getQuote();
306
        $oShipping = $oQuote->getShippingAddress();
307
308
        $aScores = [];
309
        if ($this->getConfigParam('enabled', true)) {// is addresscheck active
310
            $aScores[] = $oShipping->getPayoneAddresscheckScore();
311
        }
312
313
        $dTotal = $oQuote->getGrandTotal();
314
        if ($this->consumerscoreHelper->isCreditratingNeeded(Event::BEFORE_PAYMENT, $dTotal) === true) {
315
            $aScores[] = $this->getScoreByCreditrating($oShipping);
316
        }
317
318
        $sScore = $this->consumerscoreHelper->getWorstScore($aScores);
319
        if ($sScore != 'G') { // no need to filter
320
            $aPaymentMethods = $this->filterMethodsByScore($aPaymentMethods, $sScore);
321
        }
322
323
        $aPaymentMethods = $this->removeBannedPaymentMethods($aPaymentMethods, $oQuote);
324
        $aPaymentMethods = $this->removeNotWhitelistedPaymentMethods($aPaymentMethods);
325
        $aPaymentMethods = $this->removeAmazonPay($aPaymentMethods);
326
        $aPaymentMethods = $this->checkKlarnaMethods($aPaymentMethods);
327
328
        return $aPaymentMethods;
329
    }
330
}
331