Magento – Magento 2 – redirect to final checkout page (checkout success/failed)

checkoutmagento2redirect

I have a custom payment module. When the user select this payment method at checkout, he is redirected to custom module controller, which redirects him to payment gateway. Then he fills his credit card info and is redirected back to custom module controller. Here I process the gateway reply to see if payment was success or not and update the order status. But currently, the user stays on this page.

How do I redirect him to Checkout Success page (where the order ID tracking and create account buttons are displayed)? It works if I simply redirect him to /checkout/onepage/success, but is this a correct way how to do it?

And how do I redirect him to Checkout failed page? In Magento 1, I restored the cart and redirected him to cart view with error message like this:

//restore cart
$session = Mage::getSingleton('checkout/session');
$lastQuoteId = $session->getLastQuoteId();
$quote = Mage::getModel('sales/quote')->load($lastQuoteId);
$quote->setIsActive(true)->save();

//redirect to cart with error message
$failMsg = $this->__('Payment failed. Please try again.') . " " . $orderReplyStatus->resultText;
Mage::getSingleton('core/session')->addError($failMsg);
$this->_redirect('checkout/cart');

Do I have to do the same in Magento 2 or does Magento 2 have a checkout failed page?

Best Answer

You can follow this to achieve your target.

In your cancel controller, I'm naming this Cancel.php

<?php


namespace Magento\SamplePaymentGateway\Controller\Payment;
class Cancel extends \Magento\Framework\App\Action\Action {

    protected $checkoutSession;
    protected $orderRepository;

    /**
     * Constructor
     * 
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Checkout\Model\Session $checkoutSession
    )
    {
        $this->checkoutSession = $checkoutSession;
        $this->orderRepository = $orderRepository;

        parent::__construct($context);
    }

    /**
     * Execute view action
     * 
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $this->messageManager->addError(__('Payment has been cancelled.'));
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        //change order status to cancel
        $order = $this->orderRepository->get($this->checkoutSession->getLastOrderId());
        if ($order) {
            $order->cancel();
            $order->addStatusToHistory(\Magento\Sales\Model\Order::STATE_CANCELED, __('Canceled by customer.'));
            $order->save();
        }

        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('checkout/cart');
        return $resultRedirect;
        //return $this->resultPageFactory->create();
    }
}

And your success controller will be more simpler. I'm just showing you the execute() method. Success.php

    public function execute()
    {
        $this->messageManager->addError(__('Payment has been successful.'));            
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('checkout/onepage/success');
        return $resultRedirect;
        //return $this->resultPageFactory->create();
    }
Related Topic