Passed
Pull Request — master (#315)
by
unknown
03:23
created

Payment::getKlarnaMethodTitles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
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\Helper;
28
29
use Payone\Core\Model\PayoneConfig;
30
use Magento\Framework\Phrase;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Phrase 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 Payone\Core\Model\Source\CreditcardTypes;
32
33
/**
34
 * Helper class for everything that has to do with payment
35
 */
36
class Payment extends \Payone\Core\Helper\Base
37
{
38
    /**
39
     * List of all currently available PAYONE payment types
40
     *
41
     * @var array
42
     */
43
    protected $aAvailablePayments = [
44
        PayoneConfig::METHOD_CREDITCARD,
45
        PayoneConfig::METHOD_DEBIT,
46
        PayoneConfig::METHOD_PAYPAL,
47
        PayoneConfig::METHOD_CASH_ON_DELIVERY,
48
        PayoneConfig::METHOD_ADVANCE_PAYMENT,
49
        PayoneConfig::METHOD_INVOICE,
50
        PayoneConfig::METHOD_OBT_SOFORTUEBERWEISUNG,
51
        PayoneConfig::METHOD_OBT_GIROPAY,
52
        PayoneConfig::METHOD_OBT_EPS,
53
        PayoneConfig::METHOD_OBT_POSTFINANCE_EFINANCE,
54
        PayoneConfig::METHOD_OBT_POSTFINANCE_CARD,
55
        PayoneConfig::METHOD_OBT_IDEAL,
56
        PayoneConfig::METHOD_OBT_PRZELEWY,
57
        PayoneConfig::METHOD_BARZAHLEN,
58
        PayoneConfig::METHOD_PAYDIREKT,
59
        PayoneConfig::METHOD_SAFE_INVOICE,
60
        PayoneConfig::METHOD_PAYOLUTION_INVOICE,
61
        PayoneConfig::METHOD_PAYOLUTION_DEBIT,
62
        PayoneConfig::METHOD_PAYOLUTION_INSTALLMENT,
63
        PayoneConfig::METHOD_ALIPAY,
64
        PayoneConfig::METHOD_AMAZONPAY,
65
        PayoneConfig::METHOD_KLARNA_BASE,
66
        PayoneConfig::METHOD_KLARNA_DEBIT,
67
        PayoneConfig::METHOD_KLARNA_INVOICE,
68
        PayoneConfig::METHOD_KLARNA_INSTALLMENT,
69
    ];
70
71
    /**
72
     * Mapping of payment method code to payment abbreviation
73
     *
74
     * @var array
75
     */
76
    protected $aPaymentAbbreviation = [
77
        PayoneConfig::METHOD_CREDITCARD => 'cc',
78
        PayoneConfig::METHOD_CASH_ON_DELIVERY => 'cod',
79
        PayoneConfig::METHOD_DEBIT => 'elv',
80
        PayoneConfig::METHOD_ADVANCE_PAYMENT => 'vor',
81
        PayoneConfig::METHOD_INVOICE => 'rec',
82
        PayoneConfig::METHOD_OBT_SOFORTUEBERWEISUNG => 'sb',
83
        PayoneConfig::METHOD_OBT_GIROPAY => 'sb',
84
        PayoneConfig::METHOD_OBT_EPS => 'sb',
85
        PayoneConfig::METHOD_OBT_POSTFINANCE_EFINANCE => 'sb',
86
        PayoneConfig::METHOD_OBT_POSTFINANCE_CARD => 'sb',
87
        PayoneConfig::METHOD_OBT_IDEAL => 'sb',
88
        PayoneConfig::METHOD_OBT_PRZELEWY => 'sb',
89
        PayoneConfig::METHOD_PAYPAL => 'wlt',
90
        PayoneConfig::METHOD_PAYDIREKT => 'wlt',
91
        PayoneConfig::METHOD_BARZAHLEN => 'csh',
92
        PayoneConfig::METHOD_SAFE_INVOICE => 'rec',
93
        PayoneConfig::METHOD_PAYOLUTION_INVOICE => 'fnc',
94
        PayoneConfig::METHOD_PAYOLUTION_DEBIT => 'fnc',
95
        PayoneConfig::METHOD_PAYOLUTION_INSTALLMENT => 'fnc',
96
        PayoneConfig::METHOD_ALIPAY => 'wlt',
97
        PayoneConfig::METHOD_AMAZONPAY => 'wlt',
98
        PayoneConfig::METHOD_KLARNA_BASE => 'wlt',
99
        PayoneConfig::METHOD_KLARNA_DEBIT => 'wlt',
100
        PayoneConfig::METHOD_KLARNA_INVOICE => 'wlt',
101
        PayoneConfig::METHOD_KLARNA_INSTALLMENT => 'wlt',
102
    ];
103
104
    /**
105
     * Resource model for saved payment data
106
     *
107
     * @var \Payone\Core\Model\ResourceModel\SavedPaymentData
108
     */
109
    protected $savedPaymentData;
110
111
    /**
112
     * Constructor
113
     *
114
     * @param \Magento\Framework\App\Helper\Context             $context
115
     * @param \Magento\Store\Model\StoreManagerInterface        $storeManager
116
     * @param \Payone\Core\Helper\Shop                          $shopHelper
117
     * @param \Payone\Core\Model\ResourceModel\SavedPaymentData $savedPaymentData
118
     */
119
    public function __construct(
120
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
Bug introduced by
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...
121
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
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...
122
        \Payone\Core\Helper\Shop $shopHelper,
123
        \Payone\Core\Model\ResourceModel\SavedPaymentData $savedPaymentData
124
    ) {
125
        parent::__construct($context, $storeManager, $shopHelper);
126
        $this->savedPaymentData = $savedPaymentData;
127
    }
128
129
    /**
130
     * Return available payment types
131
     *
132
     * @return array
133
     */
134
    public function getAvailablePaymentTypes()
135
    {
136
        return $this->aAvailablePayments;
137
    }
138
139
    /**
140
     * Get all activated creditcard types
141
     *
142
     * @return array
143
     */
144
    public function getAvailableCreditcardTypes()
145
    {
146
        $aReturn = [];
147
148
        $sCreditcardTypes = $this->getConfigParam('types', PayoneConfig::METHOD_CREDITCARD, 'payone_payment');
149
        if ($sCreditcardTypes) {
150
            $aAllTypes = CreditcardTypes::getCreditcardTypes();
151
152
            $aCreditcardTypes = explode(',', $sCreditcardTypes);
153
            foreach ($aCreditcardTypes as $sTypeId) {
154
                $aReturn[] = [
155
                    'id' => $aAllTypes[$sTypeId]['cardtype'],
156
                    'title' => $aAllTypes[$sTypeId]['name'],
157
                    'cvc_length' => $aAllTypes[$sTypeId]['cvc_length'],
158
                ];
159
            }
160
        }
161
        return $aReturn;
162
    }
163
164
    /**
165
     * Return if cvc has to be checked
166
     *
167
     * @return bool
168
     */
169
    public function isCheckCvcActive()
170
    {
171
        return (bool)$this->getConfigParam('check_cvc', PayoneConfig::METHOD_CREDITCARD, 'payone_payment');
172
    }
173
174
    /**
175
     * Return if mandate management is activated
176
     *
177
     * @return bool
178
     */
179
    public function isMandateManagementActive()
180
    {
181
        return (bool)$this->getConfigParam('sepa_mandate_enabled', PayoneConfig::METHOD_DEBIT, 'payone_payment');
182
    }
183
184
    /**
185
     * Return if mandate download is activated
186
     *
187
     * @return bool
188
     */
189
    public function isMandateManagementDownloadActive()
190
    {
191
        return (bool)$this->getConfigParam('sepa_mandate_download_enabled', PayoneConfig::METHOD_DEBIT, 'payone_payment');
192
    }
193
194
    /**
195
     * Get status mapping configuration for given payment type
196
     *
197
     * @param  string $sPaymentCode
198
     * @return array
199
     */
200
    public function getStatusMappingByCode($sPaymentCode)
201
    {
202
        $sStatusMapping = $this->getConfigParam($sPaymentCode, 'statusmapping');
203
        $aStatusMapping = $this->unserialize($sStatusMapping);
204
        $aCleanMapping = [];
205
        if ($aStatusMapping) {
206
            foreach ($aStatusMapping as $aMap) {
207
                if (isset($aMap['txaction']) && isset($aMap['state_status'])) {
208
                    $aCleanMapping[$aMap['txaction']] = $aMap['state_status'];
209
                }
210
            }
211
        }
212
        return $aCleanMapping;
213
    }
214
215
    /**
216
     * Return display-message for the case that the bankaccount check
217
     * returned, that the given bankaccount was blocked
218
     *
219
     * @return Phrase
220
     */
221
    public function getBankaccountCheckBlockedMessage()
222
    {
223
        $sMessage = $this->getConfigParam('message_response_blocked', PayoneConfig::METHOD_DEBIT, 'payone_payment');
224
        if (empty($sMessage)) {
225
            $sMessage = 'Bankdata invalid.';
226
        }
227
        return __($sMessage);
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

227
        return /** @scrutinizer ignore-call */ __($sMessage);
Loading history...
228
    }
229
230
    /**
231
     * Return is PayPal Express is activated in the configuration
232
     *
233
     * @return bool
234
     */
235
    public function isPayPalExpressActive()
236
    {
237
        return (bool)$this->getConfigParam('express_active', PayoneConfig::METHOD_PAYPAL, 'payone_payment');
238
    }
239
240
    /**
241
     * Get abbreviation for the given payment type
242
     *
243
     * @param  string $sPaymentCode
244
     * @return string
245
     */
246
    public function getPaymentAbbreviation($sPaymentCode)
247
    {
248
        if (isset($this->aPaymentAbbreviation[$sPaymentCode])) {
249
            return $this->aPaymentAbbreviation[$sPaymentCode];
250
        }
251
        return 'unknown';
252
    }
253
254
    /**
255
     * Collect the Klarna store ids from the config and format it for frontend-use
256
     *
257
     * @return array
258
     */
259
    public function getKlarnaStoreIds()
260
    {
261
        $aStoreIds = [];
262
        $aKlarnaConfig = $this->unserialize($this->getConfigParam('klarna_config', PayoneConfig::METHOD_KLARNA, 'payone_payment'));
263
        if (!is_array($aKlarnaConfig)) {
264
            return $aStoreIds;
265
        }
266
267
        foreach ($aKlarnaConfig as $aItem) {
268
            if (!empty($aItem['store_id']) && isset($aItem['countries'])) {
269
                foreach ($aItem['countries'] as $sCountry) {
270
                    $aStoreIds[$sCountry] = $aItem['store_id'];
271
                }
272
            }
273
        }
274
        return $aStoreIds;
275
    }
276
277
    /**
278
     * Check if given payment method is activated
279
     *
280
     * @param  string $sMethodCode
281
     * @return bool
282
     */
283
    public function isPaymentMethodActive($sMethodCode)
284
    {
285
        return (bool)$this->getConfigParam('active', $sMethodCode, 'payment');
286
    }
287
288
    /**
289
     * Get amazon widget url depending on the mode
290
     *
291
     * @return string
292
     */
293
    public function getAmazonPayWidgetUrl()
294
    {
295
        $sSandbox = '';
296
        if ('test' == $this->getConfigParam('mode', PayoneConfig::METHOD_AMAZONPAY, 'payone_payment')) {
297
            $sSandbox = '/sandbox';
298
        }
299
        return "https://static-eu.payments-amazon.com/OffAmazonPayments/eur".$sSandbox."/lpa/js/Widgets.js";
300
    }
301
302
    /**
303
     * Returns method titles of Klarna payment methods
304
     */
305
    public function getKlarnaMethodTitles()
306
    {
307
        return [
308
            PayoneConfig::METHOD_KLARNA_INVOICE => $this->getConfigParam('title', PayoneConfig::METHOD_KLARNA_INVOICE, 'payment'),
309
            PayoneConfig::METHOD_KLARNA_DEBIT => $this->getConfigParam('title', PayoneConfig::METHOD_KLARNA_DEBIT, 'payment'),
310
            PayoneConfig::METHOD_KLARNA_INSTALLMENT => $this->getConfigParam('title', PayoneConfig::METHOD_KLARNA_INSTALLMENT, 'payment'),
311
        ];
312
    }
313
}
314