Issues (1092)

Controller/Onepage/PlaceOrder.php (8 issues)

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 - 2017 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 Payone\Core\Model\PayoneConfig;
30
31
/**
32
 * Controller for creating the PaypalExpress orders
33
 */
34
class PlaceOrder extends \Magento\Framework\App\Action\Action
0 ignored issues
show
The type Magento\Framework\App\Action\Action 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...
35
{
36
    /**
37
     * @var \Magento\Checkout\Api\AgreementsValidatorInterface
0 ignored issues
show
The type Magento\Checkout\Api\AgreementsValidatorInterface 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...
38
     */
39
    protected $agreementsValidator;
40
41
    /**
42
     * @var \Magento\Checkout\Model\Session
0 ignored issues
show
The type Magento\Checkout\Model\Session 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...
43
     */
44
    protected $checkoutSession;
45
46
    /**
47
     * @var \Magento\Quote\Api\CartManagementInterface
0 ignored issues
show
The type Magento\Quote\Api\CartManagementInterface 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...
48
     */
49
    protected $cartManagement;
50
51
    /**
52
     * Payone checkout helper
53
     *
54
     * @var \Payone\Core\Helper\Checkout
55
     */
56
    protected $checkoutHelper;
57
58
    /**
59
     * Payone order helper
60
     *
61
     * @var \Payone\Core\Helper\Order
62
     */
63
    protected $orderHelper;
64
65
    /**
66
     * @param \Magento\Framework\App\Action\Context              $context
67
     * @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator
68
     * @param \Magento\Checkout\Model\Session                    $checkoutSession
69
     * @param \Magento\Quote\Api\CartManagementInterface         $cartManagement
70
     * @param \Payone\Core\Helper\Checkout                       $checkoutHelper
71
     * @param \Payone\Core\Helper\Order                          $orderHelper
72
     */
73
    public function __construct(
74
        \Magento\Framework\App\Action\Context $context,
0 ignored issues
show
The type Magento\Framework\App\Action\Context 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...
75
        \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator,
76
        \Magento\Checkout\Model\Session $checkoutSession,
77
        \Magento\Quote\Api\CartManagementInterface $cartManagement,
78
        \Payone\Core\Helper\Checkout $checkoutHelper,
79
        \Payone\Core\Helper\Order $orderHelper
80
    ) {
81
        $this->agreementsValidator = $agreementValidator;
82
        $this->checkoutSession = $checkoutSession;
83
        $this->cartManagement = $cartManagement;
84
        $this->checkoutHelper = $checkoutHelper;
85
        $this->orderHelper = $orderHelper;
86
        parent::__construct($context);
87
    }
88
89
    /**
90
     * Submit the order
91
     *
92
     * @return void
93
     * @throws \Magento\Framework\Exception\LocalizedException
94
     */
95
    public function execute()
96
    {
97
        if ($this->isValidationRequired() &&
98
            !$this->agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))
99
        ) {
100
            $e = new \Magento\Framework\Exception\LocalizedException(
0 ignored issues
show
The type Magento\Framework\Exception\LocalizedException 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
                __('Please agree to all the terms and conditions before placing the order.')
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
                /** @scrutinizer ignore-call */ 
102
                __('Please agree to all the terms and conditions before placing the order.')
Loading history...
102
            );
103
            $this->messageManager->addExceptionMessage($e, $e->getMessage());
104
            $this->_redirect('*/*/review');
105
            return;
106
        }
107
108
        try {
109
            $oQuote = $this->checkoutSession->getQuote();
110
111
            if ($this->checkoutHelper->getQuoteComparisonString($oQuote) != $this->checkoutSession->getPayoneQuoteComparisonString()) {
112
                // The basket was changed - abort current checkout
113
                $this->messageManager->addErrorMessage('An error occured during the Checkout.');
114
                $this->_redirect('checkout/cart');
115
                return;
116
            }
117
118
            if (!empty($this->checkoutSession->getPayoneQuoteAddressHash()) && $this->checkoutHelper->getQuoteAddressHash($oQuote) != $this->checkoutSession->getPayoneQuoteAddressHash()) {
119
                // Address has been changed which is not allowed to happen - reset it to the address given by the Express payment method
120
                $aExpressAddressResponse = $this->checkoutSession->getPayoneExpressAddressResponse();
121
122
                $blUseBilling = true;
123
                if (in_array($oQuote->getPayment()->getMethod(), [PayoneConfig::METHOD_PAYPAL, PayoneConfig::METHOD_PAYPALV2])) {
124
                    $blUseBilling = false;
125
                }
126
                $oQuote = $this->orderHelper->updateAddresses($oQuote, $aExpressAddressResponse, $blUseBilling);
127
            }
128
129
            $this->placeOrder($oQuote);
130
131
            $sPayoneRedirectUrl = $this->checkoutSession->getPayoneRedirectUrl();
132
            if (!empty($sPayoneRedirectUrl)) {
133
                $this->checkoutSession->setPayoneCustomerIsRedirected(true);
134
                $this->checkoutSession->setPayonePayPalExpressRetry(true);
135
                $this->_redirect($sPayoneRedirectUrl);
136
                return;
137
            }
138
139
            // "last successful quote"
140
            $sQuoteId = $oQuote->getId();
141
            $this->checkoutSession->setLastQuoteId($sQuoteId)->setLastSuccessQuoteId($sQuoteId)->unsPayoneWorkorderId()->unsIsPayonePayPalExpress()->unsPayoneUserAgent()->unsPayoneDeviceFingerprint();
142
143
            $oQuote->setIsActive(false)->save();
144
145
            $this->_redirect('checkout/onepage/success');
146
        } catch (\Exception $e) {
147
            $this->messageManager->addExceptionMessage(
148
                $e,
149
                __('We can\'t place the order.')
150
            );
151
            $this->_redirect('*/*/review');
152
        }
153
    }
154
155
    /**
156
     * Place the order and put it in a finished state
157
     *
158
     * @param Magento\Quote\Model\Quote $oQuote
0 ignored issues
show
The type Payone\Core\Controller\O...gento\Quote\Model\Quote 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...
159
     * @return void
160
     */
161
    protected function placeOrder($oQuote)
162
    {
163
        $oQuote->getBillingAddress()->setShouldIgnoreValidation(true);
164
        if (!$oQuote->getIsVirtual()) {
165
            $oQuote->getShippingAddress()->setShouldIgnoreValidation(true);
166
        }
167
168
        $this->checkoutSession->setPayoneUserAgent($this->getRequest()->getHeader('user-agent'));
169
        $this->checkoutSession->setPayoneExpressType($oQuote->getPayment()->getMethod());
170
        if ($oQuote->getPayment()->getMethod() == PayoneConfig::METHOD_PAYPAL) {
171
            $this->checkoutSession->setIsPayonePayPalExpress(true);
172
        }
173
        if ($oQuote->getPayment()->getMethod() == PayoneConfig::METHOD_AMAZONPAYV2) {
174
            $this->checkoutSession->setIsPayoneAmazonPayAuth(true);
175
        }
176
        if ($this->getRequest()->getParam('fingerprint')) {
177
            $this->checkoutSession->setPayoneDeviceFingerprint($this->getRequest()->getParam('fingerprint'));
178
        }
179
180
        $this->cartManagement->placeOrder($oQuote->getId());
181
    }
182
183
    /**
184
     * Return true if agreements validation required
185
     *
186
     * @return bool
187
     */
188
    protected function isValidationRequired()
189
    {
190
        return is_array($this->getRequest()->getBeforeForwardInfo()) && empty($this->getRequest()->getBeforeForwardInfo());
191
    }
192
}
193