cancel
Showing results for 
Search instead for 
Did you mean: 

Show custom payment method error

Show custom payment method error

Hello,

 

I have developed a payment module.

 

Everything working fine except showing error messages.

After click place order, form submitted to 

/rest/default/V1/carts/mine/payment-information

if there is an error, returns 400 Bad Request with message and trace 

 

{"message":"An error occurred on the server. Please try to place the order again.",
"trace":
"#0 /vendor\/magento\/framework\/Interception\/Interceptor.php(146): Magento\\Checkout\\Model\\PaymentInformationManagement->savePaymentInformationAndPlaceOrder(386, Object(Magento\\Quote\\Model\\Quote\\Payment), Object(Magento\\Quote\\Model\\Quote\\Address))
#1 var\/generation\/Magento\/Checkout\/Model\/PaymentInformationManagement\/Interceptor.php(26): Magento\\Checkout\\Model\\PaymentInformationManagement\\Interceptor->___callPlugins('savePaymentInfo...', Array, Array)
#2 [internal function]: Magento\\Checkout\\Model\\PaymentInformationManagement\\Interceptor->savePaymentInformationAndPlaceOrder(386, Object(Magento\\Quote\\Model\\Quote\\Payment), Object(Magento\\Quote\\Model\\Quote\\Address))
#3 /vendor\/magento\/module-webapi\/Controller\/Rest.php(307): call_user_func_array(Array, Array)
#4 /vendor\/magento\/module-webapi\/Controller\/Rest.php(216): Magento\\Webapi\\Controller\\Rest->processApiRequest()
#5 /var\/generation\/Magento\/Webapi\/Controller\/Rest\/Interceptor.php(37): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))
#6 /vendor\/magento\/framework\/App\/Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))
#7 /vendor\/magento\/framework\/App\/Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()
#8 /index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))
#9 {main}"}

 I need to show an error like "Payment failed: Reason - Message"

 

How can i do this ?

 

Thanks

3 REPLIES 3

Re: Show custom payment method error

Hello,

 

Any luck on this? I am having the same issue.

Re: Show custom payment method error

Hello,

 

I have had the same issue and found away around this with the following steps:

 

Edit the function savePaymentInformationAndPlaceOrder in vendor/magento/module-checkout/Model/PaymentInformationManagement.php

or for Guest orders in vendor/magento/module-checkout/Model/GuestPaymentInformationManagement.php

 

Change the function from:

public function savePaymentInformationAndPlaceOrder(
        $cartId,
        $email,
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
        \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
    ) {
        $this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
        try {
            $orderId = $this->cartManagement->placeOrder($cartId);
        } catch (\Exception $e) {
            throw new CouldNotSaveException(
                __('An error occurred on the server. Please try to place the order again.'),
                $e
            );
        }
        return $orderId;
    }

to:

public function savePaymentInformationAndPlaceOrder(
        $cartId,
        $email,
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
        \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
    ) {
        $this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
        try {
            $orderId = $this->cartManagement->placeOrder($cartId);
        } catch (\Exception $e) {
           var_dump($e);
            throw new CouldNotSaveException(
                __('An error occurred on the server. Please try to place the order again.'),
                $e
            );
        }
        return $orderId;
    }

So there has only been added a var_dump() to display the actual error.

 

When you changed this the error will be shown in the Network tab in your browser. (Open the element inspector go to the Network tab and click on the payment-information request en open the preview and you will see the actual error). You will probably be redirected to the cart so I recommend to enable Preserver Log in the Network tab.

 

This is just a work around for now and I am trying to find a clean way to maybe log the error. I will keep you posted as soon as I have a good solution.

 

Let me know if the above answer was the solution to find the problem in the checkout.

 

 

Re: Show custom payment method error

var_dump($e) is not good idea for debug ... use var_dump($e->getMessage()) ...it's worked and i can see all errors