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

Add new process payment exception #8834

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/add-new-process-payment-exception
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Add New_Process_Payment_Exception
15 changes: 11 additions & 4 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, 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;
Expand Down Expand Up @@ -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 Exception Error processing the payment.
*/
public function new_process_payment( WC_Order $order ) {
$manual_capture = $this->get_capture_type() === Payment_Capture_Type::MANUAL();
Expand Down Expand Up @@ -1119,7 +1119,14 @@ public function new_process_payment( WC_Order $order ) {
];
}

throw new 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(
__( 'The payment process could not be completed.', 'woocommerce-payments' ),
'new_process_payment'
)
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
16 changes: 16 additions & 0 deletions includes/exceptions/class-new-process-payment-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Class New_Process_Payment_Exception
*
* @package WooCommerce\Payments
*/

namespace WCPay\Exceptions;

defined( 'ABSPATH' ) || exit;

/**
* Exception for throwing an error when failed on processing payment.
*/
class New_Process_Payment_Exception extends Process_Payment_Exception {
}
29 changes: 29 additions & 0 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3661,6 +3662,34 @@ public function test_new_process_payment() {
);
}

public function test_new_process_payment_throw_exception() {
// The new payment process is only accessible in dev mode.
WC_Payments::mode()->dev();

$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() {
$order = WC_Helper_Order::create_order();

Expand Down
Loading