Passed
Branch [email protected] (aae97d)
by Bruno
10:40
created

WalletFetchTransactionInfoHandler::processPay()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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