Completed
Push — master ( 9097d5...0ce82e )
by Florian
06:13
created

PayoneMethod   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 276
rs 9.3999
c 0
b 0
f 0
wmc 33

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getClearingtype() 0 3 1
B sendPayoneAuthorization() 0 18 5
A getAuthorizationMode() 0 7 2
A getCancelUrl() 0 3 1
A needsProductInfo() 0 3 1
A getSubType() 0 3 1
A needsRedirectUrls() 0 3 1
A getPaymentSpecificParameters() 0 3 1
A getCustomConfigParam() 0 3 1
A getGroupName() 0 3 1
A sendPayoneCapture() 0 7 3
A getSubTypeSpecificParameters() 0 3 1
A sendPayoneDebit() 0 7 3
A getErrorUrl() 0 3 1
A formatReferenceNumber() 0 3 1
A getOperationMode() 0 3 1
A getSuccessUrl() 0 3 1
A isGroupMethod() 0 6 2
A needsSepaDataOnDebit() 0 3 1
A getNarrativeTextMaxLength() 0 3 1
A handleResponse() 0 2 1
A hasCustomConfig() 0 6 2
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;
30
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...
31
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...
32
33
/**
34
 * Abstract model for all the PAYONE payment methods
35
 */
36
abstract class PayoneMethod extends BaseMethod
37
{
38
    /**
39
     * Returns clearingtype
40
     *
41
     * @return string
42
     * @throws LocalizedException
43
     */
44
    public function getClearingtype()
45
    {
46
        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...
47
    }
48
49
    /**
50
     * Returns authorization-mode
51
     * preauthorization or authorization
52
     *
53
     * @return string
54
     */
55
    public function getAuthorizationMode()
56
    {
57
        $sRequestType = $this->shopHelper->getConfigParam('request_type');
58
        if ($this->hasCustomConfig()) {
59
            $sRequestType = $this->getCustomConfigParam('request_type');
60
        }
61
        return $sRequestType;
62
    }
63
64
    /**
65
     * Method handling the debit request and the response
66
     *
67
     * @param  InfoInterface $payment
68
     * @param  float         $amount
69
     * @return void
70
     * @throws LocalizedException
71
     */
72
    protected function sendPayoneDebit(InfoInterface $payment, $amount)
73
    {
74
        $aResponse = $this->debitRequest->sendRequest($this, $payment, $amount);
75
        if ($aResponse['status'] == 'ERROR') {
76
            throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']));
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

76
            throw new LocalizedException(/** @scrutinizer ignore-call */ __($aResponse['errorcode'].' - '.$aResponse['customermessage']));
Loading history...
77
        } elseif (!$aResponse) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $aResponse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
The condition $aResponse is always true.
Loading history...
78
            throw new LocalizedException(__('Unkown error'));
79
        }
80
    }
81
82
    /**
83
     * Method handling the capture request and the response
84
     *
85
     * @param  InfoInterface $payment
86
     * @param  float         $amount
87
     * @return void
88
     * @throws LocalizedException
89
     */
90
    protected function sendPayoneCapture(InfoInterface $payment, $amount)
91
    {
92
        $aResponse = $this->captureRequest->sendRequest($this, $payment, $amount);
93
        if ($aResponse['status'] == 'ERROR') {// request returned an error
94
            throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']));
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

94
            throw new LocalizedException(/** @scrutinizer ignore-call */ __($aResponse['errorcode'].' - '.$aResponse['customermessage']));
Loading history...
95
        } elseif (!$aResponse) {// response not existing
0 ignored issues
show
Bug Best Practice introduced by
The expression $aResponse of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
The condition $aResponse is always true.
Loading history...
96
            throw new LocalizedException(__('Unkown error'));
97
        }
98
    }
99
100
    /**
101
     * Method handling the authorization request and the response
102
     *
103
     * @param  InfoInterface $payment
104
     * @param  float         $amount
105
     * @return void
106
     * @throws LocalizedException
107
     */
108
    protected function sendPayoneAuthorization(InfoInterface $payment, $amount)
109
    {
110
        $oOrder = $payment->getOrder();
111
        $oOrder->setCanSendNewEmailFlag(false); // dont send email now, will be sent on appointed
112
        $this->checkoutSession->unsPayoneRedirectUrl(); // remove redirect url from session
113
        $this->checkoutSession->unsPayoneRedirectedPaymentMethod();
114
        $this->checkoutSession->unsPayoneCanceledPaymentMethod();
115
        $this->checkoutSession->unsPayoneIsError();
116
        $aResponse = $this->authorizationRequest->sendRequest($this, $oOrder, $amount);
117
        $this->handleResponse($aResponse, $oOrder);
118
        if ($aResponse['status'] == 'ERROR') {// request returned an error
119
            throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']));
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

119
            throw new LocalizedException(/** @scrutinizer ignore-call */ __($aResponse['errorcode'].' - '.$aResponse['customermessage']));
Loading history...
120
        } elseif ($aResponse['status'] == 'APPROVED' || $aResponse['status'] == 'REDIRECT') {// request successful
121
            $payment->setTransactionId($aResponse['txid']);
122
            $payment->setIsTransactionClosed(0);
123
            if ($aResponse['status'] == 'REDIRECT') {// user needs to be redirected to external payment page
124
                $this->checkoutSession->setPayoneRedirectUrl($aResponse['redirecturl']);
125
                $this->checkoutSession->setPayoneRedirectedPaymentMethod($this->getCode());
126
            }
127
        }
128
    }
129
130
    /**
131
     * Perform certain actions with the response
132
     *
133
     * @param  array $aResponse
134
     * @param  Order $oOrder
135
     * @return void
136
     */
137
    protected function handleResponse($aResponse, Order $oOrder)
138
    {
139
        // hook for certain payment methods
140
    }
141
142
    /**
143
     * Returns operationmode live or test for this payment method
144
     *
145
     * @return string
146
     */
147
    public function getOperationMode()
148
    {
149
        return $this->getCustomConfigParam('mode');
150
    }
151
152
    /**
153
     * Return parameters specific to this payment type
154
     *
155
     * @param  Order $oOrder
156
     * @return array
157
     */
158
    public function getPaymentSpecificParameters(Order $oOrder)
159
    {
160
        return []; // filled in child classes
161
    }
162
163
    /**
164
     * Return success url for redirect payment types
165
     *
166
     * @return string
167
     */
168
    public function getSuccessUrl()
169
    {
170
        return $this->url->getUrl('payone/onepage/returned');
171
    }
172
173
    /**
174
     * Return cancel url for redirect payment types
175
     *
176
     * @return string
177
     */
178
    public function getCancelUrl()
179
    {
180
        return $this->url->getUrl('payone/onepage/cancel');
181
    }
182
183
    /**
184
     * Return error url for redirect payment types
185
     *
186
     * @return string
187
     */
188
    public function getErrorUrl()
189
    {
190
        return $this->url->getUrl('payone/onepage/cancel?error=1');
191
    }
192
193
    /**
194
     * Return if redirect urls have to be added to the authroization request
195
     *
196
     * @return bool
197
     */
198
    public function needsRedirectUrls()
199
    {
200
        return $this->blNeedsRedirectUrls;
201
    }
202
203
    /**
204
     * Return if invoice data has to be added to the authroization request
205
     *
206
     * @return bool
207
     */
208
    public function needsProductInfo()
209
    {
210
        return $this->blNeedsProductInfo;
211
    }
212
213
214
    /**
215
     * Return if bank data has to be added to the debit request
216
     *
217
     * @return bool
218
     */
219
    public function needsSepaDataOnDebit()
220
    {
221
        return $this->blNeedsSepaDataOnDebit;
222
    }
223
224
    /**
225
     * Get config parameter for this payment type
226
     *
227
     * @param  string $sParam
228
     * @return string
229
     */
230
    public function getCustomConfigParam($sParam)
231
    {
232
        return $this->shopHelper->getConfigParam($sParam, $this->getCode(), 'payone_payment');
233
    }
234
235
    /**
236
     * Returns if global PAYONE config is used for this payment type
237
     *
238
     * @return bool
239
     */
240
    public function hasCustomConfig()
241
    {
242
        if ($this->getCustomConfigParam('use_global') == '0') {// has non-global config
243
            return true;
244
        }
245
        return false;
246
    }
247
248
    /**
249
     * Return if this payment method is part of a group
250
     *
251
     * @return bool
252
     */
253
    public function isGroupMethod()
254
    {
255
        if ($this->sGroupName === false) {
256
            return false;
257
        }
258
        return true;
259
    }
260
261
    /**
262
     * Returns group identifier
263
     *
264
     * @return string|bool
265
     */
266
    public function getGroupName()
267
    {
268
        return $this->sGroupName;
269
    }
270
271
    /**
272
     * Returns group identifier
273
     *
274
     * @return string|bool
275
     */
276
    public function getSubType()
277
    {
278
        return $this->sSubType;
279
    }
280
281
    /**
282
     * Return parameters specific to this payment sub type
283
     *
284
     * @param  Order $oOrder
285
     * @return array
286
     */
287
    public function getSubTypeSpecificParameters(Order $oOrder)
288
    {
289
        return []; // filled in child classes
290
    }
291
292
    /**
293
     * Formats the reference number if needed for this payment method
294
     * Needed for Paydirekt
295
     *
296
     * @param  string $sRefNr
297
     * @return string
298
     */
299
    public function formatReferenceNumber($sRefNr)
300
    {
301
        return $sRefNr;
302
    }
303
304
    /**
305
     * Return max length of narrative text
306
     *
307
     * @return int
308
     */
309
    public function getNarrativeTextMaxLength()
310
    {
311
        return $this->iNarrativeTextMax;
312
    }
313
}
314