TxnIdHandler::mapperCcType()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 16
rs 8.4444
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Response;
10
11
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...mentDataObjectInterface 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...
12
use Magento\Payment\Gateway\Response\HandlerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Response\HandlerInterface 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...
13
14
/**
15
 * Class TxnIdHandler - Handles reading responses for creating payment.
16
 */
17
class TxnIdHandler implements HandlerInterface
18
{
19
    /**
20
     * @const TXN ID
21
     */
22
    const TXN_ID = 'TXN_ID';
23
24
    /**
25
     * @const BOLETO LINE CODE
26
     */
27
    const BOLETO_LINE_CODE = 'boleto_line_code';
28
29
    /**
30
     * @const BOLETO PRINT HREF
31
     */
32
    const BOLETO_PRINT_HREF = 'boleto_print_href';
33
34
    /**
35
     * @const Credit Card Number
36
     */
37
    const PAYER_CC_NUMBER = 'cc_number';
38
39
    /**
40
     * @const Credit Card Type
41
     */
42
    const PAYER_CC_TYPE = 'cc_type';
43
44
    /**
45
     * @const Installment
46
     */
47
    const PAYER_CC_INSTALLMENTS = 'cc_installments';
48
49
    /**
50
     * @const Holder Full Nane
51
     */
52
    const PAYER_HOLDER_FULLNAME = 'cc_holder_fullname';
53
54
    /**
55
     * @const Holder Birth Date
56
     */
57
    const PAYER_HOLDER_BIRTH_DATE = 'cc_holder_birth_date';
58
59
    /**
60
     * @const Holder Tax Document
61
     */
62
    const PAYER_HOLDER_TAX_DOCUMENT = 'cc_holder_tax_document';
63
64
    /**
65
     * @const Holder Phone
66
     */
67
    const PAYER_HOLDER_PHONE = 'cc_holder_phone';
68
69
    /**
70
     * Handles.
71
     *
72
     * @param array $handlingSubject
73
     * @param array $response
74
     */
75
    public function handle(array $handlingSubject, array $response)
76
    {
77
        if (!isset($handlingSubject['payment'])
78
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
79
        ) {
80
            throw new \InvalidArgumentException('Payment data object should be provided');
81
        }
82
83
        $paymentDO = $handlingSubject['payment'];
84
85
        $payment = $paymentDO->getPayment();
86
87
        if ($payment->getMethod() === 'moip_magento2_cc') {
88
            $paymentAddtional = $response['fundingInstrument'];
89
90
            if (isset($paymentAddtional['creditCard'])) {
91
                $ccType = $this->mapperCcType($paymentAddtional['creditCard']['brand']);
92
                $ccLast4 = $paymentAddtional['creditCard']['last4'];
93
                $ccHolderName = $paymentAddtional['creditCard']['holder']['fullname'];
94
            }
95
            $payment->setCcType($ccType);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ccType does not seem to be defined for all execution paths leading up to this point.
Loading history...
96
            $payment->setCcLast4($ccLast4);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ccLast4 does not seem to be defined for all execution paths leading up to this point.
Loading history...
97
            $payment->setCcOwner($ccHolderName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ccHolderName does not seem to be defined for all execution paths leading up to this point.
Loading history...
98
        }
99
100
        if ($payment->getMethod() === 'moip_magento2_cc_vault') {
101
            $paymentAddtional = $response['fundingInstrument'];
102
103
            if (isset($paymentAddtional['creditCard'])) {
104
                $ccType = $this->mapperCcType($paymentAddtional['creditCard']['brand']);
105
                $ccLast4 = $paymentAddtional['creditCard']['last4'];
106
                $ccHolderName = $paymentAddtional['creditCard']['holder']['fullname'];
107
            }
108
            $payment->setCcType($ccType);
109
            $payment->setCcLast4($ccLast4);
110
            $payment->setCcOwner($ccHolderName);
111
112
            $payment->setAdditionalInformation(
113
                self::PAYER_CC_TYPE,
114
                $ccType
115
            );
116
            $payment->setAdditionalInformation(
117
                self::PAYER_CC_NUMBER,
118
                'xxxx xxxx xxxx '.$ccLast4
119
            );
120
            $payment->setAdditionalInformation(
121
                self::PAYER_HOLDER_FULLNAME,
122
                $ccHolderName
123
            );
124
            $payment->setAdditionalInformation(
125
                self::PAYER_CC_INSTALLMENTS,
126
                $response['installmentCount']
127
            );
128
        }
129
130
        if ($payment->getMethod() === 'moip_magento2_boleto') {
131
            $payment->setAdditionalInformation(
132
                self::BOLETO_LINE_CODE,
133
                $response['fundingInstrument']['boleto']['lineCode']
134
            );
135
            $payment->setAdditionalInformation(
136
                self::BOLETO_PRINT_HREF,
137
                $response['_links']['payBoleto']['printHref']
138
            );
139
        }
140
141
        $payment->setTransactionId($response[self::TXN_ID]);
142
        $payment->setIsTransactionPending(1);
143
        $payment->setIsTransactionClosed(false);
144
    }
145
146
    /**
147
     * Get Type Cc by response payment.
148
     *
149
     * @param string $type
150
     */
151
    public function mapperCcType($type)
152
    {
153
        if ($type === 'MASTERCARD') {
154
            return 'MC';
155
        } elseif ($type === 'VISA') {
156
            return 'VI';
157
        } elseif ($type === 'AMEX') {
158
            return 'AE';
159
        } elseif ($type === 'DINERS') {
160
            return 'DN';
161
        } elseif ($type === 'HIPERCARD') {
162
            return 'HC';
163
        } elseif ($type === 'HIPER') {
164
            return 'HI';
165
        } elseif ($type === 'ELO') {
166
            return 'ELO';
167
        }
168
    }
169
}
170