From 1e702ac936cd34533dfd360d9bcdb2564a720b09 Mon Sep 17 00:00:00 2001 From: frosso Date: Fri, 22 Mar 2024 13:30:41 +0100 Subject: [PATCH 1/3] fix: error message on 402 status code --- changelog/fix-402-status-code-messaging | 4 ++++ includes/class-wc-payments-utils.php | 13 +++++++++++-- tests/unit/test-class-wc-payments-utils.php | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 changelog/fix-402-status-code-messaging diff --git a/changelog/fix-402-status-code-messaging b/changelog/fix-402-status-code-messaging new file mode 100644 index 00000000000..bbecb389453 --- /dev/null +++ b/changelog/fix-402-status-code-messaging @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +fix: error message on 402 status code diff --git a/includes/class-wc-payments-utils.php b/includes/class-wc-payments-utils.php index 8f56863e34f..8dc1338787c 100644 --- a/includes/class-wc-payments-utils.php +++ b/includes/class-wc-payments-utils.php @@ -593,10 +593,19 @@ public static function get_filtered_error_message( Exception $e ) { * @return int */ public static function get_filtered_error_status_code( Exception $e ) : int { + $status_code = null; if ( $e instanceof API_Exception ) { - return $e->get_http_code() ?? 400; + $status_code = $e->get_http_code(); } - return 400; + + // Hosting companies might use the 402 status code to return a custom error page. + // When 402 is returned by Stripe, let's return 400 instead. + // The frontend doesn't make use of the status code. + if ( 402 === $status_code ) { + $status_code = 400; + } + + return $status_code ?? 400; } /** diff --git a/tests/unit/test-class-wc-payments-utils.php b/tests/unit/test-class-wc-payments-utils.php index 0248b757f3b..07ec0c03b36 100644 --- a/tests/unit/test-class-wc-payments-utils.php +++ b/tests/unit/test-class-wc-payments-utils.php @@ -544,4 +544,16 @@ public function provider_format_explicit_currency(): array { 'VND (decimal currency) - not skip symbol' => [ 123456, 'VND', false, [], '123.456 ₫ VND' ], ]; } + + public function test_get_filtered_error_status_code_with_exception() { + $this->assertSame(400, WC_Payments_Utils::get_filtered_error_status_code(new Exception('Just an exception'))); + } + + public function test_get_filtered_error_status_code_with_api_exception() { + $this->assertSame(401, WC_Payments_Utils::get_filtered_error_status_code(new \WCPay\Exceptions\API_Exception( 'Error: Your card has insufficient funds.', 'card_declined', 401 ))); + } + + public function test_get_filtered_error_status_code_with_api_exception_and_402_status() { + $this->assertSame(400, WC_Payments_Utils::get_filtered_error_status_code(new \WCPay\Exceptions\API_Exception( 'Error: Your card was declined.', 'card_declined', 402 ))); + } } From 33c6c380211473f5b0cd4e12da05711574de7ff4 Mon Sep 17 00:00:00 2001 From: frosso Date: Fri, 22 Mar 2024 13:31:00 +0100 Subject: [PATCH 2/3] fix: error message on 402 status code --- tests/unit/test-class-wc-payments-utils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/test-class-wc-payments-utils.php b/tests/unit/test-class-wc-payments-utils.php index 07ec0c03b36..493d00f0f9b 100644 --- a/tests/unit/test-class-wc-payments-utils.php +++ b/tests/unit/test-class-wc-payments-utils.php @@ -546,14 +546,14 @@ public function provider_format_explicit_currency(): array { } public function test_get_filtered_error_status_code_with_exception() { - $this->assertSame(400, WC_Payments_Utils::get_filtered_error_status_code(new Exception('Just an exception'))); + $this->assertSame( 400, WC_Payments_Utils::get_filtered_error_status_code( new Exception( 'Just an exception' ) ) ); } public function test_get_filtered_error_status_code_with_api_exception() { - $this->assertSame(401, WC_Payments_Utils::get_filtered_error_status_code(new \WCPay\Exceptions\API_Exception( 'Error: Your card has insufficient funds.', 'card_declined', 401 ))); + $this->assertSame( 401, WC_Payments_Utils::get_filtered_error_status_code( new \WCPay\Exceptions\API_Exception( 'Error: Your card has insufficient funds.', 'card_declined', 401 ) ) ); } public function test_get_filtered_error_status_code_with_api_exception_and_402_status() { - $this->assertSame(400, WC_Payments_Utils::get_filtered_error_status_code(new \WCPay\Exceptions\API_Exception( 'Error: Your card was declined.', 'card_declined', 402 ))); + $this->assertSame( 400, WC_Payments_Utils::get_filtered_error_status_code( new \WCPay\Exceptions\API_Exception( 'Error: Your card was declined.', 'card_declined', 402 ) ) ); } } From 82b41d346de5796dbb6b7fd181255465d3f7d00a Mon Sep 17 00:00:00 2001 From: Francesco Date: Mon, 25 Mar 2024 12:15:50 +0100 Subject: [PATCH 3/3] Update class-wc-payments-utils.php --- includes/class-wc-payments-utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-wc-payments-utils.php b/includes/class-wc-payments-utils.php index 8f4b7f6e094..8dc1338787c 100644 --- a/includes/class-wc-payments-utils.php +++ b/includes/class-wc-payments-utils.php @@ -593,6 +593,7 @@ public static function get_filtered_error_message( Exception $e ) { * @return int */ public static function get_filtered_error_status_code( Exception $e ) : int { + $status_code = null; if ( $e instanceof API_Exception ) { $status_code = $e->get_http_code(); }