DataAssignObserverCc::execute()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 32
rs 8.6506
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 DataAssignObserverCc - Capture credit card payment information.
18
 */
19
class DataAssignObserverCc 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_SAVE = 'is_active_payment_token_enabler';
70
71
    /**
72
     * @const string
73
     */
74
    public const PAYMENT_INFO_CC_CID = 'cc_cid';
75
76
    /**
77
     * @const string
78
     */
79
    public const PAYMENT_INFO_CC_PUBLIC_ID = 'cc_public_id';
80
    /**
81
     * @var array
82
     */
83
    protected $addInformationList = [
84
        self::PAYMENT_INFO_NUMBER_TOKEN,
85
        self::PAYMENT_INFO_CARDHOLDER_NAME,
86
        self::PAYMENT_INFO_CC_NUMBER,
87
        self::PAYMENT_INFO_CC_TYPE,
88
        self::PAYMENT_INFO_CC_CID,
89
        self::PAYMENT_INFO_CC_EXP_M,
90
        self::PAYMENT_INFO_CC_EXP_Y,
91
        self::PAYMENT_INFO_CC_INSTALLMENTS,
92
        self::PAYMENT_INFO_CC_SAVE,
93
        self::PAYMENT_INFO_HOLDER_TAX_DOCUMENT,
94
        self::PAYMENT_INFO_HOLDER_PHONE,
95
        self::PAYMENT_INFO_CC_PUBLIC_ID,
96
    ];
97
98
    /**
99
     * @var ConfigCc
100
     */
101
    protected $config;
102
103
    /**
104
     * @param ConfigCc $config
105
     */
106
    public function __construct(
107
        ConfigCc $config
108
    ) {
109
        $this->config = $config;
110
    }
111
112
    /**
113
     * Execute.
114
     *
115
     * @param Observer $observer
116
     *
117
     * @return void
118
     */
119
    public function execute(Observer $observer)
120
    {
121
        $data = $this->readDataArgument($observer);
122
123
        $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
124
125
        if (!is_array($additionalData)) {
126
            return;
127
        }
128
129
        $paymentInfo = $this->readPaymentModelArgument($observer);
130
131
        foreach ($this->addInformationList as $addInformationKey) {
132
            if (isset($additionalData[$addInformationKey])) {
133
                if ($addInformationKey === self::PAYMENT_INFO_CC_TYPE) {
134
                    $paymentInfo->setAdditionalInformation(
135
                        $addInformationKey,
136
                        $this->getFullTypeName($additionalData[$addInformationKey])
137
                    );
138
                    continue;
139
                }
140
                if ($addInformationKey === self::PAYMENT_INFO_CC_NUMBER) {
141
                    $paymentInfo->setAdditionalInformation(
142
                        $addInformationKey,
143
                        'xxxx xxxx xxxx '.substr($additionalData[$addInformationKey], -4)
144
                    );
145
                    continue;
146
                }
147
                if ($additionalData[$addInformationKey]) {
148
                    $paymentInfo->setAdditionalInformation(
149
                        $addInformationKey,
150
                        $additionalData[$addInformationKey]
151
                    );
152
                }
153
            }
154
        }
155
    }
156
157
    /**
158
     * Get Name for Cc Type.
159
     *
160
     * @param string $type
161
     *
162
     * @return string
163
     */
164
    public function getFullTypeName(string $type): string
165
    {
166
        $mapper = $this->config->getCcTypesMapper();
167
        $type = array_search($type, $mapper);
168
169
        return  ucfirst($type);
170
    }
171
}
172