Magento - How to stop invoicing orders on failed payment transactions

Magento - How to stop invoicing orders on failed payment transactions

July 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How to stop invoicing orders on failed payment transactions

Stopping Invoice Creation for Failed Payment Transactions in Magento

As a senior developer handling custom payment modules in Magento, you’ve encountered a common scenario - preventing invoicing for orders with failed payment transactions while saving them as “pending_payment”. This guide will walk you through the process of achieving this without sending new order emails and invoices or creating them.

Understanding the Current Setup

You’re currently using an Observer on the sales_order_place_after event in your custom payment module to invoice orders upon successful transactions. The module is written in PHP, as evident from the provided code snippets:

  1. PaymentMethod.php: This file contains the capture method for handling pre-authorization and transaction completion.
  2. Observer.php: This file implements an Observer for checking order status and processing it if necessary.

Modifying PaymentMethod.php

The primary modification will occur in your PaymentMethod.php file, specifically within the capture method:

public function capture(Varien_Object $payment, $amount)
{
    //... existing code ...

    if ($authResponse->getResponseCode() > self::ERROR_CODE_LIMIT && $authResponse->getResponseCode() < self::ERROR_CODE_UPPER_LIMIT) {
        $order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, 'pending_payment', '', false)->save();
        $order->setCanSendNewEmailFlag(false);
        $order->setCanInvoiceFlag(false);

    } else if ($authResponse->getResponseCode() > self::ERROR_CODE_UPPER_LIMIT) {
        // Handle severe errors by setting the order to pending payment and marking it as unable to invoice
        $order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, 'pending_payment', '', false)->save();
        $order->setCanSendNewEmailFlag(false);
        $order->setCanInvoiceFlag(false);

    } else {
        // Handle minor errors by setting the order to pending payment and marking it as unable to invoice
        $order->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT, 'pending_payment', '', false)->save();
        $order->setCanSendNewEmailFlag(false);
        $order->setCanInvoiceFlag(false);
    }

    //... remaining code ...
}

In this updated code, regardless of the severity of the failed transaction response code:

  • The order is set to a STATE_PENDING_PAYMENT status.
  • The ability to send new emails and invoice is disabled.

Redirecting Customers upon Failed Payment

To redirect customers to the cart page with an appropriate message after a failed payment, you’ll need to implement this logic in your custom gateway’s response handling. Here’s a basic example:

if ($authResponse->getResponseCode() > self::ERROR_CODE_LIMIT) {
    // Redirect customer to cart or checkout page
    Mage::app()->getFrontController()->getAction()->getResponse()->setHttpResponseCode(302);
    $baseUrl = Mage::getSingleton('core/url')->getBaseUrl();
    header("Location: {$baseUrl}checkout/cart");
    exit;
}

This code snippet checks if the response code indicates an error, and if so, it redirects customers to the checkout/cart page. You can replace the URL with your desired redirection target.

The Role of Observer.php

The Observer.php file is currently checking for specific order states and processing them accordingly:

public function implementOrderStatus($event)
{
    $order = $event->getEvent()->getOrder();

    if ($this->_getPaymentMethod($order) == 'custompaymentmodule') {
        //... existing code ...
    }

    return $this;
}

Since you’re already setting CanInvoiceFlag to false in PaymentMethod.php, this Observer is no longer necessary for preventing invoicing. However, it remains a good practice to check the payment method before processing any actions.

Conclusion

Preventing invoicing and redirection upon failed payment transactions involves strategic modification of your custom payment module’s processing logic:

  • In the PaymentMethod.php file, update the capture method to handle various error scenarios by setting the order status to “pending_payment” and disabling invoice generation.
  • Add a basic redirection mechanism in the same file to redirect customers to the cart or checkout page upon receiving an error response from your payment gateway.

Remember to thoroughly test these changes in a development environment before deploying them to your live Magento store. Proper error handling is crucial for maintaining customer trust and ensuring smooth transactions.

For more advanced customization of payment gateways, consider exploring Laravel Company’s resources on Magento development. We provide comprehensive guides and services tailored to enhance your e-commerce experience.

Happy coding!

Extensions Email Orders Errors

Our network