We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Validate3d.php 8.45 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */
23 24

namespace Adyen\Payment\Controller\Process;
25
use Magento\Framework\App\Request\Http as HttpRequest;
26 27 28 29 30 31 32 33

class Validate3d extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Sales\Model\OrderFactory
     */
    protected $_orderFactory;

34 35 36
    /**
     * @var \Magento\Sales\Model\Order
     */
37 38
    protected $_order;

39 40 41
    /**
     * @var \Adyen\Payment\Logger\AdyenLogger
     */
42 43
    protected $_adyenLogger;

44 45 46
    /**
     * @var \Adyen\Payment\Helper\Data
     */
47 48
    protected $_adyenHelper;

49 50 51 52 53
    /**
     * @var \Adyen\Payment\Model\Api\PaymentRequest
     */
    protected $_paymentRequest;

54 55 56 57 58
    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $_orderRepository;

59
    /**
60 61
     * Validate3d constructor.
     *
62
     * @param \Magento\Framework\App\Action\Context $context
63 64
     * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
     * @param \Adyen\Payment\Helper\Data $adyenHelper
65
     * @param \Adyen\Payment\Model\Api\PaymentRequest $paymentRequest
66 67 68 69
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
70
        \Adyen\Payment\Helper\Data $adyenHelper,
71 72
        \Adyen\Payment\Model\Api\PaymentRequest $paymentRequest,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
73 74 75 76
    ) {
        parent::__construct($context);
        $this->_adyenLogger = $adyenLogger;
        $this->_adyenHelper = $adyenHelper;
77
        $this->_paymentRequest = $paymentRequest;
78
        $this->_orderRepository = $orderRepository;
79 80 81 82 83 84 85
        // Fix for Magento2.3 adding isAjax to the request params
        if(interface_exists("\Magento\Framework\App\CsrfAwareActionInterface")) {
            $request = $this->getRequest();
            if ($request instanceof HttpRequest && $request->isPost()) {
                $request->setParam('isAjax', true);
            }
        }
86 87
    }

88 89 90
    /**
     * Validate 3D secure payment
     */
91 92
    public function execute()
    {
93 94
        $active = null;

95 96 97
        // check if 3d is active
        $order = $this->_getOrder();

98
        if ($order->getPayment()) {
99
            $active = $order->getPayment()->getAdditionalInformation('3dActive');
100
            $success = $order->getPayment()->getAdditionalInformation('3dSuccess');
101
        }
102 103

        // check if 3D secure is active. If not just go to success page
104
        if ($active && $success != true) {
105
            $this->_adyenLogger->addAdyenResult("3D secure is active");
106

107 108
            // check if it is already processed
            if ($this->getRequest()->isPost()) {
109
                $this->_adyenLogger->addAdyenResult("Process 3D secure payment");
110 111
                $requestMD = $this->getRequest()->getPost('MD');
                $requestPaRes = $this->getRequest()->getPost('PaRes');
112
                $md = $order->getPayment()->getAdditionalInformation('md');
113 114 115 116 117

                if ($requestMD == $md) {
                    $order->getPayment()->setAdditionalInformation('paResponse', $requestPaRes);

                    try {
118 119 120 121 122 123 124 125 126 127
                        /**
                         * Magento should allow this.
                         * https://github.com/magento/magento2/issues/5819
                         */
//                        $result = $order->getPayment()->getMethodInstance()->executeCommand(
//                            'authorise_3d',
//                            ['payment' => $order->getPayment(), 'amount' => $order->getGrandTotal()]
//                        );
                        // old fashion way:
                        $result = $this->_authorise3d($order->getPayment());
128 129
                    } catch (\Exception $e) {
                        $this->_adyenLogger->addAdyenResult("Process 3D secure payment was refused");
130 131 132
                        $result = 'Refused';
                    }

133 134
                    $this->_adyenLogger->addAdyenResult("Process 3D secure payment result is: " . $result);

135 136 137
                    // check if authorise3d was successful
                    if ($result == 'Authorised') {
                        $order->addStatusHistoryComment(__('3D-secure validation was successful'))->save();
138 139
                        // set back to false so when pressed back button on the success page it will reactivate 3D secure
                        $order->getPayment()->setAdditionalInformation('3dActive', '');
140
                        $order->getPayment()->setAdditionalInformation('3dSuccess', true);
141 142
                        $this->_orderRepository->save($order);

143
                        $this->_redirect('checkout/onepage/success', ['_query' => ['utm_nooverride' => '1']]);
144
                    } else {
145
                        $order->addStatusHistoryComment(__('3D-secure validation was unsuccessful.'))->save();
146 147 148

                        // Move the order from PAYMENT_REVIEW to NEW, so that can be cancelled
                        $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
149
                        $this->_adyenHelper->cancelOrder($order);
150 151 152 153 154 155 156 157
                        $this->messageManager->addErrorMessage("3D-secure validation was unsuccessful");
                        
                        // reactivate the quote
                        $session = $this->_getCheckout();

                        // restore the quote
                        $session->restoreQuote();

158
                        $this->_redirect($this->_adyenHelper->getAdyenAbstractConfigData('return_path'));
159 160 161
                    }
                }
            } else {
162
                $this->_adyenLogger->addAdyenResult("Customer was redirected to bank for 3D-secure validation.");
163
                $order->addStatusHistoryComment(
164 165 166
                    __('Customer was redirected to bank for 3D-secure validation. Once the shopper authenticated, the order status will be updated accordingly. 
                        <br />Make sure that your notifications are being processed! 
                        <br />If the order is stuck on this status, the shopper abandoned the session. The payment can be seen as unsuccessful. 
167 168
                        <br />The order can be automatically cancelled based on the OFFER_CLOSED notification. Please contact Adyen Support to enable this.')
                )->save();
169 170 171 172 173
                $this->_view->loadLayout();
                $this->_view->getLayout()->initMessages();
                $this->_view->renderLayout();
            }
        } else {
174
            $this->_redirect('checkout/onepage/success', ['_query' => ['utm_nooverride' => '1']]);
175 176 177
        }
    }

178 179 180 181 182 183 184 185 186 187 188
    /**
     * Called by validate3d controller when cc payment has 3D secure
     *
     * @param $payment
     * @return mixed
     * @throws \Exception
     */
    protected function _authorise3d($payment)
    {
        try {
            $response = $this->_paymentRequest->authorise3d($payment);
189
        } catch (\Exception $e) {
190 191 192 193 194 195
            throw $e;
        }
        $responseCode = $response['resultCode'];
        return $responseCode;
    }

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    /**
     * Get order object
     *
     * @return \Magento\Sales\Model\Order
     */
    protected function _getOrder()
    {
        if (!$this->_order) {
            $incrementId = $this->_getCheckout()->getLastRealOrderId();
            $this->_orderFactory = $this->_objectManager->get('Magento\Sales\Model\OrderFactory');
            $this->_order = $this->_orderFactory->create()->loadByIncrementId($incrementId);
        }
        return $this->_order;
    }

    /**
     * @return \Magento\Checkout\Model\Session
     */
    protected function _getCheckout()
    {
        return $this->_objectManager->get('Magento\Checkout\Model\Session');
    }
Rik ter Beek's avatar
Rik ter Beek committed
218
}