Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error message on 402 status code #8443

Merged
merged 9 commits into from
Mar 25, 2024
4 changes: 4 additions & 0 deletions changelog/fix-402-status-code-messaging
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

fix: error message on 402 status code
13 changes: 11 additions & 2 deletions includes/class-wc-payments-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test-class-wc-payments-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) );
}
}
Loading