1

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?

  • Which payment method you are using ? – Keyur Shah Oct 15 '17 at 13:41
  • @KeyurShah Custom module I created – Adam Ježek Oct 15 '17 at 13:47
  • Yes but which payment method ? – Keyur Shah Oct 15 '17 at 13:48
  • @KeyurShah I'm not sure what you mean – Adam Ježek Oct 15 '17 at 13:49
  • I mean which payment gateway you are using ? – Keyur Shah Oct 15 '17 at 13:49
0

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();
    }
0

Based on several Magento 2 needs on this issue, including myself, I recently created a composer package that can be downloaded with the command:

composer require gustavoulyssea/magento-redirect-checkout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.