we are about developing a custom payment gateway plugin for one of our customers using the magento-sample-gateway-payment plugin as a draft in a Magento 2.1.4.

We have already successfully implemented the payment and if the gateway say's ok all works fine.

Our problem now is, that if the gateway refuses the payment, it will deliver one of many error messages. We want to show this error messages to the customer in the frontend.

Our Validator looks like this:

 * Performs validation of result code
 * @param array $validationSubject
 * @return ResultInterface
public function validate(array $validationSubject)
    if (!isset($validationSubject['response']) || !is_array($validationSubject['response'])) {
        throw new \InvalidArgumentException('Response does not exist');
    $response = $validationSubject['response'];
    if ($this->isSuccessfulTransaction($response)) {
        return $this->createResult(
    } else {
        // We have also tryed to throw a LocalizedException here like i saw in other payment plugins on github
        return $this->createResult(
            [__('My custom error message later out of $response')]

The problem is, no matter what we tryed the frontend always shows 'An error occurred on the server. Please try to place the order again.' and the actual error message gets only logged to the system.log

I hope someone is able to help me,

Greetings Sven

1 Answer 1


Generally this message is handle from two core files:

Magento\Checkout\Model\PaymentInformationManagement and Magento\Checkout\Model\GuestPaymentInformationManagement

from function savePaymentInformationAndPlaceOrder if you want to change default message and display your exception message then just

throw new CouldNotSaveException(
            __('An error occurred on the server. Please try to place the order again.'),

replace above line with below code

throw new CouldNotSaveException(
            new \Magento\Framework\Phrase($e->getMessage()),
  • How this isn't the default setup, I'll never understand, Magento 2 is such a step backwards in its current state.
    – Ecksters
    Aug 3, 2017 at 0:23
  • This does not help if you are trying to build a payment gateway. You can't change the core files for the customers who are going to install the gateway model. Any other answers will be appreciated.
    – bets
    Feb 9, 2022 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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