TxnIdCcHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 214
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 55 4
A __construct() 0 8 1
A getCreditCardType() 0 5 1
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Response;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use InvalidArgumentException;
14
use Magento\Framework\Serialize\Serializer\Json;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Response\HandlerInterface;
17
18
/**
19
 * Txn Id Cc Handler - Handles reading responses for Boleto payment.
20
 */
21
class TxnIdCcHandler implements HandlerInterface
22
{
23
    /**
24
     * Cc Terminal NSU - Payment Addtional Information.
25
     */
26
    public const PAYMENT_INFO_TERMINAL_NSU = 'terminal_nsu';
27
28
    /**
29
     * Cc Authorization Code - Payment Addtional Information.
30
     */
31
    public const PAYMENT_INFO_AUTHORIZATION_CODE = 'authorization_code';
32
33
    /**
34
     * Cc Acquirer Transaction Id - Payment Addtional Information.
35
     */
36
    public const PAYMENT_INFO_ACQUIRER_TRANSACTION_ID = 'acquirer_transaction_id';
37
38
    /**
39
     * Cc Transaction Id - Payment Addtional Information.
40
     */
41
    public const PAYMENT_INFO_TRANSACTION_ID = 'transaction_id';
42
43
    /**
44
     * Cc Type - Payment Addtional Information.
45
     */
46
    public const PAYMENT_INFO_CC_TYPE = 'cc_type';
47
48
    /**
49
     * Cc Number - Payment Addtional Information.
50
     */
51
    public const PAYMENT_INFO_CC_NUMBER = 'cc_number';
52
53
    /**
54
     * Cc Owner - Payment Addtional Information.
55
     */
56
    public const PAYMENT_INFO_CC_OWNER = 'cc_holder_fullname';
57
58
    /**
59
     * Cc Exp Month - Payment Addtional Information.
60
     */
61
    public const PAYMENT_INFO_CC_EXP_MONTH = 'cc_exp_month';
62
63
    /**
64
     * Cc Exp Year - Payment Addtional Information.
65
     */
66
    public const PAYMENT_INFO_CC_EXP_YEAR = 'cc_exp_year';
67
68
    /**
69
     * Cc Number Token - Payment Addtional Information.
70
     */
71
    public const PAYMENT_INFO_CC_NUMBER_ENC = 'cc_hash';
72
73
    /**
74
     * Response Pay Credit - Block name.
75
     */
76
    public const CREDIT = 'credit';
77
78
    /**
79
     * Response Pay Payment Id - Block name.
80
     */
81
    public const RESPONSE_PAYMENT_ID = 'payment_id';
82
83
    /**
84
     * Response Pay Terminal NSU - Block name.
85
     */
86
    public const RESPONSE_TERMINAL_NSU = 'terminal_nsu';
87
88
    /**
89
     * Response Pay Authorization Code - Block name.
90
     */
91
    public const RESPONSE_AUTHORIZATION_CODE = 'authorization_code';
92
93
    /**
94
     * Response Pay Acquirer Transaction Id - Block name.
95
     */
96
    public const RESPONSE_ACQUIRER_TRANSACTION_ID = 'acquirer_transaction_id';
97
98
    /**
99
     * Response Pay Transaction Id - Block name.
100
     */
101
    public const RESPONSE_TRANSACTION_ID = 'transaction_id';
102
103
    /**
104
     * Response Pay Delayed - Block name.
105
     */
106
    public const RESPONSE_DELAYED = 'delayed';
107
108
    /**
109
     * Response Pay Status - Block name.
110
     */
111
    public const RESPONSE_STATUS = 'status';
112
113
    /**
114
     * Response Pay Approved - Block name.
115
     */
116
    public const RESPONSE_APPROVED = 'APPROVED';
117
118
    /**
119
     * Response Pay Authorized - Block name.
120
     */
121
    public const RESPONSE_AUTHORIZED = 'AUTHORIZED';
122
123
    /**
124
     * Response Pay Pending - Block name.
125
     */
126
    public const RESPONSE_PENDING = 'PENDING';
127
128
    /**
129
     * @var Json
130
     */
131
    private $json;
132
133
    /**
134
     * @var Config
135
     */
136
    private $config;
137
138
    /**
139
     * @var ConfigCc
140
     */
141
    private $configCc;
142
143
    /**
144
     * @param Json     $json
145
     * @param Config   $config
146
     * @param ConfigCc $configCc
147
     */
148
    public function __construct(
149
        Json $json,
150
        Config $config,
151
        ConfigCc $configCc
152
    ) {
153
        $this->json = $json;
154
        $this->config = $config;
155
        $this->configCc = $configCc;
156
    }
157
158
    /**
159
     * Handles.
160
     *
161
     * @param array $handlingSubject
162
     * @param array $response
163
     *
164
     * @return void
165
     */
166
    public function handle(array $handlingSubject, array $response)
167
    {
168
        if (!isset($handlingSubject['payment'])
169
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
170
        ) {
171
            throw new InvalidArgumentException('Payment data object should be provided');
172
        }
173
174
        $paymentDO = $handlingSubject['payment'];
175
176
        $payment = $paymentDO->getPayment();
177
178
        $payCredit = $response[self::CREDIT];
179
180
        $payment->setAdditionalInformation(
181
            self::PAYMENT_INFO_TERMINAL_NSU,
182
            $payCredit[self::RESPONSE_TERMINAL_NSU]
183
        );
184
185
        $payment->setAdditionalInformation(
186
            self::PAYMENT_INFO_AUTHORIZATION_CODE,
187
            $payCredit[self::RESPONSE_AUTHORIZATION_CODE]
188
        );
189
190
        $payment->setAdditionalInformation(
191
            self::PAYMENT_INFO_ACQUIRER_TRANSACTION_ID,
192
            $payCredit[self::RESPONSE_ACQUIRER_TRANSACTION_ID]
193
        );
194
195
        $payment->setAdditionalInformation(
196
            self::PAYMENT_INFO_TRANSACTION_ID,
197
            $payCredit[self::RESPONSE_TRANSACTION_ID]
198
        );
199
200
        $ccType = $payment->getAdditionalInformation(self::PAYMENT_INFO_CC_TYPE);
201
        if ($ccType) {
202
            $ccType = $this->getCreditCardType($ccType);
203
            $payment->setCcType($ccType);
204
        }
205
206
        $ccLast4 = $payment->getAdditionalInformation(self::PAYMENT_INFO_CC_NUMBER);
207
        $ccLast4 = preg_replace('/[^0-9]/', '', $ccLast4);
208
        $payment->setCcLast4($ccLast4);
209
210
        $ccOwner = $payment->getAdditionalInformation(self::PAYMENT_INFO_CC_OWNER);
211
        $payment->setCcOwner($ccOwner);
212
213
        $ccExpMonth = $payment->getAdditionalInformation(self::PAYMENT_INFO_CC_EXP_MONTH);
214
        $payment->setCcExpMonth($ccExpMonth);
215
216
        $ccExpYear = $payment->getAdditionalInformation(self::PAYMENT_INFO_CC_EXP_YEAR);
217
        $payment->setCcExpYear($ccExpYear);
218
219
        $ccNumberEnc = $payment->getAdditionalInformation(self::PAYMENT_INFO_CC_NUMBER_ENC);
220
        $payment->setCcNumberEnc($ccNumberEnc);
221
    }
222
223
    /**
224
     * Get type of credit card mapped from Getnet.
225
     *
226
     * @param string $type
227
     *
228
     * @return string
229
     */
230
    public function getCreditCardType(string $type): ?string
231
    {
232
        $mapper = $this->configCc->getCcTypesMapper();
233
234
        return $mapper[$type];
235
    }
236
}
237