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 Getnet\PaymentMagento\Api\Data\NumberTokenInterface; |
14
|
|
|
use Getnet\PaymentMagento\Api\NumberTokenManagementInterface; |
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 Number Token Management - Generate number token by card number. |
28
|
|
|
* |
29
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
30
|
|
|
*/ |
31
|
|
|
class NumberTokenManagement implements NumberTokenManagementInterface |
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
|
|
|
* NumberTokenManagement 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
|
|
|
* Generate Number Token by Card Number. |
91
|
|
|
* |
92
|
|
|
* @param int $cartId |
93
|
|
|
* @param \Getnet\PaymentMagento\Api\Data\NumberTokenInterface $cardNumber |
94
|
|
|
* |
95
|
|
|
* @throws CouldNotSaveException |
96
|
|
|
* @throws NoSuchEntityException |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function generateNumberToken( |
101
|
|
|
$cartId, |
102
|
|
|
NumberTokenInterface $cardNumber |
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
|
|
|
$cardNumber = $cardNumber->getCardNumber(); |
111
|
|
|
|
112
|
|
|
$storeId = $quote->getData(QuoteCartInterface::KEY_STORE_ID); |
113
|
|
|
|
114
|
|
|
$numberToken = $this->getNumberToken($storeId, $cardNumber); |
115
|
|
|
|
116
|
|
|
$token['tokenize'] = $numberToken; |
117
|
|
|
|
118
|
|
|
return $token; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Generate Number Token by Card Number for Adminhtml. |
123
|
|
|
* |
124
|
|
|
* @param int $storeId |
125
|
|
|
* @param \Getnet\PaymentMagento\Api\Data\NumberTokenInterface $cardNumber |
126
|
|
|
* |
127
|
|
|
* @throws CouldNotSaveException |
128
|
|
|
* @throws NoSuchEntityException |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function generateNumberTokenForAdmin( |
133
|
|
|
$storeId, |
134
|
|
|
NumberTokenInterface $cardNumber |
135
|
|
|
) { |
136
|
|
|
$token = []; |
137
|
|
|
|
138
|
|
|
$cardNumber = $cardNumber->getCardNumber(); |
139
|
|
|
|
140
|
|
|
$numberToken = $this->getNumberToken($storeId, $cardNumber); |
141
|
|
|
|
142
|
|
|
$token['tokenize'] = $numberToken; |
143
|
|
|
|
144
|
|
|
return $token; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get Number Token. |
149
|
|
|
* |
150
|
|
|
* @param int $storeId |
151
|
|
|
* @param string $cardNumber |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function getNumberToken($storeId, $cardNumber) |
156
|
|
|
{ |
157
|
|
|
/** @var ZendClient $client */ |
158
|
|
|
$client = $this->httpClientFactory->create(); |
159
|
|
|
$request = ['card_number' => $cardNumber]; |
160
|
|
|
$url = $this->configBase->getApiUrl($storeId); |
161
|
|
|
$apiBearer = $this->configBase->getMerchantGatewayOauth($storeId); |
162
|
|
|
|
163
|
|
|
try { |
164
|
|
|
$client->setUri($url.'/v1/tokens/card'); |
165
|
|
|
$client->setConfig(['maxredirects' => 0, 'timeout' => 45000]); |
166
|
|
|
$client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
167
|
|
|
$client->setRawData($this->json->serialize($request), 'application/json'); |
168
|
|
|
$client->setMethod(ZendClient::POST); |
169
|
|
|
|
170
|
|
|
$responseBody = $client->request()->getBody(); |
171
|
|
|
$data = $this->json->unserialize($responseBody); |
172
|
|
|
$response = [ |
173
|
|
|
'success' => 0, |
174
|
|
|
]; |
175
|
|
|
if (!empty($data['number_token'])) { |
176
|
|
|
$response = [ |
177
|
|
|
'success' => 1, |
178
|
|
|
'number_token' => $data['number_token'], |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
$this->logger->debug( |
182
|
|
|
[ |
183
|
|
|
'url' => $url.'v1/tokens/card', |
184
|
|
|
'response' => $responseBody, |
185
|
|
|
] |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
if (!$client->request()->isSuccessful()) { |
189
|
|
|
$response = [ |
190
|
|
|
'success' => 0, |
191
|
|
|
'message' => [ |
192
|
|
|
'text' => __('Error creating payment. Please, contact the store owner or try again.'), |
193
|
|
|
], |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
} catch (\InvalidArgumentException $e) { |
197
|
|
|
$this->logger->debug( |
198
|
|
|
[ |
199
|
|
|
'url' => $url.'v1/tokens/card', |
200
|
|
|
'response' => $responseBody, |
201
|
|
|
] |
202
|
|
|
); |
203
|
|
|
// phpcs:ignore Magento2.Exceptions.DirectThrow |
204
|
|
|
throw new \Exception('Invalid JSON was returned by the gateway'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $response; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
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