Issues (139)

Gateway/Config/ConfigCc.php (1 issue)

1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
declare(strict_types=1);
9
10
namespace Getnet\PaymentMagento\Gateway\Config;
11
12
use Magento\Framework\App\Config\ScopeConfigInterface;
13
use Magento\Framework\Serialize\Serializer\Json;
14
use Magento\Payment\Gateway\Config\Config as PaymentConfig;
15
use Magento\Payment\Model\Method\AbstractMethod;
16
use Magento\Store\Model\ScopeInterface;
17
18
/**
19
 * Class ConfigCc - Returns form of payment configuration properties.
20
 */
21
class ConfigCc extends PaymentConfig
22
{
23
    /**
24
     * @const string
25
     */
26
    public const METHOD = 'getnet_paymentmagento_cc';
27
28
    /**
29
     * @const string
30
     */
31
    public const CC_TYPES = 'payment/getnet_paymentmagento_cc/cctypes';
32
33
    /**
34
     * @const string
35
     */
36
    public const CVV_ENABLED = 'cvv_enabled';
37
38
    /**
39
     * @const string
40
     */
41
    public const ACTIVE = 'active';
42
43
    /**
44
     * @const string
45
     */
46
    public const TITLE = 'title';
47
48
    /**
49
     * @const string
50
     */
51
    public const CC_MAPPER = 'cctypes_mapper';
52
53
    /**
54
     * @const string
55
     */
56
    public const USE_GET_TAX_DOCUMENT = 'get_tax_document';
57
58
    /**
59
     * @const string
60
     */
61
    public const USE_GET_PHONE = 'get_phone';
62
63
    /**
64
     * @const string
65
     */
66
    public const USE_FRAUD_MANAGER = 'fraud_manager';
67
68
    /**
69
     * @const string
70
     */
71
    public const INSTALL_WITH_INTEREST = 'INSTALL_WITH_INTEREST';
72
73
    /**
74
     * @const string
75
     */
76
    public const INSTALL_NO_INTEREST = 'INSTALL_NO_INTEREST';
77
78
    /**
79
     * @const string
80
     */
81
    public const INSTALL_FULL = 'FULL';
82
83
    /**
84
     * @const string
85
     */
86
    public const PAYMENT_ACTION = 'payment_action';
87
88
    /**
89
     * @var ScopeConfigInterface
90
     */
91
    protected $scopeConfig;
92
93
    /**
94
     * @var Json
95
     */
96
    protected $json;
97
98
    /**
99
     * @param ScopeConfigInterface $scopeConfig
100
     * @param Json                 $json
101
     * @param string               $methodCode
102
     */
103
    public function __construct(
104
        ScopeConfigInterface $scopeConfig,
105
        Json $json,
106
        $methodCode = self::METHOD
107
    ) {
108
        parent::__construct($scopeConfig, $methodCode);
109
        $this->scopeConfig = $scopeConfig;
110
        $this->json = $json;
111
    }
112
113
    /**
114
     * Should the cvv field be shown.
115
     *
116
     * @param int|null $storeId
117
     *
118
     * @return bool
119
     */
120
    public function isCvvEnabled($storeId = null): bool
121
    {
122
        $pathPattern = 'payment/%s/%s';
123
124
        return (bool) $this->scopeConfig->getValue(
125
            sprintf($pathPattern, self::METHOD, self::CVV_ENABLED),
126
            ScopeInterface::SCOPE_STORE,
127
            $storeId
128
        );
129
    }
130
131
    /**
132
     * Get Payment configuration status.
133
     *
134
     * @param int|null $storeId
135
     *
136
     * @return bool
137
     */
138
    public function isActive($storeId = null): bool
139
    {
140
        $pathPattern = 'payment/%s/%s';
141
142
        return (bool) $this->scopeConfig->getValue(
143
            sprintf($pathPattern, self::METHOD, self::ACTIVE),
144
            ScopeInterface::SCOPE_STORE,
145
            $storeId
146
        );
147
    }
148
149
    /**
150
     * Get title of payment.
151
     *
152
     * @param int|null $storeId
153
     *
154
     * @return string|null
155
     */
156
    public function getTitle($storeId = null): ?string
157
    {
158
        $pathPattern = 'payment/%s/%s';
159
160
        return $this->scopeConfig->getValue(
161
            sprintf($pathPattern, self::METHOD, self::TITLE),
162
            ScopeInterface::SCOPE_STORE,
163
            $storeId
164
        );
165
    }
166
167
    /**
168
     * Get if you use document capture on the form.
169
     *
170
     * @param int|null $storeId
171
     *
172
     * @return bool
173
     */
174
    public function hasUseTaxDocumentCapture($storeId = null): bool
175
    {
176
        $pathPattern = 'payment/%s/%s';
177
178
        return (bool) $this->scopeConfig->getValue(
179
            sprintf($pathPattern, self::METHOD, self::USE_GET_TAX_DOCUMENT),
180
            ScopeInterface::SCOPE_STORE,
181
            $storeId
182
        );
183
    }
184
185
    /**
186
     * Get if you use phone capture on the form.
187
     *
188
     * @param int|null $storeId
189
     *
190
     * @return bool
191
     */
192
    public function hasUsePhoneCapture($storeId = null): bool
193
    {
194
        $pathPattern = 'payment/%s/%s';
195
196
        return (bool) $this->scopeConfig->getValue(
197
            sprintf($pathPattern, self::METHOD, self::USE_GET_PHONE),
198
            ScopeInterface::SCOPE_STORE,
199
            $storeId
200
        );
201
    }
202
203
    /**
204
     * Defines whether to use FraudManager.
205
     *
206
     * @param int|null $storeId
207
     *
208
     * @return bool
209
     */
210
    public function hasUseFraudManager($storeId = null): bool
211
    {
212
        $pathPattern = 'payment/%s/%s';
213
214
        return (bool) $this->scopeConfig->getValue(
215
            sprintf($pathPattern, self::METHOD, self::USE_FRAUD_MANAGER),
216
            ScopeInterface::SCOPE_STORE,
217
            $storeId
218
        );
219
    }
220
221
    /**
222
     * Has Delayed.
223
     *
224
     * @param int|null $storeId
225
     *
226
     * @return bool
227
     */
228
    public function hasDelayed($storeId = null): bool
229
    {
230
        $pathPattern = 'payment/%s/%s';
231
        $typePaymentAction = $this->scopeConfig->getValue(
232
            sprintf($pathPattern, self::METHOD, self::PAYMENT_ACTION),
233
            ScopeInterface::SCOPE_STORE,
234
            $storeId
235
        );
236
        if ($typePaymentAction === AbstractMethod::ACTION_AUTHORIZE_CAPTURE) {
237
            return false;
238
        }
239
240
        return true;
241
    }
242
243
    /**
244
     * Should the cc types.
245
     *
246
     * @param int|null $storeId
247
     *
248
     * @return string
249
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
250
     */
251
    public function getCcAvailableTypes($storeId = null): string
252
    {
253
        return $this->scopeConfig->getValue(
254
            self::CC_TYPES,
255
            ScopeInterface::SCOPE_STORE,
256
            $storeId
257
        );
258
    }
259
260
    /**
261
     * Cc Mapper.
262
     *
263
     * @param int|null $storeId
264
     *
265
     * @return array
266
     */
267
    public function getCcTypesMapper($storeId = null): array
268
    {
269
        $pathPattern = 'payment/%s/%s';
270
271
        $ccTypesMapper = $this->scopeConfig->getValue(
272
            sprintf($pathPattern, self::METHOD, self::CC_MAPPER),
273
            ScopeInterface::SCOPE_STORE,
274
            $storeId
275
        );
276
277
        $result = $this->json->unserialize($ccTypesMapper);
278
279
        return is_array($result) ? $result : [];
280
    }
281
282
    /**
283
     * Get info interest.
284
     *
285
     * @param int|null $storeId
286
     *
287
     * @return array
288
     */
289
    public function getInfoInterest($storeId = null): array
290
    {
291
        $interest = [];
292
        $interest['0'] = 0;
293
        $interest['1'] = -$this->scopeConfig->getValue(
294
            'payment/getnet_paymentmagento_cc/installment_interest_1',
295
            ScopeInterface::SCOPE_STORE,
296
            $storeId
297
        );
298
        $interest['2'] = $this->scopeConfig->getValue(
299
            'payment/getnet_paymentmagento_cc/installment_interest_2',
300
            ScopeInterface::SCOPE_STORE,
301
            $storeId
302
        );
303
        $interest['3'] = $this->scopeConfig->getValue(
304
            'payment/getnet_paymentmagento_cc/installment_interest_3',
305
            ScopeInterface::SCOPE_STORE,
306
            $storeId
307
        );
308
        $interest['4'] = $this->scopeConfig->getValue(
309
            'payment/getnet_paymentmagento_cc/installment_interest_4',
310
            ScopeInterface::SCOPE_STORE,
311
            $storeId
312
        );
313
        $interest['5'] = $this->scopeConfig->getValue(
314
            'payment/getnet_paymentmagento_cc/installment_interest_5',
315
            ScopeInterface::SCOPE_STORE,
316
            $storeId
317
        );
318
        $interest['6'] = $this->scopeConfig->getValue(
319
            'payment/getnet_paymentmagento_cc/installment_interest_6',
320
            ScopeInterface::SCOPE_STORE,
321
            $storeId
322
        );
323
        $interest['7'] = $this->scopeConfig->getValue(
324
            'payment/getnet_paymentmagento_cc/installment_interest_7',
325
            ScopeInterface::SCOPE_STORE,
326
            $storeId
327
        );
328
        $interest['8'] = $this->scopeConfig->getValue(
329
            'payment/getnet_paymentmagento_cc/installment_interest_8',
330
            ScopeInterface::SCOPE_STORE,
331
            $storeId
332
        );
333
        $interest['9'] = $this->scopeConfig->getValue(
334
            'payment/getnet_paymentmagento_cc/installment_interest_9',
335
            ScopeInterface::SCOPE_STORE,
336
            $storeId
337
        );
338
        $interest['10'] = $this->scopeConfig->getValue(
339
            'payment/getnet_paymentmagento_cc/installment_interest_10',
340
            ScopeInterface::SCOPE_STORE,
341
            $storeId
342
        );
343
        $interest['11'] = $this->scopeConfig->getValue(
344
            'payment/getnet_paymentmagento_cc/installment_interest_11',
345
            ScopeInterface::SCOPE_STORE,
346
            $storeId
347
        );
348
        $interest['12'] = $this->scopeConfig->getValue(
349
            'payment/getnet_paymentmagento_cc/installment_interest_12',
350
            ScopeInterface::SCOPE_STORE,
351
            $storeId
352
        );
353
354
        return $interest;
355
    }
356
357
    /**
358
     * Get min installment.
359
     *
360
     * @param int|null $storeId
361
     *
362
     * @return string
363
     */
364
    public function getMinInstallment($storeId = null): ?string
365
    {
366
        return $this->scopeConfig->getValue(
367
            'payment/getnet_paymentmagento_cc/installment_min_installment',
368
            ScopeInterface::SCOPE_STORE,
369
            $storeId
370
        );
371
    }
372
373
    /**
374
     * Get max installment.
375
     *
376
     * @param int|null $storeId
377
     *
378
     * @return int|null
379
     */
380
    public function getMaxInstallment($storeId = null): ?int
381
    {
382
        return (int) $this->scopeConfig->getValue(
383
            'payment/getnet_paymentmagento_cc/installment_max_installment',
384
            ScopeInterface::SCOPE_STORE,
385
            $storeId
386
        );
387
    }
388
389
    /**
390
     * Get type Interest by Installment.
391
     *
392
     * @param int      $installment
393
     * @param int|null $storeId
394
     *
395
     * @return string
396
     */
397
    public function getTypeInterestByInstallment(int $installment, $storeId = null): ?string
398
    {
399
        if ($installment === 1) {
400
            return self::INSTALL_FULL;
401
        }
402
403
        $this->getInfoInterest($storeId);
404
        /** A função de repassar juros depende do emissor do cartão por razão comercial a funcionalidade está desativada */
405
        // $interest = $this->getInfoInterest($storeId);
406
        // if ((int) $interest[$installment] > 0) {
407
        //     return self::INSTALL_WITH_INTEREST;
408
        // }
409
410
        return self::INSTALL_NO_INTEREST;
411
    }
412
413
    /**
414
     * Get Interest to Amount.
415
     *
416
     * @param int      $installment
417
     * @param float    $amount
418
     * @param int|null $storeId
419
     *
420
     * @return float
421
     */
422
    public function getInterestToAmount($installment, $amount, $storeId = null): float
423
    {
424
        $valueInterest = $this->getCalcInterest($installment, $amount, $storeId);
425
426
        return round($valueInterest, 2);
427
    }
428
429
    /**
430
     * Get Calculate Interest.
431
     *
432
     * @param int   $installment
433
     * @param float $amount
434
     * @param int   $storeId
435
     *
436
     * @return array
437
     */
438
    public function getCalcInterest($installment, $amount, $storeId = null): float
439
    {
440
        $interest = 0.00;
441
        $interestByInstall = $this->getInfoInterest($storeId);
442
        if ($interestByInstall[$installment] > 0) {
443
            $interest = $this->getInterestSimple($amount, $interestByInstall[$installment]);
444
        }
445
446
        return round($interest, 2);
0 ignored issues
show
Bug Best Practice introduced by
The expression return round($interest, 2) returns the type double which is incompatible with the documented return type array.
Loading history...
447
    }
448
449
    /**
450
     * Interest Simple - Cc.
451
     *
452
     * @param float $amount
453
     * @param float $interest
454
     *
455
     * @return float
456
     */
457
    public function getInterestSimple($amount, $interest): float
458
    {
459
        $valinterest = 0.00;
460
461
        if ($interest) {
462
            $taxa = $interest / 100;
463
            $valinterest = $amount * $taxa;
464
        }
465
466
        return round($valinterest, 2);
467
    }
468
}
469