cancel
Showing results for 
Search instead for 
Did you mean: 

redirecting to payment methods and showing error message

redirecting to payment methods and showing error message

Hey,

I am developing a custom M2 payment module. Within the authorize method of the model I am calling our gateway to check the customer data. Depending on the response I want to redirect to payment methods section and display an error message why the order couldnt have been finalized or finish the order and show the success page.

Finalizing the order is no problem. But I dont get it redirecting to payment methods and showing the error message.

I have tried to add an error message via the messageManager but it doesn't get displayed.

//injection in __construct
\Magento\Framework\Message\ManagerInterface $messageManager
$this->_messageManager = $messageManager;

//add sample error message
$this->_messageManager->addErrorMessage('Error on authorize'); 

 

Maybe you can help me how i am able to redirect and display the error message.

 

All help is appreciated.

2 REPLIES 2

Re: redirecting to payment methods and showing error message

Hi @shopmodule

 

Very interesting question.

Can you please show full controller implementation to get better context?

 

Thanks.

Re: redirecting to payment methods and showing error message

Hey @Pronko

 

there is no Controller implemented to authorize the order at our Gateway everything is done in the authorize method of the model.

 

This is the simplified implementation of the authorize method

 

public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
   $response = $this->callGatewayRequest();
   if($response == 'error'){
      throw new \Magento\Framework\Exception\LocalizedException(__('Error on authorize'));
   } 
   return $this;
}

So now I've tried to throw the LocalizedException to deny the order and display the Error Message. When the throw new LocalizedException is reached the order get aborted but no Message is displayed. Maybe I get something wrong about the Exceptionhandling in M2.
I've tried to follow the approach of vendor/Magento/module-payment/Model/Mehtod/AbstractMethod:

public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
    {
        if (!$this->canAuthorize()) {
            throw new \Magento\Framework\Exception\LocalizedException(__('The authorize action is not available.'));
        }
        return $this;
    }