Deny::createCsrfValidationException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Action was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Magento\Framework\App\Action\Context;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Context was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Magento\Framework\App\CsrfAwareActionInterface as Csrf;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CsrfAwareActionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Magento\Framework\App\Request\InvalidRequestException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Re...InvalidRequestException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Magento\Framework\App\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Magento\Framework\Controller\Result\JsonFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\JsonFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Magento\Framework\Serialize\Serializer\Json;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Serialize\Serializer\Json was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Magento\Payment\Model\Method\Logger;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\Method\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Magento\Sales\Api\Data\OrderInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\OrderInterfaceFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Magento\Sales\Model\Order\CreditmemoFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\CreditmemoFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Orde...nder\OrderCommentSender was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Magento\Sales\Model\Order\Invoice;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\Invoice was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Magento\Sales\Model\Service\CreditmemoService;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Service\CreditmemoService was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Magento\Store\Model\StoreManagerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {
0 ignored issues
show
introduced by
$request is of type Magento\Framework\App\RequestInterface, thus it always evaluated to true.
Loading history...
43
            return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return Magento\Framework\App\Re...InvalidRequestException.
Loading history...
44
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 42 is false. This is incompatible with the type-hinted return Magento\Framework\App\Re...InvalidRequestException. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
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) {
0 ignored issues
show
introduced by
$request is of type Magento\Framework\App\RequestInterface, thus it always evaluated to true.
Loading history...
57
            return true;
58
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 56 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
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'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
                        'message' => /** @scrutinizer ignore-call */ __('Can not find this order'),
Loading history...
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