Commit 209e7545fa8 for woocommerce
commit 209e7545fa8e12a8c621982e652ecc25685e7765
Author: Neeraj <64060864+Indian-boult@users.noreply.github.com>
Date: Tue Nov 24 20:24:35 2026 +0530
Show a legacy gateway's error in the Checkout block when process_payment() returns nothing (#71295)
* Show a legacy gateway's error in the Checkout block when process_payment() returns nothing
Legacy gateways add an error notice and return nothing when they stop early. Treat a result that is not an array as a failed payment, so the failure path turns the gateway's notice into the shopper's error instead of a TypeError from array_merge() and an HTTP 500.
* Keep checkout recovery for a gateway that took payment and returned nothing
A gateway that moved the order past payment and then returned nothing did take payment. Hand that case to the checkout's existing recovery, which reports success, instead of treating it as a failed payment. Adds a regression test.
* Clear gateway notices before handing a paid order to checkout recovery
The normal path clears notices because the API does not show them. Do the same before the recovery throw, so a gateway error from a paid checkout is not shown on a later page.
* Name the gateway in the no-result recovery message
The order note and log said the gateway returned no result "after taking
payment", but an order the gateway put on-hold reaches this path too.
Name the gateway ID instead, so merchants can tell which plugin to report.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Mike Jolley <mike.jolley@me.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/fix-46927-legacy-gateway-null-result b/plugins/woocommerce/changelog/fix-46927-legacy-gateway-null-result
new file mode 100644
index 00000000000..f99850308c0
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-46927-legacy-gateway-null-result
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Show a payment gateway's own error in the Checkout block when it stops early, instead of an internal server error.
diff --git a/plugins/woocommerce/src/StoreApi/Legacy.php b/plugins/woocommerce/src/StoreApi/Legacy.php
index dddc11c7246..5040174cfef 100644
--- a/plugins/woocommerce/src/StoreApi/Legacy.php
+++ b/plugins/woocommerce/src/StoreApi/Legacy.php
@@ -1,6 +1,7 @@
<?php
namespace Automattic\WooCommerce\StoreApi;
+use Automattic\WooCommerce\Internal\Checkout\PaymentRecovery;
use Automattic\WooCommerce\StoreApi\Payments\PaymentContext;
use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
use Automattic\WooCommerce\StoreApi\Utilities\NoticeHandler;
@@ -53,11 +54,33 @@ class Legacy {
NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_payment_error' );
// Process Payment.
- $gateway_result = $payment_method_object->process_payment( $context->order->get_id() );
+ $order_id = $context->order->get_id();
+ $gateway_result = $payment_method_object->process_payment( $order_id );
// Restore $_POST data.
$_POST = $post_data;
+ // Legacy gateways add an error notice and return nothing when they bail out early, so treat that as a failed payment.
+ if ( ! is_array( $gateway_result ) ) {
+ // A gateway that moved the order past payment before returning nothing must not be reported as failed. Hand it to the checkout's recovery, which reports success.
+ $order = wc_get_order( $order_id );
+ if ( $order instanceof \WC_Order && PaymentRecovery::order_moved_past_payment( $order ) ) {
+ // Notices are not shown from the API, so clear them as the normal path below does.
+ wc_clear_notices();
+ throw new RouteException(
+ 'woocommerce_rest_invalid_payment_result',
+ sprintf(
+ /* translators: %s: payment gateway ID. */
+ esc_html__( 'The %s payment method returned no result.', 'woocommerce' ),
+ esc_html( $payment_method_object->id )
+ ),
+ 500
+ );
+ }
+
+ $gateway_result = array( 'result' => 'failure' );
+ }
+
// If the payment failed with a message, throw an exception.
if ( isset( $gateway_result['result'] ) && 'failure' === $gateway_result['result'] ) {
if ( isset( $gateway_result['message'] ) ) {
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
index 115a24b64fc..b19340e332b 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
@@ -7316,4 +7316,114 @@ class Checkout extends \WP_Test_REST_TestCase {
$this->assertArrayHasKey( 'payment_result', $data );
$this->assertSame( 'success', $data['payment_result']['payment_status'], 'Recovery must still report success on the result the response uses.' );
}
+
+ /**
+ * @testdox A legacy gateway that adds an error notice and returns nothing shows that notice to the shopper.
+ */
+ public function test_legacy_gateway_returning_nothing_shows_its_error_notice() {
+ $this->use_bacs_gateway_returning_nothing( 'The test gateway declined the card.' );
+
+ $response = rest_get_server()->dispatch( $this->build_checkout_post_request() );
+ $data = $response->get_data();
+
+ $this->assertSame( 400, $response->get_status(), 'Unexpected response: ' . print_r( $data, true ) );
+ $this->assertSame( 'woocommerce_rest_checkout_process_payment_error', $data['code'] );
+ $this->assertSame( 'The test gateway declined the card.', $data['message'] );
+ }
+
+ /**
+ * @testdox A legacy gateway that returns nothing without a notice ends the checkout as a failed payment.
+ */
+ public function test_legacy_gateway_returning_nothing_without_a_notice_fails_the_payment() {
+ $this->use_bacs_gateway_returning_nothing( null );
+
+ $response = rest_get_server()->dispatch( $this->build_checkout_post_request() );
+ $data = $response->get_data();
+
+ $this->assertSame( 400, $response->get_status(), 'Unexpected response: ' . print_r( $data, true ) );
+ $this->assertArrayHasKey( 'payment_result', $data );
+ $this->assertSame( 'failure', $data['payment_result']['payment_status'] );
+ }
+
+ /**
+ * @testdox A legacy gateway that takes payment and then returns nothing still completes the checkout.
+ */
+ public function test_legacy_gateway_returning_nothing_after_taking_payment_completes_the_checkout() {
+ $this->use_bacs_gateway_returning_nothing( 'An error the paid shopper should not see later.', true );
+
+ $response = rest_get_server()->dispatch( $this->build_checkout_post_request() );
+ $data = $response->get_data();
+
+ $this->assertSame( 200, $response->get_status(), 'Unexpected response: ' . print_r( $data, true ) );
+ $this->assertArrayHasKey( 'payment_result', $data );
+ $this->assertSame( 'success', $data['payment_result']['payment_status'] );
+ $this->assertSame( 0, wc_notice_count( 'error' ), 'The gateway notice should not stay queued for a later page.' );
+
+ $notes = wp_list_pluck( wc_get_order_notes( array( 'order_id' => $data['order_id'] ) ), 'content' );
+ $this->assertNotEmpty(
+ array_filter(
+ $notes,
+ function ( $note ) {
+ return false !== strpos( $note, 'The bacs payment method returned no result.' );
+ }
+ ),
+ 'The order note should name the gateway that returned no result.'
+ );
+ }
+
+ /**
+ * Replaces BACS with a gateway whose process_payment() returns nothing, as legacy gateways do when they bail out early.
+ *
+ * @param string|null $notice Error notice the gateway adds before returning, or null for none.
+ * @param bool $complete_payment Whether the gateway marks the order paid before returning.
+ */
+ private function use_bacs_gateway_returning_nothing( ?string $notice, bool $complete_payment = false ): void {
+ $gateway = new class( $notice, $complete_payment ) extends WC_Gateway_BACS {
+ /**
+ * Error notice to add, or null for none.
+ *
+ * @var string|null
+ */
+ private $notice;
+
+ /**
+ * Whether to mark the order paid before returning.
+ *
+ * @var bool
+ */
+ private $complete_payment;
+
+ /**
+ * Constructor.
+ *
+ * @param string|null $notice Error notice to add, or null for none.
+ * @param bool $complete_payment Whether to mark the order paid before returning.
+ */
+ public function __construct( ?string $notice, bool $complete_payment ) {
+ parent::__construct();
+ $this->notice = $notice;
+ $this->complete_payment = $complete_payment;
+ }
+
+ /**
+ * Marks the order paid and adds the error notice when asked, then returns nothing.
+ *
+ * @param int $order_id Order ID.
+ */
+ public function process_payment( $order_id ) {
+ if ( $this->complete_payment ) {
+ wc_get_order( $order_id )->payment_complete();
+ }
+ if ( null !== $this->notice ) {
+ wc_add_notice( $this->notice, 'error' );
+ }
+ }
+ };
+
+ foreach ( WC()->payment_gateways()->payment_gateways as $key => $existing ) {
+ if ( WC_Gateway_BACS::ID === $existing->id ) {
+ WC()->payment_gateways()->payment_gateways[ $key ] = $gateway;
+ }
+ }
+ }
}