WooCommerce: Auto-Cancel Orders After 3 Failed Payments

You’re under a carding attack, and the bot tries to submit the payment for a given order 300 times… Which means 300 failed order emails, and a database mess.

WooCommerce doesn’t limit how many times a customer (or bot) can attempt to pay for a failed order via the “Order Pay” page. This opens the door to abuse, unnecessary clutter in the order table, and frustration for both store owners and customers.

One way to mitigate this is by tracking how many times a failed order has been retried and automatically cancelling it after the third attempt. This solution keeps things clean, secure, and under control.

Dozens of failed order emails flooding the inbox during a carding attack. For the same order ID!

PHP Snippet: Limit Payment Retries on Failed Orders @ WooCommerce Order Pay Page (WooCommerce Stripe Payment Gateway)

The following snippet is based on the official WooCommerce Stripe Payment Gateway plugin, but you can adapt it to any other payment provider, as long as they provide a “hook” upon failed payment.

This code snippet automatically cancels WooCommerce orders after three failed Stripe payment attempts. When a customer tries to pay with Stripe and the payment fails (due to declined cards, insufficient funds, or other issues), this function runs automatically.

The code keeps track of how many times payment has failed for each order by storing a counter in the order’s metadata. Each time a Stripe payment fails, it increases this counter by one and saves it to the database.

If this is the customer’s first or second failed attempt, nothing dramatic happens – they can try paying again. However, once the payment fails for the third time, the system automatically changes the order status to “cancelled” and adds a note explaining why.

This prevents customers (and bots) from making endless payment attempts on the same order and helps store owners manage failed transactions more efficiently. It’s particularly useful for preventing fraud attempts or clearing out orders that customers have abandoned after multiple failed payment tries.

/**
 * @snippet       Max 3 Stripe Payment Retries @ WooCommerce Order Pay
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'wc_gateway_stripe_process_payment_error', 'bbloomer_handle_stripe_payment_failure', 10, 2 );

function bbloomer_handle_stripe_payment_failure( $error, $order ) {
	
    if ( ! $order || ! is_a( $order, 'WC_Order' ) ) return;
    
    $order_id = $order->get_id();
    
    // Attempt count
    $attempt_count = $order->get_meta( '_stripe_payment_attempts', true );
    $attempt_count = $attempt_count ? intval( $attempt_count ) + 1 : 1;
    $order->update_meta_data( '_stripe_payment_attempts', $attempt_count );
    $order->save_meta_data();
    
    // Cancel order after 3 attempts
    if ( $attempt_count >= 3 ) {
        $order->update_status( 'cancelled', sprintf( 'Order cancelled after %d failed Stripe payment attempts', $attempt_count ) );
    }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *