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

TwoCcPaymentDataRequest::build()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 20
rs 9.8333
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\Request;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use Getnet\PaymentMagento\Gateway\SubjectReader;
14
use InvalidArgumentException;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Request\BuilderInterface;
17
use Magento\Payment\Model\InfoInterface;
18
19
/**
20
 * Class Two Cc Payment Data Request - Payment data structure for Credit Card.
21
 */
22
class TwoCcPaymentDataRequest implements BuilderInterface
23
{
24
    /**
25
     * Payments - Block name.
26
     */
27
    public const PAYMENTS = 'payments';
28
29
    /**
30
     * Type - Block name.
31
     */
32
    public const TYPE = 'type';
33
34
    /**
35
     * Amount - Block name.
36
     */
37
    public const AMOUNT = 'amount';
38
39
    /**
40
     * Currency - Block name.
41
     */
42
    public const CURRENCY = 'currency';
43
44
    /**
45
     * Credit - Block name.
46
     */
47
    public const CREDIT = 'CREDIT';
48
49
    /**
50
     * Credit Delayed - Block name.
51
     */
52
    public const DELAYED = 'delayed';
53
54
    /**
55
     * Save Card Data - Block name.
56
     */
57
    public const TRANSACTION_TYPE = 'transaction_type';
58
59
    /**
60
     * Number Installment - Block name.
61
     */
62
    public const NUMBER_INSTALLMENTS = 'number_installments';
63
64
    /**
65
     * Statement descriptor - Invoice description.
66
     */
67
    public const SOFT_DESCRIPTOR = 'soft_descriptor';
68
69
    /**
70
     * Payment Tag - Invoice description.
71
     */
72
    public const PAYMENT_TAG = 'payment_tag';
73
74
    /**
75
     * Credit card - Block name.
76
     */
77
    public const CREDIT_CARD = 'card';
78
79
    /**
80
     * Credit card store - Block Name.
81
     */
82
    public const CREDIT_CARD_STORE = 'store';
83
84
    /**
85
     * Credit card Holder Name - Block name.
86
     */
87
    public const CREDIT_CARDHOLDER_NAME = 'cardholder_name';
88
89
    /**
90
     * Credit Card Brand - Block Name.
91
     */
92
    public const CREDIT_CARD_BRAND = 'brand';
93
94
    /**
95
     * Credit card Number Token - Block Name.
96
     */
97
    public const CREDIT_NUMBER_TOKEN = 'number_token';
98
99
    /**
100
     * Credit card CVV - Block Name.
101
     */
102
    public const CREDIT_CID = 'security_code';
103
104
    /**
105
     * Credit card Expiration Month - Block name.
106
     */
107
    public const CREDIT_MONTH = 'expiration_month';
108
109
    /**
110
     * Credit card Expiration Year - Block name.
111
     */
112
    public const CREDIT_YEAR = 'expiration_year';
113
114
    /**
115
     * Save Card Data - Block name.
116
     */
117
    public const SAVE_CARD_DATA = 'save_card_data';
118
119
    /**
120
     * @var SubjectReader
121
     */
122
    protected $subjectReader;
123
124
    /**
125
     * @var Config
126
     */
127
    protected $config;
128
129
    /**
130
     * @var ConfigCc
131
     */
132
    protected $configCc;
133
134
    /**
135
     * @param SubjectReader $subjectReader
136
     * @param Config        $config
137
     * @param ConfigCc      $configCc
138
     */
139
    public function __construct(
140
        SubjectReader $subjectReader,
141
        Config $config,
142
        ConfigCc $configCc
143
    ) {
144
        $this->subjectReader = $subjectReader;
145
        $this->config = $config;
146
        $this->configCc = $configCc;
147
    }
148
149
    /**
150
     * Build.
151
     *
152
     * @param array $buildSubject
153
     */
154
    public function build(array $buildSubject)
155
    {
156
        if (!isset($buildSubject['payment'])
157
            || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
158
        ) {
159
            throw new InvalidArgumentException('Payment data object should be provided');
160
        }
161
162
        $paymentDO = $buildSubject['payment'];
163
        $payment = $paymentDO->getPayment();
164
        $order = $paymentDO->getOrder();
165
        $storeId = $order->getStoreId();
166
        $incrementId = $order->getOrderIncrementId();
167
        $currency = $order->getCurrencyCode();
168
        $result = [];
169
170
        $result[self::PAYMENTS][] = $this->getDataPaymentCc($payment, $incrementId, $currency, $storeId);
171
        $result[self::PAYMENTS][] = $this->getDataSecondaryPaymentCc($payment, $incrementId, $currency, $storeId);
172
173
        return $result;
174
    }
175
176
    /**
177
     * Data for CC.
178
     *
179
     * @param InfoInterface $payment
180
     * @param string        $incrementId
181
     * @param string        $currency
182
     * @param int           $storeId
183
     *
184
     * @return array
185
     */
186
    public function getDataPaymentCc($payment, $incrementId, $currency, $storeId)
187
    {
188
        $payments = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $payments is dead and can be removed.
Loading history...
189
190
        $totalFirst = $payment->getAdditionalInformation('cc_payment_first_amount');
191
        $installment = $payment->getAdditionalInformation('cc_installments') ?: 1;
192
        $transactionType = $this->configCc->getTypeInterestByInstallment($installment, $storeId);
193
        $month = $payment->getAdditionalInformation('cc_exp_month');
194
        if (strlen($month) === 1) {
195
            $month = '0'.$month;
196
        }
197
        $year = str_replace('20', '', $payment->getAdditionalInformation('cc_exp_year'));
198
199
        $payments = [
200
            self::TYPE                 => self::CREDIT,
201
            self::AMOUNT               => $this->config->formatPrice($totalFirst),
202
            self::CURRENCY             => $currency,
203
            self::SAVE_CARD_DATA       => false,
204
            self::TRANSACTION_TYPE     => $transactionType,
205
            self::NUMBER_INSTALLMENTS  => $installment,
206
            self::SOFT_DESCRIPTOR      => $this->config->getStatementDescriptor($storeId),
207
            self::PAYMENT_TAG          => $incrementId.'-1',
208
            self::CREDIT_CARD          => [
209
                self::CREDIT_CARD_BRAND       => $payment->getAdditionalInformation('cc_type'),
210
                self::CREDIT_NUMBER_TOKEN     => $payment->getAdditionalInformation('cc_number_token'),
211
                self::CREDIT_CARDHOLDER_NAME  => $payment->getAdditionalInformation('cc_cardholder_name'),
212
                self::CREDIT_CID              => $payment->getAdditionalInformation('cc_cid'),
213
                self::CREDIT_MONTH            => $month,
214
                self::CREDIT_YEAR             => $year,
215
            ],
216
        ];
217
        $payment->unsAdditionalInformation('cc_cid');
218
219
        return $payments;
220
    }
221
222
    /**
223
     * Data for Secondary CC.
224
     *
225
     * @param InfoInterface $payment
226
     * @param string        $incrementId
227
     * @param string        $currency
228
     * @param int           $storeId
229
     *
230
     * @return array
231
     */
232
    public function getDataSecondaryPaymentCc(
233
        $payment,
234
        $incrementId,
235
        $currency,
236
        $storeId
237
    ) {
238
        $payments = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $payments is dead and can be removed.
Loading history...
239
        $totalSecondary = $payment->getAdditionalInformation('cc_payment_secondary_amount');
240
        $installmentSecondary = $payment->getAdditionalInformation('cc_secondary_installments') ?: 1;
241
        $transTypeSecondary = $this->configCc->getTypeInterestByInstallment($installmentSecondary, $storeId);
242
        $monthSecondary = $payment->getAdditionalInformation('cc_secondary_exp_month');
243
        if (strlen($monthSecondary) === 1) {
244
            $monthSecondary = '0'.$monthSecondary;
245
        }
246
        $yearSecondary = str_replace('20', '', $payment->getAdditionalInformation('cc_secondary_exp_year'));
247
248
        $payments = [
249
            self::TYPE                 => self::CREDIT,
250
            self::AMOUNT               => $this->config->formatPrice($totalSecondary),
251
            self::CURRENCY             => $currency,
252
            self::SAVE_CARD_DATA       => false,
253
            self::TRANSACTION_TYPE     => $transTypeSecondary,
254
            self::NUMBER_INSTALLMENTS  => $installmentSecondary,
255
            self::SOFT_DESCRIPTOR      => $this->config->getStatementDescriptor($storeId),
256
            self::PAYMENT_TAG          => $incrementId.'-2',
257
            self::CREDIT_CARD          => [
258
                self::CREDIT_CARD_BRAND       => $payment->getAdditionalInformation('cc_secondary_type'),
259
                self::CREDIT_NUMBER_TOKEN     => $payment->getAdditionalInformation('cc_secondary_number_token'),
260
                self::CREDIT_CARDHOLDER_NAME  => $payment->getAdditionalInformation('cc_secondary_cardholder_name'),
261
                self::CREDIT_CID              => $payment->getAdditionalInformation('cc_secondary_cid'),
262
                self::CREDIT_MONTH            => $monthSecondary,
263
                self::CREDIT_YEAR             => $yearSecondary,
264
            ],
265
        ];
266
267
        $payment->unsAdditionalInformation('cc_secondary_cid');
268
269
        return $payments;
270
    }
271
}
272