3

In the Magento\Checkout\Model\PaymentInformationManagement::savePaymentInformationAndPlaceOrder() method all exceptions are caught and CouldNotSaveException is created with a generic message.

I want to make this message user friendly as I am implementing a fairly complex restriction and the generic message would cause lots of confusion for the customer.

Error Message

    public function savePaymentInformationAndPlaceOrder(
    $cartId,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
    $this->savePaymentInformation($cartId, $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;
}

My idea to work around this issue was to create an around plugin so that I could catch the new Exception the throw the previous exception which I create via an observer level check.

public function aroundSavePaymentInformationAndPlaceOrder(
    \Magento\Checkout\Model\PaymentInformationManagement $subject,
    callable $proceed,
    ...$args
) {
    try {
        $result = $proceed(...$args);
    } catch (\Magento\Framework\Exception\CouldNotSaveException $e) {
        throw $e->getPrevious();
    }

    return $result;
}
4
  • you should avoid usages of around plugins, they reduce Magento performance. About custom exception message, Magento provides only high-level errors to make an application more secure, if you use error messages from payment methods as is, you should be sure what you don't make your application vulnerable.
    – joni jones
    Jan 31, 2017 at 9:10
  • @jonijones oh really, do you have any further reading on the performance implications? Regarding the security issue I've created a custom exception and am checking the previous exception is my specific exception before re--throwing it. I was going to include it in the question but thought it might over complicate it.
    – jzahedieh
    Jan 31, 2017 at 11:27
  • Magento dev docs describe the usage of plugins but do not describe negative aspects. The common issue of around plugins, it's how they implemented in Magento, a chain of around plugins consumes a lot of memory. In Magento 2.2 will be removed most of around plugins in core modules.
    – joni jones
    Jan 31, 2017 at 11:51
  • I recently raised an issue on github about a generic high level error which didn't make a great deal of sense. I can understand the reasoning behind wanting to hide low level message from the user. But sometimes the messages are just confusing. Confused customers means confused store owner which means busy developer. May 22, 2019 at 20:42

1 Answer 1

3

This does actually work it was just the di.xml wasn't triggering on the if it was defined in etc/frontend/di.xml but was fine in etc/di.xml.

Unsure why but it seems to be a decent way of working around Magento's decision to use a generic exception.

<type name="Magento\Checkout\Model\PaymentInformationManagement">
    <plugin name="namespaceModulePaymentInformationManagement"
            type="Namespace\Module\Plugin\Checkout\Model\PaymentInformationManagement"/>
</type>

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.