1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Wirecard Brasil. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* @author Bruno Elisei <[email protected]> |
6
|
|
|
* See COPYING.txt for license details. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Moip\Magento2\Model\Console\Command\Preference; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use Magento\Config\Model\ResourceModel\Config; |
|
|
|
|
15
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface; |
|
|
|
|
16
|
|
|
use Magento\Framework\App\State; |
|
|
|
|
17
|
|
|
use Magento\Framework\HTTP\ZendClient; |
|
|
|
|
18
|
|
|
use Magento\Framework\HTTP\ZendClientFactory; |
|
|
|
|
19
|
|
|
use Magento\Framework\Serialize\Serializer\Json; |
|
|
|
|
20
|
|
|
use Magento\Framework\Url\Validator; |
|
|
|
|
21
|
|
|
use Moip\Magento2\Gateway\Config\Config as MoipConfig; |
22
|
|
|
use Moip\Magento2\Model\Console\Command\AbstractModel; |
23
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class Create Preference Webhook. |
27
|
|
|
*/ |
28
|
|
|
class Create extends AbstractModel |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* State. |
32
|
|
|
* |
33
|
|
|
* @var \Magento\Framework\App\State |
34
|
|
|
*/ |
35
|
|
|
private $state; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* ScopeConfigInterface. |
39
|
|
|
* |
40
|
|
|
* @var \Magento\Framework\App\Config\ScopeConfigInterface |
41
|
|
|
*/ |
42
|
|
|
protected $scopeConfig; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Config. |
46
|
|
|
* |
47
|
|
|
* @var \Magento\Config\Model\ResourceModel\Config |
48
|
|
|
*/ |
49
|
|
|
private $config; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* moipConfig. |
53
|
|
|
* |
54
|
|
|
* @var Moip\Magento2\Gateway\Config\Config |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
private $moipConfig; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Validator. |
60
|
|
|
* |
61
|
|
|
* @var \Magento\Framework\Url\Validator |
62
|
|
|
*/ |
63
|
|
|
private $validator; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* ZendClientFactory. |
67
|
|
|
* |
68
|
|
|
* @var \Magento\Framework\HTTP\ZendClientFactory |
69
|
|
|
*/ |
70
|
|
|
private $httpClientFactory; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Json. |
74
|
|
|
* |
75
|
|
|
* @var \Magento\Framework\Serialize\Serializer\Json |
76
|
|
|
*/ |
77
|
|
|
private $json; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create constructor. |
81
|
|
|
* |
82
|
|
|
* @param LoggerInterface $logger |
83
|
|
|
* @param ScopeConfigInterface $scopeConfig |
84
|
|
|
* @param State $state |
85
|
|
|
* @param MoipConfig $moipConfig |
86
|
|
|
* @param Config $config |
87
|
|
|
* @param Json $json |
88
|
|
|
* @param ZendClientFactory $httpClientFactory |
89
|
|
|
*/ |
90
|
|
|
public function __construct( |
91
|
|
|
LoggerInterface $logger, |
92
|
|
|
ScopeConfigInterface $scopeConfig, |
93
|
|
|
State $state, |
94
|
|
|
MoipConfig $moipConfig, |
95
|
|
|
Config $config, |
96
|
|
|
Validator $validator, |
97
|
|
|
Json $json, |
98
|
|
|
ZendClientFactory $httpClientFactory |
99
|
|
|
) { |
100
|
|
|
parent::__construct( |
101
|
|
|
$logger |
102
|
|
|
); |
103
|
|
|
$this->state = $state; |
104
|
|
|
$this->scopeConfig = $scopeConfig; |
105
|
|
|
$this->moipConfig = $moipConfig; |
106
|
|
|
$this->config = $config; |
107
|
|
|
$this->validator = $validator; |
108
|
|
|
$this->json = $json; |
109
|
|
|
$this->httpClientFactory = $httpClientFactory; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function preference(string $baseUrl) |
113
|
|
|
{ |
114
|
|
|
$this->writeln('Init Set Preference'); |
115
|
|
|
$this->writeln(__('<info>Setting preferences for the domain: %1</info>', $baseUrl)); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$valid = $this->validator->isValid($baseUrl); |
118
|
|
|
if (!$valid) { |
119
|
|
|
$this->writeln(__('<error>The URL entered is invalid %1, it must contain https://...</error>', $baseUrl)); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$formatValid = str_ends_with($baseUrl, '/'); |
125
|
|
|
if (!$formatValid) { |
126
|
|
|
$this->writeln(__("<error>Your url %1 is valid, but must end with '/'</error>", $baseUrl)); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->createPreference($baseUrl, 'accept'); |
132
|
|
|
$this->createPreference($baseUrl, 'deny'); |
133
|
|
|
$this->createPreference($baseUrl, 'refund'); |
134
|
|
|
$this->writeln(__('Finished')); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Create Preference. |
141
|
|
|
* |
142
|
|
|
* @param $baseUrl |
143
|
|
|
* @param $type |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
private function createPreference(string $baseUrl, string $type) |
148
|
|
|
{ |
149
|
|
|
$data = $this->createWebhookData($baseUrl, $type); |
150
|
|
|
$create = $this->sendPreference($data); |
151
|
|
|
|
152
|
|
|
if ($create['success']) { |
153
|
|
|
$preference = $create['preference']; |
154
|
|
|
if (isset($preference['id'])) { |
155
|
|
|
$this->writeln(__('<info>Your preference for method %1 has been successfully created: %2</info>', $type, $preference['id'])); |
|
|
|
|
156
|
|
|
$registryConfig = $this->setConfigPreferenceInfo($preference, $type); |
157
|
|
|
|
158
|
|
|
if (!$registryConfig) { |
159
|
|
|
$this->writeln(__('<error>Error saving information in database: %1</error>', $registryConfig['error'])); |
160
|
|
|
} |
161
|
|
|
} elseif (isset($preference['code'])) { |
162
|
|
|
$this->writeln(__('<error>Error creating preference %1: %2</error>', $type, $preference['description'])); |
163
|
|
|
} |
164
|
|
|
} else { |
165
|
|
|
$this->writeln(__('<error>Error creating preference %1: %2</error>', $type, $create['error'])); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this; |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Set Config Preference Info. |
173
|
|
|
* |
174
|
|
|
* @param $baseUrl |
175
|
|
|
* @param $type |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
private function setConfigPreferenceInfo(array $data, string $type): array |
180
|
|
|
{ |
181
|
|
|
$environment = $this->moipConfig->getEnvironmentMode(); |
182
|
|
|
$pathPattern = 'payment/moip_magento2/%s_%s_%s'; |
183
|
|
|
|
184
|
|
|
if ($type === 'accept') { |
185
|
|
|
$pathConfigId = sprintf($pathPattern, 'capture', 'id', $environment); |
186
|
|
|
$pathConfigToken = sprintf($pathPattern, 'capture', 'token', $environment); |
187
|
|
|
} elseif ($type === 'deny') { |
188
|
|
|
$pathConfigId = sprintf($pathPattern, 'cancel', 'id', $environment); |
189
|
|
|
$pathConfigToken = sprintf($pathPattern, 'cancel', 'token', $environment); |
190
|
|
|
} elseif ($type === 'refund') { |
191
|
|
|
$pathConfigId = sprintf($pathPattern, 'refund', 'id', $environment); |
192
|
|
|
$pathConfigToken = sprintf($pathPattern, 'refund', 'token', $environment); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
try { |
196
|
|
|
$this->config->saveConfig( |
197
|
|
|
$pathConfigId, |
|
|
|
|
198
|
|
|
$data['id'], |
199
|
|
|
'default', |
200
|
|
|
0 |
201
|
|
|
); |
202
|
|
|
$this->resourceConfig->saveConfig( |
|
|
|
|
203
|
|
|
$pathConfigToken, |
|
|
|
|
204
|
|
|
$data['token'], |
205
|
|
|
'default', |
206
|
|
|
0 |
207
|
|
|
); |
208
|
|
|
} catch (Exception $e) { |
209
|
|
|
return ['success' => false, 'error' => $e->getMessage()]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return ['success' => true]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Create Url For Accept. |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
private function createUrlForAccept($baseUrl): string |
221
|
|
|
{ |
222
|
|
|
return $baseUrl.'moip/webhooks/accept'; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Create Url For Deny. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
private function createUrlForDeny($baseUrl): string |
231
|
|
|
{ |
232
|
|
|
return $baseUrl.'moip/webhooks/deny'; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Create Url For Refund. |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
private function createUrlForRefund($baseUrl): string |
241
|
|
|
{ |
242
|
|
|
return $baseUrl.'moip/webhooks/refund'; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Create Webhook Data. |
247
|
|
|
* |
248
|
|
|
* @param $baseUrl |
249
|
|
|
* @param $type |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
private function createWebhookData(string $baseUrl, string $type): array |
254
|
|
|
{ |
255
|
|
|
if ($type === 'accept') { |
256
|
|
|
$event = ['ORDER.PAID']; |
257
|
|
|
$url = $this->createUrlForAccept($baseUrl); |
258
|
|
|
} elseif ($type === 'deny') { |
259
|
|
|
$event = ['ORDER.NOT_PAID']; |
260
|
|
|
$url = $this->createUrlForDeny($baseUrl); |
261
|
|
|
} elseif ($type === 'refund') { |
262
|
|
|
$event = ['REFUND.COMPLETED', 'REFUND.FAILED']; |
263
|
|
|
$url = $this->createUrlForRefund($baseUrl); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$webhook = [ |
267
|
|
|
'events' => $event, |
|
|
|
|
268
|
|
|
'target' => $url, |
|
|
|
|
269
|
|
|
'media' => 'WEBHOOK', |
270
|
|
|
]; |
271
|
|
|
|
272
|
|
|
return $webhook; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/* |
276
|
|
|
* Set Webhooks |
277
|
|
|
* |
278
|
|
|
* @param $webhook |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
private function sendPreference($data): array |
282
|
|
|
{ |
283
|
|
|
$uri = $this->moipConfig->getApiUrl(); |
284
|
|
|
$apiBearer = $this->moipConfig->getMerchantGatewayOauth(); |
285
|
|
|
$client = $this->httpClientFactory->create(); |
286
|
|
|
$dataSend = $this->json->serialize($data); |
287
|
|
|
|
288
|
|
|
$client->setUri($uri.'preferences/notifications'); |
289
|
|
|
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]); |
290
|
|
|
$client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
291
|
|
|
$client->setRawData($dataSend, 'application/json'); |
292
|
|
|
$client->setMethod(ZendClient::POST); |
293
|
|
|
|
294
|
|
|
try { |
295
|
|
|
$result = $client->request()->getBody(); |
296
|
|
|
|
297
|
|
|
return [ |
298
|
|
|
'success' => true, |
299
|
|
|
'preference' => $this->json->unserialize($result), |
300
|
|
|
]; |
301
|
|
|
} catch (Exception $e) { |
302
|
|
|
return ['success' => true, 'error' => $e->getMessage()]; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
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