Commit 1921cd0f642 for woocommerce
commit 1921cd0f6420ce371971b79fe904170f53c7b5d0
Author: Jorge A. Torres <jorge.torres@automattic.com>
Date: Thu Nov 26 10:06:22 2026 -0500
Keep the place-order debug log for unexpected checkout failures (HTTP 5xx errors) (#70313)
* Keep the place-order debug log for unexpected checkout failures
* Add changelog entry for the place-order debug log fix
* Keep the place-order debug log when a gateway errors out without throwing
* Build the checkout response before logging, so a response-build failure still gets logged
* Use the public PaymentResult status getter instead of reaching into the protected property
* Reword the FAIL-step comment in plain language
* Use the public PaymentResult status getter in the #9 log context too
* Move the place-order-debug test cleanup into tearDown so a failed assertion can't skip it
* Add a test for a gateway that reports a payment error without throwing
* Trim two test comments
diff --git a/plugins/woocommerce/changelog/62410-keep-place-order-debug-log-for-server-errors b/plugins/woocommerce/changelog/62410-keep-place-order-debug-log-for-server-errors
new file mode 100644
index 00000000000..284e3cb731c
--- /dev/null
+++ b/plugins/woocommerce/changelog/62410-keep-place-order-debug-log-for-server-errors
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Keep the place order debug log for unexpected checkout failures instead of deleting it.
diff --git a/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php b/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
index 431978fcb9b..1842162a309 100644
--- a/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
+++ b/plugins/woocommerce/src/StoreApi/Routes/V1/Checkout.php
@@ -219,14 +219,15 @@ class Checkout extends AbstractCartRoute {
}
if ( $request->get_method() === \WP_REST_Server::CREATABLE ) {
- // Step logs the exception. If nothing abnormal occurred during the place order POST request, flow the log is removed.
+ // Step logs the exception. If nothing abnormal occurred (a 4xx failure, like a validation error or a decline), the log is removed.
+ // If something unexpected happened (a 5xx failure), the log is kept so it can be debugged.
wc_log_order_step(
'[Store API #FAIL] Placing Order failed',
array(
'status' => $response->get_status(),
'data' => $response->get_data(),
),
- true
+ $response->get_status() < 500
);
}
}
@@ -767,6 +768,15 @@ class Checkout extends AbstractCartRoute {
$this->process_without_payment( $request, $payment_result );
}
+ // Build the response before logging, so a failure here still gets logged.
+ $response = $this->prepare_item_for_response(
+ (object) [
+ 'order' => wc_get_order( $order ),
+ 'payment_result' => $payment_result,
+ ],
+ $request
+ );
+
wc_log_order_step(
'[Store API #9] Order processed',
array(
@@ -774,16 +784,10 @@ class Checkout extends AbstractCartRoute {
'processed_with_payment' => $order->needs_payment() ? 'yes' : 'no',
'payment_status' => $payment_result->get_status(),
),
- true
+ 'error' !== $payment_result->get_status()
);
- return $this->prepare_item_for_response(
- (object) [
- 'order' => wc_get_order( $order ),
- 'payment_result' => $payment_result,
- ],
- $request
- );
+ return $response;
}
/**
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 b19340e332b..0946957f434 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php
@@ -190,6 +190,9 @@ class Checkout extends \WP_Test_REST_TestCase {
*/
protected function tearDown(): void {
try {
+ // Resets wc_log_order_step()'s static state between tests.
+ wc_log_order_step( 'Test cleanup', null, true );
+
$this->remove_inserted_tax_rates();
remove_filter( 'woocommerce_set_cookie_enabled', array( $this, 'filter_woocommerce_set_cookie_enabled' ) );
@@ -7069,6 +7072,97 @@ class Checkout extends \WP_Test_REST_TestCase {
$this->assertTrue( $orders[0]->has_status( OrderStatus::PENDING ), 'The order never took payment, so it must be left awaiting payment.' );
}
+ /**
+ * Checkout failures and whether the debug log should survive each one.
+ *
+ * @return array<string, array{\Throwable, int, string, bool}>
+ */
+ public function provider_checkout_failures_and_expected_debug_log_outcome(): array {
+ return array(
+ 'unexpected server error' => array(
+ new \Error( 'Extension raised an engine error while taking payment.' ),
+ 500,
+ 'woocommerce_rest_unknown_server_error',
+ true,
+ ),
+ 'ordinary client error (declined payment)' => array(
+ new \Exception( 'Card declined.' ),
+ 400,
+ 'woocommerce_rest_checkout_process_payment_error',
+ false,
+ ),
+ );
+ }
+
+ /**
+ * @testdox The place-order debug log is kept only for an unexpected server error, not an ordinary client error: $_dataName.
+ * @dataProvider provider_checkout_failures_and_expected_debug_log_outcome
+ *
+ * @param \Throwable $failure The failure raised while the gateway is taking payment.
+ * @param int $expected_status The HTTP status the checkout should fail with.
+ * @param string $expected_code The REST error code the checkout should fail with.
+ * @param bool $log_is_kept Whether the debug log trail should survive this failure.
+ */
+ public function test_debug_log_kept_only_for_unexpected_server_errors( \Throwable $failure, int $expected_status, string $expected_code, bool $log_is_kept ) {
+ add_action(
+ 'woocommerce_rest_checkout_process_payment_with_context',
+ function () use ( $failure ) {
+ throw $failure;
+ },
+ 998
+ );
+
+ $response = rest_get_server()->dispatch( $this->build_checkout_post_request() );
+
+ $this->assertSame( $expected_status, $response->get_status(), print_r( $response->get_data(), true ) );
+ $this->assertSame( $expected_code, $response->get_data()['code'] );
+
+ $orders = wc_get_orders(
+ array(
+ 'limit' => -1,
+ 'status' => 'any',
+ )
+ );
+ $this->assertCount( 1, $orders, 'Exactly one order should exist.' );
+
+ if ( $log_is_kept ) {
+ $this->assertStringStartsWith( 'place-order-debug-', $orders[0]->get_meta( '_debug_log_source' ), 'The debug log trail must be kept for an unexpected server error, not cleared.' );
+ $this->assertEmpty( $orders[0]->get_meta( '_debug_log_source_pending_deletion' ), 'The debug log must not be scheduled for deletion on an unexpected server error.' );
+ } else {
+ $this->assertEmpty( $orders[0]->get_meta( '_debug_log_source' ), 'The debug log trail must be cleared for an ordinary client error.' );
+ $this->assertStringStartsWith( 'place-order-debug-', $orders[0]->get_meta( '_debug_log_source_pending_deletion' ), 'The debug log must be scheduled for deletion on an ordinary client error.' );
+ }
+ }
+
+ /**
+ * @testdox The place-order debug log is kept when a gateway reports an error without throwing.
+ */
+ public function test_debug_log_kept_when_gateway_reports_error_without_throwing() {
+ add_action(
+ 'woocommerce_rest_checkout_process_payment_with_context',
+ function ( $context, &$payment_result ) {
+ $payment_result->set_status( 'error' );
+ },
+ 998,
+ 2
+ );
+
+ $response = rest_get_server()->dispatch( $this->build_checkout_post_request() );
+
+ $this->assertSame( 500, $response->get_status(), print_r( $response->get_data(), true ) );
+
+ $orders = wc_get_orders(
+ array(
+ 'limit' => -1,
+ 'status' => 'any',
+ )
+ );
+ $this->assertCount( 1, $orders, 'Exactly one order should exist.' );
+
+ $this->assertStringStartsWith( 'place-order-debug-', $orders[0]->get_meta( '_debug_log_source' ), 'The debug log trail must be kept when a gateway reports an error, not cleared.' );
+ $this->assertEmpty( $orders[0]->get_meta( '_debug_log_source_pending_deletion' ), 'The debug log must not be scheduled for deletion when a gateway reports an error.' );
+ }
+
/**
* @testdox An Error raised inside a status transition after the gateway moved the order on is recovered.
*/