From deab5fbc626232b4c361ab55efefddd33a05165c Mon Sep 17 00:00:00 2001 From: hsingyuc Date: Wed, 15 May 2024 20:58:57 -0400 Subject: [PATCH 1/6] Add New_Process_Payment_Exception --- includes/class-wc-payment-gateway-wcpay.php | 6 +++--- includes/class-wc-payments.php | 1 + .../class-new-process-payment-exception.php | 16 ++++++++++++++++ .../unit/test-class-wc-payment-gateway-wcpay.php | 9 +++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 includes/exceptions/class-new-process-payment-exception.php diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index fdb12b247e3..3f15a491192 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -1072,8 +1072,8 @@ function_exists( 'wcs_order_contains_subscription' ) * and if the answer is yes, uses it and returns the result. * * @param WC_Order $order Order that needs payment. - * @return array|null Array if processed, null if the new process is not supported. - * @throws Exception If the payment process could not be completed. + * @return array|null Array if processed, null if the new process is not supported. + * @throws New_Process_Payment_Exception Error processing the payment. */ public function new_process_payment( WC_Order $order ) { $manual_capture = $this->get_capture_type() === Payment_Capture_Type::MANUAL(); @@ -1119,7 +1119,7 @@ public function new_process_payment( WC_Order $order ) { ]; } - throw new Exception( __( 'The payment process could not be completed.', 'woocommerce-payments' ) ); + throw new New_Process_Payment_Exception( __( 'The payment process could not be completed.', 'woocommerce-payments' ) ); } /** diff --git a/includes/class-wc-payments.php b/includes/class-wc-payments.php index 3a70e64758f..c459d0cefec 100644 --- a/includes/class-wc-payments.php +++ b/includes/class-wc-payments.php @@ -428,6 +428,7 @@ public static function init() { 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-new-process-payment-exception.php'; include_once __DIR__ . '/exceptions/class-order-not-found-exception.php'; include_once __DIR__ . '/exceptions/class-order-id-mismatch-exception.php'; include_once __DIR__ . '/exceptions/class-rate-limiter-enabled-exception.php'; diff --git a/includes/exceptions/class-new-process-payment-exception.php b/includes/exceptions/class-new-process-payment-exception.php new file mode 100644 index 00000000000..742c1415b98 --- /dev/null +++ b/includes/exceptions/class-new-process-payment-exception.php @@ -0,0 +1,16 @@ +card_gateway->process_payment( $order->get_id() ); + + $this->expectException( New_Process_Payment_Exception::class ); + $this->expectExceptionMessage( 'The payment process could not be completed.' ); + } + public function test_process_payment_rate_limiter_enabled_throw_exception() { $order = WC_Helper_Order::create_order(); From 75b2d98e83fc048a9395d178f7c7bd7f06233808 Mon Sep 17 00:00:00 2001 From: hsingyuc Date: Thu, 16 May 2024 19:22:53 -0400 Subject: [PATCH 2/6] Add new process payment exception to the previous --- includes/class-wc-payment-gateway-wcpay.php | 4 ++-- tests/unit/test-class-wc-payment-gateway-wcpay.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index 3f15a491192..f17c7eb3515 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -1073,7 +1073,7 @@ function_exists( 'wcs_order_contains_subscription' ) * * @param WC_Order $order Order that needs payment. * @return array|null Array if processed, null if the new process is not supported. - * @throws New_Process_Payment_Exception Error processing the payment. + * @throws Exception Error processing the payment. */ public function new_process_payment( WC_Order $order ) { $manual_capture = $this->get_capture_type() === Payment_Capture_Type::MANUAL(); @@ -1119,7 +1119,7 @@ public function new_process_payment( WC_Order $order ) { ]; } - throw new New_Process_Payment_Exception( __( 'The payment process could not be completed.', 'woocommerce-payments' ) ); + throw new Exception( __( 'The payment process could not be completed.', 'woocommerce-payments' ), 0, new New_Process_Payment_Exception() ); } /** diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay.php b/tests/unit/test-class-wc-payment-gateway-wcpay.php index f3e1b1b99d0..bbb8e58d6d7 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay.php @@ -3666,7 +3666,7 @@ public function test_new_process_payment_throw_exception() { $this->card_gateway->process_payment( $order->get_id() ); - $this->expectException( New_Process_Payment_Exception::class ); + $this->expectException( Exception::class ); $this->expectExceptionMessage( 'The payment process could not be completed.' ); } From d4580bb599c576424a1c5184e34c3ce02fc009ac Mon Sep 17 00:00:00 2001 From: hsingyuc Date: Fri, 24 May 2024 14:06:08 -0400 Subject: [PATCH 3/6] Pass arguemnts to New_Process_Payment_Exception --- includes/class-wc-payment-gateway-wcpay.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index f17c7eb3515..d740aa4df5e 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -1119,7 +1119,14 @@ public function new_process_payment( WC_Order $order ) { ]; } - throw new Exception( __( 'The payment process could not be completed.', 'woocommerce-payments' ), 0, new New_Process_Payment_Exception() ); + throw new Exception( + __( 'The payment process could not be completed.', 'woocommerce-payments' ), + 0, + new New_Process_Payment_Exception( + __( 'The payment process could not be completed.', 'woocommerce-payments' ), + 'new_process_payment' + ) + ); } /** From 39f5231994927c3e35bd605e9baa312778ce37db Mon Sep 17 00:00:00 2001 From: hsingyuc Date: Fri, 24 May 2024 14:09:59 -0400 Subject: [PATCH 4/6] Add changelog entry --- changelog/add-new-process-payment-exception | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/add-new-process-payment-exception diff --git a/changelog/add-new-process-payment-exception b/changelog/add-new-process-payment-exception new file mode 100644 index 00000000000..f2ac469fefc --- /dev/null +++ b/changelog/add-new-process-payment-exception @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Add New_Process_Payment_Exception From 74948eeb0e4dfe8238c6a9b27cffad9982f481a5 Mon Sep 17 00:00:00 2001 From: hsingyuc Date: Tue, 28 May 2024 20:58:00 -0400 Subject: [PATCH 5/6] Fix phpunit test --- .../test-class-wc-payment-gateway-wcpay.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay.php b/tests/unit/test-class-wc-payment-gateway-wcpay.php index bbb8e58d6d7..e39a0c73b91 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay.php @@ -27,6 +27,7 @@ use WCPay\Internal\Payment\Factor; use WCPay\Internal\Payment\Router; use WCPay\Internal\Payment\State\CompletedState; +use WCPay\Internal\Payment\State\PaymentErrorState; use WCPay\Internal\Service\Level3Service; use WCPay\Internal\Service\OrderService; use WCPay\Internal\Service\PaymentProcessingService; @@ -3662,12 +3663,31 @@ public function test_new_process_payment() { } public function test_new_process_payment_throw_exception() { - $order = WC_Helper_Order::create_order(); + // The new payment process is only accessible in dev mode. + WC_Payments::mode()->dev(); - $this->card_gateway->process_payment( $order->get_id() ); + $mock_service = $this->createMock( PaymentProcessingService::class ); + $mock_router = $this->createMock( Router::class ); + $order = WC_Helper_Order::create_order(); + $mock_state = $this->createMock( PaymentErrorState::class ); + + wcpay_get_test_container()->replace( PaymentProcessingService::class, $mock_service ); + wcpay_get_test_container()->replace( Router::class, $mock_router ); + + $mock_router->expects( $this->once() ) + ->method( 'should_use_new_payment_process' ) + ->willReturn( true ); + + // Assert: The new service is called. + $mock_service->expects( $this->once() ) + ->method( 'process_payment' ) + ->with( $order->get_id() ) + ->willReturn( $mock_state ); $this->expectException( Exception::class ); $this->expectExceptionMessage( 'The payment process could not be completed.' ); + + $this->card_gateway->process_payment( $order->get_id() ); } public function test_process_payment_rate_limiter_enabled_throw_exception() { From 80a2ccfc6db482b8a245f180731d8d5091fe0e3f Mon Sep 17 00:00:00 2001 From: hsingyuc Date: Tue, 28 May 2024 21:01:43 -0400 Subject: [PATCH 6/6] Add New_Process_Payment_Exception after rebase --- includes/class-wc-payment-gateway-wcpay.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-payment-gateway-wcpay.php b/includes/class-wc-payment-gateway-wcpay.php index d740aa4df5e..0ca27dd3788 100644 --- a/includes/class-wc-payment-gateway-wcpay.php +++ b/includes/class-wc-payment-gateway-wcpay.php @@ -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, Fraud_Prevention_Enabled_Exception, Invalid_Phone_Number_Exception, Rate_Limiter_Enabled_Exception, Order_ID_Mismatch_Exception, Order_Not_Found_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, Order_ID_Mismatch_Exception, Order_Not_Found_Exception, New_Process_Payment_Exception }; use WCPay\Core\Server\Request\Cancel_Intention; use WCPay\Core\Server\Request\Capture_Intention; use WCPay\Core\Server\Request\Create_And_Confirm_Intention;