Cancel   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 73
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B execute() 0 34 5
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 - 2016 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\Controller\Onepage;
28
29
use Magento\Framework\Controller\ResultFactory;
30
use Magento\Framework\Exception\LocalizedException;
31
use Magento\Framework\App\Action\Action;
32
33
/**
34
 * Cancel controller for back links from redirect payment-types
35
 */
36
class Cancel extends Action
37
{
38
    /**
39
     * Checkout session
40
     *
41
     * @var \Magento\Checkout\Model\Session
42
     */
43
    protected $checkoutSession;
44
45
    /**
46
     * Order factory
47
     *
48
     * @var \Magento\Sales\Model\OrderFactory
49
     */
50
    protected $orderFactory;
51
52
    /**
53
     * Constructor
54
     *
55
     * @param \Magento\Framework\App\Action\Context $context
56
     * @param \Magento\Checkout\Model\Session       $checkoutSession
57
     * @param \Magento\Sales\Model\OrderFactory     $orderFactory
58
     */
59
    public function __construct(
60
        \Magento\Framework\App\Action\Context $context,
61
        \Magento\Checkout\Model\Session $checkoutSession,
62
        \Magento\Sales\Model\OrderFactory $orderFactory
63
    ) {
64
        parent::__construct($context);
65
        $this->checkoutSession = $checkoutSession;
66
        $this->orderFactory = $orderFactory;
67
    }
68
69
    /**
70
     * Checkout is canceled and old basket is reactivated
71
     *
72
     * @return \Magento\Framework\Controller\Result\Redirect
73
     */
74
    public function execute()
75
    {
76
        try {
77
            $this->checkoutSession->setIsPayoneRedirectCancellation(true);
78
79
            $orderId = $this->checkoutSession->getLastOrderId();
80
            $order = $orderId ? $this->orderFactory->create()->load($orderId) : false;
81
            if ($order) {
82
                $sQuoteId = $order->getQuoteId();
83
84
                $order->cancel()->save();
85
                $this->checkoutSession
86
                    ->unsLastQuoteId()
87
                    ->unsLastSuccessQuoteId()
88
                    ->unsLastOrderId()
89
                    ->unsLastRealOrderId();
90
91
                // Use the old quote/basket again
92
                $this->checkoutSession->setQuoteId($sQuoteId);
93
                $this->checkoutSession->setLoadInactive();
94
                $oQuote = $this->checkoutSession->getQuote();
95
                $oQuote->setIsActive(1)->setReservedOrderId(null)->save();
96
                $this->checkoutSession->replaceQuote($oQuote);
97
            }
98
        } catch (LocalizedException $e) {
99
            $this->messageManager->addExceptionMessage($e, $e->getMessage());
100
        } catch (\Exception $e) {
101
            $this->messageManager->addExceptionMessage($e, __('Error while canceling the payment'));
102
        }
103
104
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
105
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
106
        return $resultRedirect->setPath('checkout');
107
    }
108
}
109