PayoneMethod::getStoreCode()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
c 0
b 0
f 0
nc 7
nop 0
dl 0
loc 24
rs 9.2222
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\Methods;
28
29
use Magento\Framework\Exception\LocalizedException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Exception\LocalizedException 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 Payone\Core\Model\Exception\AuthorizationException;
31
use Magento\Payment\Model\InfoInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\InfoInterface 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 Magento\Sales\Model\Order;
0 ignored issues
show
Bug introduced by
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
Bug introduced by
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
 * Abstract model for all the PAYONE payment methods
37
 */
38
abstract class PayoneMethod extends BaseMethod
39
{
40
    /**
41
     * Returns clearingtype
42
     *
43
     * @return string
44
     */
45
    public function getClearingtype()
46
    {
47
        return $this->sClearingtype;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sClearingtype also could return the type boolean which is incompatible with the documented return type string.
Loading history...
48
    }
49
50
    /**
51
     * Returns wallettype
52
     *
53
     * @return string
54
     */
55
    public function getWallettype()
56
    {
57
        return $this->sWallettype;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sWallettype also could return the type boolean which is incompatible with the documented return type string.
Loading history...
58
    }
59
60
    /**
61
     * Returns authorization-mode
62
     * preauthorization or authorization
63
     *
64
     * @return string
65
     */
66
    public function getAuthorizationMode()
67
    {
68
        $sRequestType = $this->shopHelper->getConfigParam('request_type');
69
        if ($this->hasCustomConfig()) {
70
            $sCustomRequestType = $this->getCustomConfigParam('request_type');
71
            if (!empty($sCustomRequestType)) {
72
                $sRequestType = $sCustomRequestType;
73
            }
74
        }
75
        return $sRequestType;
76
    }
77
78
    /**
79
     * Method handling the debit request and the response
80
     *
81
     * @param  InfoInterface $payment
82
     * @param  float         $amount
83
     * @return void
84
     * @throws LocalizedException
85
     */
86
    protected function sendPayoneDebit(InfoInterface $payment, $amount)
87
    {
88
        $aResponse = $this->debitRequest->sendRequest($this, $payment, $amount);
89
        if (!$aResponse) {
90
            throw new LocalizedException(__('Unkown error'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

90
            throw new LocalizedException(/** @scrutinizer ignore-call */ __('Unkown error'));
Loading history...
91
        } elseif ($aResponse['status'] == 'ERROR') {
92
            $this->checkoutSession->setPayoneDebitRequest($this->debitRequest->getParameters());
93
            $this->checkoutSession->setPayoneDebitResponse($this->debitRequest->getResponse());
94
            $this->checkoutSession->setPayoneDebitOrderId($this->debitRequest->getOrderId());
95
            throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']));
96
        }
97
    }
98
99
    /**
100
     * Method handling the capture request and the response
101
     *
102
     * @param  InfoInterface $payment
103
     * @param  float         $amount
104
     * @return void
105
     * @throws LocalizedException
106
     */
107
    protected function sendPayoneCapture(InfoInterface $payment, $amount)
108
    {
109
        $aResponse = $this->captureRequest->sendRequest($this, $payment, $amount);
110
        if (!$aResponse) {// response not existing
111
            throw new LocalizedException(__('Unkown error'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

111
            throw new LocalizedException(/** @scrutinizer ignore-call */ __('Unkown error'));
Loading history...
112
        } elseif ($aResponse['status'] == 'ERROR') {// request returned an error
113
            throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']));
114
        }
115
    }
116
117
    /**
118
     * Removes status flag used during checkout process from session
119
     *
120
     * @return void
121
     */
122
    protected function unsetSessionStatusFlags() {
123
        $this->checkoutSession->unsPayoneRedirectUrl();
124
        $this->checkoutSession->unsPayoneRedirectedPaymentMethod();
125
        $this->checkoutSession->unsPayoneCanceledPaymentMethod();
126
        $this->checkoutSession->unsPayoneIsError();
127
        $this->checkoutSession->unsShowAmazonPendingNotice();
128
        $this->checkoutSession->unsAmazonRetryAsync();
129
    }
130
131
    /**
132
     * Method handling the authorization request and the response
133
     *
134
     * @param  InfoInterface $payment
135
     * @param  float         $amount
136
     * @return void
137
     * @throws LocalizedException
138
     */
139
    protected function sendPayoneAuthorization(InfoInterface $payment, $amount)
140
    {
141
        $this->unsetSessionStatusFlags();
142
        $oOrder = $payment->getOrder();
143
        $oOrder->setCanSendNewEmailFlag(false); // dont send email now, will be sent on appointed
144
145
        if ($this->shopHelper->getConfigParam('currency', 'global', 'payone_general', $payment->getOrder()->getStore()->getCode()) == 'display') {
146
            $amount = $oOrder->getTotalDue(); // send display amount instead of base amount
147
        }
148
149
        $this->checkoutSession->unsPayoneRedirectUrl(); // remove redirect url from session
150
        $this->checkoutSession->unsPayoneRedirectedPaymentMethod();
151
        $this->checkoutSession->unsPayoneCanceledPaymentMethod();
152
        $this->checkoutSession->unsPayoneIsError();
153
154
        $aResponse = $this->authorizationRequest->sendRequest($this, $oOrder, $amount);
155
        $aResponse = $this->handleResponse($aResponse, $oOrder, $amount);
156
        if ($aResponse['status'] == 'ERROR') {// request returned an error
157
            throw new AuthorizationException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']), $aResponse);
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

157
            throw new AuthorizationException(/** @scrutinizer ignore-call */ __($aResponse['errorcode'].' - '.$aResponse['customermessage']), $aResponse);
Loading history...
158
        } elseif ($aResponse['status'] == 'APPROVED' || $aResponse['status'] == 'REDIRECT') {// request successful
159
            $payment->setTransactionId($aResponse['txid']);
160
            $payment->setIsTransactionClosed(0);
161
            if ($aResponse['status'] == 'REDIRECT') {// user needs to be redirected to external payment page
162
                $this->checkoutSession->setPayoneRedirectUrl($aResponse['redirecturl']);
163
                $this->checkoutSession->setPayoneRedirectedPaymentMethod($this->getCode());
164
            }
165
        }
166
    }
167
168
    /**
169
     * Perform certain actions with the response
170
     * Extension hook for certain payment methods
171
     *
172
     * @param  array $aResponse
173
     * @param  Order $oOrder
174
     * @param  float $amount
175
     * @return array
176
     */
177
    protected function handleResponse($aResponse, Order $oOrder, $amount)
178
    {
179
        $aAddData = $oOrder->getPayment()->getAdditionalInformation();
180
        if (!empty($aAddData['iban'])) {
181
            $oOrder->getPayment()->setAdditionalInformation('iban', $this->toolkitHelper->maskIban($aAddData['iban']));
182
        }
183
        return $aResponse;
184
    }
185
186
    /**
187
     * Convert DataObject to needed array format
188
     * Hook for overriding in specific payment type class
189
     *
190
     * @param  DataObject $data
191
     * @return array
192
     */
193
    protected function getPaymentStorageData(DataObject $data)
194
    {
195
        return [];
196
    }
197
198
    /**
199
     * Check config and save payment data
200
     *
201
     * @param  DataObject $data
202
     * @return void
203
     */
204
    protected function handlePaymentDataStorage(DataObject $data)
205
    {
206
        if ((bool)$this->getCustomConfigParam('save_data_enabled') === true) {
207
            $aPaymentData = $this->getPaymentStorageData($data);
208
            $iCustomerId = $this->checkoutSession->getQuote()->getCustomerId();
209
            if (!empty($aPaymentData) && $iCustomerId) {
210
                $this->savedPaymentData->addSavedPaymentData($iCustomerId, $this->getCode(), $aPaymentData);
211
            }
212
        }
213
    }
214
215
    /**
216
     * Returns operationmode live or test for this payment method
217
     *
218
     * @return string
219
     */
220
    public function getOperationMode()
221
    {
222
        return $this->getCustomConfigParam('mode');
223
    }
224
225
    /**
226
     * Return parameters specific to this payment type
227
     *
228
     * @param  Order $oOrder
229
     * @return array
230
     */
231
    public function getPaymentSpecificParameters(Order $oOrder)
232
    {
233
        return []; // filled in child classes
234
    }
235
236
    /**
237
     * Return capture parameters specific to this payment type
238
     *
239
     * @param  Order $oOrder
240
     * @return array
241
     */
242
    public function getPaymentSpecificCaptureParameters(Order $oOrder)
243
    {
244
        return []; // filled in child classes
245
    }
246
247
    /**
248
     * Return debit parameters specific to this payment type
249
     *
250
     * @param  Order $oOrder
251
     * @return array
252
     */
253
    public function getPaymentSpecificDebitParameters(Order $oOrder)
254
    {
255
        return []; // filled in child classes
256
    }
257
258
    /**
259
     * Return success url for redirect payment types
260
     *
261
     * @param  Order $oOrder
262
     * @return string
263
     */
264
    public function getSuccessUrl(?Order $oOrder = null)
265
    {
266
        $sAddedParams = '';
267
        if ($oOrder !== null) {
268
            $sAddedParams = '?incrementId='.$oOrder->getIncrementId();
269
        }
270
        return $this->url->getUrl('payone/onepage/returned').$sAddedParams;
271
    }
272
273
    /**
274
     * Return cancel url for redirect payment types
275
     *
276
     * @return string
277
     */
278
    public function getCancelUrl()
279
    {
280
        return $this->url->getUrl('payone/onepage/cancel');
281
    }
282
283
    /**
284
     * Return error url for redirect payment types
285
     *
286
     * @return string
287
     */
288
    public function getErrorUrl()
289
    {
290
        return $this->url->getUrl('payone/onepage/cancel?error=1');
291
    }
292
293
    /**
294
     * Return if redirect urls have to be added to the authroization request
295
     *
296
     * @return bool
297
     */
298
    public function needsRedirectUrls()
299
    {
300
        return $this->blNeedsRedirectUrls;
301
    }
302
303
    /**
304
     * Return if transaction_param has to be added to the authroization request
305
     *
306
     * @return bool
307
     */
308
    public function needsTransactionParam()
309
    {
310
        return $this->blNeedsTransactionParam;
311
    }
312
313
    /**
314
     * Return if invoice data has to be added to the authroization request
315
     *
316
     * @return bool
317
     */
318
    public function needsProductInfo()
319
    {
320
        return $this->blNeedsProductInfo;
321
    }
322
323
324
    /**
325
     * Return if bank data has to be added to the debit request
326
     *
327
     * @return bool
328
     */
329
    public function needsSepaDataOnDebit()
330
    {
331
        return $this->blNeedsSepaDataOnDebit;
332
    }
333
334
    /**
335
     * Get config parameter for this payment type
336
     *
337
     * @param  string $sParam
338
     * @param  string $sStoreCode
339
     * @return string
340
     */
341
    public function getCustomConfigParam($sParam, $sStoreCode = null)
342
    {
343
        if ($sStoreCode === null) {
344
            $sStoreCode = $this->getStoreCode();
345
        }
346
        return $this->shopHelper->getConfigParam($sParam, $this->getCode(), 'payone_payment', $sStoreCode);
347
    }
348
349
    /**
350
     * Trys to retrieve the storecode from the order
351
     *
352
     * @return string|null
353
     */
354
    protected function getStoreCode()
355
    {
356
        try {
357
            $oInfoInstance = $this->getInfoInstance();
358
            if (empty($oInfoInstance)) {
359
                return null;
360
            }
361
        } catch (\Exception $oExc) {
362
            return null;
363
        }
364
365
        $oOrder = $oInfoInstance->getOrder();
366
        if (empty($oOrder)) {
367
            $oOrder = $oInfoInstance->getQuote();
368
            if (empty($oOrder)) {
369
                return null;
370
            }
371
        }
372
373
        $oStore = $oOrder->getStore();
374
        if (empty($oStore)) {
375
            return null;
376
        }
377
        return $oStore->getCode();
378
    }
379
380
    /**
381
     * Returns if global PAYONE config is used for this payment type
382
     *
383
     * @return bool
384
     */
385
    public function hasCustomConfig()
386
    {
387
        if ($this->getCustomConfigParam('use_global') == '0') {// has non-global config
388
            return true;
389
        }
390
        return false;
391
    }
392
393
    /**
394
     * Return if this payment method is part of a group
395
     *
396
     * @return bool
397
     */
398
    public function isGroupMethod()
399
    {
400
        if ($this->sGroupName === false) {
401
            return false;
402
        }
403
        return true;
404
    }
405
406
    /**
407
     * Returns group identifier
408
     *
409
     * @return string|bool
410
     */
411
    public function getGroupName()
412
    {
413
        return $this->sGroupName;
414
    }
415
416
    /**
417
     * Returns group identifier
418
     *
419
     * @return string|bool
420
     */
421
    public function getSubType()
422
    {
423
        return $this->sSubType;
424
    }
425
426
    /**
427
     * Return parameters specific to this payment sub type
428
     *
429
     * @param  Order $oOrder
430
     * @return array
431
     */
432
    public function getSubTypeSpecificParameters(Order $oOrder)
433
    {
434
        return []; // filled in child classes
435
    }
436
437
    /**
438
     * Formats the reference number if needed for this payment method
439
     *
440
     * @param  string $sRefNr
441
     * @return string
442
     */
443
    public function formatReferenceNumber($sRefNr)
444
    {
445
        return $sRefNr;
446
    }
447
448
    /**
449
     * Return max length of narrative text
450
     *
451
     * @return int
452
     */
453
    public function getNarrativeTextMaxLength()
454
    {
455
        return $this->iNarrativeTextMax;
456
    }
457
458
    /**
459
     * @return array
460
     */
461
    public function getFrontendConfig()
462
    {
463
        // Hook to be overloaded by child classes
464
        return [];
465
    }
466
}
467