Passed
Push — master ( a8eb1b...51355c )
by
unknown
02:48 queued 12s
created

Model/Handler/Cancellation.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2018 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Model\Handler;
28
29
use Magento\Checkout\Model\Session;
30
use Magento\Sales\Model\Order;
31
use Magento\Sales\Model\OrderFactory;
32
use Magento\Framework\Exception\LocalizedException;
33
use \Magento\Quote\Api\CartRepositoryInterface as QuoteRepo;
34
use Payone\Core\Model\ResourceModel\TransactionStatus;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Payone\Core\Model\Handler\TransactionStatus. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
35
36
class Cancellation
37
{
38
    /**
39
     * Checkout session
40
     *
41
     * @var \Magento\Checkout\Model\Session
42
     */
43
    protected $checkoutSession;
44
45
    /**
46
     * Order factory
47
     *
48
     * @var OrderFactory
49
     */
50
    protected $orderFactory;
51
52
    /**
53
     * Order repository
54
     *
55
     * @var QuoteRepo
56
     */
57
    protected $quoteRepository;
58
59
    /**
60
     * TransactionStatus resource model
61
     *
62
     * @var TransactionStatus
63
     */
64
    protected $transactionStatus;
65
66
    /**
67
     * Constructor
68
     *
69
     * @param Session           $checkoutSession
70
     * @param OrderFactory      $orderFactory
71
     * @param QuoteRepo         $quoteRepository
72
     * @param TransactionStatus $transactionStatus
73
     */
74
    public function __construct(Session $checkoutSession, OrderFactory $orderFactory, QuoteRepo $quoteRepository, TransactionStatus $transactionStatus)
75
    {
76
        $this->checkoutSession = $checkoutSession;
77
        $this->orderFactory = $orderFactory;
78
        $this->quoteRepository = $quoteRepository;
79
        $this->transactionStatus = $transactionStatus;
80
    }
81
82
    /**
83
     * Determines if order should be canceled
84
     *
85
     * @param  Order $order
86
     * @return bool
87
     */
88
    protected function canCancelOrder(Order $order)
89
    {
90
        if ($this->hasReceivedAppointedStatus($order) === true) {
91
            return false;
92
        }
93
94
        if ($order->hasInvoices() === true) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * Checks if an appointed status was sent for this order
103
     *
104
     * @param  Order $order
105
     * @return bool
106
     */
107
    protected function hasReceivedAppointedStatus(Order $order)
108
    {
109
        $AppointedId = $this->transactionStatus->getAppointedIdByTxid($order->getPayoneTxid());
110
        if (!empty($AppointedId)) {
111
            return true;
112
        }
113
        return false;
114
    }
115
116
    /**
117
     * @return void
118
     */
119
    public function handle()
120
    {
121
        if ($this->checkoutSession->getPayoneCustomerIsRedirected()) {
122
            try {
123
                $orderId = $this->checkoutSession->getLastOrderId();
124
                /** @var Order $order */
125
                $order = $orderId ? $this->orderFactory->create()->load($orderId) : false;
126
                if ($order) {
127
                    if ($this->canCancelOrder($order)) {
128
                        $order->cancel();
129
                        $order
130
                            ->addStatusToHistory(Order::STATE_CANCELED, __('The Payone transaction has been canceled.'), false);
131
                        $order->save();
132
133
                        $oCurrentQuote = $this->checkoutSession->getQuote();
134
135
                        $quoteId = $this->checkoutSession->getLastQuoteId();
136
                        $oOldQuote = $this->quoteRepository->get($quoteId);
137
                        if ($oOldQuote && $oOldQuote->getId()) {
138
                            $oCurrentQuote->merge($oOldQuote);
139
                            $oCurrentQuote->collectTotals();
140
                            $oCurrentQuote->save();
141
                        }
142
                    }
143
144
                    $this->checkoutSession
145
                        ->unsLastQuoteId()
146
                        ->unsLastSuccessQuoteId()
147
                        ->unsLastOrderId()
148
                        ->unsLastRealOrderId();
149
                }
150
            } catch (LocalizedException $e) {
151
                // catch and continue - do something when needed
152
            } catch (\Exception $e) {
153
                // catch and continue - do something when needed
154
            }
155
156
            $this->checkoutSession->unsPayoneCustomerIsRedirected();
157
            $this->checkoutSession->setIsPayoneRedirectCancellation(true);
158
        }
159
    }
160
}
161