<\/div>/' );
- }
-
- public function test_update_payment_intent_adds_customer_save_payment_and_level3_data() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $order_number = $order->get_order_number();
- $product_item = current( $order->get_items( 'line_item' ) );
- $intent_id = 'pi_mock';
- $user = '';
- $customer_id = 'cus_mock';
- $save_payment_method = true;
-
- $this->set_cart_contains_subscription_items( false );
-
- $this->mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'id' => $intent_id,
- 'amount' => 5000,
- ]
- );
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' );
-
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( 5000 );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( 'usd' );
-
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_mock' );
-
- $request->expects( $this->once() )
- ->method( 'setup_future_usage' );
-
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_upe' ] );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $result = $this->mock_upe_gateway->update_payment_intent( $intent_id, $order_id, $save_payment_method );
- }
-
- public function test_update_payment_intent_with_selected_upe_payment_method() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $order_number = $order->get_order_number();
- $product_item = current( $order->get_items( 'line_item' ) );
- $intent_id = 'pi_mock';
- $user = '';
- $customer_id = 'cus_mock';
- $save_payment_method = true;
- $selected_upe_payment_type = 'giropay';
-
- $this->set_cart_contains_subscription_items( false );
-
- $this->mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'id' => $intent_id,
- 'amount' => 5000,
- ]
- );
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' );
-
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( 5000 );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( 'usd' );
-
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_mock' );
-
- $request->expects( $this->once() )
- ->method( 'set_payment_method_types' )
- ->with( [ 'giropay' ] );
-
- $request->expects( $this->once() )
- ->method( 'setup_future_usage' );
-
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_upe' ] );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $result = $this->mock_upe_gateway->update_payment_intent( $intent_id, $order_id, $save_payment_method, $selected_upe_payment_type );
- }
-
- public function test_update_payment_intent_with_payment_country() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent_id = 'pi_mock';
- $order_number = $order->get_order_number();
- $product_item = current( $order->get_items( 'line_item' ) );
-
- $this->set_cart_contains_subscription_items( false );
+ public function test_process_payment_returns_correct_redirect_when_using_saved_payment() {
+ $order = WC_Helper_Order::create_order();
+ $_POST = $this->setup_saved_payment_method();
+ $intent = WC_Helper_Intention::create_intention();
- $this->mock_upe_gateway->expects( $this->once() )
+ $this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
- $this->returnValue( [ '', 'cus_mock' ] )
+ $this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
);
-
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'id' => $intent_id,
- 'amount' => 5000,
- ]
- );
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' );
-
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( 5000 );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( 'usd' );
-
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_mock' );
-
- $request->expects( $this->once() )
- ->method( 'set_payment_country' )
- ->with( 'US' );
-
- $request->expects( $this->never() )
- ->method( 'setup_future_usage' );
-
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_upe' ] );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $this->mock_upe_gateway->update_payment_intent( 'pi_mock', $order_id, false, null, 'US' );
- }
-
- public function test_create_payment_intent_uses_order_amount_if_order() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( $intent->get_amount() );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( strtolower( $intent->get_currency() ) );
-
- $request->expects( $this->once() )
- ->method( 'set_capture_method' )
- ->with( false );
-
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'order_number' => $order->get_order_number() ] );
-
- $request->expects( $this->once() )
- ->method( 'set_payment_method_types' );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $this->set_cart_contains_subscription_items( false );
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $result = $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
-
- public function test_create_payment_intent_defaults_to_automatic_capture() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( $intent->get_amount() );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( strtolower( $intent->get_currency() ) );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
-
- public function test_create_payment_intent_with_automatic_capture() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
- $this->mock_upe_gateway->settings['manual_capture'] = 'no';
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( $intent->get_amount() );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( strtolower( $intent->get_currency() ) );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
-
- public function test_create_payment_intent_with_manual_capture() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
- $this->mock_upe_gateway->settings['manual_capture'] = 'yes';
- $request = $this->mock_wcpay_request( Create_Intention::class );
-
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( $intent->get_amount() );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( strtolower( $intent->get_currency() ) );
-
- $request->expects( $this->once() )
- ->method( 'set_capture_method' )
- ->with( true );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
-
- public function test_create_payment_intent_with_fingerprint() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $fingerprint = 'abc123';
- $intent = WC_Helper_Intention::create_intention();
-
- $request = $this->mock_wcpay_request( Create_Intention::class, 1 );
- $request
- ->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( 5000 );
- $request
- ->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( 'usd' );
- $request
+ $this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() )
->expects( $this->once() )
- ->method( 'set_payment_method_types' )
- ->with( [ 'card' ] );
-
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'order_number' => $order->get_order_number() ] );
- $request
- ->expects( $this->once() )
- ->method( 'set_fingerprint' )
- ->with( $fingerprint );
- $request
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order_id, $fingerprint );
- }
-
- public function test_create_payment_intent_with_no_fingerprint() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention();
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( $intent->get_amount() );
-
- $request->expects( $this->once() )
- ->method( 'set_fingerprint' )
- ->with( '' );
-
- $request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $intent );
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
-
- public function test_create_setup_intent_existing_customer() {
- $_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
-
- $this->mock_customer_service
- ->expects( $this->once() )
- ->method( 'get_customer_id_by_user_id' )
- ->will( $this->returnValue( 'cus_mock' ) );
-
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- $request = $this->mock_wcpay_request( Create_Setup_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_mock' );
-
- $request->expects( $this->once() )
- ->method( 'set_payment_method_types' );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- WC_Helper_Intention::create_setup_intention(
- [
- 'id' => 'seti_mock',
- 'client_secret' => 'client_secret_mock',
- ]
- )
- );
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $this->mock_upe_gateway->create_setup_intent( [ 'card' ] );
-
- $this->assertEquals( 'seti_mock', $result['id'] );
- $this->assertEquals( 'client_secret_mock', $result['client_secret'] );
- }
-
- public function test_create_setup_intent_no_customer() {
- $_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
-
- $this->mock_customer_service
- ->expects( $this->once() )
- ->method( 'get_customer_id_by_user_id' )
- ->will( $this->returnValue( null ) );
-
- $this->mock_customer_service
- ->expects( $this->once() )
- ->method( 'create_customer_for_user' )
- ->will( $this->returnValue( 'cus_12346' ) );
-
- $request = $this->mock_wcpay_request( Create_Setup_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_12346' );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- WC_Helper_Intention::create_setup_intention(
- [
- 'id' => 'seti_mock',
- 'client_secret' => 'client_secret_mock',
- ]
- )
- );
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $this->mock_upe_gateway->create_setup_intent( [ 'card' ] );
-
- $this->assertEquals( 'seti_mock', $result['id'] );
- $this->assertEquals( 'client_secret_mock', $result['client_secret'] );
- }
-
- public function test_process_payment_returns_correct_redirect_url() {
- $order = WC_Helper_Order::create_order();
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] );
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $payment_intent );
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $this->mock_upe_gateway->process_payment( $order->get_id() );
-
- unset( $_POST['wc_payment_intent_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
-
- $this->assertEquals( 'success', $result['result'] );
- $this->assertEquals( true, $result['payment_needed'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=no/', $result['redirect_url'] );
- }
-
- public function test_process_payment_passes_save_payment_method_to_store() {
- $order = WC_Helper_Order::create_order();
- $gateway_id = UPE_Payment_Gateway::GATEWAY_ID;
- $save_payment_param = "wc-$gateway_id-new-payment-method";
- $_POST[ $save_payment_param ] = 'yes';
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] );
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $payment_intent );
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $this->mock_upe_gateway->process_payment( $order->get_id() );
-
- unset( $_POST[ $save_payment_param ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
- unset( $_POST['wc_payment_intent_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
-
- $this->assertEquals( 'success', $result['result'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=yes/', $result['redirect_url'] );
- }
-
- public function test_process_subscription_payment_passes_save_payment_method() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] );
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $payment_intent );
-
- $this->mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'is_payment_recurring' )
- ->willReturn( true );
-
- $result = $this->mock_upe_gateway->process_payment( $order->get_id() );
-
- unset( $_POST['wc_payment_intent_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
-
- $this->assertEquals( 'success', $result['result'] );
- $this->assertEquals( true, $result['payment_needed'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=yes/', $result['redirect_url'] );
- }
-
- public function test_process_payment_returns_correct_redirect_when_using_saved_payment() {
- $order = WC_Helper_Order::create_order();
- $_POST = $this->setup_saved_payment_method();
$this->set_cart_contains_subscription_items( false );
- $this->mock_upe_gateway
- ->expects( $this->never() )
- ->method( 'manage_customer_details_for_order' );
-
- $result = $this->mock_upe_gateway->process_payment( $order->get_id() );
+ $result = $this->mock_gateway->process_payment( $order->get_id() );
$this->assertEquals( 'success', $result['result'] );
- $this->assertMatchesRegularExpression( '/key=mock_order_key/', $result['redirect'] );
+ $this->assertEquals( $this->return_url, $result['redirect'] );
}
public function test_process_payment_returns_correct_redirect_when_using_payment_request() {
$order = WC_Helper_Order::create_order();
+ $intent = WC_Helper_Intention::create_intention();
$_POST['payment_request_type'] = 'google_pay';
- $this->set_cart_contains_subscription_items( false );
-
- $result = $this->mock_upe_gateway->process_payment( $order->get_id() );
-
- $this->mock_upe_gateway
- ->expects( $this->never() )
- ->method( 'manage_customer_details_for_order' );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertMatchesRegularExpression( '/key=mock_order_key/', $result['redirect'] );
- }
-
- public function test_upe_process_payment_check_session_order_redirect_to_previous_order() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $response = [
- 'dummy_result' => 'xyz',
- ];
-
- // Arrange the order is being processed.
- $current_order = WC_Helper_Order::create_order();
- $current_order_id = $current_order->get_id();
-
- // Arrange the DPPS to return an order from the session.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_against_session_processing_order' )
- ->with( wc_get_order( $current_order ) )
- ->willReturn( $response );
-
- // Assert: no call to the server to confirm the payment.
- $this->mock_wcpay_request( Update_Intention::class, 0, 'pi_XXXXX' );
-
- // Act: process the order but redirect to the previous/session paid order.
- $result = $this->mock_upe_gateway->process_payment( $current_order_id );
-
- // Assert: the result of check_against_session_processing_order.
- $this->assertSame( $response, $result );
- }
-
- public function test_upe_process_payment_check_session_with_failed_intent_then_order_id_saved_to_session() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- // Arrange the order is being processed.
- $current_order = WC_Helper_Order::create_order();
- $current_order_id = $current_order->get_id();
-
- // Arrange a failed intention.
- $intent = WC_Helper_Intention::create_intention( [ 'status' => 'failed' ] );
-
- // Assert.
- $update_request = $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() );
- $update_request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- // Arrange the DPPS not to return an order from the session.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_against_session_processing_order' )
- ->with( wc_get_order( $current_order ) )
- ->willReturn( null );
-
- // Assert: maybe_update_session_processing_order takes action and its value is kept.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'maybe_update_session_processing_order' )
- ->with( $current_order_id );
-
- // Act: process the order but redirect to the previous/session paid order.
- $this->mock_upe_gateway->process_payment( $current_order_id );
- }
-
- public function test_upe_process_payment_check_session_and_continue_processing() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- // Arrange the order is being processed.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange a successful intention.
- $intent = WC_Helper_Intention::create_intention();
-
- // Arrange the DPPS not to return an order from the session.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_against_session_processing_order' )
- ->with( wc_get_order( $order ) )
- ->willReturn( null );
-
- // Assert: Order is removed from the session.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'remove_session_processing_order' )
- ->with( $order_id );
-
- // Assert: the payment process continues.
- $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- // Act.
- $this->mock_upe_gateway->process_payment( $order_id );
- }
-
- public function test_upe_check_payment_intent_attached_to_order_succeeded_return_redirection() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $response = [
- 'dummy_result' => 'xyz',
- ];
-
- // Arrange order.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange the DPPS to return a prepared response.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_payment_intent_attached_to_order_succeeded' )
- ->with( wc_get_order( $order ) )
- ->willReturn( $response );
-
- // Assert: no more call to the server to update the intention.
- $this->mock_wcpay_request( Update_Intention::class, 0 );
+ $this->mock_gateway->expects( $this->once() )
+ ->method( 'manage_customer_details_for_order' )
+ ->will(
+ $this->returnValue( [ wp_get_current_user(), 'cus_123' ] )
+ );
+ $this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1, $intent->get_id() )
+ ->expects( $this->once() )
+ ->method( 'format_response' )
+ ->willReturn( $intent );
+ $this->set_cart_contains_subscription_items( false );
- // Act: process the order but redirect to the order.
- $result = $this->mock_upe_gateway->process_payment( $order_id );
+ $result = $this->mock_gateway->process_payment( $order->get_id() );
- // Assert: the result of check_intent_attached_to_order_succeeded.
- $this->assertSame( $response, $result );
+ $this->assertEquals( 'success', $result['result'] );
+ $this->assertEquals( $this->return_url, $result['redirect'] );
}
public function is_proper_intent_used_with_order_returns_false() {
- $this->assertFalse( $this->mock_upe_gateway->is_proper_intent_used_with_order( WC_Helper_Order::create_order(), 'wrong_intent_id' ) );
+ $this->assertFalse( $this->mock_gateway->is_proper_intent_used_with_order( WC_Helper_Order::create_order(), 'wrong_intent_id' ) );
}
public function test_process_redirect_payment_intent_processing() {
@@ -1034,7 +426,7 @@ public function test_process_redirect_payment_intent_processing() {
]
);
- $this->mock_upe_gateway->expects( $this->once() )
+ $this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ $user, $customer_id ] )
@@ -1048,7 +440,7 @@ public function test_process_redirect_payment_intent_processing() {
$this->set_cart_contains_subscription_items( false );
- $this->mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
+ $this->mock_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
$result_order = wc_get_order( $order_id );
$note = wc_get_order_notes(
@@ -1090,7 +482,7 @@ public function test_process_redirect_payment_intent_succeded() {
]
);
- $this->mock_upe_gateway->expects( $this->once() )
+ $this->mock_gateway->expects( $this->once() )
->method( 'manage_customer_details_for_order' )
->will(
$this->returnValue( [ $user, $customer_id ] )
@@ -1104,7 +496,7 @@ public function test_process_redirect_payment_intent_succeded() {
$this->set_cart_contains_subscription_items( false );
- $this->mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
+ $this->mock_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
$result_order = wc_get_order( $order_id );
@@ -1124,7 +516,7 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_throw_ex
$this->expectExceptionMessage( "We're not able to process this payment due to the order ID mismatch. Please try again later." );
\PHPUnit_Utils::call_method(
- $this->mock_upe_gateway,
+ $this->mock_gateway,
'validate_order_id_received_vs_intent_meta_order_id',
[ $order, $intent_metadata ]
);
@@ -1135,145 +527,13 @@ public function test_validate_order_id_received_vs_intent_meta_order_id_returnin
$intent_metadata = [ 'order_id' => (string) ( $order->get_id() ) ];
$res = \PHPUnit_Utils::call_method(
- $this->mock_upe_gateway,
+ $this->mock_gateway,
'validate_order_id_received_vs_intent_meta_order_id',
[ $order, $intent_metadata ]
);
$this->assertSame( null, $res );
}
- public function test_process_redirect_setup_intent_succeded() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $save_payment_method = true;
- $user = wp_get_current_user();
- $intent_status = Intent_Status::SUCCEEDED;
- $client_secret = 'cs_mock';
- $customer_id = 'cus_mock';
- $intent_id = 'si_mock';
- $payment_method_id = 'pm_mock';
- $token = WC_Helper_Token::create_token( $payment_method_id );
-
- // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
- $order->update_meta_data( '_intent_id', $intent_id );
- $order->save();
-
- $order->set_shipping_total( 0 );
- $order->set_shipping_tax( 0 );
- $order->set_cart_tax( 0 );
- $order->set_total( 0 );
- $order->save();
-
- $setup_intent = WC_Helper_Intention::create_setup_intention(
- [
- 'id' => $intent_id,
- 'client_secret' => $client_secret,
- 'status' => $intent_status,
- 'payment_method' => $payment_method_id,
- 'payment_method_options' => [
- 'card' => [
- 'request_three_d_secure' => 'automatic',
- ],
- ],
- 'last_setup_error' => [],
- ]
- );
-
- $this->mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $request = $this->mock_wcpay_request( Get_Setup_Intention::class, 1, $intent_id );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $setup_intent );
-
- $this->mock_token_service->expects( $this->once() )
- ->method( 'add_payment_method_to_user' )
- ->will(
- $this->returnValue( $token )
- );
-
- $this->set_cart_contains_subscription_items( true );
-
- $this->mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
-
- $result_order = wc_get_order( $order_id );
-
- $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
- $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
- $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
- $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
- $this->assertEquals( Order_Status::PROCESSING, $result_order->get_status() );
- $this->assertEquals( 1, count( $result_order->get_payment_tokens() ) );
- }
-
- public function test_process_redirect_payment_save_payment_token() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $save_payment_method = true;
- $user = wp_get_current_user();
- $intent_status = Intent_Status::PROCESSING;
- $intent_metadata = [ 'order_id' => (string) $order_id ];
- $charge_id = 'ch_mock';
- $customer_id = 'cus_mock';
- $intent_id = 'pi_mock';
- $payment_method_id = 'pm_mock';
- $token = WC_Helper_Token::create_token( $payment_method_id );
-
- // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
- $order->update_meta_data( '_intent_id', $intent_id );
- $order->save();
-
- $payment_intent = WC_Helper_Intention::create_intention(
- [
- 'status' => $intent_status,
- 'metadata' => $intent_metadata,
- ]
- );
-
- $this->mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $request = $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->will( $this->returnValue( $payment_intent ) );
-
- $this->mock_token_service->expects( $this->once() )
- ->method( 'add_payment_method_to_user' )
- ->will(
- $this->returnValue( $token )
- );
-
- $this->set_cart_contains_subscription_items( false );
-
- $this->mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
-
- $result_order = wc_get_order( $order_id );
- $note = wc_get_order_notes(
- [
- 'order_id' => $order_id,
- 'limit' => 1,
- ]
- )[0];
-
- $this->assertStringContainsString( 'authorized', $note->content );
- $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
- $this->assertEquals( $charge_id, $result_order->get_meta( '_charge_id', true ) );
- $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
- $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
- $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
- $this->assertEquals( Order_Status::ON_HOLD, $result_order->get_status() );
- $this->assertEquals( 1, count( $result_order->get_payment_tokens() ) );
- }
public function test_correct_payment_method_title_for_order() {
$order = WC_Helper_Order::create_order();
@@ -1353,7 +613,7 @@ public function test_correct_payment_method_title_for_order() {
];
foreach ( $charge_payment_method_details as $i => $payment_method_details ) {
- $this->mock_upe_gateway->set_payment_method_title_for_order( $order, $payment_method_details['type'], $payment_method_details );
+ $this->mock_gateway->set_payment_method_title_for_order( $order, $payment_method_details['type'], $payment_method_details );
$this->assertEquals( $expected_payment_method_titles[ $i ], $order->get_payment_method_title() );
}
}
@@ -1651,92 +911,7 @@ public function test_create_token_from_setup_intent_adds_token() {
$this->returnValue( $mock_token )
);
- $this->assertEquals( $mock_token, $this->mock_upe_gateway->create_token_from_setup_intent( $mock_setup_intent_id, $mock_user ) );
- }
-
-
-
-
- public function test_create_payment_intent_uses_cached_minimum_amount() {
- $order = WC_Helper_Order::create_order();
- $order->set_total( 0.45 );
- $order->save();
-
- set_transient( 'wcpay_minimum_amount_usd', '50', DAY_IN_SECONDS );
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD,
- 'amount' => 50,
- ]
- );
-
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( $intent->get_amount() );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order->get_id() );
- }
-
- public function test_create_payment_intent_creates_new_intent_with_minimum_amount() {
- $order = WC_Helper_Order::create_order();
- $order->set_currency( 'USD' );
- $order->set_total( 0.45 );
- $order->save();
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD,
- 'amount' => 50,
- ]
- );
-
- $request = $this->mock_wcpay_request( Create_Intention::class, 2 );
-
- $request->expects( $this->exactly( 2 ) )
- ->method( 'set_amount' )
- ->withConsecutive( [ 45 ], [ $intent->get_amount() ] );
-
- $request->expects( $this->once() )
- ->method( 'set_currency_code' )
- ->with( strtolower( $intent->get_currency() ) );
-
- $request->expects( $this->exactly( 2 ) )
- ->method( 'format_response' )
- ->will(
- $this->onConsecutiveCalls(
- $this->throwException( new Amount_Too_Small_Exception( 'Error: Amount must be at least $0.50 usd', 50, 'usd', 400 ) ),
- $this->returnValue( $intent )
- )
- );
- $this->set_get_upe_enabled_payment_method_statuses_return_value();
-
- $result = $this->mock_upe_gateway->create_payment_intent( [ 'card' ], $order->get_id() );
- $this->assertsame( 'cs_mock', $result['client_secret'] );
- }
-
- public function test_process_payment_rejects_with_cached_minimum_acount() {
- $order = WC_Helper_Order::create_order();
- $order->set_currency( 'USD' );
- $order->set_total( 0.45 );
- $order->save();
-
- set_transient( 'wcpay_minimum_amount_usd', '50', DAY_IN_SECONDS );
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- // Make sure that the payment was not actually processed.
- $price = wp_strip_all_tags( html_entity_decode( wc_price( 0.5, [ 'currency' => 'USD' ] ) ) );
- $message = 'The selected payment method requires a total amount of at least ' . $price . '.';
- $this->expectException( Exception::class );
- $this->expectExceptionMessage( $message );
- $this->mock_upe_gateway->process_payment( $order->get_id() );
+ $this->assertEquals( $mock_token, $this->mock_gateway->create_token_from_setup_intent( $mock_setup_intent_id, $mock_user ) );
}
public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
@@ -1745,199 +920,20 @@ public function test_exception_will_be_thrown_if_phone_number_is_invalid() {
$order->save();
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Invalid phone number.' );
- $this->mock_upe_gateway->process_payment( $order->get_id() );
- }
-
- public function test_process_payment_caches_mimimum_amount_and_displays_error_upon_exception() {
- $amount = 0.45;
- $customer = 'cus_12345';
- $payment_intent_id = 'pi_mock';
-
- $order = WC_Helper_Order::create_order();
- $order->set_total( $amount );
- $order->save();
-
- delete_transient( 'wcpay_minimum_amount_usd' );
-
- $_POST['wc_payment_intent_id'] = $payment_intent_id;
-
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, $payment_intent_id );
-
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( (int) ( $amount * 100 ) );
-
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_upe' ] );
-
- $request->expects( $this->once() )
- ->method( 'set_level3' )
- ->with(
- $this->callback(
- function( $argument ) {
- return is_array( $argument );
- }
- )
- );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->will( $this->throwException( new Amount_Too_Small_Exception( 'Error: Amount must be at least $60 usd', 6000, 'usd', 400 ) ) );
-
- $price = wp_strip_all_tags( html_entity_decode( wc_price( 60, [ 'currency' => 'USD' ] ) ) );
- $message = 'The selected payment method requires a total amount of at least ' . $price . '.';
- $this->expectException( Exception::class );
- $this->expectExceptionMessage( $message );
-
- try {
- $this->mock_upe_gateway->process_payment( $order->get_id() );
- } catch ( Exception $e ) {
- $this->assertEquals( '6000', get_transient( 'wcpay_minimum_amount_usd' ) );
- throw $e;
- }
+ $this->mock_gateway->process_payment( $order->get_id() );
}
public function test_remove_link_payment_method_if_card_disabled() {
+ $this->mock_gateway->settings['upe_enabled_payment_method_ids'] = [ 'link' ];
- $mock_upe_gateway = $this->getMockBuilder( UPE_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->mock_order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_upe_enabled_payment_method_statuses',
- 'get_upe_enabled_payment_method_ids',
- ]
- )
- ->getMock();
-
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_ids' )
- ->will(
- $this->returnValue( [ 'link' ] )
- );
- $mock_upe_gateway
+ $this->mock_gateway
->expects( $this->once() )
->method( 'get_upe_enabled_payment_method_statuses' )
->will(
$this->returnValue( [ 'link_payments' => [ 'status' => 'active' ] ] )
);
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame( $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'], [] );
- }
-
- public function test_link_payment_method_if_card_enabled() {
- WC_Helper_Site_Currency::$mock_site_currency = 'USD';
-
- $mock_upe_gateway = $this->getMockBuilder( UPE_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->mock_order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_upe_enabled_payment_method_statuses',
- 'get_upe_enabled_payment_method_ids',
- ]
- )
- ->getMock();
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_ids' )
- ->will(
- $this->returnValue( [ 'card', 'link' ] )
- );
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_statuses' )
- ->will(
- $this->returnValue(
- [
- 'link_payments' => [ 'status' => 'active' ],
- 'card_payments' => [ 'status' => 'active' ],
- ]
- )
- );
-
- $this->mock_payment_methods[ Payment_Method::LINK ]->expects( $this->any() )
- ->method( 'get_icon' )
- ->will(
- $this->returnValue( $this->icon_url )
- );
- $this->mock_payment_methods[ Payment_Method::CARD ]->expects( $this->any() )
- ->method( 'get_icon' )
- ->will(
- $this->returnValue( $this->icon_url )
- );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame(
- $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'],
- [
- 'card' => [
- 'isReusable' => true,
- 'title' => 'Credit card / debit card',
- 'icon' => $this->icon_url,
- 'showSaveOption' => true,
- 'countries' => [],
- 'upePaymentIntentData' => null,
- 'upeSetupIntentData' => null,
- 'testingInstructions' => '
Test mode: use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed
here .',
- 'forceNetworkSavedCards' => false,
- ],
- 'link' => [
- 'isReusable' => true,
- 'title' => 'Link',
- 'icon' => $this->icon_url,
- 'showSaveOption' => true,
- 'countries' => [],
- 'upePaymentIntentData' => null,
- 'upeSetupIntentData' => null,
- 'testingInstructions' => '',
- 'forceNetworkSavedCards' => false,
- ],
- ]
- );
+ $this->assertSame( $this->mock_gateway->get_payment_method_ids_enabled_at_checkout(), [] );
}
/**
@@ -1950,12 +946,13 @@ public function test_get_upe_available_payment_methods( $payment_methods, $expec
->method( 'get_fees' )
->willReturn( $payment_methods );
- $gateway = new UPE_Payment_Gateway(
+ $gateway = new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
$mock_wcpay_account,
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $this->mock_payment_method,
$this->mock_payment_methods,
$this->mock_rate_limiter,
$this->mock_order_service,
@@ -2021,7 +1018,7 @@ private function set_get_upe_enabled_payment_method_statuses_return_value( $retu
],
];
}
- $this->mock_upe_gateway
+ $this->mock_gateway
->expects( $this->any() )
->method( 'get_upe_enabled_payment_method_statuses' )
->will( $this->returnValue( $return_value ) );
diff --git a/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php b/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php
index 2d6e888229e..e788e467a14 100644
--- a/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php
+++ b/tests/unit/payment-methods/test-class-upe-split-payment-gateway.php
@@ -1,6 +1,6 @@
disableOriginalConstructor()
->getMock();
- $this->mock_legacy_checkout = $this->getMockBuilder( WC_Payments_Checkout::class )
- ->disableOriginalConstructor()
- ->getMock();
-
$this->mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class );
$this->order_service = new WC_Payments_Order_Service( $this->mock_api_client );
@@ -269,7 +254,7 @@ public function set_up() {
// so that get_payment_method_from_request() does not throw error.
$_POST = [
'wcpay-payment-method' => 'pm_mock',
- 'payment_method' => UPE_Split_Payment_Gateway::GATEWAY_ID,
+ 'payment_method' => WC_Payment_Gateway_WCPay::GATEWAY_ID,
];
$get_payment_gateway_by_id_return_value_map = [];
@@ -281,7 +266,7 @@ public function set_up() {
->getMock();
$this->mock_payment_methods[ $mock_payment_method->get_id() ] = $mock_payment_method;
- $mock_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
+ $mock_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
@@ -373,36 +358,19 @@ public function tear_down() {
*
* @return void
*/
- public function test_payment_fields_outputs_fields() {
- $checkout = new WC_Payments_UPE_Checkout(
- $this->mock_payment_gateways[ Payment_Method::CARD ],
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
- $checkout->init_hooks();
-
- $registered_card_gateway = WC_Payments::get_registered_card_gateway();
- WC_Payments::set_registered_card_gateway( $this->mock_payment_gateways[ Payment_Method::CARD ] );
-
+ public function test_display_gateway_html_for_multiple_gateways() {
foreach ( $this->mock_payment_gateways as $payment_method_id => $mock_payment_gateway ) {
- $mock_payment_gateway
- ->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [] );
/**
* This tests each payment method output separately without concatenating the output
* into 1 single buffer. Each iteration has 1 assertion.
*/
ob_start();
- $mock_payment_gateway->payment_fields();
+ $mock_payment_gateway->display_gateway_html();
$actual_output = ob_get_contents();
ob_end_clean();
$this->assertStringContainsString( '
', $actual_output );
}
-
- WC_Payments::set_registered_card_gateway( $registered_card_gateway );
}
public function test_should_not_use_stripe_platform_on_checkout_page_for_upe() {
@@ -442,600 +410,273 @@ public function test_non_reusable_payment_method_is_not_available_when_subscript
$this->assertFalse( $payment_gateway->is_available() );
}
- public function test_update_payment_intent_adds_customer_save_payment_and_level3_data() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $order_number = $order->get_order_number();
- $user = '';
- $customer_id = 'cus_mock';
- $save_payment_method = true;
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
+ public function test_process_payment_returns_correct_redirect_when_using_saved_payment() {
+ $mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $user = wp_get_current_user();
+ $customer_id = 'cus_mock';
+
+ $order = WC_Helper_Order::create_order();
+ $_POST = $this->setup_saved_payment_method();
+ $mock_card_payment_gateway->expects( $this->once() )
+ ->method( 'manage_customer_details_for_order' )
+ ->will(
+ $this->returnValue( [ $user, $customer_id ] )
+ );
+ $mock_card_payment_gateway->expects( $this->any() )
+ ->method( 'get_upe_enabled_payment_method_ids' )
+ ->will(
+ $this->returnValue( [ Payment_Method::CARD ] )
+ );
+ $this->mock_wcpay_request( Create_And_Confirm_Intention::class, 1 )
+ ->expects( $this->once() )
+ ->method( 'format_response' )
+ ->willReturn(
+ WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] )
+ );
$this->set_cart_contains_subscription_items( false );
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- // Test update_payment_intent on each payment gateway.
- foreach ( $this->mock_payment_gateways as $mock_payment_gateway ) {
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() );
- $request->expects( $this->once() )->method( 'set_amount' )->with( 5000 );
- $request->expects( $this->once() )->method( 'set_currency_code' )->with( 'usd' );
- $request->expects( $this->once() )->method( 'setup_future_usage' );
- $request->expects( $this->once() )->method( 'set_customer' )->with( 'cus_mock' );
- $request->expects( $this->once() )->method( 'set_metadata' )->with( [ 'gateway_type' => 'split_upe_with_deferred_intent_creation' ] );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
+ $result = $mock_card_payment_gateway->process_payment( $order->get_id() );
- $mock_payment_gateway
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
- $result = $mock_payment_gateway->update_payment_intent( $intent->get_id(), $order_id, $save_payment_method );
- $this->assertSame( [ 'success' => true ], $result );
- }
+ $this->assertEquals( 'success', $result['result'] );
+ $this->assertEquals( $this->return_url, $result['redirect'] );
}
- public function test_update_payment_intent_with_selected_upe_payment_method() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $order_number = $order->get_order_number();
- $product_item = current( $order->get_items( 'line_item' ) );
- $user = '';
- $customer_id = 'cus_mock';
- $save_payment_method = true;
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
+ public function test_upe_process_payment_check_session_order_redirect_to_previous_order() {
+ $_POST['wc_payment_intent_id'] = 'pi_mock';
+ $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];
- $this->set_cart_contains_subscription_items( false );
+ $response = [
+ 'dummy_result' => 'xyz',
+ ];
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
+ // Arrange the order is being processed.
+ $order = WC_Helper_Order::create_order();
+ $order_id = $order->get_id();
- /**
- * In order to test each gateway, we need to setup mock_api_client so that
- * its input are mocked in sequence, matching the gateways.
- */
- foreach ( $this->mock_payment_gateways as $payment_method_id => $mock_payment_gateway ) {
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() );
- $request->expects( $this->once() )->method( 'set_amount' )->with( 5000 );
- $request->expects( $this->once() )->method( 'set_currency_code' )->with( 'usd' );
- $request->expects( $this->once() )->method( 'setup_future_usage' );
- $request->expects( $this->once() )->method( 'set_customer' )->with( 'cus_mock' );
- $request->expects( $this->once() )->method( 'set_metadata' )->with( [ 'gateway_type' => 'split_upe_with_deferred_intent_creation' ] );
- $request->expects( $this->once() )->method( 'set_payment_method_types' )->with( [ $payment_method_id ] );
+ // Arrange the DPPs to return a redirect.
+ $this->mock_dpps->expects( $this->once() )
+ ->method( 'check_against_session_processing_order' )
+ ->with( wc_get_order( $order ) )
+ ->willReturn( $response );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
+ // Act: process the order but redirect to the previous/session paid order.
+ $result = $mock_upe_gateway->process_payment( $order_id );
- // Test update_payment_intent on each payment gateway.
- $mock_payment_gateway
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
- $result = $mock_payment_gateway->update_payment_intent( $intent->get_id(), $order_id, $save_payment_method, $payment_method_id );
- $this->assertSame( [ 'success' => true ], $result );
- }
+ // Assert: the result of check_against_session_processing_order.
+ $this->assertSame( $response, $result );
}
- public function test_update_payment_intent_with_payment_country() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $order_number = $order->get_order_number();
- $product_item = current( $order->get_items( 'line_item' ) );
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
+ public function test_process_redirect_payment_intent_processing() {
- $this->set_cart_contains_subscription_items( false );
+ $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $order = WC_Helper_Order::create_order();
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- // Test update_payment_intent on each payment gateway.
- foreach ( $this->mock_payment_gateways as $mock_payment_gateway ) {
- $request = $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() );
- $request->expects( $this->once() )->method( 'set_amount' )->with( 5000 );
- $request->expects( $this->once() )->method( 'set_currency_code' )->with( 'usd' );
- $request->expects( $this->once() )->method( 'set_customer' )->with( 'cus_mock' );
- $request->expects( $this->once() )->method( 'set_metadata' )->with( [ 'gateway_type' => 'split_upe_with_deferred_intent_creation' ] );
- $request->expects( $this->once() )->method( 'set_payment_country' )->with( 'US' );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
+ $order_id = $order->get_id();
+ $save_payment_method = false;
+ $user = wp_get_current_user();
+ $intent_status = Intent_Status::PROCESSING;
+ $intent_metadata = [ 'order_id' => (string) $order_id ];
+ $charge_id = 'ch_mock';
+ $customer_id = 'cus_mock';
+ $intent_id = 'pi_mock';
+ $payment_method_id = 'pm_mock';
- $mock_payment_gateway
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ '', 'cus_mock' ] )
- );
- $result = $mock_payment_gateway->update_payment_intent( $intent->get_id(), $order_id, false, null, 'US' );
- $this->assertSame( [ 'success' => true ], $result );
- }
- }
+ // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
+ $order->update_meta_data( '_intent_id', $intent_id );
+ $order->save();
- public function test_create_payment_intent_uses_order_amount_if_order() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $card_method = $this->mock_payment_methods['card'];
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
+ $payment_intent = WC_Helper_Intention::create_intention(
+ [
+ 'status' => $intent_status,
+ 'metadata' => $intent_metadata,
+ ]
+ );
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_amount' )
- ->with( 5000 );
- $request->expects( $this->once() )
+ $mock_upe_gateway->expects( $this->once() )
+ ->method( 'manage_customer_details_for_order' )
+ ->will(
+ $this->returnValue( [ $user, $customer_id ] )
+ );
+
+ $mock_upe_gateway->expects( $this->any() )
+ ->method( 'get_selected_payment_method' )
+ ->willReturn( $card_method );
+
+ $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id )
+ ->expects( $this->once() )
->method( 'format_response' )
- ->willReturn( $intent );
+ ->willReturn( $payment_intent );
$this->set_cart_contains_subscription_items( false );
- $mock_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
+ $mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
+
+ $result_order = wc_get_order( $order_id );
+ $note = wc_get_order_notes(
+ [
+ 'order_id' => $order_id,
+ 'limit' => 1,
+ ]
+ )[0];
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_payment_gateway );
- $mock_payment_gateway->create_payment_intent( [ 'card' ], $order_id );
+ $this->assertStringContainsString( 'authorized', $note->content );
+ $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
+ $this->assertEquals( $charge_id, $result_order->get_meta( '_charge_id', true ) );
+ $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
+ $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
+ $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
+ $this->assertEquals( Order_Status::ON_HOLD, $result_order->get_status() );
}
- public function test_create_payment_intent_defaults_to_automatic_capture() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ public function test_process_redirect_payment_intent_succeded() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
+ $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $order = WC_Helper_Order::create_order();
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )->method( 'set_capture_method' )->with( false );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
+ $order_id = $order->get_id();
+ $save_payment_method = false;
+ $user = wp_get_current_user();
+ $intent_status = Intent_Status::SUCCEEDED;
+ $intent_metadata = [ 'order_id' => (string) $order_id ];
+ $charge_id = 'ch_mock';
+ $customer_id = 'cus_mock';
+ $intent_id = 'pi_mock';
+ $payment_method_id = 'pm_mock';
- $mock_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
+ // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
+ $order->update_meta_data( '_intent_id', $intent_id );
+ $order->save();
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_payment_gateway );
+ $card_method = $this->mock_payment_methods['card'];
- $mock_payment_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
+ $payment_intent = WC_Helper_Intention::create_intention(
+ [
+ 'status' => $intent_status,
+ 'metadata' => $intent_metadata,
+ ]
+ );
- public function test_create_payment_intent_with_automatic_capture() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $mock_upe_gateway->expects( $this->once() )
+ ->method( 'manage_customer_details_for_order' )
+ ->will(
+ $this->returnValue( [ $user, $customer_id ] )
+ );
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
- $mock_payment_gateway->settings['manual_capture'] = 'no';
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )->method( 'set_capture_method' )->with( false );
- $request->expects( $this->once() )
+ $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id )
+ ->expects( $this->once() )
->method( 'format_response' )
- ->willReturn( $intent );
+ ->willReturn( $payment_intent );
- $mock_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
+ $mock_upe_gateway->expects( $this->any() )
+ ->method( 'get_selected_payment_method' )
+ ->willReturn( $card_method );
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_payment_gateway );
+ $this->set_cart_contains_subscription_items( false );
- $mock_payment_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
+ $mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
- public function test_create_payment_intent_with_manual_capture() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $result_order = wc_get_order( $order_id );
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD ] );
- $mock_payment_gateway->settings['manual_capture'] = 'yes';
+ $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
+ $this->assertEquals( $charge_id, $result_order->get_meta( '_charge_id', true ) );
+ $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
+ $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
+ $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
+ $this->assertEquals( Order_Status::PROCESSING, $result_order->get_status() );
+ }
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )->method( 'set_capture_method' )->with( true );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
+ public function is_proper_intent_used_with_order_returns_false() {
+ $this->assertFalse( $this->mock_upe_gateway->is_proper_intent_used_with_order( WC_Helper_Order::create_order(), 'wrong_intent_id' ) );
+ }
- $mock_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
+ public function test_process_redirect_setup_intent_succeded() {
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_payment_gateway );
+ $order = WC_Helper_Order::create_order();
+ $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
- $mock_payment_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
+ $order_id = $order->get_id();
+ $save_payment_method = true;
+ $user = wp_get_current_user();
+ $intent_status = Intent_Status::SUCCEEDED;
+ $client_secret = 'cs_mock';
+ $customer_id = 'cus_mock';
+ $intent_id = 'si_mock';
+ $payment_method_id = 'pm_mock';
+ $token = WC_Helper_Token::create_token( $payment_method_id );
- public function test_create_payment_intent_with_fingerprint() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $fingerprint = 'abc123';
- $intent = WC_Helper_Intention::create_intention();
- $mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
+ $order->update_meta_data( '_intent_id', $intent_id );
+ $order->save();
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )->method( 'set_fingerprint' )->with( $fingerprint );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
+ $card_method = $this->mock_payment_methods['card'];
- $mock_card_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_card_payment_gateway );
+ $order->set_shipping_total( 0 );
+ $order->set_shipping_tax( 0 );
+ $order->set_cart_tax( 0 );
+ $order->set_total( 0 );
+ $order->save();
- $mock_card_payment_gateway->create_payment_intent( [ 'card' ], $order_id, $fingerprint );
- }
+ $setup_intent = WC_Helper_Intention::create_setup_intention(
+ [
+ 'id' => 'pi_mock',
+ 'client_secret' => $client_secret,
+ 'status' => $intent_status,
+ 'payment_method' => $payment_method_id,
+ 'payment_method_options' => [
+ 'card' => [
+ 'request_three_d_secure' => 'automatic',
+ ],
+ ],
+ 'last_setup_error' => [],
+ ]
+ );
- public function test_create_payment_intent_with_no_fingerprint() {
- $mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $intent = WC_Helper_Intention::create_intention();
+ $mock_upe_gateway->expects( $this->once() )
+ ->method( 'manage_customer_details_for_order' )
+ ->will(
+ $this->returnValue( [ $user, $customer_id ] )
+ );
+
+ $request = $this->mock_wcpay_request( Get_Setup_Intention::class, 1, $intent_id );
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )->method( 'set_fingerprint' )->with( '' );
$request->expects( $this->once() )
->method( 'format_response' )
- ->willReturn( $intent );
+ ->willReturn( $setup_intent );
- $mock_card_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_card_payment_gateway );
+ $this->mock_token_service->expects( $this->once() )
+ ->method( 'add_payment_method_to_user' )
+ ->will(
+ $this->returnValue( $token )
+ );
- $mock_card_payment_gateway->create_payment_intent( [ 'card' ], $order_id );
- }
+ $mock_upe_gateway->expects( $this->any() )
+ ->method( 'get_selected_payment_method' )
+ ->willReturn( $card_method );
- public function test_create_setup_intent_existing_customer() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
+ $this->set_cart_contains_subscription_items( true );
- $_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
+ $mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
- $this->mock_customer_service
- ->expects( $this->once() )
- ->method( 'get_customer_id_by_user_id' )
- ->will( $this->returnValue( 'cus_mock' ) );
-
- $this->mock_customer_service
- ->expects( $this->never() )
- ->method( 'create_customer_for_user' );
-
- $request = $this->mock_wcpay_request( Create_Setup_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_mock' );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- WC_Helper_Intention::create_setup_intention(
- [
- 'id' => 'seti_mock',
- 'client_secret' => 'client_secret_mock',
- ]
- )
- );
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $mock_payment_gateway->create_setup_intent( [ 'card' ] );
-
- $this->assertEquals( 'seti_mock', $result['id'] );
- $this->assertEquals( 'client_secret_mock', $result['client_secret'] );
- }
-
- public function test_create_setup_intent_no_customer() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $_POST = [ 'wcpay-payment-method' => 'pm_mock' ];
-
- $this->mock_customer_service
- ->expects( $this->once() )
- ->method( 'get_customer_id_by_user_id' )
- ->will( $this->returnValue( null ) );
-
- $this->mock_customer_service
- ->expects( $this->once() )
- ->method( 'create_customer_for_user' )
- ->will( $this->returnValue( 'cus_12346' ) );
-
- $request = $this->mock_wcpay_request( Create_Setup_Intention::class );
- $request->expects( $this->once() )
- ->method( 'set_customer' )
- ->with( 'cus_12346' );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- WC_Helper_Intention::create_setup_intention(
- [
- 'id' => 'seti_mock',
- 'client_secret' => 'client_secret_mock',
- ]
- )
- );
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $mock_payment_gateway->create_setup_intent( [ 'card' ] );
-
- $this->assertEquals( 'seti_mock', $result['id'] );
- $this->assertEquals( 'client_secret_mock', $result['client_secret'] );
- }
-
- public function test_process_payment_returns_correct_redirect_url() {
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] );
-
- $this->set_cart_contains_subscription_items( false );
-
- foreach ( $this->mock_payment_gateways as $mock_payment_gateway ) {
- $this->mock_wcpay_request( Update_Intention::class, 1, $payment_intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $payment_intent );
-
- $result = $mock_payment_gateway->process_payment( $order->get_id() );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertEquals( true, $result['payment_needed'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=no/', $result['redirect_url'] );
- }
-
- unset( $_POST['wc_payment_intent_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
- }
-
- public function test_process_payment_passes_save_payment_method_to_store() {
- $mock_sepa_payment_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];
-
- $order = WC_Helper_Order::create_order();
- $gateway_id = UPE_Split_Payment_Gateway::GATEWAY_ID . '_' . Payment_Method::SEPA;
- $save_payment_param = "wc-$gateway_id-new-payment-method";
- $_POST[ $save_payment_param ] = 'yes';
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] );
-
- $this->mock_wcpay_request( Update_Intention::class, 1, $payment_intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- $payment_intent
- );
-
- $this->set_cart_contains_subscription_items( false );
-
- // Test saving with SEPA.
- $result = $mock_sepa_payment_gateway->process_payment( $order->get_id() );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=yes/', $result['redirect_url'] );
-
- unset( $_POST[ $save_payment_param ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
- unset( $_POST['wc_payment_intent_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
- }
-
- public function test_process_subscription_payment_passes_save_payment_method() {
- $mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
- $mock_sepa_payment_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];
-
- $order = WC_Helper_Order::create_order();
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $payment_intent = WC_Helper_Intention::create_intention( [ 'status' => Intent_Status::PROCESSING ] );
-
- // Test card.
- $this->mock_wcpay_request( Update_Intention::class, 1, $payment_intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- $payment_intent
- );
-
- $mock_card_payment_gateway
- ->expects( $this->once() )
- ->method( 'is_payment_recurring' )
- ->willReturn( true );
- $result = $mock_card_payment_gateway->process_payment( $order->get_id() );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertEquals( true, $result['payment_needed'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=yes/', $result['redirect_url'] );
-
- // Test SEPA.
- $this->mock_wcpay_request( Update_Intention::class, 1, $payment_intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn(
- $payment_intent
- );
-
- $mock_sepa_payment_gateway
- ->expects( $this->once() )
- ->method( 'is_payment_recurring' )
- ->willReturn( true );
- $result = $mock_sepa_payment_gateway->process_payment( $order->get_id() );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertEquals( true, $result['payment_needed'] );
- $this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
- $this->assertMatchesRegularExpression( '/save_payment_method=yes/', $result['redirect_url'] );
-
- unset( $_POST['wc_payment_intent_id'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
- }
-
- public function test_process_payment_returns_correct_redirect_when_using_saved_payment() {
- $mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $order = WC_Helper_Order::create_order();
- $_POST = $this->setup_saved_payment_method();
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $mock_card_payment_gateway->process_payment( $order->get_id() );
-
- $mock_card_payment_gateway
- ->expects( $this->never() )
- ->method( 'manage_customer_details_for_order' );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertMatchesRegularExpression( '/key=mock_order_key/', $result['redirect'] );
- }
-
- public function test_process_payment_returns_correct_redirect_when_using_payment_request() {
- $mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $order = WC_Helper_Order::create_order();
- $_POST['payment_request_type'] = 'google_pay';
-
- $this->set_cart_contains_subscription_items( false );
-
- $result = $mock_card_payment_gateway->process_payment( $order->get_id() );
-
- $mock_card_payment_gateway
- ->expects( $this->never() )
- ->method( 'manage_customer_details_for_order' );
- $this->assertEquals( 'success', $result['result'] );
- $this->assertMatchesRegularExpression( '/key=mock_order_key/', $result['redirect'] );
- }
-
- public function test_upe_process_payment_check_session_order_redirect_to_previous_order() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
- $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];
-
- $response = [
- 'dummy_result' => 'xyz',
- ];
-
- // Arrange the order is being processed.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange the DPPs to return a redirect.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_against_session_processing_order' )
- ->with( wc_get_order( $order ) )
- ->willReturn( $response );
-
- // Act: process the order but redirect to the previous/session paid order.
- $result = $mock_upe_gateway->process_payment( $order_id );
-
- // Assert: the result of check_against_session_processing_order.
- $this->assertSame( $response, $result );
- }
-
- public function test_upe_process_payment_check_session_with_failed_intent_then_order_id_saved_to_session() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- // Arrange the order is being processed.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange a failed intention.
- $intent = WC_Helper_Intention::create_intention( [ 'status' => 'failed' ] );
-
- // Assert.
- $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- // Make sure the DPPS will store the order.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'maybe_update_session_processing_order' )
- ->with( $order_id );
-
- // Act: process the order but redirect to the previous/session paid order.
- $this->mock_payment_gateways[ Payment_Method::SEPA ]->process_payment( $order_id );
- }
-
- public function test_upe_process_payment_check_session_and_continue_processing() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
- $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];
-
- // Arrange the order is being processed.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange a successful intention.
- $intent = WC_Helper_Intention::create_intention();
-
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [] );
-
- // Arrange the DPPs not to return anything.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_against_session_processing_order' )
- ->with( wc_get_order( $order ) )
- ->willReturn( null );
-
- $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- // Act.
- $mock_upe_gateway->process_payment( $order_id );
- }
-
- public function test_upe_check_payment_intent_attached_to_order_succeeded_with_invalid_intent_id_continue_process_payment() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- // Arrange order.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange the DPPS not to return a redirect.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_payment_intent_attached_to_order_succeeded' )
- ->with( wc_get_order( $order ) )
- ->willReturn( null );
-
- // Assert: the payment process continues.
- $intent = WC_Helper_Intention::create_intention();
- $this->mock_wcpay_request( Update_Intention::class, 1, $intent->get_id() )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- // Act: process the order.
- $this->mock_payment_gateways[ Payment_Method::SEPA ]->process_payment( $order_id );
- }
-
- public function test_upe_check_payment_intent_attached_to_order_succeeded_return_redirection() {
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $response = [
- 'dummy_result' => 'xyz',
- ];
-
- // Arrange order.
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
-
- // Arrange the DPPS to return a redirect based on a redirect.
- $this->mock_dpps->expects( $this->once() )
- ->method( 'check_payment_intent_attached_to_order_succeeded' )
- ->with( wc_get_order( $order ) )
- ->willReturn( $response );
-
- // Assert: no more call to the server to update the intention.
- $this->mock_wcpay_request( Update_Intention::class, 0 );
-
- // Act: process the order but redirect to the order.
- $result = $this->mock_payment_gateways[ Payment_Method::SEPA ]->process_payment( $order_id );
+ $result_order = wc_get_order( $order_id );
- // Assert: the result of check_intent_attached_to_order_succeeded.
- $this->assertSame( $response, $result );
+ $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
+ $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
+ $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
+ $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
+ $this->assertEquals( Order_Status::PROCESSING, $result_order->get_status() );
+ $this->assertEquals( 1, count( $result_order->get_payment_tokens() ) );
}
- public function test_process_redirect_payment_intent_processing() {
+ public function test_process_redirect_payment_save_payment_token() {
$mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
- $order = WC_Helper_Order::create_order();
+ $order = WC_Helper_Order::create_order();
$order_id = $order->get_id();
- $save_payment_method = false;
+ $save_payment_method = true;
$user = wp_get_current_user();
$intent_status = Intent_Status::PROCESSING;
$intent_metadata = [ 'order_id' => (string) $order_id ];
@@ -1043,6 +684,7 @@ public function test_process_redirect_payment_intent_processing() {
$customer_id = 'cus_mock';
$intent_id = 'pi_mock';
$payment_method_id = 'pm_mock';
+ $token = WC_Helper_Token::create_token( $payment_method_id );
// Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
$order->update_meta_data( '_intent_id', $intent_id );
@@ -1063,15 +705,21 @@ public function test_process_redirect_payment_intent_processing() {
$this->returnValue( [ $user, $customer_id ] )
);
- $mock_upe_gateway->expects( $this->any() )
- ->method( 'get_selected_payment_method' )
- ->willReturn( $card_method );
-
$this->mock_wcpay_request( Get_Intention::class, 1, $intent_id )
->expects( $this->once() )
->method( 'format_response' )
->willReturn( $payment_intent );
+ $this->mock_token_service->expects( $this->once() )
+ ->method( 'add_payment_method_to_user' )
+ ->will(
+ $this->returnValue( $token )
+ );
+
+ $mock_upe_gateway->expects( $this->any() )
+ ->method( 'get_selected_payment_method' )
+ ->willReturn( $card_method );
+
$this->set_cart_contains_subscription_items( false );
$mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
@@ -1091,221 +739,11 @@ public function test_process_redirect_payment_intent_processing() {
$this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
$this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
$this->assertEquals( Order_Status::ON_HOLD, $result_order->get_status() );
+ $this->assertEquals( 1, count( $result_order->get_payment_tokens() ) );
}
- public function test_process_redirect_payment_intent_succeded() {
-
- $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
- $order = WC_Helper_Order::create_order();
-
- $order_id = $order->get_id();
- $save_payment_method = false;
- $user = wp_get_current_user();
- $intent_status = Intent_Status::SUCCEEDED;
- $intent_metadata = [ 'order_id' => (string) $order_id ];
- $charge_id = 'ch_mock';
- $customer_id = 'cus_mock';
- $intent_id = 'pi_mock';
- $payment_method_id = 'pm_mock';
-
- // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
- $order->update_meta_data( '_intent_id', $intent_id );
- $order->save();
-
- $card_method = $this->mock_payment_methods['card'];
-
- $payment_intent = WC_Helper_Intention::create_intention(
- [
- 'status' => $intent_status,
- 'metadata' => $intent_metadata,
- ]
- );
-
- $mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $payment_intent );
-
- $mock_upe_gateway->expects( $this->any() )
- ->method( 'get_selected_payment_method' )
- ->willReturn( $card_method );
-
- $this->set_cart_contains_subscription_items( false );
-
- $mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
-
- $result_order = wc_get_order( $order_id );
-
- $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
- $this->assertEquals( $charge_id, $result_order->get_meta( '_charge_id', true ) );
- $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
- $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
- $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
- $this->assertEquals( Order_Status::PROCESSING, $result_order->get_status() );
- }
-
- public function is_proper_intent_used_with_order_returns_false() {
- $this->assertFalse( $this->mock_upe_gateway->is_proper_intent_used_with_order( WC_Helper_Order::create_order(), 'wrong_intent_id' ) );
- }
-
- public function test_process_redirect_setup_intent_succeded() {
-
- $order = WC_Helper_Order::create_order();
- $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $order_id = $order->get_id();
- $save_payment_method = true;
- $user = wp_get_current_user();
- $intent_status = Intent_Status::SUCCEEDED;
- $client_secret = 'cs_mock';
- $customer_id = 'cus_mock';
- $intent_id = 'si_mock';
- $payment_method_id = 'pm_mock';
- $token = WC_Helper_Token::create_token( $payment_method_id );
-
- // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
- $order->update_meta_data( '_intent_id', $intent_id );
- $order->save();
-
- $card_method = $this->mock_payment_methods['card'];
-
- $order->set_shipping_total( 0 );
- $order->set_shipping_tax( 0 );
- $order->set_cart_tax( 0 );
- $order->set_total( 0 );
- $order->save();
-
- $setup_intent = WC_Helper_Intention::create_setup_intention(
- [
- 'id' => 'pi_mock',
- 'client_secret' => $client_secret,
- 'status' => $intent_status,
- 'payment_method' => $payment_method_id,
- 'payment_method_options' => [
- 'card' => [
- 'request_three_d_secure' => 'automatic',
- ],
- ],
- 'last_setup_error' => [],
- ]
- );
-
- $mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $request = $this->mock_wcpay_request( Get_Setup_Intention::class, 1, $intent_id );
-
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $setup_intent );
-
- $this->mock_token_service->expects( $this->once() )
- ->method( 'add_payment_method_to_user' )
- ->will(
- $this->returnValue( $token )
- );
-
- $mock_upe_gateway->expects( $this->any() )
- ->method( 'get_selected_payment_method' )
- ->willReturn( $card_method );
-
- $this->set_cart_contains_subscription_items( true );
-
- $mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
-
- $result_order = wc_get_order( $order_id );
-
- $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
- $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
- $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
- $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
- $this->assertEquals( Order_Status::PROCESSING, $result_order->get_status() );
- $this->assertEquals( 1, count( $result_order->get_payment_tokens() ) );
- }
-
- public function test_process_redirect_payment_save_payment_token() {
-
- $mock_upe_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $order = WC_Helper_Order::create_order();
- $order_id = $order->get_id();
- $save_payment_method = true;
- $user = wp_get_current_user();
- $intent_status = Intent_Status::PROCESSING;
- $intent_metadata = [ 'order_id' => (string) $order_id ];
- $charge_id = 'ch_mock';
- $customer_id = 'cus_mock';
- $intent_id = 'pi_mock';
- $payment_method_id = 'pm_mock';
- $token = WC_Helper_Token::create_token( $payment_method_id );
-
- // Supply the order with the intent id so that it can be retrieved during the redirect payment processing.
- $order->update_meta_data( '_intent_id', $intent_id );
- $order->save();
-
- $card_method = $this->mock_payment_methods['card'];
-
- $payment_intent = WC_Helper_Intention::create_intention(
- [
- 'status' => $intent_status,
- 'metadata' => $intent_metadata,
- ]
- );
-
- $mock_upe_gateway->expects( $this->once() )
- ->method( 'manage_customer_details_for_order' )
- ->will(
- $this->returnValue( [ $user, $customer_id ] )
- );
-
- $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $payment_intent );
-
- $this->mock_token_service->expects( $this->once() )
- ->method( 'add_payment_method_to_user' )
- ->will(
- $this->returnValue( $token )
- );
-
- $mock_upe_gateway->expects( $this->any() )
- ->method( 'get_selected_payment_method' )
- ->willReturn( $card_method );
-
- $this->set_cart_contains_subscription_items( false );
-
- $mock_upe_gateway->process_redirect_payment( $order, $intent_id, $save_payment_method );
-
- $result_order = wc_get_order( $order_id );
- $note = wc_get_order_notes(
- [
- 'order_id' => $order_id,
- 'limit' => 1,
- ]
- )[0];
-
- $this->assertStringContainsString( 'authorized', $note->content );
- $this->assertEquals( $intent_id, $result_order->get_meta( '_intent_id', true ) );
- $this->assertEquals( $charge_id, $result_order->get_meta( '_charge_id', true ) );
- $this->assertEquals( $intent_status, $result_order->get_meta( '_intention_status', true ) );
- $this->assertEquals( $payment_method_id, $result_order->get_meta( '_payment_method_id', true ) );
- $this->assertEquals( $customer_id, $result_order->get_meta( '_stripe_customer_id', true ) );
- $this->assertEquals( Order_Status::ON_HOLD, $result_order->get_status() );
- $this->assertEquals( 1, count( $result_order->get_payment_tokens() ) );
- }
-
- public function test_correct_payment_method_title_for_order() {
- $order = WC_Helper_Order::create_order();
+ public function test_correct_payment_method_title_for_order() {
+ $order = WC_Helper_Order::create_order();
$visa_credit_details = [
'type' => 'card',
@@ -1611,611 +1049,13 @@ public function test_create_token_from_setup_intent_adds_token() {
}
}
- public function test_create_payment_intent_uses_cached_minimum_amount() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $order = WC_Helper_Order::create_order();
- $order->set_total( 0.45 );
- $order->save();
-
- set_transient( 'wcpay_minimum_amount_usd', '50', DAY_IN_SECONDS );
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD,
- 'amount' => 50,
- ]
- );
-
- $request = $this->mock_wcpay_request( Create_Intention::class );
- $request->expects( $this->once() )->method( 'set_amount' )->with( 50 );
- $request->expects( $this->once() )
- ->method( 'format_response' )
- ->willReturn( $intent );
-
- $mock_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
-
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_payment_gateway );
-
- $mock_payment_gateway->create_payment_intent( [ 'card' ], $order->get_id() );
- }
-
- public function test_create_payment_intent_creates_new_intent_with_minimum_amount() {
- $mock_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
-
- $order = WC_Helper_Order::create_order();
- $order->set_currency( 'USD' );
- $order->set_total( 0.45 );
- $order->save();
-
- $intent = WC_Helper_Intention::create_intention(
- [
- 'status' => Intent_Status::REQUIRES_PAYMENT_METHOD,
- 'amount' => 50,
- ]
- );
-
- $request = $this->mock_wcpay_request( Create_Intention::class, 2 );
- $request->expects( $this->exactly( 2 ) )->method( 'set_amount' )->withConsecutive( [ 45 ], [ 50 ] );
- $request->expects( $this->exactly( 2 ) )
- ->method( 'format_response' )
- ->will(
- $this->onConsecutiveCalls(
- $this->throwException( new Amount_Too_Small_Exception( 'Error: Amount must be at least $0.50 usd', 50, 'usd', 400 ) ),
- $this->returnValue( $intent )
- )
- );
-
- $mock_payment_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::CARD ] );
-
- $this->set_get_upe_enabled_payment_method_statuses_return_value( $mock_payment_gateway );
-
- $result = $mock_payment_gateway->create_payment_intent( [ 'card' ], $order->get_id() );
- $this->assertsame( 'cs_mock', $result['client_secret'] );
- }
-
- public function test_process_payment_rejects_with_cached_minimum_acount() {
-
- $order = WC_Helper_Order::create_order();
- $order->set_currency( 'USD' );
- $order->set_total( 0.45 );
- $order->save();
-
- set_transient( 'wcpay_minimum_amount_usd', '50', DAY_IN_SECONDS );
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- // Make sure that the payment was not actually processed.
- $price = wp_strip_all_tags( html_entity_decode( wc_price( 0.5, [ 'currency' => 'USD' ] ) ) );
- $message = 'The selected payment method requires a total amount of at least ' . $price . '.';
-
- foreach ( $this->mock_payment_gateways as $mock_payment_gateway ) {
- $this->expectException( Exception::class );
- $this->expectExceptionMessage( $message );
- $mock_payment_gateway->process_payment( $order->get_id() );
- }
- }
-
- public function test_process_payment_caches_mimimum_amount_and_displays_error_upon_exception() {
- $order = WC_Helper_Order::create_order();
- $order->set_total( 0.45 );
- $order->save();
-
- delete_transient( 'wcpay_minimum_amount_usd' );
- $_POST['wc_payment_intent_id'] = 'pi_mock';
-
- $price = wp_strip_all_tags( html_entity_decode( wc_price( 60, [ 'currency' => 'USD' ] ) ) );
- $message = 'The selected payment method requires a total amount of at least ' . $price . '.';
- $this->expectException( Exception::class );
- $this->expectExceptionMessage( $message );
-
- try {
- foreach ( $this->mock_payment_gateways as $mock_payment_gateway ) {
- $this->mock_wcpay_request( Update_Intention::class, 1, 'pi_mock' )
- ->expects( $this->once() )
- ->method( 'format_response' )
- ->will( $this->throwException( new Amount_Too_Small_Exception( 'Error: Amount must be at least $60 usd', 6000, 'usd', 400 ) ) );
-
- $mock_payment_gateway->process_payment( $order->get_id() );
-
- break;
- }
- } catch ( Exception $e ) {
- $this->assertEquals( '6000', get_transient( 'wcpay_minimum_amount_usd' ) );
- throw $e;
- }
- }
-
- public function test_no_save_option_for_non_sepa_upe() {
- $payment_methods_with_no_save_option = [
- Payment_Method::BANCONTACT,
- Payment_Method::EPS,
- Payment_Method::GIROPAY,
- Payment_Method::IDEAL,
- Payment_Method::P24,
- Payment_Method::SOFORT,
- ];
-
- foreach ( $payment_methods_with_no_save_option as $payment_method ) {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods[ $payment_method ],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_payment_method_ids_enabled_at_checkout',
- 'wc_payments_get_payment_method_by_id',
- 'wc_payments_get_payment_gateway_by_id',
- 'is_saved_cards_enabled',
- 'is_subscription_item_in_cart',
- ]
- )
- ->getMock();
-
- $mock_upe_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ $payment_method ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_method_by_id' )
- ->with( $payment_method )
- ->willReturn( $this->mock_payment_methods[ $payment_method ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_gateway_by_id' )
- ->with( $payment_method )
- ->willReturn( $this->mock_payment_gateways[ $payment_method ] );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame( $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'][ $payment_method ]['showSaveOption'], false );
- }
- }
-
- public function test_no_save_option_for_sepa_due_to_subscription_cart() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods[ Payment_Method::SEPA ],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_payment_method_ids_enabled_at_checkout',
- 'wc_payments_get_payment_method_by_id',
- 'wc_payments_get_payment_gateway_by_id',
- 'is_saved_cards_enabled',
- 'is_subscription_item_in_cart',
- ]
- )
- ->getMock();
-
- // saved cards enabled.
- $mock_upe_gateway
- ->method( 'is_saved_cards_enabled' )
- ->will(
- $this->returnValue( true )
- );
-
- // there is a subscription item in cart, which should disable the save option checkbox for a payment method.
- $mock_upe_gateway
- ->method( 'is_subscription_item_in_cart' )
- ->will(
- $this->returnValue( true )
- );
-
- $mock_upe_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::SEPA ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_method_by_id' )
- ->with( Payment_Method::SEPA )
- ->willReturn( $this->mock_payment_methods[ Payment_Method::SEPA ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_gateway_by_id' )
- ->with( Payment_Method::SEPA )
- ->willReturn( $this->mock_payment_gateways[ Payment_Method::SEPA ] );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame( $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'][ Payment_Method::SEPA ]['showSaveOption'], false );
- }
-
- public function test_no_save_option_for_sepa_due_to_saved_cards_disabled() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods[ Payment_Method::SEPA ],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_payment_method_ids_enabled_at_checkout',
- 'wc_payments_get_payment_method_by_id',
- 'wc_payments_get_payment_gateway_by_id',
- 'is_saved_cards_enabled',
- 'is_subscription_item_in_cart',
- ]
- )
- ->getMock();
-
- // saved cards disabled.
- $mock_upe_gateway
- ->method( 'is_saved_cards_enabled' )
- ->will(
- $this->returnValue( false )
- );
-
- // no subscription item in cart.
- $mock_upe_gateway
- ->method( 'is_subscription_item_in_cart' )
- ->will(
- $this->returnValue( false )
- );
-
- $mock_upe_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::SEPA ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_method_by_id' )
- ->with( Payment_Method::SEPA )
- ->willReturn( $this->mock_payment_methods[ Payment_Method::SEPA ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_gateway_by_id' )
- ->with( Payment_Method::SEPA )
- ->willReturn( $this->mock_payment_gateways[ Payment_Method::SEPA ] );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame( $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'][ Payment_Method::SEPA ]['showSaveOption'], false );
- }
-
- public function test_save_option_for_sepa_debit() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods[ Payment_Method::SEPA ],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_payment_method_ids_enabled_at_checkout',
- 'wc_payments_get_payment_method_by_id',
- 'wc_payments_get_payment_gateway_by_id',
- 'is_saved_cards_enabled',
- 'is_subscription_item_in_cart',
- ]
- )
- ->getMock();
-
- $mock_upe_gateway->method( 'get_payment_method_ids_enabled_at_checkout' )
- ->willReturn( [ Payment_Method::SEPA ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_method_by_id' )
- ->with( Payment_Method::SEPA )
- ->willReturn( $this->mock_payment_methods[ Payment_Method::SEPA ] );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_gateway_by_id' )
- ->with( Payment_Method::SEPA )
- ->willReturn( $this->mock_payment_gateways[ Payment_Method::SEPA ] );
-
- // saved cards enabled.
- $mock_upe_gateway
- ->method( 'is_saved_cards_enabled' )
- ->will(
- $this->returnValue( true )
- );
-
- // no subscription items in cart.
- $mock_upe_gateway
- ->method( 'is_subscription_item_in_cart' )
- ->will(
- $this->returnValue( false )
- );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame( $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'][ Payment_Method::SEPA ]['showSaveOption'], false );
- }
-
- public function test_remove_link_payment_method_if_card_disabled() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods[ Payment_Method::LINK ],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_upe_enabled_payment_method_statuses',
- 'get_upe_enabled_payment_method_ids',
- 'wc_payments_get_payment_method_by_id',
- ]
- )
- ->getMock();
-
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_ids' )
- ->will(
- $this->returnValue( [ 'link' ] )
- );
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_statuses' )
- ->will(
- $this->returnValue( [ 'link_payments' => [ 'status' => 'active' ] ] )
- );
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_method_by_id' )
- ->with( Payment_Method::LINK )
- ->willReturn( $this->mock_payment_methods[ Payment_Method::LINK ] );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame( $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'], [] );
- }
-
- public function test_link_payment_method_if_card_enabled() {
- $this->mock_cache->method( 'get' )->willReturn( [ 'is_deferred_intent_creation_upe_enabled' => true ] );
- WC_Helper_Site_Currency::$mock_site_currency = 'USD';
-
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods[ Payment_Method::CARD ],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods(
- [
- 'get_upe_enabled_payment_method_statuses',
- 'get_upe_enabled_payment_method_ids',
- 'wc_payments_get_payment_method_by_id',
- 'wc_payments_get_payment_gateway_by_id',
- ]
- )
- ->getMock();
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_ids' )
- ->will(
- $this->returnValue( [ 'card', 'link' ] )
- );
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'get_upe_enabled_payment_method_statuses' )
- ->will(
- $this->returnValue(
- [
- 'link_payments' => [ 'status' => 'active' ],
- 'card_payments' => [ 'status' => 'active' ],
- ]
- )
- );
-
- $this->mock_payment_methods[ Payment_Method::LINK ]->expects( $this->any() )
- ->method( 'get_icon' )
- ->will(
- $this->returnValue( $this->icon_url )
- );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_method_by_id' )
- ->willReturnMap(
- [
- [ Payment_Method::CARD, $this->mock_payment_methods[ Payment_Method::CARD ] ],
- [ Payment_Method::LINK, $this->mock_payment_methods[ Payment_Method::LINK ] ],
- ]
- );
-
- $mock_upe_gateway
- ->method( 'wc_payments_get_payment_gateway_by_id' )
- ->willReturnCallback(
- function ( $payment_method ) {
- if ( Payment_Method::CARD === $payment_method ) {
- return $this->mock_payment_gateways[ Payment_Method::CARD ];
- } elseif ( Payment_Method::LINK === $payment_method ) {
- return $this->mock_payment_gateways[ Payment_Method::LINK ];
- }
- }
- );
-
- $upe_checkout = new WC_Payments_UPE_Checkout(
- $mock_upe_gateway,
- $this->mock_woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertSame(
- $upe_checkout->get_payment_fields_js_config()['paymentMethodsConfig'],
- [
- 'card' => [
- 'isReusable' => true,
- 'title' => 'Credit card / debit card',
- 'icon' => null,
- 'showSaveOption' => true,
- 'countries' => [],
- 'upePaymentIntentData' => null,
- 'upeSetupIntentData' => null,
- 'testingInstructions' => '
Test mode: use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed
here .',
- 'forceNetworkSavedCards' => false,
- ],
- 'link' => [
- 'isReusable' => true,
- 'title' => 'Link',
- 'icon' => $this->icon_url,
- 'showSaveOption' => true,
- 'countries' => [],
- 'upePaymentIntentData' => null,
- 'upeSetupIntentData' => null,
- 'testingInstructions' => '',
- 'forceNetworkSavedCards' => false,
- ],
- ]
- );
- }
-
- public function test_remove_upe_setup_intent_from_session() {
- // Two payment methods (SEPA and giropay) are enabled.
- $sepa_setup_intent_key = UPE_Split_Payment_Gateway::KEY_UPE_SETUP_INTENT . '_sepa_debit';
- $giropay_setup_intent_key = UPE_Split_Payment_Gateway::KEY_UPE_SETUP_INTENT . '_giropay';
-
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
- ->setConstructorArgs(
- [
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- $this->mock_payment_methods['card'],
- $this->mock_payment_methods,
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service,
- ]
- )
- ->setMethods( [ 'wc_payments_get_payment_method_map' ] )
- ->getMock();
-
- $mock_upe_gateway
- ->expects( $this->once() )
- ->method( 'wc_payments_get_payment_method_map' )
- ->will(
- $this->returnValue(
- [
- 'sepa_debit' => $this->mock_payment_methods[ Payment_Method::SEPA ],
- 'giropay' => $this->mock_payment_methods[ Payment_Method::GIROPAY ],
- ]
- )
- );
-
- // and both SEPA and giropay have a setup intent stored in WC session object.
- WC()->session->set( $sepa_setup_intent_key, 'pi_test_setup_intent_sepa_debit' );
- WC()->session->set( $giropay_setup_intent_key, 'pi_test_setup_intent_giropay' );
-
- $this->assertNotNull( WC()->session->get( $sepa_setup_intent_key ) );
- $this->assertNotNull( WC()->session->get( $giropay_setup_intent_key ) );
-
- $mock_upe_gateway->remove_upe_setup_intent_from_session();
-
- $this->assertNull( WC()->session->get( $sepa_setup_intent_key ) );
- $this->assertNull( WC()->session->get( $giropay_setup_intent_key ) );
- }
-
/**
* Test get_payment_method_types with regular checkout post request context.
*
* @return void
*/
public function test_get_payment_methods_with_request_context() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
+ $mock_upe_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
@@ -2260,7 +1100,7 @@ public function test_get_payment_methods_with_request_context() {
* @return void
*/
public function test_get_payment_methods_without_request_context() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
+ $mock_upe_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
@@ -2304,7 +1144,7 @@ public function test_get_payment_methods_without_request_context() {
* @return void
*/
public function test_get_payment_methods_without_request_context_or_token() {
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
+ $mock_upe_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
@@ -2357,7 +1197,7 @@ public function test_get_payment_methods_without_request_context_or_token() {
*/
public function test_get_payment_methods_from_gateway_id_upe() {
WC_Helper_Order::create_order();
- $mock_upe_gateway = $this->getMockBuilder( UPE_Split_Payment_Gateway::class )
+ $mock_upe_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
->setConstructorArgs(
[
$this->mock_api_client,
@@ -2391,7 +1231,7 @@ public function test_get_payment_methods_from_gateway_id_upe() {
$this->returnValue( [ Payment_Method::CARD, Payment_Method::LINK ] )
);
- $payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( UPE_Split_Payment_Gateway::GATEWAY_ID . '_' . Payment_Method::BANCONTACT );
+ $payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::BANCONTACT );
$this->assertSame( [ Payment_Method::BANCONTACT ], $payment_methods );
$mock_upe_gateway->expects( $this->any() )
@@ -2403,10 +1243,10 @@ public function test_get_payment_methods_from_gateway_id_upe() {
)
);
- $payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( UPE_Split_Payment_Gateway::GATEWAY_ID );
+ $payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( WC_Payment_Gateway_WCPay::GATEWAY_ID );
$this->assertSame( [ Payment_Method::CARD, Payment_Method::LINK ], $payment_methods );
- $payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( UPE_Split_Payment_Gateway::GATEWAY_ID );
+ $payment_methods = $mock_upe_gateway->get_payment_methods_from_gateway_id( WC_Payment_Gateway_WCPay::GATEWAY_ID );
$this->assertSame( [ Payment_Method::CARD ], $payment_methods );
WC_Payments::set_gateway( $gateway );
diff --git a/tests/unit/src/Internal/Payment/State/ProcessedStateTest.php b/tests/unit/src/Internal/Payment/State/ProcessedStateTest.php
index b7f2febece9..aca6138622e 100644
--- a/tests/unit/src/Internal/Payment/State/ProcessedStateTest.php
+++ b/tests/unit/src/Internal/Payment/State/ProcessedStateTest.php
@@ -16,8 +16,7 @@
use WCPay\Internal\Service\DuplicatePaymentPreventionService;
use WCPay\Internal\Service\OrderService;
use WCPay\Internal\Proxy\LegacyProxy;
-use WCPay\Payment_Methods\UPE_Payment_Gateway;
-use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
+use WC_Payment_Gateway_WCPay;
use WCPAY_UnitTestCase;
/**
@@ -167,12 +166,6 @@ function ( $arg ) use ( $mock_cart ) {
$mock_cart->expects( $this->once() )
->method( 'empty_cart' );
- // Test methods to remove upe payment intent are called.
- $this->mock_legacy_proxy
- ->expects( $this->exactly( 2 ) )
- ->method( 'call_static' )
- ->withConsecutive( [ UPE_Payment_Gateway::class, 'remove_upe_payment_intent_from_session' ], [ UPE_Split_Payment_Gateway::class, 'remove_upe_payment_intent_from_session' ] );
-
$this->sut->complete_processing();
}
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-payment-types.php b/tests/unit/test-class-wc-payment-gateway-wcpay-payment-types.php
index e80c97b1834..8cd1668986d 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay-payment-types.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay-payment-types.php
@@ -11,7 +11,7 @@
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Session_Rate_Limiter;
use WCPay\Fraud_Prevention\Fraud_Prevention_Service;
-use WCPay\Internal\Service\OrderService;
+use WCPay\Payment_Methods\CC_Payment_Method;
/**
* WC_Payment_Gateway_WCPay unit tests.
@@ -133,7 +133,8 @@ public function set_up() {
$this->mock_order_service = $this->createMock( WC_Payments_Order_Service::class );
- $mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
// Arrange: Mock WC_Payment_Gateway_WCPay so that some of its methods can be
// mocked, and their return values can be used for testing.
@@ -145,6 +146,8 @@ public function set_up() {
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_rate_limiter,
$this->mock_order_service,
$mock_dpps,
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-process-payment.php b/tests/unit/test-class-wc-payment-gateway-wcpay-process-payment.php
index da7ed2798db..1ce7132f420 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay-process-payment.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay-process-payment.php
@@ -6,19 +6,17 @@
*/
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
-use WCPay\Core\Server\Request\WooPay_Create_And_Confirm_Intention;
use WCPay\Core\Server\Request\Create_And_Confirm_Setup_Intention;
use WCPay\Core\Server\Request\Get_Charge;
-use WCPay\Core\Server\Response;
use WCPay\Constants\Order_Status;
use WCPay\Constants\Intent_Status;
-use WCPay\Core\Server\Request\Get_Intention;
-use WCPay\Core\Server\Request\Update_Intention;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Exceptions\API_Exception;
use WCPay\Exceptions\Connection_Exception;
use WCPay\Session_Rate_Limiter;
use WCPay\Constants\Payment_Method;
+use WCPay\Payment_Methods\CC_Payment_Method;
+
// Need to use WC_Mock_Data_Store.
require_once dirname( __FILE__ ) . '/helpers/class-wc-mock-wc-data-store.php';
@@ -149,7 +147,8 @@ public function set_up() {
$this->mock_order_service = $this->createMock( WC_Payments_Order_Service::class );
- $this->mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $this->mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
// Arrange: Mock WC_Payment_Gateway_WCPay so that some of its methods can be
// mocked, and their return values can be used for testing.
@@ -161,6 +160,8 @@ public function set_up() {
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_rate_limiter,
$this->mock_order_service,
$this->mock_dpps,
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php b/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php
index 79c11b5fe1b..7350621aedc 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay-process-refund.php
@@ -13,6 +13,7 @@
use WCPay\Core\Server\Response;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Exceptions\API_Exception;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
// Need to use WC_Mock_Data_Store.
@@ -89,6 +90,7 @@ public function set_up() {
$this->mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class );
$this->mock_order_service = $this->createMock( WC_Payments_Order_Service::class );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
$this->wcpay_gateway = new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
@@ -96,6 +98,8 @@ public function set_up() {
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_rate_limiter,
$this->mock_order_service,
$mock_dpps,
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-payment-method-order-note.php b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-payment-method-order-note.php
index 73be48c5cbc..9715d61a193 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-payment-method-order-note.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-payment-method-order-note.php
@@ -6,7 +6,7 @@
*/
use WCPay\Duplicate_Payment_Prevention_Service;
-use WCPay\Exceptions\API_Exception;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
/**
@@ -96,6 +96,9 @@ public function set_up() {
->getMock();
$this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
+ $this->mock_wcpay_account
+ ->method( 'get_account_default_currency' )
+ ->willReturn( 'USD' );
$this->mock_customer_service = $this->getMockBuilder( 'WC_Payments_Customer_Service' )
->disableOriginalConstructor()
@@ -119,12 +122,19 @@ public function set_up() {
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->onlyMethods( [ 'is_subscription_item_in_cart' ] )
+ ->getMock();
+
$this->wcpay_gateway = new \WC_Payment_Gateway_WCPay(
$this->mock_api_client,
$this->mock_wcpay_account,
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_session_rate_limiter,
$this->mock_order_service,
$mock_dpps,
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php
index 0db2ec88933..168349f902d 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions-process-payment.php
@@ -7,10 +7,10 @@
use WCPay\Core\Server\Request\Create_And_Confirm_Intention;
use WCPay\Core\Server\Request\Create_And_Confirm_Setup_Intention;
-use WCPay\Core\Server\Response;
use WCPay\Constants\Order_Status;
use WCPay\Constants\Intent_Status;
use WCPay\Duplicate_Payment_Prevention_Service;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
/**
@@ -122,6 +122,9 @@ public function set_up() {
->getMock();
$this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
+ $this->mock_wcpay_account
+ ->method( 'get_account_default_currency' )
+ ->willReturn( 'usd' );
$this->mock_customer_service = $this->getMockBuilder( 'WC_Payments_Customer_Service' )
->disableOriginalConstructor()
@@ -139,7 +142,8 @@ public function set_up() {
$this->order_service = new WC_Payments_Order_Service( $this->mock_api_client );
- $mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
$this->mock_wcpay_gateway = $this->getMockBuilder( '\WC_Payment_Gateway_WCPay' )
->setConstructorArgs(
@@ -149,6 +153,8 @@ public function set_up() {
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_rate_limiter,
$this->order_service,
$mock_dpps,
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php
index 60d00031f8c..a5b33c1581c 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php
@@ -11,6 +11,7 @@
use WCPay\Exceptions\API_Exception;
use WCPay\Internal\Service\Level3Service;
use WCPay\Internal\Service\OrderService;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
/**
@@ -111,6 +112,9 @@ public function set_up() {
->getMock();
$this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
+ $this->mock_wcpay_account
+ ->method( 'get_account_default_currency' )
+ ->willReturn( 'usd' );
$this->mock_customer_service = $this->getMockBuilder( 'WC_Payments_Customer_Service' )
->disableOriginalConstructor()
@@ -135,12 +139,19 @@ public function set_up() {
$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
$this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
+ $mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->onlyMethods( [ 'is_subscription_item_in_cart' ] )
+ ->getMock();
+
$this->wcpay_gateway = new \WC_Payment_Gateway_WCPay(
$this->mock_api_client,
$this->mock_wcpay_account,
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_session_rate_limiter,
$this->order_service,
$this->mock_dpps,
@@ -148,6 +159,7 @@ public function set_up() {
$this->mock_fraud_service
);
$this->wcpay_gateway->init_hooks();
+ WC_Payments::set_gateway( $this->wcpay_gateway );
// Mock the level3 service to always return an empty array.
$mock_level3_service = $this->createMock( Level3Service::class );
@@ -318,10 +330,6 @@ public function test_scheduled_subscription_payment() {
->method( 'set_capture_method' )
->with( false );
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
-
$request->expects( $this->once() )
->method( 'format_response' )
->willReturn( WC_Helper_Intention::create_intention() );
@@ -479,9 +487,6 @@ public function test_scheduled_subscription_payment_adds_mandate() {
->method( 'set_capture_method' )
->with( false );
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$request->expects( $this->once() )
->method( 'format_response' )
->willReturn( WC_Helper_Intention::create_intention() );
@@ -808,12 +813,19 @@ public function test_adds_custom_payment_meta_input_fallback_until_subs_3_0_7()
WC_Subscriptions::$version = '3.0.7';
+ $mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->onlyMethods( [ 'is_subscription_item_in_cart' ] )
+ ->getMock();
+
$payment_gateway = new \WC_Payment_Gateway_WCPay(
$this->mock_api_client,
$this->mock_wcpay_account,
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_session_rate_limiter,
$this->order_service,
$this->mock_dpps,
@@ -834,6 +846,11 @@ public function test_adds_custom_payment_meta_input_fallback_until_subs_3_0_7()
public function test_does_not_add_custom_payment_meta_input_fallback_for_subs_3_0_8() {
remove_all_actions( 'woocommerce_admin_order_data_after_billing_address' );
+ $mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->onlyMethods( [ 'is_subscription_item_in_cart' ] )
+ ->getMock();
+
WC_Subscriptions::$version = '3.0.8';
new \WC_Payment_Gateway_WCPay(
$this->mock_api_client,
@@ -841,6 +858,8 @@ public function test_does_not_add_custom_payment_meta_input_fallback_for_subs_3_
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$this->mock_session_rate_limiter,
$this->order_service,
$this->mock_dpps,
diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay.php b/tests/unit/test-class-wc-payment-gateway-wcpay.php
index c1bb486f296..02505f94d51 100644
--- a/tests/unit/test-class-wc-payment-gateway-wcpay.php
+++ b/tests/unit/test-class-wc-payment-gateway-wcpay.php
@@ -13,9 +13,7 @@
use WCPay\Core\Server\Request\Get_Charge;
use WCPay\Core\Server\Request\Get_Intention;
use WCPay\Core\Server\Request\Get_Setup_Intention;
-use WCPay\Core\Server\Request\Update_Intention;
use WCPay\Constants\Order_Status;
-use WCPay\Constants\Payment_Type;
use WCPay\Constants\Intent_Status;
use WCPay\Duplicate_Payment_Prevention_Service;
use WCPay\Exceptions\Amount_Too_Small_Exception;
@@ -30,10 +28,8 @@
use WCPay\Payment_Information;
use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Payment_Methods\Sepa_Payment_Method;
-use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
use WCPay\WooPay\WooPay_Utilities;
use WCPay\Session_Rate_Limiter;
-use WCPay\WC_Payments_Checkout;
// Need to use WC_Mock_Data_Store.
require_once dirname( __FILE__ ) . '/helpers/class-wc-mock-wc-data-store.php';
@@ -95,6 +91,13 @@ class WC_Payment_Gateway_WCPay_Test extends WCPAY_UnitTestCase {
*/
private $mock_rate_limiter;
+ /**
+ * UPE_Payment_Method instance.
+ *
+ * @var UPE_Payment_Method|MockObject
+ */
+ private $mock_payment_method;
+
/**
* WC_Payments_Order_Service instance.
*
@@ -109,12 +112,6 @@ class WC_Payment_Gateway_WCPay_Test extends WCPAY_UnitTestCase {
*/
private $woopay_utilities;
- /**
- * WC_Payments_Checkout instance.
- * @var WC_Payments_Checkout
- */
- private $payments_checkout;
-
/**
* Duplicate_Payment_Prevention_Service instance.
* @var Duplicate_Payment_Prevention_Service|MockObject
@@ -171,6 +168,16 @@ public function set_up() {
$this->mock_api_client->expects( $this->any() )->method( 'get_blog_id' )->willReturn( 1234567 );
$this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
+ $this->mock_wcpay_account
+ ->expects( $this->any() )
+ ->method( 'get_fees' )
+ ->willReturn(
+ [
+ 'card' => [
+ 'base' => 0.1,
+ ],
+ ]
+ );
// Mock the main class's cache service.
$this->_cache = WC_Payments::get_database_cache();
@@ -190,7 +197,19 @@ public function set_up() {
$this->mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
$this->mock_localization_service = $this->createMock( WC_Payments_Localization_Service::class );
- $this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
+ $this->mock_localization_service->expects( $this->any() )
+ ->method( 'get_country_locale_data' )
+ ->willReturn(
+ [
+ 'currency_code' => 'usd',
+ ]
+ );
+ $this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
+
+ $this->mock_payment_method = $this->getMockBuilder( CC_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->setMethods( [ 'is_subscription_item_in_cart' ] )
+ ->getMock();
$this->wcpay_gateway = new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
@@ -198,24 +217,19 @@ public function set_up() {
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $this->mock_payment_method,
+ [ 'card' => $this->mock_payment_method ],
$this->mock_rate_limiter,
$this->order_service,
$this->mock_dpps,
$this->mock_localization_service,
$this->mock_fraud_service
);
+
WC_Payments::set_gateway( $this->wcpay_gateway );
$this->woopay_utilities = new WooPay_Utilities();
- $this->payments_checkout = new WC_Payments_Checkout(
- $this->wcpay_gateway,
- $this->woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
// Mock the level3 service to always return an empty array.
$mock_level3_service = $this->createMock( Level3Service::class );
$mock_level3_service->expects( $this->any() )
@@ -368,83 +382,58 @@ public function test_attach_exchange_info_to_order_with_different_order_currency
$this->assertEquals( 0.853, $order->get_meta( '_wcpay_multi_currency_stripe_exchange_rate' ) );
}
- public function test_save_card_checkbox_not_displayed_when_saved_cards_disabled() {
- $this->wcpay_gateway->update_option( 'saved_cards', 'no' );
-
- $this->refresh_payments_checkout();
-
+ public function test_save_payment_method_checkbox_displayed() {
// Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
$this->setOutputCallback(
function ( $output ) {
- $result = preg_match_all( '/.*
.*/', $output );
+ $input_element = preg_match_all( '/.*
.*/', $output );
+ $parent_div = preg_match_all( '/
.*<\/div>/s', $output );
- $this->assertSame( 0, $result );
+ $this->assertSame( 1, $input_element );
+ $this->assertSame( 1, $parent_div );
}
);
- $this->wcpay_gateway->payment_fields();
+ $this->wcpay_gateway->save_payment_method_checkbox();
}
- public function test_save_card_checkbox_not_displayed_for_non_logged_in_users() {
- $this->wcpay_gateway->update_option( 'saved_cards', 'yes' );
- wp_set_current_user( 0 );
-
- $this->refresh_payments_checkout();
-
- // Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
+ public function test_save_payment_method_checkbox_not_displayed_when_force_checked() {
$this->setOutputCallback(
function ( $output ) {
- $result = preg_match_all( '/.*
.*/', $output );
+ $input_element = preg_match_all( '/.*
.*/', $output );
+ $parent_div = preg_match_all( '/
.*<\/div>/s', $output );
- $this->assertSame( 0, $result );
+ $this->assertSame( 1, $input_element );
+ $this->assertSame( 1, $parent_div );
}
);
- $this->wcpay_gateway->payment_fields();
+ $this->wcpay_gateway->save_payment_method_checkbox( true );
}
- public function test_save_card_checkbox_displayed_for_logged_in_users() {
- $this->wcpay_gateway->update_option( 'saved_cards', 'yes' );
- wp_set_current_user( 1 );
-
- $this->refresh_payments_checkout();
+ public function test_save_payment_method_checkbox_not_displayed_when_stripe_platform_account_used() {
+ // Setup the test so that should_use_stripe_platform_on_checkout_page returns true.
+ $this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
+ $this->wcpay_gateway->update_option( 'platform_checkout', 'yes' );
+ add_filter( 'woocommerce_is_checkout', '__return_true' );
+ WC()->session->init();
+ WC()->cart->add_to_cart( WC_Helper_Product::create_simple_product()->get_id(), 1 );
+ WC()->cart->calculate_totals();
- // Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
$this->setOutputCallback(
function ( $output ) {
- $result = preg_match_all( '/.*
.*/', $output );
+ $input_element = preg_match_all( '/.*
.*/', $output );
+ $parent_div = preg_match_all( '/
.*<\/div>/s', $output );
- $this->assertSame( 1, $result );
+ $this->assertSame( 1, $input_element );
+ $this->assertSame( 1, $parent_div );
}
);
- $this->wcpay_gateway->payment_fields();
- }
+ $this->wcpay_gateway->save_payment_method_checkbox( false );
- public function test_fraud_prevention_token_added_when_enabled() {
- $token_value = 'test-token';
- $fraud_prevention_service_mock = $this->get_fraud_prevention_service_mock();
- $fraud_prevention_service_mock
- ->expects( $this->once() )
- ->method( 'is_enabled' )
- ->willReturn( true );
- $fraud_prevention_service_mock
- ->expects( $this->once() )
- ->method( 'get_token' )
- ->willReturn( $token_value );
-
- $this->refresh_payments_checkout();
-
- // Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
- $this->setOutputCallback(
- function ( $output ) use ( $token_value ) {
- $result = preg_match_all( '/.*
.*/', $output );
-
- $this->assertSame( 0, $result );
- }
- );
-
- $this->wcpay_gateway->payment_fields();
+ remove_filter( 'woocommerce_is_checkout', '__return_true' );
+ WC()->cart->empty_cart();
}
public function test_capture_charge_success() {
@@ -464,9 +453,6 @@ public function test_capture_charge_success() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( WC_Helper_Intention::create_intention() );
@@ -519,9 +505,6 @@ public function test_capture_charge_success_non_usd() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( WC_Helper_Intention::create_intention( [ 'currency' => 'eur' ] ) );
@@ -571,9 +554,6 @@ public function test_capture_charge_failure() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $mock_intent );
@@ -626,9 +606,6 @@ public function test_capture_charge_failure_non_usd() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( $mock_intent );
@@ -683,9 +660,6 @@ public function test_capture_charge_api_failure() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->will( $this->throwException( new API_Exception( 'test exception', 'server_error', 500 ) ) );
@@ -745,9 +719,6 @@ public function test_capture_charge_api_failure_non_usd() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->will( $this->throwException( new API_Exception( 'test exception', 'server_error', 500 ) ) );
@@ -803,9 +774,6 @@ public function test_capture_charge_expired() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->will( $this->throwException( new API_Exception( 'test exception', 'server_error', 500 ) ) );
@@ -861,9 +829,6 @@ public function test_capture_charge_metadata() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( WC_Helper_Intention::create_intention() );
@@ -910,9 +875,6 @@ public function test_capture_charge_without_level3() {
$capture_intent_request->expects( $this->once() )
->method( 'set_amount_to_capture' )
->with( $mock_intent->get_amount() );
- $capture_intent_request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
$capture_intent_request->expects( $this->once() )
->method( 'format_response' )
->willReturn( WC_Helper_Intention::create_intention() );
@@ -1284,7 +1246,7 @@ public function test_add_payment_method_no_customer() {
$this->assertEquals( 'error', $result['result'] );
}
- public function test_add_payment_method_canceled_intent() {
+ public function test_add_payment_method_cancelled_intent() {
$_POST = [ 'wcpay-setup-intent' => 'sti_mock' ];
$this->mock_customer_service
@@ -1576,22 +1538,6 @@ public function test_process_payment_for_order_rejects_with_cached_minimum_amoun
}
public function test_mandate_data_not_added_to_payment_intent_if_not_required() {
- // Mandate data is required for SEPA and Stripe Link only, hence initializing the gateway with a CC payment method should not add mandate data.
- $gateway = new UPE_Split_Payment_Gateway(
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- new CC_Payment_Method( $this->mock_token_service ),
- [],
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service
- );
- WC_Payments::set_gateway( $gateway );
$payment_method = 'woocommerce_payments_sepa_debit';
$order = WC_Helper_Order::create_order();
$order->set_currency( 'USD' );
@@ -1609,27 +1555,13 @@ public function test_mandate_data_not_added_to_payment_intent_if_not_required()
$request->expects( $this->never() )
->method( 'set_mandate_data' );
- $gateway->process_payment_for_order( WC()->cart, $pi );
- WC_Payments::set_gateway( $this->wcpay_gateway );
+ // Mandate data is required for SEPA and Stripe Link only, $this->wcpay_gateway is created with card hence mandate data should not be added.
+ $this->wcpay_gateway->process_payment_for_order( WC()->cart, $pi );
}
public function test_mandate_data_added_to_payment_intent_if_required() {
- // Mandate data is required for SEPA and Stripe Link, hence initializing the gateway with a SEPA payment method should add mandate data.
- $gateway = new UPE_Split_Payment_Gateway(
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- new Sepa_Payment_Method( $this->mock_token_service ),
- [],
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service
- );
- WC_Payments::set_gateway( $gateway );
+ // Mandate data is required for SEPA and Stripe Link, hence creating the gateway with a SEPA payment method should add mandate data.
+ $gateway = $this->create_gateway_with( new Sepa_Payment_Method( $this->mock_token_service ) );
$payment_method = 'woocommerce_payments_sepa_debit';
$order = WC_Helper_Order::create_order();
$order->set_currency( 'USD' );
@@ -1659,26 +1591,11 @@ function ( $data ) {
);
$gateway->process_payment_for_order( WC()->cart, $pi );
- WC_Payments::set_gateway( $this->wcpay_gateway );
}
public function test_mandate_data_not_added_to_setup_intent_request_when_link_is_disabled() {
- $gateway = new UPE_Split_Payment_Gateway(
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- new CC_Payment_Method( $this->mock_token_service ),
- [],
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service
- );
- WC_Payments::set_gateway( $gateway );
- $gateway->settings['upe_enabled_payment_method_ids'] = [ 'card' ];
+ // Disabled link is reflected in upe_enabled_payment_method_ids: when link is disabled, the array contains only card.
+ $this->wcpay_gateway->settings['upe_enabled_payment_method_ids'] = [ 'card' ];
$payment_method = 'woocommerce_payments';
$order = WC_Helper_Order::create_order();
@@ -1714,27 +1631,11 @@ public function test_mandate_data_not_added_to_setup_intent_request_when_link_is
$request->expects( $this->never() )
->method( 'set_mandate_data' );
- $gateway->process_payment_for_order( WC()->cart, $pi );
- WC_Payments::set_gateway( $this->wcpay_gateway );
+ $this->wcpay_gateway->process_payment_for_order( WC()->cart, $pi );
}
public function test_mandate_data_added_to_setup_intent_request_when_link_is_enabled() {
- $gateway = new UPE_Split_Payment_Gateway(
- $this->mock_api_client,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_token_service,
- $this->mock_action_scheduler_service,
- new CC_Payment_Method( $this->mock_token_service ),
- [],
- $this->mock_rate_limiter,
- $this->order_service,
- $this->mock_dpps,
- $this->mock_localization_service,
- $this->mock_fraud_service
- );
- WC_Payments::set_gateway( $gateway );
- $gateway->settings['upe_enabled_payment_method_ids'] = [ 'card', 'link' ];
+ $this->wcpay_gateway->settings['upe_enabled_payment_method_ids'] = [ 'card', 'link' ];
$payment_method = 'woocommerce_payments';
$order = WC_Helper_Order::create_order();
@@ -1780,8 +1681,8 @@ function ( $data ) {
)
);
- $gateway->process_payment_for_order( WC()->cart, $pi );
- WC_Payments::set_gateway( $this->wcpay_gateway );
+ $this->wcpay_gateway->process_payment_for_order( WC()->cart, $pi );
+ $this->wcpay_gateway->settings['upe_enabled_payment_method_ids'] = [ 'card' ];
}
public function test_process_payment_for_order_cc_payment_method() {
@@ -1870,10 +1771,6 @@ public function test_process_payment_caches_mimimum_amount_and_displays_error_up
->method( 'set_capture_method' )
->with( false );
- $request->expects( $this->once() )
- ->method( 'set_metadata' )
- ->with( [ 'gateway_type' => 'legacy_card' ] );
-
$request->expects( $this->once() )
->method( 'format_response' )
->will( $this->throwException( new Amount_Too_Small_Exception( 'Error: Amount must be at least $60 usd', 6000, 'usd', 400 ) ) );
@@ -2039,7 +1936,6 @@ public function test_is_woopay_enabled_returns_true() {
add_filter( 'woocommerce_is_checkout', '__return_true' );
$this->assertTrue( $this->woopay_utilities->should_enable_woopay( $this->wcpay_gateway ) );
- $this->assertTrue( $this->payments_checkout->get_payment_fields_js_config()['isWooPayEnabled'] );
remove_filter( 'woocommerce_is_checkout', '__return_true' );
}
@@ -2056,90 +1952,6 @@ public function test_should_use_stripe_platform_on_checkout_page_not_woopay() {
$this->assertFalse( $this->wcpay_gateway->should_use_stripe_platform_on_checkout_page() );
}
- public function test_force_network_saved_cards_is_returned_as_true_if_should_use_stripe_platform() {
- $mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'should_use_stripe_platform_on_checkout_page' ] );
-
- $mock_wcpay_gateway
- ->expects( $this->once() )
- ->method( 'should_use_stripe_platform_on_checkout_page' )
- ->willReturn( true );
-
- $registered_card_gateway = WC_Payments::get_registered_card_gateway();
- WC_Payments::set_registered_card_gateway( $mock_wcpay_gateway );
-
- $payments_checkout = new WC_Payments_Checkout(
- $mock_wcpay_gateway,
- $this->woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertTrue( $payments_checkout->get_payment_fields_js_config()['forceNetworkSavedCards'] );
- WC_Payments::set_registered_card_gateway( $registered_card_gateway );
- }
-
- public function test_registered_gateway_replaced_by_gateway_if_not_initialized() {
- // Set the registered gateway to null to simulate the case where the gateway is not initialized and thus should be replaced by $mock_wcpay_gateway.
- WC_Payments::set_registered_card_gateway( null );
-
- $mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'should_use_stripe_platform_on_checkout_page' ] );
-
- $mock_wcpay_gateway
- ->expects( $this->once() )
- ->method( 'should_use_stripe_platform_on_checkout_page' )
- ->willReturn( true );
-
- $payments_checkout = new WC_Payments_Checkout(
- $mock_wcpay_gateway,
- $this->woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertTrue( $payments_checkout->get_payment_fields_js_config()['forceNetworkSavedCards'] );
- }
-
- public function test_force_network_saved_cards_is_returned_as_false_if_should_not_use_stripe_platform() {
- $mock_wcpay_gateway = $this->get_partial_mock_for_gateway( [ 'should_use_stripe_platform_on_checkout_page' ] );
-
- $mock_wcpay_gateway
- ->expects( $this->once() )
- ->method( 'should_use_stripe_platform_on_checkout_page' )
- ->willReturn( false );
-
- $registered_card_gateway = WC_Payments::get_registered_card_gateway();
- WC_Payments::set_registered_card_gateway( $mock_wcpay_gateway );
-
- $payments_checkout = new WC_Payments_Checkout(
- $mock_wcpay_gateway,
- $this->woopay_utilities,
- $this->mock_wcpay_account,
- $this->mock_customer_service,
- $this->mock_fraud_service
- );
-
- $this->assertFalse( $payments_checkout->get_payment_fields_js_config()['forceNetworkSavedCards'] );
- WC_Payments::set_registered_card_gateway( $registered_card_gateway );
- }
-
- public function test_is_woopay_enabled_returns_false_if_ineligible() {
- $this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => false ] );
- $this->assertFalse( $this->payments_checkout->get_payment_fields_js_config()['isWooPayEnabled'] );
- }
-
- public function test_is_woopay_enabled_returns_false_if_ineligible_and_enabled() {
- $this->wcpay_gateway->update_option( 'platform_checkout', 'yes' );
- $this->assertFalse( $this->payments_checkout->get_payment_fields_js_config()['isWooPayEnabled'] );
- }
-
- public function test_return_icon_url() {
- $returned_icon = $this->payments_checkout->get_payment_fields_js_config()['icon'];
- $this->assertNotNull( $returned_icon );
- $this->assertStringContainsString( 'assets/images/payment-methods/cc.svg', $returned_icon );
- }
-
public function is_woopay_falsy_value_provider() {
return [
[ '0' ],
@@ -2199,6 +2011,8 @@ private function get_partial_mock_for_gateway( array $methods = [] ) {
$this->mock_customer_service,
$this->mock_token_service,
$this->mock_action_scheduler_service,
+ $this->mock_payment_method,
+ [ $this->mock_payment_method ],
$this->mock_rate_limiter,
$this->order_service,
$this->mock_dpps,
@@ -2545,17 +2359,20 @@ private function create_charge_object() {
return new WC_Payments_API_Charge( $this->mock_charge_id, 1500, $created );
}
- private function refresh_payments_checkout() {
- remove_all_actions( 'wc_payments_add_payment_fields' );
-
- $this->payments_checkout = new WC_Payments_Checkout(
- $this->wcpay_gateway,
- $this->woopay_utilities,
+ private function create_gateway_with( $payment_method ) {
+ return new WC_Payment_Gateway_WCPay(
+ $this->mock_api_client,
$this->mock_wcpay_account,
$this->mock_customer_service,
+ $this->mock_token_service,
+ $this->mock_action_scheduler_service,
+ $payment_method,
+ [ $payment_method ],
+ $this->mock_rate_limiter,
+ $this->order_service,
+ $this->mock_dpps,
+ $this->mock_localization_service,
$this->mock_fraud_service
);
-
- $this->payments_checkout->init_hooks();
}
}
diff --git a/tests/unit/test-class-wc-payments-checkout.php b/tests/unit/test-class-wc-payments-checkout.php
new file mode 100644
index 00000000000..d6273a1fcd2
--- /dev/null
+++ b/tests/unit/test-class-wc-payments-checkout.php
@@ -0,0 +1,562 @@
+mock_wcpay_gateway = $this->getMockBuilder( WC_Payment_Gateway_WCPay::class )
+ ->onlyMethods( [ 'get_account_domestic_currency', 'get_payment_method_ids_enabled_at_checkout', 'should_use_stripe_platform_on_checkout_page', 'should_support_saved_payments', 'is_saved_cards_enabled', 'save_payment_method_checkbox', 'get_account_statement_descriptor', 'get_icon_url', 'get_payment_method_ids_enabled_at_checkout_filtered_by_fees', 'is_subscription_item_in_cart', 'wc_payments_get_payment_method_by_id', 'display_gateway_html' ] )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->mock_wcpay_gateway->id = 'woocommerce_payments';
+ $this->mock_wcpay_gateway
+ ->method( 'get_account_domestic_currency' )
+ ->willReturn( 'USD' );
+
+ $this->mock_woopay_utilities = $this->createMock( WooPay_Utilities::class );
+ $this->mock_woopay_utilities = $this->getMockBuilder( WooPay_Utilities::class )
+ ->onlyMethods( [ 'should_enable_woopay', 'should_enable_woopay_on_cart_or_checkout' ] )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
+ $this->mock_customer_service = $this->createMock( WC_Payments_Customer_Service::class );
+ $this->mock_fraud_service = $this->createMock( WC_Payments_Fraud_Service::class );
+
+ $this->mock_wcpay_gateway
+ ->method( 'get_account_statement_descriptor' )
+ ->willReturn( 'localhost' );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout_filtered_by_fees' )
+ ->willReturn( [] );
+
+ $this->mock_token_service = $this->createMock( WC_Payments_Token_Service::class );
+
+ // This is needed to ensure that only the mocked gateway is always used by the checkout class.
+ $this->default_gateway = WC_Payments::get_gateway();
+ WC_Payments::set_gateway( $this->mock_wcpay_gateway );
+
+ // Use a callback to suppresses the output buffering being printed to the CLI.
+ $this->setOutputCallback(
+ function ( $output ) {
+ preg_match_all( '/.*
.*/s', $output );
+ }
+ );
+
+ $this->system_under_test = new WC_Payments_Checkout( $this->mock_wcpay_gateway, $this->mock_woopay_utilities, $this->mock_wcpay_account, $this->mock_customer_service, $this->mock_fraud_service );
+ }
+
+ public function tear_down() {
+ parent::tear_down();
+ WC_Payments::set_gateway( $this->default_gateway );
+ }
+
+ public function test_fraud_prevention_token_added_when_prevention_service_enabled() {
+ $token_value = 'test-token';
+ $fraud_prevention_service_mock = $this->getMockBuilder( Fraud_Prevention_Service::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $fraud_prevention_service_mock
+ ->expects( $this->once() )
+ ->method( 'is_enabled' )
+ ->willReturn( true );
+
+ $fraud_prevention_service_mock
+ ->expects( $this->once() )
+ ->method( 'get_token' )
+ ->willReturn( $token_value );
+
+ Fraud_Prevention_Service::set_instance( $fraud_prevention_service_mock );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ // Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
+ $this->setOutputCallback(
+ function ( $output ) use ( $token_value ) {
+ $result = preg_match_all( '/ ]*type="hidden"[^>]*name="wcpay-fraud-prevention-token"[^>]*value="' . preg_quote( $token_value, '/' ) . '"[^>]*>/', $output );
+
+ $this->assertSame( 1, $result );
+ }
+ );
+
+ $this->system_under_test->payment_fields();
+ }
+
+ public function test_fraud_prevention_token_not_added_when_prevention_service_disabled() {
+ $token_value = 'test-token';
+ $fraud_prevention_service_mock = $this->getMockBuilder( Fraud_Prevention_Service::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $fraud_prevention_service_mock
+ ->expects( $this->once() )
+ ->method( 'is_enabled' )
+ ->willReturn( true );
+
+ Fraud_Prevention_Service::set_instance( $fraud_prevention_service_mock );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ // Use a callback to get and test the output (also suppresses the output buffering being printed to the CLI).
+ $this->setOutputCallback(
+ function ( $output ) use ( $token_value ) {
+ $result = preg_match_all( '/ ]*type="hidden"[^>]*name="wcpay-fraud-prevention-token"[^>]*value="' . preg_quote( $token_value, '/' ) . '"[^>]*>/', $output );
+
+ $this->assertSame( 0, $result );
+ }
+ );
+
+ $this->system_under_test->payment_fields();
+ }
+
+ public function test_save_payment_method_checkbox_not_called_when_saved_cards_disabled() {
+ // given: prepare the dependencies.
+ wp_set_current_user( 1 );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( false );
+
+ // then: check that the save_payment_method_checkbox method was called.
+ $this->mock_wcpay_gateway
+ ->expects( $this->never() )
+ ->method( 'save_payment_method_checkbox' );
+
+ $this->system_under_test->payment_fields();
+ }
+
+ public function test_save_payment_method_checkbox_not_called_for_non_logged_in_user() {
+ // given: prepare the dependencies.
+ wp_set_current_user( 0 );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( true );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->once() )
+ ->method( 'should_support_saved_payments' )
+ ->willReturn( true );
+
+ // then: check that the save_payment_method_checkbox method was called.
+ $this->mock_wcpay_gateway
+ ->expects( $this->never() )
+ ->method( 'save_payment_method_checkbox' );
+
+ $this->system_under_test->payment_fields();
+ }
+
+ public function test_save_payment_method_checkbox_called() {
+ // given: prepare the dependencies.
+ wp_set_current_user( 1 );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( true );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->once() )
+ ->method( 'should_support_saved_payments' )
+ ->willReturn( true );
+
+ // then: check that the save_payment_method_checkbox method was called.
+ $this->mock_wcpay_gateway
+ ->expects( $this->once() )
+ ->method( 'save_payment_method_checkbox' );
+
+ $this->system_under_test->payment_fields();
+ }
+
+ public function test_display_gateway_html_called() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->once() )
+ ->method( 'display_gateway_html' );
+
+ $this->system_under_test->payment_fields();
+ }
+
+ public function test_is_woopay_enabled_when_should_enable_woopay_and_enable_it_on_cart_or_checkout() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_woopay_utilities->method( 'should_enable_woopay' )->willReturn( true );
+ $this->mock_woopay_utilities->method( 'should_enable_woopay_on_cart_or_checkout' )->willReturn( true );
+
+ $is_woopay_enabled = $this->system_under_test->get_payment_fields_js_config()['isWooPayEnabled'];
+ $this->assertTrue( $is_woopay_enabled );
+ }
+
+ public function test_is_woopay_enabled_false_when_should_not_enable_woopay() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_woopay_utilities->method( 'should_enable_woopay' )->willReturn( false );
+ $this->mock_woopay_utilities->method( 'should_enable_woopay_on_cart_or_checkout' )->willReturn( true );
+
+ $is_woopay_enabled = $this->system_under_test->get_payment_fields_js_config()['isWooPayEnabled'];
+ $this->assertFalse( $is_woopay_enabled );
+ }
+
+ public function test_is_woopay_enabled_false_when_should_enable_woopay_but_not_enable_it_on_cart_or_checkout() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_woopay_utilities->method( 'should_enable_woopay' )->willReturn( true );
+ $this->mock_woopay_utilities->method( 'should_enable_woopay_on_cart_or_checkout' )->willReturn( false );
+
+ $is_woopay_enabled = $this->system_under_test->get_payment_fields_js_config()['isWooPayEnabled'];
+ $this->assertFalse( $is_woopay_enabled );
+ }
+
+ public function test_return_icon_url() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'get_icon_url' )
+ ->willReturn( 'assets/images/payment-methods/cc.svg' );
+
+ $returned_icon = $this->system_under_test->get_payment_fields_js_config()['icon'];
+
+ $this->assertNotNull( $returned_icon );
+ $this->assertStringContainsString( 'assets/images/payment-methods/cc.svg', $returned_icon );
+ }
+
+ public function test_force_network_saved_cards_enabled_when_should_use_stripe_platform() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'should_use_stripe_platform_on_checkout_page' )
+ ->willReturn( true );
+
+ $force_network_saved_cards = $this->system_under_test->get_payment_fields_js_config()['forceNetworkSavedCards'];
+ $this->assertTrue( $force_network_saved_cards );
+ }
+
+ public function test_force_network_saved_cards_disabled_when_should_not_use_stripe_platform() {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'should_use_stripe_platform_on_checkout_page' )
+ ->willReturn( false );
+
+ $force_network_saved_cards = $this->system_under_test->get_payment_fields_js_config()['forceNetworkSavedCards'];
+ $this->assertFalse( $force_network_saved_cards );
+ }
+
+ public function test_link_payment_method_provided_when_card_enabled() {
+ $icon_url = 'test-icon-url';
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn( [ 'card', 'link' ] );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( true );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_subscription_item_in_cart' )
+ ->willReturn( false );
+
+ $payment_methods = [
+ 'card' => [ 'base' => 0.1 ],
+ 'link' => [
+ 'base' => 0.1,
+ ],
+ ];
+
+ $card_pm = $this->getMockBuilder( CC_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->onlyMethods( [ 'get_icon' ] )
+ ->getMock();
+
+ $link_pm = $this->getMockBuilder( Link_Payment_Method::class )
+ ->setConstructorArgs( [ $this->mock_token_service ] )
+ ->onlyMethods( [ 'get_icon' ] )
+ ->getMock();
+
+ $card_pm->expects( $this->any() )
+ ->method( 'get_icon' )
+ ->will(
+ $this->returnValue( $icon_url )
+ );
+
+ $link_pm->expects( $this->any() )
+ ->method( 'get_icon' )
+ ->will(
+ $this->returnValue( $icon_url )
+ );
+
+ $this->mock_wcpay_gateway
+ ->method( 'wc_payments_get_payment_method_by_id' )
+ ->withConsecutive( [ 'card' ], [ 'link' ] )
+ ->willReturnOnConsecutiveCalls( $card_pm, $link_pm );
+
+ $this->mock_wcpay_account
+ ->expects( $this->any() )
+ ->method( 'get_fees' )
+ ->willReturn( $payment_methods );
+
+ $this->assertSame(
+ $this->system_under_test->get_payment_fields_js_config()['paymentMethodsConfig'],
+ [
+ 'card' => [
+ 'isReusable' => true,
+ 'title' => 'Credit card / debit card',
+ 'icon' => $icon_url,
+ 'showSaveOption' => true,
+ 'countries' => [],
+ 'testingInstructions' => 'Test mode: use the test VISA card 4242424242424242 with any expiry date and CVC. Other payment methods may redirect to a Stripe test page to authorize payment. More test card numbers are listed here .',
+ 'forceNetworkSavedCards' => false,
+ ],
+ 'link' => [
+ 'isReusable' => true,
+ 'title' => 'Link',
+ 'icon' => $icon_url,
+ 'showSaveOption' => true,
+ 'countries' => [],
+ 'testingInstructions' => '',
+ 'forceNetworkSavedCards' => false,
+ ],
+ ]
+ );
+ }
+
+ /**
+ * @dataProvider non_reusable_payment_method_provider
+ */
+ public function test_no_save_option_for_non_reusable_payment_method( $payment_method_id, $payment_method_class ) {
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn(
+ [
+ $payment_method_id,
+ ]
+ );
+
+ $this->mock_wcpay_gateway
+ ->method( 'wc_payments_get_payment_method_by_id' )
+ ->willReturn(
+ new $payment_method_class( $this->mock_token_service )
+ );
+
+ $this->assertSame( false, $this->system_under_test->get_payment_fields_js_config()['paymentMethodsConfig'][ $payment_method_id ]['showSaveOption'] );
+ }
+
+ public function non_reusable_payment_method_provider() {
+ return [
+ [ Payment_Method::BANCONTACT, Bancontact_Payment_Method::class ],
+ [ Payment_Method::EPS, Eps_Payment_Method::class ],
+ [ Payment_Method::GIROPAY, Giropay_Payment_Method::class ],
+ [ Payment_Method::IDEAL, Ideal_Payment_Method::class ],
+ [ Payment_Method::P24, P24_Payment_Method::class ],
+ [ Payment_Method::SOFORT, Sofort_Payment_Method::class ],
+ ];
+ }
+
+ public function test_no_save_option_for_reusable_payment_payment_with_subscription_in_cart() {
+ $this->mock_wcpay_gateway
+ ->method( 'is_subscription_item_in_cart' )
+ ->willReturn( true );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( true );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn(
+ [
+ Payment_Method::CARD,
+ ]
+ );
+
+ $this->mock_wcpay_gateway
+ ->method( 'wc_payments_get_payment_method_by_id' )
+ ->willReturn(
+ new CC_Payment_Method( $this->mock_token_service )
+ );
+ $this->assertSame( false, $this->system_under_test->get_payment_fields_js_config()['paymentMethodsConfig'][ Payment_Method::CARD ]['showSaveOption'] );
+ }
+
+ public function test_no_save_option_for_reusable_payment_payment_but_with_saved_cards_disabled() {
+ $this->mock_wcpay_gateway
+ ->method( 'is_subscription_item_in_cart' )
+ ->willReturn( false );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( false );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn(
+ [
+ Payment_Method::CARD,
+ ]
+ );
+
+ $this->mock_wcpay_gateway
+ ->method( 'wc_payments_get_payment_method_by_id' )
+ ->willReturn(
+ new CC_Payment_Method( $this->mock_token_service )
+ );
+ $this->assertSame( false, $this->system_under_test->get_payment_fields_js_config()['paymentMethodsConfig'][ Payment_Method::CARD ]['showSaveOption'] );
+ }
+
+ public function test_save_option_for_reusable_payment_payment() {
+ $this->mock_wcpay_gateway
+ ->method( 'is_subscription_item_in_cart' )
+ ->willReturn( false );
+
+ $this->mock_wcpay_gateway
+ ->method( 'is_saved_cards_enabled' )
+ ->willReturn( true );
+
+ $this->mock_wcpay_gateway
+ ->expects( $this->any() )
+ ->method( 'get_payment_method_ids_enabled_at_checkout' )
+ ->willReturn(
+ [
+ Payment_Method::CARD,
+ ]
+ );
+
+ $this->mock_wcpay_gateway
+ ->method( 'wc_payments_get_payment_method_by_id' )
+ ->willReturn(
+ new CC_Payment_Method( $this->mock_token_service )
+ );
+ $this->assertSame( true, $this->system_under_test->get_payment_fields_js_config()['paymentMethodsConfig'][ Payment_Method::CARD ]['showSaveOption'] );
+ }
+}
diff --git a/tests/unit/test-class-wc-payments-express-checkout-button-display-handler.php b/tests/unit/test-class-wc-payments-express-checkout-button-display-handler.php
index 220f3d2ff03..899e530aa67 100644
--- a/tests/unit/test-class-wc-payments-express-checkout-button-display-handler.php
+++ b/tests/unit/test-class-wc-payments-express-checkout-button-display-handler.php
@@ -7,6 +7,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use WCPay\Duplicate_Payment_Prevention_Service;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
use WCPay\WooPay\WooPay_Utilities;
@@ -63,6 +64,13 @@ class WC_Payments_Express_Checkout_Button_Display_Handler_Test extends WCPAY_Uni
*/
private $mock_woopay_utilities;
+ /**
+ * Express Checkout Helper instance.
+ *
+ * @var WC_Payments_Express_Checkout_Button_Helper
+ */
+ private $express_checkout_helper;
+
/**
* Sets up things all tests need.
*/
@@ -103,11 +111,14 @@ public function set_up() {
)
->getMock();
+ $this->express_checkout_helper = new WC_Payments_Express_Checkout_Button_Helper( $this->mock_wcpay_account );
+
$this->mock_payment_request_button_handler = $this->getMockBuilder( WC_Payments_Payment_Request_Button_Handler::class )
->setConstructorArgs(
[
$this->mock_wcpay_account,
$this->mock_wcpay_gateway,
+ $this->express_checkout_helper,
]
)
->setMethods(
@@ -118,7 +129,7 @@ public function set_up() {
)
->getMock();
- $this->express_checkout_button_display_handler = new WC_Payments_Express_Checkout_Button_Display_Handler( $this->mock_wcpay_gateway, $this->mock_payment_request_button_handler, $this->mock_woopay_button_handler );
+ $this->express_checkout_button_display_handler = new WC_Payments_Express_Checkout_Button_Display_Handler( $this->mock_wcpay_gateway, $this->mock_payment_request_button_handler, $this->mock_woopay_button_handler, $this->express_checkout_helper );
add_filter(
'woocommerce_available_payment_gateways',
@@ -138,6 +149,7 @@ private function make_wcpay_gateway() {
$mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class );
$mock_order_service = $this->createMock( WC_Payments_Order_Service::class );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
return new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
@@ -145,6 +157,8 @@ private function make_wcpay_gateway() {
$mock_customer_service,
$mock_token_service,
$mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$mock_rate_limiter,
$mock_order_service,
$mock_dpps,
diff --git a/tests/unit/test-class-wc-payments-payment-request-button-handler.php b/tests/unit/test-class-wc-payments-payment-request-button-handler.php
index 7940d7b20f0..e4d7412bf15 100644
--- a/tests/unit/test-class-wc-payments-payment-request-button-handler.php
+++ b/tests/unit/test-class-wc-payments-payment-request-button-handler.php
@@ -6,6 +6,7 @@
*/
use WCPay\Duplicate_Payment_Prevention_Service;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
/**
@@ -76,6 +77,13 @@ class WC_Payments_Payment_Request_Button_Handler_Test extends WCPAY_UnitTestCase
*/
private $mock_wcpay_gateway;
+ /**
+ * Express Checkout Helper instance.
+ *
+ * @var WC_Payments_Express_Checkout_Button_Helper
+ */
+ private $express_checkout_helper;
+
/**
* Sets up things all tests need.
*/
@@ -100,7 +108,9 @@ public function set_up() {
$this->mock_wcpay_gateway = $this->make_wcpay_gateway();
- $this->pr = new WC_Payments_Payment_Request_Button_Handler( $this->mock_wcpay_account, $this->mock_wcpay_gateway );
+ $this->express_checkout_helper = new WC_Payments_Express_Checkout_Button_Helper( $this->mock_wcpay_account );
+
+ $this->pr = new WC_Payments_Payment_Request_Button_Handler( $this->mock_wcpay_account, $this->mock_wcpay_gateway, $this->express_checkout_helper );
$this->simple_product = WC_Helper_Product::create_simple_product();
@@ -189,6 +199,7 @@ private function make_wcpay_gateway() {
$mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class );
$mock_order_service = $this->createMock( WC_Payments_Order_Service::class );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
return new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
@@ -196,6 +207,8 @@ private function make_wcpay_gateway() {
$mock_customer_service,
$mock_token_service,
$mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$mock_rate_limiter,
$mock_order_service,
$mock_dpps,
@@ -294,7 +307,7 @@ public function test_get_shipping_options_keeps_chosen_option() {
public function test_get_button_settings() {
$this->mock_wcpay_gateway = $this->make_wcpay_gateway();
- $this->pr = new WC_Payments_Payment_Request_Button_Handler( $this->mock_wcpay_account, $this->mock_wcpay_gateway );
+ $this->pr = new WC_Payments_Payment_Request_Button_Handler( $this->mock_wcpay_account, $this->mock_wcpay_gateway, $this->express_checkout_helper );
$this->assertEquals(
[
@@ -320,7 +333,7 @@ function() {
}
);
$this->mock_wcpay_gateway = $this->make_wcpay_gateway();
- $this->pr = new WC_Payments_Payment_Request_Button_Handler( $this->mock_wcpay_account, $this->mock_wcpay_gateway );
+ $this->pr = new WC_Payments_Payment_Request_Button_Handler( $this->mock_wcpay_account, $this->mock_wcpay_gateway, $this->express_checkout_helper );
$this->assertFalse( $this->pr->has_allowed_items_in_cart() );
}
@@ -505,10 +518,10 @@ public function test_get_product_data_returns_the_same_as_build_display_items_wi
add_filter( 'pre_option_woocommerce_tax_display_cart', [ $this, "__return_$tax_display_cart" ] ); // reset in tear_down.
add_filter( 'wc_shipping_enabled', '__return_false' ); // reset in tear_down.
WC()->cart->calculate_totals();
- $build_display_items_result = $this->pr->build_display_items( true );
+ $build_display_items_result = $this->express_checkout_helper->build_display_items( true );
$mock_pr = $this->getMockBuilder( WC_Payments_Payment_Request_Button_Handler::class )
- ->setConstructorArgs( [ $this->mock_wcpay_account, $this->mock_wcpay_gateway ] )
+ ->setConstructorArgs( [ $this->mock_wcpay_account, $this->mock_wcpay_gateway, $this->express_checkout_helper ] )
->setMethods( [ 'is_product', 'get_product' ] )
->getMock();
diff --git a/tests/unit/test-class-wc-payments-utils.php b/tests/unit/test-class-wc-payments-utils.php
index f9ed09117f6..0248b757f3b 100644
--- a/tests/unit/test-class-wc-payments-utils.php
+++ b/tests/unit/test-class-wc-payments-utils.php
@@ -518,7 +518,7 @@ public function test_format_currency( float $amount, string $currency, string $e
public function provider_format_currency(): array {
return [
'US dollar' => [ 123.456, 'USD', '$123.46' ],
- 'US dollar with negative amount' => [ -123.456, 'USD', '$-123.46' ],
+ 'US dollar with negative amount' => [ -123.456, 'USD', '-$123.46' ],
'Euro' => [ 12000, 'EUR', '12.000,00 €' ],
'CHF - no currency symbol' => [ 123, 'CHF', 'CHF 123.00' ],
'VND - decimal currency' => [ 123456, 'VND', '123.456 ₫' ],
diff --git a/tests/unit/test-class-wc-payments-woopay-button-handler.php b/tests/unit/test-class-wc-payments-woopay-button-handler.php
index 78f464cadf2..161c5796101 100644
--- a/tests/unit/test-class-wc-payments-woopay-button-handler.php
+++ b/tests/unit/test-class-wc-payments-woopay-button-handler.php
@@ -6,6 +6,7 @@
*/
use WCPay\Duplicate_Payment_Prevention_Service;
+use WCPay\Payment_Methods\CC_Payment_Method;
use WCPay\Session_Rate_Limiter;
use WCPay\WooPay\WooPay_Utilities;
@@ -127,6 +128,7 @@ private function make_wcpay_gateway() {
$mock_rate_limiter = $this->createMock( Session_Rate_Limiter::class );
$mock_order_service = $this->createMock( WC_Payments_Order_Service::class );
$mock_dpps = $this->createMock( Duplicate_Payment_Prevention_Service::class );
+ $mock_payment_method = $this->createMock( CC_Payment_Method::class );
return new WC_Payment_Gateway_WCPay(
$this->mock_api_client,
@@ -134,6 +136,8 @@ private function make_wcpay_gateway() {
$mock_customer_service,
$mock_token_service,
$mock_action_scheduler_service,
+ $mock_payment_method,
+ [ 'card' => $mock_payment_method ],
$mock_rate_limiter,
$mock_order_service,
$mock_dpps,
diff --git a/tests/unit/test-class-wc-payments.php b/tests/unit/test-class-wc-payments.php
index 692a45b96df..d5e7da12239 100644
--- a/tests/unit/test-class-wc-payments.php
+++ b/tests/unit/test-class-wc-payments.php
@@ -5,7 +5,6 @@
* @package WooCommerce\Payments\Tests
*/
-use WCPay\Payment_Methods\UPE_Split_Payment_Gateway;
use WCPay\WooPay\WooPay_Session;
/**
@@ -81,7 +80,7 @@ public function test_it_does_not_register_woopay_hooks_if_feature_flag_is_disabl
public function test_it_skips_stripe_link_gateway_registration() {
$this->mock_cache->method( 'get' )->willReturn( [ 'is_deferred_intent_creation_upe_enabled' => true ] );
- $card_gateway_mock = $this->createMock( UPE_Split_Payment_Gateway::class );
+ $card_gateway_mock = $this->createMock( WC_Payment_Gateway_WCPay::class );
$card_gateway_mock
->expects( $this->once() )
->method( 'get_payment_method_ids_enabled_at_checkout' )
@@ -100,7 +99,7 @@ public function test_it_skips_stripe_link_gateway_registration() {
$registered_gateways = WC_Payments::register_gateway( [] );
$this->assertCount( 1, $registered_gateways );
- $this->assertInstanceOf( UPE_Split_Payment_Gateway::class, $registered_gateways[0] );
+ $this->assertInstanceOf( WC_Payment_Gateway_WCPay::class, $registered_gateways[0] );
$this->assertEquals( $registered_gateways[0]->get_stripe_id(), 'card' );
}
diff --git a/woocommerce-payments.php b/woocommerce-payments.php
index df1a348ec90..df7afe47766 100644
--- a/woocommerce-payments.php
+++ b/woocommerce-payments.php
@@ -9,7 +9,7 @@
* Text Domain: woocommerce-payments
* Domain Path: /languages
* WC requires at least: 7.6
- * WC tested up to: 8.3.1
+ * WC tested up to: 8.4.0
* Requires at least: 6.0
* Requires PHP: 7.3
* Version: 6.9.2