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

Pass previous Exception with Exception #8824

Merged
merged 9 commits into from
May 22, 2024
4 changes: 4 additions & 0 deletions changelog/update-pass-previous-exception-with-exception
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Pass previous exception with exception
11 changes: 5 additions & 6 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use WCPay\Constants\Intent_Status;
use WCPay\Constants\Payment_Type;
use WCPay\Constants\Payment_Method;
use WCPay\Exceptions\{ Add_Payment_Method_Exception, Amount_Too_Small_Exception, Process_Payment_Exception, Intent_Authentication_Exception, API_Exception, Invalid_Address_Exception};
use WCPay\Exceptions\{ Add_Payment_Method_Exception, Amount_Too_Small_Exception, Process_Payment_Exception, Intent_Authentication_Exception, API_Exception, Invalid_Address_Exception, Fraud_Prevention_Enabled_Exception, Invalid_Phone_Number_Exception, Rate_Limiter_Enabled_Exception };
use WCPay\Core\Server\Request\Cancel_Intention;
use WCPay\Core\Server\Request\Capture_Intention;
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
Expand Down Expand Up @@ -1128,7 +1128,6 @@ public function new_process_payment( WC_Order $order ) {
* @param int $order_id Order ID to process the payment for.
*
* @return array|null An array with result of payment and redirect URL, or nothing.
* @throws Process_Payment_Exception Error processing the payment.
* @throws Exception Error processing the payment.
*/
public function process_payment( $order_id ) {
Expand All @@ -1141,7 +1140,7 @@ public function process_payment( $order_id ) {

try {
if ( 20 < strlen( $order->get_billing_phone() ) ) {
throw new Process_Payment_Exception(
throw new Invalid_Phone_Number_Exception(
__( 'Invalid phone number.', 'woocommerce-payments' ),
'invalid_phone_number'
);
Expand All @@ -1151,15 +1150,15 @@ public function process_payment( $order_id ) {
$fraud_prevention_service = Fraud_Prevention_Service::get_instance();
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( $fraud_prevention_service->is_enabled() && ! $fraud_prevention_service->verify_token( $_POST['wcpay-fraud-prevention-token'] ?? null ) ) {
throw new Process_Payment_Exception(
throw new Fraud_Prevention_Enabled_Exception(
__( "We're not able to process this payment. Please refresh the page and try again.", 'woocommerce-payments' ),
'fraud_prevention_enabled'
);
}
}

if ( $this->failed_transaction_rate_limiter->is_limited() ) {
throw new Process_Payment_Exception(
throw new Rate_Limiter_Enabled_Exception(
__( 'Your payment was not processed.', 'woocommerce-payments' ),
'rate_limiter_enabled'
);
Expand Down Expand Up @@ -1281,7 +1280,7 @@ public function process_payment( $order_id ) {

// Re-throw the exception after setting everything up.
// This makes the error notice show up both in the regular and block checkout.
throw new Exception( WC_Payments_Utils::get_filtered_error_message( $e, $blocked_by_fraud_rules ) );
throw new Exception( WC_Payments_Utils::get_filtered_error_message( $e, $blocked_by_fraud_rules ), 0, $e );
}
}

Expand Down
3 changes: 3 additions & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,13 @@ public static function init() {
include_once __DIR__ . '/exceptions/class-intent-authentication-exception.php';
include_once __DIR__ . '/exceptions/class-invalid-payment-method-exception.php';
include_once __DIR__ . '/exceptions/class-process-payment-exception.php';
include_once __DIR__ . '/exceptions/class-invalid-phone-number-exception.php';
include_once __DIR__ . '/exceptions/class-invalid-webhook-data-exception.php';
include_once __DIR__ . '/exceptions/class-invalid-price-exception.php';
include_once __DIR__ . '/exceptions/class-fraud-ruleset-exception.php';
include_once __DIR__ . '/exceptions/class-fraud-prevention-enabled-exception.php';
include_once __DIR__ . '/exceptions/class-order-not-found-exception.php';
include_once __DIR__ . '/exceptions/class-rate-limiter-enabled-exception.php';
include_once __DIR__ . '/constants/class-base-constant.php';
include_once __DIR__ . '/constants/class-country-code.php';
include_once __DIR__ . '/constants/class-currency-code.php';
Expand Down
16 changes: 16 additions & 0 deletions includes/exceptions/class-fraud-prevention-enabled-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Class Fraud_Prevention_Enabled_Exception
*
* @package WooCommerce\Payments
*/

namespace WCPay\Exceptions;

defined( 'ABSPATH' ) || exit;

/**
* Exception for throwing an error when fraud prevension service is enabled.
*/
class Fraud_Prevention_Enabled_Exception extends Process_Payment_Exception {
}
16 changes: 16 additions & 0 deletions includes/exceptions/class-invalid-phone-number-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Class Invalid_Phone_Number_Exception
*
* @package WooCommerce\Payments
*/

namespace WCPay\Exceptions;

defined( 'ABSPATH' ) || exit;

/**
* Exception for throwing an error when phone number is invalid.
*/
class Invalid_Phone_Number_Exception extends Process_Payment_Exception {
}
16 changes: 16 additions & 0 deletions includes/exceptions/class-rate-limiter-enabled-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Class Rate_Limiter_Enabled_Exception
*
* @package WooCommerce\Payments
*/

namespace WCPay\Exceptions;

defined( 'ABSPATH' ) || exit;

/**
* Exception for throwing an error when rate limiter is enabled.
*/
class Rate_Limiter_Enabled_Exception extends Process_Payment_Exception {
}
48 changes: 39 additions & 9 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use WCPay\Duplicates_Detection_Service;
use WCPay\Exceptions\Amount_Too_Small_Exception;
use WCPay\Exceptions\API_Exception;
use WCPay\Exceptions\Fraud_Prevention_Enabled_Exception;
use WCPay\Exceptions\Process_Payment_Exception;
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
use WCPay\Internal\Payment\Factor;
Expand Down Expand Up @@ -824,9 +825,13 @@ public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
$order = WC_Helper_Order::create_order();
$order->set_billing_phone( '+1123456789123456789123' );
$order->save();
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Invalid phone number.' );
$this->card_gateway->process_payment( $order->get_id() );
try {
$this->card_gateway->process_payment( $order->get_id() );
} catch ( Exception $e ) {
$this->assertEquals( 'Exception', get_class( $e ) );
$this->assertEquals( 'Invalid phone number.', $e->getMessage() );
$this->assertEquals( 'WCPay\Exceptions\Invalid_Phone_Number_Exception', get_class( $e->getPrevious() ) );
}
}

public function test_remove_link_payment_method_if_card_disabled() {
Expand Down Expand Up @@ -2860,9 +2865,13 @@ public function test_process_payment_rejects_if_missing_fraud_prevention_token()
->method( 'is_enabled' )
->willReturn( true );

$this->expectException( Exception::class );
$this->expectExceptionMessage( "We're not able to process this payment. Please refresh the page and try again." );
$this->card_gateway->process_payment( $order->get_id() );
try {
$this->card_gateway->process_payment( $order->get_id() );
} catch ( Exception $e ) {
$this->assertEquals( 'Exception', get_class( $e ) );
$this->assertEquals( "We're not able to process this payment. Please refresh the page and try again.", $e->getMessage() );
$this->assertEquals( 'WCPay\Exceptions\Fraud_Prevention_Enabled_Exception', get_class( $e->getPrevious() ) );
}
}

public function test_process_payment_rejects_if_invalid_fraud_prevention_token() {
Expand All @@ -2883,9 +2892,13 @@ public function test_process_payment_rejects_if_invalid_fraud_prevention_token()

$_POST['wcpay-fraud-prevention-token'] = 'incorrect-token';

$this->expectException( Exception::class );
$this->expectExceptionMessage( "We're not able to process this payment. Please refresh the page and try again." );
$this->card_gateway->process_payment( $order->get_id() );
try {
$this->card_gateway->process_payment( $order->get_id() );
} catch ( Exception $e ) {
$this->assertEquals( 'Exception', get_class( $e ) );
$this->assertEquals( "We're not able to process this payment. Please refresh the page and try again.", $e->getMessage() );
$this->assertEquals( 'WCPay\Exceptions\Fraud_Prevention_Enabled_Exception', get_class( $e->getPrevious() ) );
}
}

public function test_process_payment_marks_order_as_blocked_for_fraud() {
Expand Down Expand Up @@ -3622,6 +3635,23 @@ public function test_new_process_payment() {
);
}

public function test_process_payment_rate_limiter_enabled_throw_exception() {
$order = WC_Helper_Order::create_order();

$this->mock_rate_limiter
->expects( $this->once() )
->method( 'is_limited' )
->willReturn( true );

try {
$this->card_gateway->process_payment( $order->get_id() );
} catch ( Exception $e ) {
$this->assertEquals( 'Exception', get_class( $e ) );
$this->assertEquals( 'Your payment was not processed.', $e->getMessage() );
$this->assertEquals( 'WCPay\Exceptions\Rate_Limiter_Enabled_Exception', get_class( $e->getPrevious() ) );
}
}

public function test_process_payment_returns_correct_redirect() {
$order = WC_Helper_Order::create_order();
$_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
Expand Down
Loading