GetpayFetchTransactionInfoHandler::handle()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 31
rs 8.8333
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\Gateway\Response;
10
11
use Exception;
12
use InvalidArgumentException;
13
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
14
use Magento\Payment\Gateway\Response\HandlerInterface;
15
use Magento\Sales\Model\Service\InvoiceService;
16
use Magento\Sales\Model\Service\OrderService;
17
18
/**
19
 * Class GetpayFetchTransactionInfoHandler - Looks for information about updating order status.
20
 */
21
class GetpayFetchTransactionInfoHandler implements HandlerInterface
22
{
23
    /**
24
     * @const string
25
     */
26
    public const RESULT_CODE = 'RESULT_CODE';
27
28
    /**
29
     * @const string
30
     */
31
    public const RESPONSE_STATUS = 'STATUS';
32
33
    /**
34
     * @var OrderService
35
     */
36
    protected $orderService;
37
38
    /**
39
     * @var InvoiceService
40
     */
41
    protected $invoiceService;
42
43
    /**
44
     * @param OrderService   $orderService
45
     * @param InvoiceService $invoiceService
46
     */
47
    public function __construct(
48
        OrderService $orderService,
49
        InvoiceService $invoiceService
50
    ) {
51
        $this->orderService = $orderService;
52
        $this->invoiceService = $invoiceService;
53
    }
54
55
    /**
56
     * Handles.
57
     *
58
     * @param array $handlingSubject
59
     * @param array $response
60
     *
61
     * @return void
62
     */
63
    public function handle(array $handlingSubject, array $response)
64
    {
65
        if (!isset($handlingSubject['payment'])
66
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
67
        ) {
68
            throw new InvalidArgumentException('Payment data object should be provided');
69
        }
70
71
        if ($response[self::RESULT_CODE] === 1) {
72
            $isAproved = false;
73
            $isDeny = true;
74
75
            $status = $response[self::RESPONSE_STATUS];
76
77
            if ($status) {
78
                $isAproved = true;
79
                $isDeny = false;
80
            }
81
82
            $paymentDO = $handlingSubject['payment'];
83
84
            $payment = $paymentDO->getPayment();
85
86
            $order = $payment->getOrder();
87
88
            if ($isAproved) {
89
                $this->processPay($order);
90
            }
91
92
            if ($isDeny) {
93
                $this->processCancel($order);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Process Pay.
100
     *
101
     * @param OrderInterfaceFactory $order
0 ignored issues
show
Bug introduced by
The type Getnet\PaymentMagento\Ga...e\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...
102
     *
103
     * @throws Exception
104
     *
105
     * @return void
106
     */
107
    public function processPay($order)
108
    {
109
        $totalDue = $order->getTotalDue();
110
        $payment = $order->getPayment();
111
112
        $payment->setNotificationResult(true);
113
        $payment->registerCaptureNotification($totalDue);
114
        $payment->accept(true);
115
116
        try {
117
            $order->save();
118
            $this->communicateStatus($order, 'pay');
119
        } catch (Exception $exc) {
120
            // phpcs:ignore Magento2.Exceptions.DirectThrow
121
            throw new Exception($exc->getMessage());
122
        }
123
    }
124
125
    /**
126
     * Process Cancel.
127
     *
128
     * @param OrderInterfaceFactory $order
129
     *
130
     * @throws Exception
131
     *
132
     * @return void
133
     */
134
    public function processCancel($order)
135
    {
136
        $totalDue = $order->getTotalDue();
137
        $payment = $order->getPayment();
138
139
        $payment->setNotificationResult(true);
140
        $payment->registerVoidNotification($totalDue);
141
        $payment->deny(true);
142
143
        try {
144
            $order->save();
145
            $this->communicateStatus($order, 'cancel');
146
        } catch (Exception $exc) {
147
            // phpcs:ignore Magento2.Exceptions.DirectThrow
148
            throw new Exception($exc->getMessage());
149
        }
150
    }
151
152
    /**
153
     * Communicate status.
154
     *
155
     * @param OrderInterfaceFactory $order
156
     * @param string                $type
157
     *
158
     * @return void
159
     */
160
    public function communicateStatus($order, $type)
161
    {
162
        if ($type === 'pay') {
163
            $invoice = $order->getInvoiceCollection()->getFirstItem();
164
            $this->invoiceService->notify($invoice->getId());
165
        }
166
167
        if ($type === 'cancel') {
168
            $orderId = $order->getId();
169
            $comment = __('Order Canceled.');
170
            $history = $order->addStatusHistoryComment($comment, $order->getStatus());
171
            $history->setIsVisibleOnFront(true);
172
            $history->setIsCustomerNotified(true);
173
            $this->orderService->addComment($orderId, $history);
174
        }
175
    }
176
}
177