Show Payment Gateway Error Message on Checkout Failure in Magento 2

magento-2.1magento2paymentpayment-gateway

I have a custom payment gateway, with custom controller and failure page.
I am trying to show the error message on checkout/custom/failure page, but I find it difficult and am thinking my flow maybe wrong.

How it works:
On payment selection in the checkout page, the order is created and you are redirected to the payment provider, if successful, all is good. If the card is declined or the user cancels or for whatever reason if fails, it redirects to my custom controller (with POST values with payment information), cancels the order, and then redirects to checkout/custom/failure. I want the error message from the $_POST array to show as a message in the checkout/custom/failure page.

What I have (related to failure scenario):

  • Block/Standard/Failure.php [1]
  • Controller/Standard/Failure.php [2]
  • Controller/Standard/Response.php [3]
  • view/frontend/templates/failure.php [4]

Code for [3]:

$params = $this->getRequest()->getPostValue();
if (!empty($params['ResponseCode']) && !empty($params['ReasonCode'])) {
    if ($params['ResponseCode'] == '1' && $params['ReasonCode'] == '1') {
        // Set status "paid" changeOrderStatus and redirect to checkout/standard/success
        $result_data['result'] = 'Success! ' . $params['ReasonCode'];
        return $this->resultRedirectFactory->create()->setPath('checkout/onepage/success');
    } else {
        // Set status "cancelled" changeOrderStatus and redirect back to cart
        $result_data['result'] = 'Failed! ' . $params['ReasonCode'];
        $this->cancelOrder($loadOrderId);
        return $this->resultRedirectFactory->create()->setPath('custompaymentgateway/standard/failure');

    }
} else {
    // Set status "cancelled" changeOrderStatus
    $result_data['result'] = 'Failed! ' . $params['ReasonCode'];
    $this->cancelOrder($loadOrderId);
    return $this->resultRedirectFactory->create()->setPath('custompaymentgateway/standard/failure');
}

[2] has a simple render Layout call:

public function execute() {
    $this->_view->loadLayout(['default', 'jccpaymentgateway_standard_failure']);
    $this->_view->renderLayout();
}

and lastly [4] has calls to block functions:

<?php if ($block->getRealOrderId()) : ?><p>
    <?php /* @escapeNotVerified */ echo __('Order #') . $block->getRealOrderId() ?></p>
<?php endif ?>
<?php if ($error = $block->getErrorMessage()) : ?>
    <p><?php /* @escapeNotVerified */ echo $error ?></p>
<?php endif ?>
<p><?php /* @escapeNotVerified */ echo __('Click <a href="%1">here</a> to continue shopping.', $block->escapeUrl($block->getContinueShoppingUrl())) ?></p>

So with all this my question is, how can [4] print the response data from [3]?? Specifically $params['ReasonCode']

Best Answer

In the end it was much simpler than I thought, Ill post it for anyone else who unnecessary struggles with it

In the response Controller I simply added

$this->_checkoutSession->setErrorMessage($params['ReasonCodeDesc']);

So when the .phtml requests $block->getErrorMessage() the error is set correctly