Passed
Push — main ( 6b22cc...840532 )
by Bruno
09:36 queued 04:25
created

GetpayFetchTransactionInfoHandler::processPay()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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