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
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Moip\Magento2\Controller\Webhooks; |
11
|
|
|
|
12
|
|
|
use Magento\Framework\App\Action\Action; |
|
|
|
|
13
|
|
|
use Magento\Framework\App\Action\Context; |
|
|
|
|
14
|
|
|
use Magento\Framework\App\CsrfAwareActionInterface as Csrf; |
|
|
|
|
15
|
|
|
use Magento\Framework\App\Request\InvalidRequestException; |
|
|
|
|
16
|
|
|
use Magento\Framework\App\RequestInterface; |
|
|
|
|
17
|
|
|
use Magento\Framework\Controller\Result\JsonFactory; |
|
|
|
|
18
|
|
|
use Magento\Framework\Serialize\Serializer\Json; |
|
|
|
|
19
|
|
|
use Magento\Payment\Model\Method\Logger; |
|
|
|
|
20
|
|
|
use Magento\Sales\Api\Data\OrderInterfaceFactory; |
|
|
|
|
21
|
|
|
use Magento\Sales\Model\Order\CreditmemoFactory; |
|
|
|
|
22
|
|
|
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender; |
|
|
|
|
23
|
|
|
use Magento\Sales\Model\Order\Invoice; |
|
|
|
|
24
|
|
|
use Magento\Sales\Model\Service\CreditmemoService; |
|
|
|
|
25
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
|
|
|
|
26
|
|
|
use Moip\Magento2\Gateway\Config\Config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Deny - Receives communication for deny payment. |
30
|
|
|
*/ |
31
|
|
|
class Deny extends Action implements Csrf |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* createCsrfValidationException. |
35
|
|
|
* |
36
|
|
|
* @param RequestInterface $request |
37
|
|
|
* |
38
|
|
|
* @return null |
39
|
|
|
*/ |
40
|
|
|
public function createCsrfValidationException(RequestInterface $request): InvalidRequestException |
41
|
|
|
{ |
42
|
|
|
if ($request) { |
|
|
|
|
43
|
|
|
return null; |
|
|
|
|
44
|
|
|
} |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* validateForCsrf. |
49
|
|
|
* |
50
|
|
|
* @param RequestInterface $request |
51
|
|
|
* |
52
|
|
|
* @return bool true |
53
|
|
|
*/ |
54
|
|
|
public function validateForCsrf(RequestInterface $request): bool |
55
|
|
|
{ |
56
|
|
|
if ($request) { |
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var Config |
63
|
|
|
*/ |
64
|
|
|
protected $config; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Logger |
68
|
|
|
*/ |
69
|
|
|
protected $logger; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var OrderInterfaceFactory |
73
|
|
|
*/ |
74
|
|
|
protected $orderFactory; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var CreditmemoFactory |
78
|
|
|
*/ |
79
|
|
|
protected $creditmemoFactory; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var CreditmemoService |
83
|
|
|
*/ |
84
|
|
|
protected $creditmemoService; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var Invoice |
88
|
|
|
*/ |
89
|
|
|
protected $invoice; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var StoreManagerInterface |
93
|
|
|
*/ |
94
|
|
|
protected $storeManager; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var JsonFactory |
98
|
|
|
*/ |
99
|
|
|
protected $resultJsonFactory; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var Json |
103
|
|
|
*/ |
104
|
|
|
protected $json; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var OrderCommentSender |
108
|
|
|
*/ |
109
|
|
|
protected $orderCommentSender; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Context $context |
113
|
|
|
* @param Logger $logger |
114
|
|
|
* @param Config $config |
115
|
|
|
* @param OrderInterfaceFactory $orderFactory |
116
|
|
|
* @param CreditmemoFactory $creditmemoFactory |
117
|
|
|
* @param Invoice $invoice |
118
|
|
|
* @param StoreManagerInterface $storeManager |
119
|
|
|
* @param JsonFactory $resultJsonFactory |
120
|
|
|
* @param Json $json |
121
|
|
|
* @param OrderCommentSender $orderCommentSender |
122
|
|
|
*/ |
123
|
|
|
public function __construct( |
124
|
|
|
Context $context, |
125
|
|
|
Config $config, |
126
|
|
|
Logger $logger, |
127
|
|
|
OrderInterfaceFactory $orderFactory, |
128
|
|
|
CreditmemoFactory $creditmemoFactory, |
129
|
|
|
CreditmemoService $creditmemoService, |
130
|
|
|
Invoice $invoice, |
131
|
|
|
StoreManagerInterface $storeManager, |
132
|
|
|
JsonFactory $resultJsonFactory, |
133
|
|
|
Json $json, |
134
|
|
|
OrderCommentSender $orderCommentSender |
135
|
|
|
) { |
136
|
|
|
parent::__construct($context); |
137
|
|
|
$this->config = $config; |
138
|
|
|
$this->logger = $logger; |
139
|
|
|
$this->orderFactory = $orderFactory; |
140
|
|
|
$this->creditmemoFactory = $creditmemoFactory; |
141
|
|
|
$this->creditmemoService = $creditmemoService; |
142
|
|
|
$this->invoice = $invoice; |
143
|
|
|
$this->storeManager = $storeManager; |
144
|
|
|
$this->resultJsonFactory = $resultJsonFactory; |
145
|
|
|
$this->json = $json; |
146
|
|
|
$this->orderCommentSender = $orderCommentSender; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Command Accept. |
151
|
|
|
* |
152
|
|
|
* @return json |
153
|
|
|
*/ |
154
|
|
|
public function execute() |
155
|
|
|
{ |
156
|
|
|
if (!$this->getRequest()->isPost()) { |
157
|
|
|
$resultPage = $this->resultJsonFactory->create(); |
158
|
|
|
$resultPage->setHttpResponseCode(404); |
159
|
|
|
|
160
|
|
|
return $resultPage; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$resultPage = $this->resultJsonFactory->create(); |
164
|
|
|
$response = $this->getRequest()->getContent(); |
165
|
|
|
$originalNotification = $this->json->unserialize($response); |
166
|
|
|
$authorization = $this->getRequest()->getHeader('Authorization'); |
167
|
|
|
$storeId = $this->storeManager->getStore()->getId(); |
168
|
|
|
$storeCaptureToken = $this->config->getMerchantGatewayCancelToken($storeId); |
169
|
|
|
if ($storeCaptureToken === $authorization) { |
170
|
|
|
$data = $originalNotification['resource']['order']; |
171
|
|
|
$order = $this->orderFactory->create()->load($data['id'], 'ext_order_id'); |
172
|
|
|
|
173
|
|
|
if(!$order->getId()) { |
174
|
|
|
$resultPage->setHttpResponseCode(406); |
175
|
|
|
return $resultPage->setJsonData( |
176
|
|
|
$this->json->serialize([ |
177
|
|
|
'error' => 400, |
178
|
|
|
'message' => __('Can not find this order'), |
|
|
|
|
179
|
|
|
]) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->logger->debug([ |
184
|
|
|
'webhook' => 'deny', |
185
|
|
|
'ext_order_id' => $data['id'], |
186
|
|
|
'increment_order_id' => $order->getIncrementId(), |
187
|
|
|
'webhook_data' => $response, |
188
|
|
|
]); |
189
|
|
|
$payment = $order->getPayment(); |
190
|
|
|
if ($order->canVoidPayment()) { |
191
|
|
|
try { |
192
|
|
|
$isOnline = true; |
193
|
|
|
$payment->void($isOnline); |
194
|
|
|
$payment->save(); |
195
|
|
|
$cancelDetailsAdmin = __('We did not record the payment.'); |
196
|
|
|
$cancelDetailsCus = __('The payment deadline has been exceeded.'); |
197
|
|
|
if (isset($data['payments'])) { |
198
|
|
|
foreach ($data['payments'] as $payment) { |
199
|
|
|
if (isset($payment['cancellationDetails'])) { |
200
|
|
|
$cancelCode = $payment['cancellationDetails']['code']; |
201
|
|
|
$cancelDescription = $payment['cancellationDetails']['description']; |
202
|
|
|
$cancelBy = $payment['cancellationDetails']['cancelledBy']; |
203
|
|
|
$cancelDetailsAdmin = __('%1, code %2, by %3', $cancelDescription, $cancelCode, $cancelBy); |
204
|
|
|
$cancelDetailsCus = __('%1', $cancelDescription); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
/** customer information for cancel **/ |
209
|
|
|
$history = $order->addStatusHistoryComment($cancelDetailsCus); |
210
|
|
|
$history->setIsVisibleOnFront(1); |
211
|
|
|
$history->setIsCustomerNotified(1); |
212
|
|
|
// $order->sendOrderUpdateEmail(1, $cancelDetailsCus); |
213
|
|
|
|
214
|
|
|
/** admin information for cancel **/ |
215
|
|
|
$history = $order->addStatusHistoryComment($cancelDetailsAdmin); |
216
|
|
|
$history->setIsVisibleOnFront(0); |
217
|
|
|
$history->setIsCustomerNotified(0); |
218
|
|
|
$order->save(); |
219
|
|
|
|
220
|
|
|
$this->orderCommentSender->send($order, 1, $cancelDetailsCus); |
221
|
|
|
} catch (\Exception $exc) { |
222
|
|
|
$resultPage->setHttpResponseCode(500); |
223
|
|
|
return $resultPage->setJsonData( |
224
|
|
|
$this->json->serialize([ |
225
|
|
|
'error' => 400, |
226
|
|
|
'message' => $exc->getMessage(), |
227
|
|
|
]) |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $resultPage->setJsonData( |
232
|
|
|
$this->json->serialize([ |
233
|
|
|
'success' => 1, |
234
|
|
|
'status' => $order->getStatus(), |
235
|
|
|
'state' => $order->getState(), |
236
|
|
|
]) |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$resultPage->setHttpResponseCode(201); |
241
|
|
|
|
242
|
|
|
return $resultPage->setJsonData( |
243
|
|
|
$this->json->serialize([ |
244
|
|
|
'error' => 400, |
245
|
|
|
'message' => 'The transaction could not be refund', |
246
|
|
|
]) |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$resultPage->setHttpResponseCode(401); |
251
|
|
|
|
252
|
|
|
return $resultPage; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
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