Issues (1092)

Model/Methods/Ratepay/RatepayBase.php (16 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 - 2020 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\Methods\Ratepay;
28
29
use Payone\Core\Model\Methods\Payolution\PayolutionBase;
30
use Payone\Core\Model\PayoneConfig;
31
use Payone\Core\Model\Methods\PayoneMethod;
32
use Magento\Sales\Model\Order;
0 ignored issues
show
The type Magento\Sales\Model\Order 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...
33
use Magento\Framework\DataObject;
0 ignored issues
show
The type Magento\Framework\DataObject 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...
34
35
/**
36
 * Base class for all Ratepay methods
37
 */
38
class RatepayBase extends PayoneMethod
39
{
40
    /* Payment method sub types */
41
    const METHOD_RATEPAY_SUBTYPE_INVOICE = 'RPV';
42
    const METHOD_RATEPAY_SUBTYPE_INSTALLMENT = 'RPS';
43
    const METHOD_RATEPAY_SUBTYPE_DEBIT = 'RPD';
44
45
    /**
46
     * Payone Ratepay helper
47
     *
48
     * @var \Payone\Core\Helper\Ratepay
49
     */
50
    protected $ratepayHelper;
51
52
    /**
53
     * Payone Api helper
54
     *
55
     * @var \Payone\Core\Helper\Api
56
     */
57
    protected $apiHelper;
58
59
    /**
60
     * Payment ban resource model
61
     *
62
     * @var \Payone\Core\Model\ResourceModel\PaymentBan
63
     */
64
    protected $paymentBan;
65
66
    /**
67
     * Clearingtype for PAYONE authorization request
68
     *
69
     * @var string
70
     */
71
    protected $sClearingtype = 'fnc';
72
73
    /**
74
     * Payment method group identifier
75
     *
76
     * @var string
77
     */
78
    protected $sGroupName = PayoneConfig::METHOD_GROUP_RATEPAY;
79
80
    /**
81
     * Info instructions block path
82
     *
83
     * @var string
84
     */
85
    protected $_infoBlockType = 'Payone\Core\Block\Info\ClearingReference';
86
87
    /**
88
     * Keys that need to be assigned to the additionalinformation fields
89
     *
90
     * @var array
91
     */
92
    protected $aAssignKeys = [
93
        'telephone',
94
        'dateofbirth',
95
        'birthday',
96
        'birthmonth',
97
        'birthyear',
98
        'iban',
99
        'bic',
100
        'company_uid',
101
    ];
102
103
    /**
104
     * Payment ban duration in hours
105
     *
106
     * @var int
107
     */
108
    protected $iBanDuration = 48;
109
110
    /**
111
     * Constructor
112
     *
113
     * @param \Magento\Framework\Model\Context                        $context
114
     * @param \Magento\Framework\Registry                             $registry
115
     * @param \Magento\Framework\Api\ExtensionAttributesFactory       $extensionFactory
116
     * @param \Magento\Framework\Api\AttributeValueFactory            $customAttrFactory
117
     * @param \Magento\Payment\Helper\Data                            $paymentData
118
     * @param \Magento\Framework\App\Config\ScopeConfigInterface      $scopeConfig
119
     * @param \Magento\Payment\Model\Method\Logger                    $logger
120
     * @param \Payone\Core\Helper\Toolkit                             $toolkitHelper
121
     * @param \Payone\Core\Helper\Shop                                $shopHelper
122
     * @param \Magento\Framework\Url                                  $url
123
     * @param \Magento\Checkout\Model\Session                         $checkoutSession
124
     * @param \Payone\Core\Model\Api\Request\Debit                    $debitRequest
125
     * @param \Payone\Core\Model\Api\Request\Capture                  $captureRequest
126
     * @param \Payone\Core\Model\Api\Request\Authorization            $authorizationRequest
127
     * @param \Payone\Core\Model\ResourceModel\SavedPaymentData       $savedPaymentData
128
     * @param \Payone\Core\Helper\Ratepay                             $ratepayHelper
129
     * @param \Payone\Core\Helper\Api                                 $apiHelper
130
     * @param \Payone\Core\Model\ResourceModel\PaymentBan             $paymentBan
131
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
132
     * @param \Magento\Framework\Data\Collection\AbstractDb           $resourceCollection
133
     * @param array                                                   $data
134
     */
135
    public function __construct(
136
        \Magento\Framework\Model\Context $context,
0 ignored issues
show
The type Magento\Framework\Model\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...
137
        \Magento\Framework\Registry $registry,
0 ignored issues
show
The type Magento\Framework\Registry 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...
138
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
0 ignored issues
show
The type Magento\Framework\Api\ExtensionAttributesFactory 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...
139
        \Magento\Framework\Api\AttributeValueFactory $customAttrFactory,
0 ignored issues
show
The type Magento\Framework\Api\AttributeValueFactory 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...
140
        \Magento\Payment\Helper\Data $paymentData,
0 ignored issues
show
The type Magento\Payment\Helper\Data 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...
141
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
0 ignored issues
show
The type Magento\Framework\App\Config\ScopeConfigInterface 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...
142
        \Magento\Payment\Model\Method\Logger $logger,
0 ignored issues
show
The type Magento\Payment\Model\Method\Logger 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...
143
        \Payone\Core\Helper\Toolkit $toolkitHelper,
144
        \Payone\Core\Helper\Shop $shopHelper,
145
        \Magento\Framework\Url $url,
0 ignored issues
show
The type Magento\Framework\Url 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...
146
        \Magento\Checkout\Model\Session $checkoutSession,
0 ignored issues
show
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...
147
        \Payone\Core\Model\Api\Request\Debit $debitRequest,
148
        \Payone\Core\Model\Api\Request\Capture $captureRequest,
149
        \Payone\Core\Model\Api\Request\Authorization $authorizationRequest,
150
        \Payone\Core\Model\ResourceModel\SavedPaymentData $savedPaymentData,
151
        \Payone\Core\Helper\Ratepay $ratepayHelper,
152
        \Payone\Core\Helper\Api $apiHelper,
153
        \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan,
154
        ?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
0 ignored issues
show
The type Magento\Framework\Model\...eModel\AbstractResource 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...
155
        ?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
0 ignored issues
show
The type Magento\Framework\Data\Collection\AbstractDb 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...
156
        array $data = []
157
    ) {
158
        parent::__construct($context, $registry, $extensionFactory, $customAttrFactory, $paymentData, $scopeConfig, $logger, $toolkitHelper, $shopHelper, $url, $checkoutSession, $debitRequest, $captureRequest, $authorizationRequest, $savedPaymentData, $resource, $resourceCollection, $data);
159
        $this->ratepayHelper = $ratepayHelper;
160
        $this->apiHelper = $apiHelper;
161
        $this->paymentBan = $paymentBan;
162
    }
163
164
    /**
165
     * Add the checkout-form-data to the checkout session
166
     *
167
     * @param  DataObject $data
168
     * @return $this
169
     */
170
    public function assignData(DataObject $data)
171
    {
172
        parent::assignData($data);
173
174
        $oInfoInstance = $this->getInfoInstance();
175
        foreach ($this->aAssignKeys as $sKey) {
176
            $sData = $this->toolkitHelper->getAdditionalDataEntry($data, $sKey);
177
            if ($sData) {
178
                $oInfoInstance->setAdditionalInformation($sKey, $sData);
179
            }
180
        }
181
182
        return $this;
183
    }
184
185
    /**
186
     * Return parameters specific to this payment type
187
     *
188
     * @param  Order $oOrder
189
     * @return array
190
     */
191
    public function getPaymentSpecificParameters(Order $oOrder)
192
    {
193
        $aBaseParams = [
194
            'financingtype' => $this->getSubType(),
195
            'api_version' => '3.10',
196
            'add_paydata[customer_allow_credit_inquiry]' => 'yes',
197
            'add_paydata[device_token]' => $this->ratepayHelper->getRatepayDeviceFingerprintToken(),
198
            'add_paydata[merchant_consumer_id]' => $oOrder->getCustomerId(),
199
            'add_paydata[shop_id]' => $this->getShopIdByOrder($oOrder),
200
        ];
201
202
        $sCompanyUid = $this->getInfoInstance()->getAdditionalInformation('company_uid');
203
        if (!empty($sCompanyUid)) {
204
            $aBaseParams['add_paydata[vat_id]'] = $sCompanyUid;
205
        }
206
207
        $sBirthday = $this->getInfoInstance()->getAdditionalInformation('dateofbirth');
208
        if ($sBirthday) {
209
            $aBaseParams['birthday'] = $sBirthday;
210
        } elseif (!empty($this->getInfoInstance()->getAdditionalInformation('birthday'))) {
211
            $sDayOfBirth = $this->getInfoInstance()->getAdditionalInformation('birthday');
212
            $sMonthOfBirth = $this->getInfoInstance()->getAdditionalInformation('birthmonth');
213
            $sYearOfBirth = $this->getInfoInstance()->getAdditionalInformation('birthyear');
214
            $aBaseParams['birthday'] = $sYearOfBirth.$sMonthOfBirth.$sDayOfBirth;
215
        }
216
217
        $sTelephone = $this->getInfoInstance()->getAdditionalInformation('telephone');
218
        if ($sTelephone) {
219
            $aBaseParams['telephonenumber'] = $sTelephone;
220
        }
221
222
        $aSubTypeParams = $this->getSubTypeSpecificParameters($oOrder);
223
        $aParams = array_merge($aBaseParams, $aSubTypeParams);
224
        return $aParams;
225
    }
226
227
    /**
228
     * Returns matching Ratepay shop id by given quote
229
     *
230
     * @param Order $oOrder
231
     * @return string
232
     * @throws \Magento\Framework\Exception\LocalizedException
233
     */
234
    protected function getShopIdByOrder(Order $oOrder)
235
    {
236
        $sCountryCode = $oOrder->getShippingAddress()->getCountryId();
237
        $sCurrency = $this->apiHelper->getCurrencyFromOrder($oOrder);
238
        $dGrandTotal = $this->apiHelper->getOrderAmount($oOrder);
239
240
        return $this->ratepayHelper->getRatepayShopId($this->getCode(), $sCountryCode, $sCurrency, $dGrandTotal);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ratepayHel...Currency, $dGrandTotal) could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
241
    }
242
243
    /**
244
     * Check whether payment method can be used
245
     *
246
     * @param \Magento\Quote\Api\Data\CartInterface|null $quote
247
     * @return bool
248
     */
249
    public function isAvailable(?\Magento\Quote\Api\Data\CartInterface $quote = null)
0 ignored issues
show
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...
250
    {
251
        $blParentReturn = parent::isAvailable($quote);
252
        if ($blParentReturn === false) {
253
            return false;
254
        }
255
256
        if ($quote === null) {
257
            $quote = $this->checkoutSession->getQuote();
258
        }
259
260
        if ($quote !== null && $this->ratepayHelper->getShopIdByQuote($this->getCode(), $quote) === false) {
0 ignored issues
show
The condition $this->ratepayHelper->ge...de(), $quote) === false is always false.
Loading history...
261
            return false;
262
        }
263
264
        return $blParentReturn;
265
    }
266
267
    /**
268
     * Perform certain actions with the response
269
     *
270
     * @param  array $aResponse
271
     * @param  Order $oOrder
272
     * @param  float $amount
273
     * @return array
274
     */
275
    protected function handleResponse($aResponse, Order $oOrder, $amount)
276
    {
277
        $aResponse = parent::handleResponse($aResponse, $oOrder, $amount);
278
        $this->checkoutSession->unsPayoneRatepayDeviceFingerprintToken();
279
        if (isset($aResponse['status']) && $aResponse['status'] == 'ERROR'
280
            && isset($aResponse['errorcode']) && $aResponse['errorcode'] == '307'
281
        ) {
282
            if (!empty($oOrder->getCustomerId())) {
283
                $this->paymentBan->addPaymentBan($this->getCode(), $oOrder->getCustomerId(), $this->iBanDuration);
284
            } else { // guest checkout
285
                $aBans = $this->checkoutSession->getPayonePaymentBans();
286
                if (!$aBans) {
287
                    $aBans = [];
288
                }
289
                $aBans[$this->getCode()] = $this->paymentBan->getBanEndDate($this->iBanDuration);
290
                $this->checkoutSession->setPayonePaymentBans($aBans);
291
            }
292
        }
293
        return $aResponse;
294
    }
295
296
    /**
297
     * Return capture parameters specific to this payment type
298
     *
299
     * @param  Order $oOrder
300
     * @return array
301
     */
302
    public function getPaymentSpecificCaptureParameters(Order $oOrder)
303
    {
304
        return [
305
            'add_paydata[shop_id]' => $oOrder->getPayoneRatepayShopId()
306
        ];
307
    }
308
309
    /**
310
     * Return debit parameters specific to this payment type
311
     *
312
     * @param  Order $oOrder
313
     * @return array
314
     */
315
    public function getPaymentSpecificDebitParameters(Order $oOrder)
316
    {
317
        return [
318
            'add_paydata[shop_id]' => $oOrder->getPayoneRatepayShopId()
319
        ];
320
    }
321
}
322