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 | declare(strict_types=1); |
||
10 | |||
11 | namespace Getnet\PaymentMagento\Model; |
||
12 | |||
13 | use Exception; |
||
14 | use Getnet\PaymentMagento\Api\CreateVaultManagementInterface; |
||
15 | use Getnet\PaymentMagento\Gateway\Config\Config as ConfigBase; |
||
16 | use Magento\Framework\Exception\CouldNotSaveException; |
||
17 | use Magento\Framework\Exception\NoSuchEntityException; |
||
18 | use Magento\Framework\HTTP\ZendClient; |
||
19 | use Magento\Framework\HTTP\ZendClientFactory; |
||
20 | use Magento\Framework\Serialize\Serializer\Json; |
||
21 | use Magento\Payment\Gateway\ConfigInterface; |
||
22 | use Magento\Payment\Model\Method\Logger; |
||
23 | use Magento\Quote\Api\CartRepositoryInterface; |
||
24 | use Magento\Quote\Api\Data\CartInterface as QuoteCartInterface; |
||
25 | |||
26 | /** |
||
27 | * Class Create Vault Management - Generate number token by card number in API Cofre. |
||
28 | * |
||
29 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
||
30 | */ |
||
31 | class CreateVaultManagement implements CreateVaultManagementInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var Logger |
||
35 | */ |
||
36 | private $logger; |
||
37 | |||
38 | /** |
||
39 | * @var CartRepositoryInterface |
||
40 | */ |
||
41 | protected $quoteRepository; |
||
42 | |||
43 | /** |
||
44 | * @var ConfigInterface |
||
45 | */ |
||
46 | private $config; |
||
47 | |||
48 | /** |
||
49 | * @var ConfigBase |
||
50 | */ |
||
51 | private $configBase; |
||
52 | |||
53 | /** |
||
54 | * @var ZendClientFactory |
||
55 | */ |
||
56 | private $httpClientFactory; |
||
57 | |||
58 | /** |
||
59 | * @var Json |
||
60 | */ |
||
61 | private $json; |
||
62 | |||
63 | /** |
||
64 | * CreateVaultManagement constructor. |
||
65 | * |
||
66 | * @param Logger $logger |
||
67 | * @param CartRepositoryInterface $quoteRepository |
||
68 | * @param ConfigInterface $config |
||
69 | * @param ConfigBase $configBase |
||
70 | * @param ZendClientFactory $httpClientFactory |
||
71 | * @param Json $json |
||
72 | */ |
||
73 | public function __construct( |
||
74 | Logger $logger, |
||
75 | CartRepositoryInterface $quoteRepository, |
||
76 | ConfigInterface $config, |
||
77 | ConfigBase $configBase, |
||
78 | ZendClientFactory $httpClientFactory, |
||
79 | Json $json |
||
80 | ) { |
||
81 | $this->logger = $logger; |
||
82 | $this->quoteRepository = $quoteRepository; |
||
83 | $this->config = $config; |
||
84 | $this->configBase = $configBase; |
||
85 | $this->httpClientFactory = $httpClientFactory; |
||
86 | $this->json = $json; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Create Vault Card Id. |
||
91 | * |
||
92 | * @param int $cartId |
||
93 | * @param array $vaultData |
||
94 | * |
||
95 | * @throws CouldNotSaveException |
||
96 | * @throws NoSuchEntityException |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function createVault( |
||
101 | $cartId, |
||
102 | $vaultData |
||
103 | ) { |
||
104 | $token = []; |
||
105 | $quote = $this->quoteRepository->getActive($cartId); |
||
106 | if (!$quote->getItemsCount()) { |
||
107 | throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId)); |
||
108 | } |
||
109 | |||
110 | $storeId = $quote->getData(QuoteCartInterface::KEY_STORE_ID); |
||
111 | |||
112 | $numberToken = $this->getTokenCcNumber($storeId, $vaultData); |
||
113 | $token['tokenize'] = [ |
||
114 | 'error' => __('Error creating payment, please try again later. COD: 401'), |
||
115 | ]; |
||
116 | if ($numberToken) { |
||
117 | unset($token); |
||
118 | $vaultDetails = $this->getVaultDetails($storeId, $numberToken, $vaultData); |
||
119 | $token['tokenize'] = $vaultDetails; |
||
120 | } |
||
121 | |||
122 | return $token; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get Token Cc Number. |
||
127 | * |
||
128 | * @param int $storeId |
||
129 | * @param array $vaultData |
||
130 | * |
||
131 | * @return null|string |
||
132 | */ |
||
133 | public function getTokenCcNumber($storeId, $vaultData) |
||
134 | { |
||
135 | /** @var ZendClient $client */ |
||
136 | $client = $this->httpClientFactory->create(); |
||
137 | $request = ['card_number' => $vaultData['card_number']]; |
||
138 | $url = $this->configBase->getApiUrl($storeId); |
||
139 | $apiBearer = $this->configBase->getMerchantGatewayOauth($storeId); |
||
140 | $response = null; |
||
141 | |||
142 | try { |
||
143 | $client->setUri($url.'/v1/tokens/card'); |
||
144 | $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]); |
||
145 | $client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
||
146 | $client->setRawData($this->json->serialize($request), 'application/json'); |
||
147 | $client->setMethod(ZendClient::POST); |
||
148 | |||
149 | $responseBody = $client->request()->getBody(); |
||
150 | $data = $this->json->unserialize($responseBody); |
||
151 | |||
152 | if (isset($data['number_token'])) { |
||
153 | $response = $data['number_token']; |
||
154 | } |
||
155 | $this->logger->debug( |
||
156 | [ |
||
157 | 'url' => $url.'v1/tokens/card', |
||
158 | 'response' => $responseBody, |
||
159 | ] |
||
160 | ); |
||
161 | } catch (InvalidArgumentException $e) { |
||
162 | $this->logger->debug( |
||
163 | [ |
||
164 | 'url' => $url.'v1/tokens/card', |
||
165 | 'response' => $responseBody, |
||
166 | ] |
||
167 | ); |
||
168 | // phpcs:ignore Magento2.Exceptions.DirectThrow |
||
169 | throw new Exception('Invalid JSON was returned by the gateway'); |
||
170 | } |
||
171 | |||
172 | return $response; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get Vault Details. |
||
177 | * |
||
178 | * @param int $storeId |
||
179 | * @param string $numberToken |
||
180 | * @param array $vaultData |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function getVaultDetails($storeId, $numberToken, $vaultData) |
||
185 | { |
||
186 | /** @var ZendClient $client */ |
||
187 | $client = $this->httpClientFactory->create(); |
||
188 | $url = $this->configBase->getApiUrl($storeId); |
||
189 | $apiBearer = $this->configBase->getMerchantGatewayOauth($storeId); |
||
190 | |||
191 | $month = $vaultData['expiration_month']; |
||
192 | if (strlen($month) === 1) { |
||
193 | $month = '0'.$month; |
||
194 | } |
||
195 | |||
196 | $request = [ |
||
197 | 'number_token' => $numberToken, |
||
198 | 'expiration_month' => $month, |
||
199 | 'expiration_year' => $vaultData['expiration_year'], |
||
200 | 'customer_id' => $vaultData['customer_email'], |
||
201 | 'cardholder_name' => $vaultData['cardholder_name'], |
||
202 | 'verify_card' => false, |
||
203 | ]; |
||
204 | |||
205 | try { |
||
206 | $client->setUri($url.'/v1/cards'); |
||
207 | $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]); |
||
208 | $client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
||
209 | $client->setRawData($this->json->serialize($request), 'application/json'); |
||
210 | $client->setMethod(ZendClient::POST); |
||
211 | |||
212 | $responseBody = $client->request()->getBody(); |
||
213 | $data = $this->json->unserialize($responseBody); |
||
214 | |||
215 | if (isset($data['card_id'])) { |
||
216 | $response = [ |
||
217 | 'success' => 1, |
||
218 | 'card_id' => $data['card_id'], |
||
219 | 'number_token' => $data['number_token'], |
||
220 | ]; |
||
221 | } |
||
222 | |||
223 | if (isset($data['details'])) { |
||
224 | $response = [ |
||
225 | 'success' => 0, |
||
226 | 'message' => [ |
||
227 | 'code' => $data['details'][0]['error_code'], |
||
228 | 'text' => $data['details'][0]['description_detail'], |
||
229 | ], |
||
230 | ]; |
||
231 | } |
||
232 | |||
233 | $this->logger->debug( |
||
234 | [ |
||
235 | 'url' => $url.'v1/cards', |
||
236 | 'request' => $this->json->serialize($request), |
||
237 | 'response' => $responseBody, |
||
238 | ] |
||
239 | ); |
||
240 | } catch (InvalidArgumentException $e) { |
||
241 | $this->logger->debug( |
||
242 | [ |
||
243 | 'url' => $url.'v1/cards', |
||
244 | 'request' => $request, |
||
245 | 'response' => $responseBody, |
||
246 | ] |
||
247 | ); |
||
248 | // phpcs:ignore Magento2.Exceptions.DirectThrow |
||
249 | throw new Exception('Invalid JSON was returned by the gateway'); |
||
250 | } |
||
251 | |||
252 | return $response; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
253 | } |
||
254 | } |
||
255 |