|
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
|
|
|
|
|
114
|
|
|
if ($numberToken) { |
|
115
|
|
|
$vaultDetails = $this->getVaultDetails($storeId, $numberToken, $vaultData); |
|
116
|
|
|
$token['tokenize'] = $vaultDetails; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $token; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get Token Cc Number. |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $storeId |
|
126
|
|
|
* @param array $vaultData |
|
127
|
|
|
* |
|
128
|
|
|
* @return null|string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getTokenCcNumber($storeId, $vaultData) |
|
131
|
|
|
{ |
|
132
|
|
|
/** @var ZendClient $client */ |
|
133
|
|
|
$client = $this->httpClientFactory->create(); |
|
134
|
|
|
$request = ['card_number' => $vaultData['card_number']]; |
|
135
|
|
|
$url = $this->configBase->getApiUrl($storeId); |
|
136
|
|
|
$apiBearer = $this->configBase->getMerchantGatewayOauth($storeId); |
|
137
|
|
|
$response = null; |
|
138
|
|
|
|
|
139
|
|
|
try { |
|
140
|
|
|
$client->setUri($url.'/v1/tokens/card'); |
|
141
|
|
|
$client->setConfig(['maxredirects' => 0, 'timeout' => 45000]); |
|
142
|
|
|
$client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
|
143
|
|
|
$client->setRawData($this->json->serialize($request), 'application/json'); |
|
144
|
|
|
$client->setMethod(ZendClient::POST); |
|
145
|
|
|
|
|
146
|
|
|
$responseBody = $client->request()->getBody(); |
|
147
|
|
|
$data = $this->json->unserialize($responseBody); |
|
148
|
|
|
|
|
149
|
|
|
if (isset($data['number_token'])) { |
|
150
|
|
|
$response = $data['number_token']; |
|
151
|
|
|
} |
|
152
|
|
|
$this->logger->debug( |
|
153
|
|
|
[ |
|
154
|
|
|
'url' => $url.'v1/tokens/card', |
|
155
|
|
|
'response' => $responseBody, |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
} catch (InvalidArgumentException $e) { |
|
|
|
|
|
|
159
|
|
|
$this->logger->debug( |
|
160
|
|
|
[ |
|
161
|
|
|
'url' => $url.'v1/tokens/card', |
|
162
|
|
|
'response' => $responseBody, |
|
163
|
|
|
] |
|
164
|
|
|
); |
|
165
|
|
|
// phpcs:ignore Magento2.Exceptions.DirectThrow |
|
166
|
|
|
throw new Exception('Invalid JSON was returned by the gateway'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $response; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get Vault Details. |
|
174
|
|
|
* |
|
175
|
|
|
* @param int $storeId |
|
176
|
|
|
* @param string $numberToken |
|
177
|
|
|
* @param array $vaultData |
|
178
|
|
|
* |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getVaultDetails($storeId, $numberToken, $vaultData) |
|
182
|
|
|
{ |
|
183
|
|
|
/** @var ZendClient $client */ |
|
184
|
|
|
$client = $this->httpClientFactory->create(); |
|
185
|
|
|
$url = $this->configBase->getApiUrl($storeId); |
|
186
|
|
|
$apiBearer = $this->configBase->getMerchantGatewayOauth($storeId); |
|
187
|
|
|
|
|
188
|
|
|
$month = $vaultData['expiration_month']; |
|
189
|
|
|
if (strlen($month) === 1) { |
|
190
|
|
|
$month = '0'.$month; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$request = [ |
|
194
|
|
|
'number_token' => $numberToken, |
|
195
|
|
|
'expiration_month' => $month, |
|
196
|
|
|
'expiration_year' => $vaultData['expiration_year'], |
|
197
|
|
|
'customer_id' => $vaultData['customer_email'], |
|
198
|
|
|
'cardholder_name' => $vaultData['cardholder_name'], |
|
199
|
|
|
]; |
|
200
|
|
|
|
|
201
|
|
|
try { |
|
202
|
|
|
$client->setUri($url.'/v1/cards'); |
|
203
|
|
|
$client->setConfig(['maxredirects' => 0, 'timeout' => 45000]); |
|
204
|
|
|
$client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
|
205
|
|
|
$client->setRawData($this->json->serialize($request), 'application/json'); |
|
206
|
|
|
$client->setMethod(ZendClient::POST); |
|
207
|
|
|
|
|
208
|
|
|
$responseBody = $client->request()->getBody(); |
|
209
|
|
|
$data = $this->json->unserialize($responseBody); |
|
210
|
|
|
$response = [ |
|
211
|
|
|
'success' => 0, |
|
212
|
|
|
]; |
|
213
|
|
|
if (isset($data['card_id'])) { |
|
214
|
|
|
$response = [ |
|
215
|
|
|
'success' => 1, |
|
216
|
|
|
'card_id' => $data['card_id'], |
|
217
|
|
|
'number_token' => $data['number_token'], |
|
218
|
|
|
]; |
|
219
|
|
|
} |
|
220
|
|
|
$this->logger->debug( |
|
221
|
|
|
[ |
|
222
|
|
|
'url' => $url.'v1/cards', |
|
223
|
|
|
'request' => $this->json->serialize($request), |
|
224
|
|
|
'response' => $responseBody, |
|
225
|
|
|
] |
|
226
|
|
|
); |
|
227
|
|
|
} catch (InvalidArgumentException $e) { |
|
228
|
|
|
$this->logger->debug( |
|
229
|
|
|
[ |
|
230
|
|
|
'url' => $url.'v1/cards', |
|
231
|
|
|
'request' => $request, |
|
232
|
|
|
'response' => $responseBody, |
|
233
|
|
|
] |
|
234
|
|
|
); |
|
235
|
|
|
// phpcs:ignore Magento2.Exceptions.DirectThrow |
|
236
|
|
|
throw new Exception('Invalid JSON was returned by the gateway'); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return $response; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths