All   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 118
c 1
b 0
f 0
dl 0
loc 344
rs 10
wmc 23

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A communicateStatus() 0 14 3
B resolveStatusUpdate() 0 15 7
A processPay() 0 28 2
A findMageOrder() 0 16 2
A execute() 0 53 5
A createResult() 0 8 1
A processCancel() 0 28 2
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
namespace Getnet\PaymentMagento\Controller\Notification;
10
11
use Exception;
12
use Getnet\PaymentMagento\Gateway\Config\Config;
13
use Magento\Framework\App\Action\Action;
14
use Magento\Framework\App\Action\Context;
15
use Magento\Framework\Controller\Result\JsonFactory;
16
use Magento\Framework\Controller\ResultInterface;
17
use Magento\Framework\DataObjectFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\DataObjectFactory 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\View\Result\PageFactory;
19
use Magento\Payment\Model\Method\Logger;
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;
22
use Magento\Sales\Model\Service\InvoiceService;
23
use Magento\Sales\Model\Service\OrderService;
24
use Magento\Store\Model\StoreManagerInterface;
25
26
/**
27
 * Controler Notification All - Notification of receivers for All Methods.
28
 *
29
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30
 */
31
class All extends Action
0 ignored issues
show
Deprecated Code introduced by
The class Magento\Framework\App\Action\Action has been deprecated: 103.0.0 Inheritance in controllers should be avoided in favor of composition ( Ignorable by Annotation )

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

31
class All extends /** @scrutinizer ignore-deprecated */ Action
Loading history...
32
{
33
    /**
34
     * @const string
35
     */
36
    public const APPROVED_PAID = 'APPROVED';
37
38
    /**
39
     * @const string
40
     */
41
    public const ACCEPT_PAID = 'PAID';
42
43
    /**
44
     * @const string
45
     */
46
    public const ACCEPT_PAID_ALTERNATIVE = 'AUTHORIZED';
47
48
    /**
49
     * @const string
50
     */
51
    public const CANCELED_PAID = 'CANCELED';
52
53
    /**
54
     * @const string
55
     */
56
    public const DENNY_PAID = 'DENIED';
57
58
    /**
59
     * @const string
60
     */
61
    public const ERROR = 'ERROR';
62
63
    /**
64
     * @var Logger
65
     */
66
    protected $logger;
67
68
    /**
69
     * @var OrderInterfaceFactory
70
     */
71
    protected $orderFactory;
72
73
    /**
74
     * @var PageFactory
75
     */
76
    protected $pageFactory;
77
78
    /**
79
     * @var StoreManagerInterface
80
     */
81
    protected $storeManager;
82
83
    /**
84
     * @var DataObjectFactory
85
     */
86
    protected $dataObjectFactory;
87
88
    /**
89
     * @var JsonFactory
90
     */
91
    protected $resultJsonFactory;
92
93
    /**
94
     * @var Config
95
     */
96
    protected $config;
97
98
    /**
99
     * @var OrderService
100
     */
101
    protected $orderService;
102
103
    /**
104
     * @var InvoiceService
105
     */
106
    protected $invoiceService;
107
108
    /**
109
     * @param Context               $context
110
     * @param Logger                $logger
111
     * @param OrderInterfaceFactory $orderFactory
112
     * @param PageFactory           $pageFactory
113
     * @param StoreManagerInterface $storeManager
114
     * @param DataObjectFactory     $dataObjectFactory
115
     * @param JsonFactory           $resultJsonFactory
116
     * @param Config                $config
117
     * @param OrderService          $orderService
118
     * @param InvoiceService        $invoiceService
119
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
120
     */
121
    public function __construct(
122
        Context $context,
123
        Logger $logger,
124
        OrderInterfaceFactory $orderFactory,
125
        PageFactory $pageFactory,
126
        StoreManagerInterface $storeManager,
127
        DataObjectFactory $dataObjectFactory,
128
        JsonFactory $resultJsonFactory,
129
        Config $config,
130
        OrderService $orderService,
131
        InvoiceService $invoiceService
132
    ) {
133
        $this->logger = $logger;
134
        $this->orderFactory = $orderFactory;
135
        $this->pageFactory = $pageFactory;
136
        $this->storeManager = $storeManager;
137
        $this->dataObjectFactory = $dataObjectFactory;
138
        $this->resultJsonFactory = $resultJsonFactory;
139
        $this->config = $config;
140
        $this->orderService = $orderService;
141
        $this->invoiceService = $invoiceService;
142
143
        return parent::__construct($context);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::__construct($context) targeting Magento\Framework\App\Action\Action::__construct() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
144
    }
145
146
    /**
147
     * Execute.
148
     *
149
     * @return ResultInterface
150
     */
151
    public function execute()
152
    {
153
        /** @var JsonFactory $resultPage */
154
        $resultPage = $this->resultJsonFactory->create();
155
156
        if (!$this->getRequest()->getParams()) {
157
            $resultPage->setHttpResponseCode(404);
0 ignored issues
show
Bug introduced by
The method setHttpResponseCode() does not exist on Magento\Framework\Controller\Result\JsonFactory. ( Ignorable by Annotation )

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

157
            $resultPage->/** @scrutinizer ignore-call */ 
158
                         setHttpResponseCode(404);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
159
            return $resultPage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $resultPage returns the type Magento\Framework\Controller\Result\JsonFactory which is incompatible with the documented return type Magento\Framework\Controller\ResultInterface.
Loading history...
160
        }
161
162
        $getnetData = $this->getRequest()->getParams();
163
164
        /** @var DataObjectFactory $getnetData */
165
        $getnetData = $this->dataObjectFactory->create(['data' => $getnetData]);
166
167
        $this->logger->debug(['type'=>'notification', 'data' => $getnetData->getData()]);
168
169
        $getnetDataSellerId = $getnetData->getSellerId();
170
171
        $sellerId = $this->config->getMerchantGatewaySellerId();
172
173
        if ($sellerId === $getnetDataSellerId) {
174
            $getnetDataOrderId = $getnetData->getOrderId();
175
176
            $order = $this->findMageOrder($getnetDataOrderId);
177
178
            if (!$order->getEntityId()) {
0 ignored issues
show
Bug introduced by
The method getEntityId() does not exist on Magento\Framework\Controller\ResultInterface. ( Ignorable by Annotation )

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

178
            if (!$order->/** @scrutinizer ignore-call */ getEntityId()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
179
                return $this->createResult(
180
                    406,
181
                    [
182
                        'error'   => 406,
183
                        'message' => __('Order not found.'),
184
                    ]
185
                );
186
            }
187
188
            if ($order->getState() !== Order::STATE_NEW) {
0 ignored issues
show
Bug introduced by
The method getState() does not exist on Magento\Framework\Controller\ResultInterface. ( Ignorable by Annotation )

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

188
            if ($order->/** @scrutinizer ignore-call */ getState() !== Order::STATE_NEW) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
189
                return $this->createResult(
190
                    412,
191
                    [
192
                        'error'   => 412,
193
                        'message' => __('Not available.'),
194
                    ]
195
                );
196
            }
197
198
            $getnetDataStatus = $getnetData->getStatus();
199
200
            return $this->resolveStatusUpdate($getnetDataStatus, $order);
201
        }
202
203
        return $this->createResult(401, []);
204
    }
205
206
    /**
207
     * Resolve Status Update.
208
     *
209
     * @param string                $getnetDataStatus
210
     * @param OrderInterfaceFactory $order
211
     *
212
     * @return ResultInterface
213
     */
214
    public function resolveStatusUpdate($getnetDataStatus, $order)
215
    {
216
        if ($getnetDataStatus === self::APPROVED_PAID ||
217
            $getnetDataStatus === self::ACCEPT_PAID ||
218
            $getnetDataStatus === self::ACCEPT_PAID_ALTERNATIVE) {
219
            return $this->processPay($order);
220
        }
221
222
        if ($getnetDataStatus === self::CANCELED_PAID ||
223
            $getnetDataStatus === self::DENNY_PAID ||
224
            $getnetDataStatus === self::ERROR) {
225
            return $this->processCancel($order);
226
        }
227
228
        return $this->createResult(412, []);
229
    }
230
231
    /**
232
     * Find Magento Order.
233
     *
234
     * @param string $getnetDataOrderId
235
     *
236
     * @return OrderInterfaceFactory|ResultInterface
237
     */
238
    public function findMageOrder($getnetDataOrderId)
239
    {
240
        try {
241
            /** @var OrderInterfaceFactory $order */
242
            $order = $this->orderFactory->create()->load($getnetDataOrderId, 'increment_id');
243
        } catch (Exception $exc) {
244
            return $this->createResult(
245
                500,
246
                [
247
                    'error'   => 500,
248
                    'message' => $exc->getMessage(),
249
                ]
250
            );
251
        }
252
253
        return $order;
254
    }
255
256
    /**
257
     * Process Pay.
258
     *
259
     * @param OrderInterfaceFactory $order
260
     *
261
     * @return ResultInterface
262
     */
263
    public function processPay($order)
264
    {
265
        $totalDue = $order->getTotalDue();
266
        $payment = $order->getPayment();
267
268
        $payment->setNotificationResult(true);
269
        $payment->registerCaptureNotification($totalDue);
270
        $payment->accept(true);
271
272
        try {
273
            $order->save();
274
            $this->communicateStatus($order, 'pay');
275
        } catch (Exception $exc) {
276
            return $this->createResult(
277
                500,
278
                [
279
                    'error'   => 500,
280
                    'message' => $exc->getMessage(),
281
                ]
282
            );
283
        }
284
285
        return $this->createResult(
286
            200,
287
            [
288
                'order'     => $order->getIncrementId(),
289
                'state'     => $order->getState(),
290
                'status'    => $order->getStatus(),
291
            ]
292
        );
293
    }
294
295
    /**
296
     * Process Cancel.
297
     *
298
     * @param OrderInterfaceFactory $order
299
     *
300
     * @return ResultInterface
301
     */
302
    public function processCancel($order)
303
    {
304
        $totalDue = $order->getTotalDue();
305
        $payment = $order->getPayment();
306
307
        $payment->setNotificationResult(true);
308
        $payment->registerVoidNotification($totalDue);
309
        $payment->deny(true);
310
311
        try {
312
            $order->save();
313
            $this->communicateStatus($order, 'cancel');
314
        } catch (Exception $exc) {
315
            return $this->createResult(
316
                500,
317
                [
318
                    'error'   => 500,
319
                    'message' => $exc->getMessage(),
320
                ]
321
            );
322
        }
323
324
        return $this->createResult(
325
            200,
326
            [
327
                'order'     => $order->getIncrementId(),
328
                'state'     => $order->getState(),
329
                'status'    => $order->getStatus(),
330
            ]
331
        );
332
    }
333
334
    /**
335
     * Communicate status.
336
     *
337
     * @param OrderInterfaceFactory $order
338
     * @param string                $type
339
     *
340
     * @return void
341
     */
342
    public function communicateStatus($order, $type)
343
    {
344
        if ($type === 'pay') {
345
            $invoice = $order->getInvoiceCollection()->getFirstItem();
346
            $this->invoiceService->notify($invoice->getId());
347
        }
348
349
        if ($type === 'cancel') {
350
            $orderId = $order->getId();
351
            $comment = __('Order Canceled.');
352
            $history = $order->addStatusHistoryComment($comment, $order->getStatus());
353
            $history->setIsVisibleOnFront(true);
354
            $history->setIsCustomerNotified(true);
355
            $this->orderService->addComment($orderId, $history);
356
        }
357
    }
358
359
    /**
360
     * Create Result.
361
     *
362
     * @param int   $statusCode
363
     * @param array $data
364
     *
365
     * @return ResultInterface
366
     */
367
    public function createResult($statusCode, $data)
368
    {
369
        /** @var JsonFactory $resultPage */
370
        $resultPage = $this->resultJsonFactory->create();
371
        $resultPage->setHttpResponseCode($statusCode);
372
        $resultPage->setData($data);
0 ignored issues
show
Bug introduced by
The method setData() does not exist on Magento\Framework\Controller\Result\JsonFactory. ( Ignorable by Annotation )

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

372
        $resultPage->/** @scrutinizer ignore-call */ 
373
                     setData($data);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
373
374
        return $resultPage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $resultPage returns the type Magento\Framework\Controller\Result\JsonFactory which is incompatible with the documented return type Magento\Framework\Controller\ResultInterface.
Loading history...
375
    }
376
}
377