Passed
Branch [email protected] (3810d0)
by Bruno
11:09
created

DataAssignObserverTwoCc::execute()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 34
rs 8.0555
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\Observer;
10
11
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
12
use Magento\Framework\Event\Observer;
13
use Magento\Payment\Observer\AbstractDataAssignObserver;
14
use Magento\Quote\Api\Data\PaymentInterface;
15
16
/**
17
 * Class DataAssignObserverTwoCc - Capture credit card payment information.
18
 */
19
class DataAssignObserverTwoCc extends AbstractDataAssignObserver
20
{
21
    /**
22
     * @const string
23
     */
24
    public const PAYMENT_INFO_NUMBER_TOKEN = 'cc_number_token';
25
26
    /**
27
     * @const string
28
     */
29
    public const PAYMENT_INFO_CC_NUMBER = 'cc_number';
30
31
    /**
32
     * @const string
33
     */
34
    public const PAYMENT_INFO_CC_TYPE = 'cc_type';
35
36
    /**
37
     * @const string
38
     */
39
    public const PAYMENT_INFO_CC_EXP_M = 'cc_exp_month';
40
41
    /**
42
     * @const string
43
     */
44
    public const PAYMENT_INFO_CC_EXP_Y = 'cc_exp_year';
45
46
    /**
47
     * @const string
48
     */
49
    public const PAYMENT_INFO_CC_INSTALLMENTS = 'cc_installments';
50
51
    /**
52
     * @const string
53
     */
54
    public const PAYMENT_INFO_CARDHOLDER_NAME = 'cc_cardholder_name';
55
56
    /**
57
     * @const string
58
     */
59
    public const PAYMENT_INFO_HOLDER_TAX_DOCUMENT = 'cc_holder_tax_document';
60
61
    /**
62
     * @const string
63
     */
64
    public const PAYMENT_INFO_HOLDER_PHONE = 'cc_holder_phone';
65
66
    /**
67
     * @const string
68
     */
69
    public const PAYMENT_INFO_CC_CID = 'cc_cid';
70
71
    /**
72
     * @const string
73
     */
74
    public const PAYMENT_INFO_CC_SECONDARY_NUMBER_TOKEN = 'cc_secondary_number_token';
75
76
    /**
77
     * @const string
78
     */
79
    public const PAYMENT_INFO_CC_SECONDARY_NUMBER = 'cc_secondary_number';
80
81
    /**
82
     * @const string
83
     */
84
    public const PAYMENT_INFO_CC_SECONDARY_TYPE = 'cc_secondary_type';
85
86
    /**
87
     * @const string
88
     */
89
    public const PAYMENT_INFO_CC_SECONDARY_EXP_M = 'cc_secondary_exp_month';
90
91
    /**
92
     * @const string
93
     */
94
    public const PAYMENT_INFO_CC_SECONDARY_EXP_Y = 'cc_secondary_exp_year';
95
96
    /**
97
     * @const string
98
     */
99
    public const PAYMENT_INFO_CC_SECONDARY_INSTALLMENTS = 'cc_secondary_installments';
100
101
    /**
102
     * @const string
103
     */
104
    public const PAYMENT_INFO_CC_SECONDARY_CARDHOLDER_NAME = 'cc_secondary_cardholder_name';
105
106
    /**
107
     * @const string
108
     */
109
    public const PAYMENT_INFO_CC_SECONDARY_CID = 'cc_secondary_cid';
110
111
    /**
112
     * @const string
113
     */
114
    public const PAYMENT_INFO_CC_FIRST_AMOUNT = 'cc_payment_first_amount';
115
116
    /**
117
     * @var array
118
     */
119
    protected $addInformationList = [
120
        self::PAYMENT_INFO_NUMBER_TOKEN,
121
        self::PAYMENT_INFO_CARDHOLDER_NAME,
122
        self::PAYMENT_INFO_CC_NUMBER,
123
        self::PAYMENT_INFO_CC_TYPE,
124
        self::PAYMENT_INFO_CC_CID,
125
        self::PAYMENT_INFO_CC_EXP_M,
126
        self::PAYMENT_INFO_CC_EXP_Y,
127
        self::PAYMENT_INFO_CC_INSTALLMENTS,
128
        self::PAYMENT_INFO_HOLDER_TAX_DOCUMENT,
129
        self::PAYMENT_INFO_HOLDER_PHONE,
130
        self::PAYMENT_INFO_CC_SECONDARY_NUMBER_TOKEN,
131
        self::PAYMENT_INFO_CC_SECONDARY_CARDHOLDER_NAME,
132
        self::PAYMENT_INFO_CC_SECONDARY_NUMBER,
133
        self::PAYMENT_INFO_CC_SECONDARY_TYPE,
134
        self::PAYMENT_INFO_CC_SECONDARY_CID,
135
        self::PAYMENT_INFO_CC_SECONDARY_EXP_M,
136
        self::PAYMENT_INFO_CC_SECONDARY_EXP_Y,
137
        self::PAYMENT_INFO_CC_SECONDARY_INSTALLMENTS,
138
        self::PAYMENT_INFO_CC_FIRST_AMOUNT,
139
    ];
140
141
    /**
142
     * @var ConfigCc
143
     */
144
    protected $config;
145
146
    /**
147
     * @param ConfigCc $config
148
     */
149
    public function __construct(
150
        ConfigCc $config
151
    ) {
152
        $this->config = $config;
153
    }
154
155
    /**
156
     * Execute.
157
     *
158
     * @param Observer $observer
159
     *
160
     * @return void
161
     */
162
    public function execute(Observer $observer)
163
    {
164
        $data = $this->readDataArgument($observer);
165
166
        $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
167
168
        if (!is_array($additionalData)) {
169
            return;
170
        }
171
172
        $paymentInfo = $this->readPaymentModelArgument($observer);
173
174
        foreach ($this->addInformationList as $addInformationKey) {
175
            if (isset($additionalData[$addInformationKey])) {
176
                if ($addInformationKey === self::PAYMENT_INFO_CC_TYPE
177
                    || $addInformationKey === self::PAYMENT_INFO_CC_SECONDARY_TYPE) {
178
                    $paymentInfo->setAdditionalInformation(
179
                        $addInformationKey,
180
                        $this->getFullTypeName($additionalData[$addInformationKey])
181
                    );
182
                    continue;
183
                }
184
                if ($addInformationKey === self::PAYMENT_INFO_CC_NUMBER
185
                    || $addInformationKey === self::PAYMENT_INFO_CC_SECONDARY_NUMBER) {
186
                    $paymentInfo->setAdditionalInformation(
187
                        $addInformationKey,
188
                        'xxxx xxxx xxxx '.substr($additionalData[$addInformationKey], -4)
189
                    );
190
                    continue;
191
                }
192
                if ($additionalData[$addInformationKey]) {
193
                    $paymentInfo->setAdditionalInformation(
194
                        $addInformationKey,
195
                        $additionalData[$addInformationKey]
196
                    );
197
                }
198
            }
199
        }
200
    }
201
202
    /**
203
     * Get Name for Cc Type.
204
     *
205
     * @param string $type
206
     *
207
     * @return string
208
     */
209
    public function getFullTypeName(string $type): string
210
    {
211
        $mapper = $this->config->getCcTypesMapper();
212
        $type = array_search($type, $mapper);
213
214
        return  ucfirst($type);
215
    }
216
}
217