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 Cc Payment Data Request - Payment data structure for Credit Card. |
||
21 | */ |
||
22 | class CcPaymentDataRequest implements BuilderInterface |
||
23 | { |
||
24 | /** |
||
25 | * Credit - Block name. |
||
26 | */ |
||
27 | public const CREDIT = 'credit'; |
||
28 | |||
29 | /** |
||
30 | * Credit Delayed - Block name. |
||
31 | */ |
||
32 | public const DELAYED = 'delayed'; |
||
33 | |||
34 | /** |
||
35 | * Save Card Data - Block name. |
||
36 | */ |
||
37 | public const TRANSACTION_TYPE = 'transaction_type'; |
||
38 | |||
39 | /** |
||
40 | * Number Installment - Block name. |
||
41 | */ |
||
42 | public const NUMBER_INSTALLMENTS = 'number_installments'; |
||
43 | |||
44 | /** |
||
45 | * Statement descriptor - Invoice description. |
||
46 | */ |
||
47 | public const SOFT_DESCRIPTOR = 'soft_descriptor'; |
||
48 | |||
49 | /** |
||
50 | * Credit card - Block name. |
||
51 | */ |
||
52 | public const CREDIT_CARD = 'card'; |
||
53 | |||
54 | /** |
||
55 | * Credit Card Brand - Block Name. |
||
56 | */ |
||
57 | public const CREDIT_CARD_BRAND = 'brand'; |
||
58 | |||
59 | /** |
||
60 | * Credit card store - Block Name. |
||
61 | */ |
||
62 | public const CREDIT_CARD_STORE = 'store'; |
||
63 | |||
64 | /** |
||
65 | * Dynamic Mcc block name. |
||
66 | */ |
||
67 | public const DYNAMIC_MCC = 'dynamic_mcc'; |
||
68 | |||
69 | /** |
||
70 | * Credit card Holder Name - Block name. |
||
71 | */ |
||
72 | public const CREDIT_CARDHOLDER_NAME = 'cardholder_name'; |
||
73 | |||
74 | /** |
||
75 | * Credit card Number Token - Block Name. |
||
76 | */ |
||
77 | public const CREDIT_NUMBER_TOKEN = 'number_token'; |
||
78 | |||
79 | /** |
||
80 | * Credit card CVV - Block Name. |
||
81 | */ |
||
82 | public const CREDIT_CID = 'security_code'; |
||
83 | |||
84 | /** |
||
85 | * Credit card Expiration Month - Block name. |
||
86 | */ |
||
87 | public const CREDIT_MONTH = 'expiration_month'; |
||
88 | |||
89 | /** |
||
90 | * Credit card Expiration Year - Block name. |
||
91 | */ |
||
92 | public const CREDIT_YEAR = 'expiration_year'; |
||
93 | |||
94 | /** |
||
95 | * @var SubjectReader |
||
96 | */ |
||
97 | protected $subjectReader; |
||
98 | |||
99 | /** |
||
100 | * @var Config |
||
101 | */ |
||
102 | protected $config; |
||
103 | |||
104 | /** |
||
105 | * @var ConfigCc |
||
106 | */ |
||
107 | protected $configCc; |
||
108 | |||
109 | /** |
||
110 | * @param SubjectReader $subjectReader |
||
111 | * @param Config $config |
||
112 | * @param ConfigCc $configCc |
||
113 | */ |
||
114 | public function __construct( |
||
115 | SubjectReader $subjectReader, |
||
116 | Config $config, |
||
117 | ConfigCc $configCc |
||
118 | ) { |
||
119 | $this->subjectReader = $subjectReader; |
||
120 | $this->config = $config; |
||
121 | $this->configCc = $configCc; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Build. |
||
126 | * |
||
127 | * @param array $buildSubject |
||
128 | */ |
||
129 | public function build(array $buildSubject) |
||
130 | { |
||
131 | if (!isset($buildSubject['payment']) |
||
132 | || !$buildSubject['payment'] instanceof PaymentDataObjectInterface |
||
133 | ) { |
||
134 | throw new InvalidArgumentException('Payment data object should be provided'); |
||
135 | } |
||
136 | |||
137 | $paymentDO = $buildSubject['payment']; |
||
138 | $payment = $paymentDO->getPayment(); |
||
139 | $order = $paymentDO->getOrder(); |
||
140 | $storeId = $order->getStoreId(); |
||
141 | $result = []; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
142 | |||
143 | $result = $this->getDataPaymetCc($payment, $storeId); |
||
144 | |||
145 | return $result; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Data for CC. |
||
150 | * |
||
151 | * @param InfoInterface $payment |
||
152 | * @param int $storeId |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function getDataPaymetCc($payment, $storeId) |
||
157 | { |
||
158 | $instruction = []; |
||
159 | $installment = $payment->getAdditionalInformation('cc_installments') ?: 1; |
||
160 | $transactionType = $this->configCc->getTypeInterestByInstallment($installment, $storeId); |
||
161 | $month = $payment->getAdditionalInformation('cc_exp_month'); |
||
162 | if (strlen($month) === 1) { |
||
163 | $month = '0'.$month; |
||
164 | } |
||
165 | $year = str_replace('20', '', $payment->getAdditionalInformation('cc_exp_year')); |
||
166 | |||
167 | $instruction[self::CREDIT] = [ |
||
168 | self::DELAYED => $this->configCc->hasDelayed($storeId), |
||
169 | self::TRANSACTION_TYPE => $transactionType, |
||
170 | self::NUMBER_INSTALLMENTS => $installment, |
||
171 | self::SOFT_DESCRIPTOR => $this->config->getStatementDescriptor($storeId), |
||
172 | self::DYNAMIC_MCC => $this->config->getMerchantGatewayDynamicMcc($storeId), |
||
173 | self::CREDIT_CARD => [ |
||
174 | self::CREDIT_NUMBER_TOKEN => $payment->getAdditionalInformation('cc_number_token'), |
||
175 | self::CREDIT_CARD_BRAND => $payment->getAdditionalInformation('cc_type'), |
||
176 | self::CREDIT_CARDHOLDER_NAME => $payment->getAdditionalInformation('cc_cardholder_name'), |
||
177 | self::CREDIT_CID => $payment->getAdditionalInformation('cc_cid'), |
||
178 | self::CREDIT_MONTH => $month, |
||
179 | self::CREDIT_YEAR => $year, |
||
180 | ], |
||
181 | ]; |
||
182 | $payment->unsAdditionalInformation('cc_cid'); |
||
183 | |||
184 | return $instruction; |
||
185 | } |
||
186 | } |
||
187 |