From 082753054ae4bb2112b3249aa33b78a4ac7d09c5 Mon Sep 17 00:00:00 2001 From: Oleksandr Aratovskyi <79862886+oaratovskyi@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:56:56 +0200 Subject: [PATCH] Allow redirect to the settings page from WCPay connect (#9827) Co-authored-by: oaratovskyi Co-authored-by: Daniel Mallory --- assets/images/illustrations/setup.svg | 1 + ...direct-to-settings-page-from-wcpay-connect | 4 ++ client/connect-account-page/index.tsx | 18 +++--- client/connect-account-page/style.scss | 8 +-- includes/class-wc-payments-account.php | 55 +++++++++++++++---- .../class-wc-payments-redirect-service.php | 28 ++++++++++ tests/unit/test-class-wc-payments-account.php | 47 +++++++++++++++- ...est-class-wc-payments-redirect-service.php | 11 ++++ 8 files changed, 146 insertions(+), 26 deletions(-) create mode 100644 assets/images/illustrations/setup.svg create mode 100644 changelog/dev-allow-redirect-to-settings-page-from-wcpay-connect diff --git a/assets/images/illustrations/setup.svg b/assets/images/illustrations/setup.svg new file mode 100644 index 00000000000..b5c8abde96d --- /dev/null +++ b/assets/images/illustrations/setup.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/changelog/dev-allow-redirect-to-settings-page-from-wcpay-connect b/changelog/dev-allow-redirect-to-settings-page-from-wcpay-connect new file mode 100644 index 00000000000..3fca0c1ff3e --- /dev/null +++ b/changelog/dev-allow-redirect-to-settings-page-from-wcpay-connect @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Allow redirect to the settings page from WCPay connect diff --git a/client/connect-account-page/index.tsx b/client/connect-account-page/index.tsx index ac1668de0a8..faa5d94311c 100644 --- a/client/connect-account-page/index.tsx +++ b/client/connect-account-page/index.tsx @@ -27,11 +27,12 @@ import Incentive from './incentive'; import InfoNotice from './info-notice-modal'; import OnboardingLocationCheckModal from './modal'; import LogoImg from 'assets/images/woopayments.svg?asset'; +import SetupImg from 'assets/images/illustrations/setup.svg?asset'; import strings from './strings'; import './style.scss'; import InlineNotice from 'components/inline-notice'; import { WooPaymentMethodsLogos } from 'components/payment-method-logos'; -import WooPaymentsLogo from 'assets/images/logo.svg?asset'; +import WooLogo from 'assets/images/woo-logo.svg?asset'; import { sanitizeHTML } from 'wcpay/utils/sanitize'; import { isInTestModeOnboarding } from 'wcpay/utils'; import ResetAccountModal from 'wcpay/overview/modal/reset-account'; @@ -52,18 +53,19 @@ const TestDriveLoader: React.FunctionComponent< { progress: number; } > = ( { progress } ) => ( - + + + setup + + - { __( - 'Creating your sandbox account', - 'woocommerce-payments' - ) } + { __( 'Finishing payments setup', 'woocommerce-payments' ) } { __( - 'In just a few moments, you will be ready to test payments on your store.' + "In just a few moments, you'll be ready to test payments on your store." ) } @@ -193,6 +195,8 @@ const ConnectAccountPage: React.FC = () => { 'wcpay-sandbox-success': 'true', source: determineTrackingSource(), from: 'WCPAY_CONNECT', + redirect_to_settings_page: + urlParams.get( 'redirect_to_settings_page' ) || '', } ); } else { setTimeout( checkAccountStatus, 2000 ); diff --git a/client/connect-account-page/style.scss b/client/connect-account-page/style.scss index 41e278c3bc1..96efc3493c2 100644 --- a/client/connect-account-page/style.scss +++ b/client/connect-account-page/style.scss @@ -249,12 +249,12 @@ z-index: 999999; text-align: center; - img { + img.logo { position: absolute; - height: 44px; - width: 167px; + height: 40px; + width: 40px; top: 18px; - left: calc( 50% - 84px ); + left: 36px; } } } diff --git a/includes/class-wc-payments-account.php b/includes/class-wc-payments-account.php index d2a01ec48ff..e884d582ac8 100644 --- a/includes/class-wc-payments-account.php +++ b/includes/class-wc-payments-account.php @@ -1143,12 +1143,12 @@ public function maybe_handle_onboarding() { * * 0. Make changes to the account data if needed (e.g. reset account, disable test mode onboarding) * as instructed by the GET params. - * 0.1 If we reset the account -> redirect to CONNECT PAGE + * 0.1 If we reset the account -> redirect to CONNECT PAGE / SETTINGS PAGE If redirect to settings page flag set * 1. Returning from the WPCOM/Jetpack connection screen. * 1.1 SUCCESSFUL connection * 1.1.1 NO Stripe account connected -> redirect to ONBOARDING WIZARD * 1.1.2 Stripe account connected -> redirect to OVERVIEW PAGE - * 1.2 UNSUCCESSFUL connection -> redirect to CONNECT PAGE with ERROR message + * 1.2 UNSUCCESSFUL connection -> redirect to CONNECT PAGE with ERROR message / SETTINGS PAGE if redirect to settings page flag set * 2. Working WPCOM/Jetpack connection and fully onboarded Stripe account -> redirect to OVERVIEW PAGE * 3. Specific `from` places -> redirect to CONNECT PAGE regardless of the account status * 4. NO [working] WPCOM/Jetpack connection: @@ -1167,6 +1167,7 @@ public function maybe_handle_onboarding() { * 5.1.3 All other cases -> redirect to ONBOARDING WIZARD * 5.2 If PARTIALLY onboarded Stripe account connected -> redirect to STRIPE KYC * 5.3 If fully onboarded Stripe account connected -> redirect to OVERVIEW PAGE + * 5.3.1 If redirect to settings page flags set -> redirect to SETTINGS PAGE * * This logic is so complex because we use connect links as a catch-all place to * handle everything and anything related to the WooPayments account setup. It reduces the complexity on the @@ -1182,6 +1183,7 @@ public function maybe_handle_onboarding() { $progressive = ! empty( $_GET['progressive'] ) && 'true' === $_GET['progressive']; $collect_payout_requirements = ! empty( $_GET['collect_payout_requirements'] ) && 'true' === $_GET['collect_payout_requirements']; $create_test_drive_account = ! empty( $_GET['test_drive'] ) && 'true' === $_GET['test_drive']; + $redirect_to_settings_page = ! empty( $_GET['redirect_to_settings_page'] ) && 'true' === $_GET['redirect_to_settings_page']; // There is no point in auto starting test drive onboarding if we are not in the test drive mode. $auto_start_test_drive_onboarding = $create_test_drive_account && ! empty( $_GET['auto_start_test_drive_onboarding'] ) && @@ -1253,7 +1255,16 @@ public function maybe_handle_onboarding() { $this->cleanup_on_account_reset(); - // When we reset the account we want to always go the Connect page. Redirect immediately! + // When we reset the account and want to go back to the settings page - redirect immediately! + if ( $redirect_to_settings_page ) { + $this->redirect_service->redirect_to_settings_page( + WC_Payments_Onboarding_Service::FROM_RESET_ACCOUNT, + [ 'source' => $onboarding_source ] + ); + return; + } + + // Otherwise, when we reset the account we want to always go the Connect page. Redirect immediately! $this->redirect_service->redirect_to_connect_page( null, WC_Payments_Onboarding_Service::FROM_RESET_ACCOUNT, @@ -1312,6 +1323,16 @@ public function maybe_handle_onboarding() { array_merge( $tracks_props, [ 'mode' => WC_Payments_Onboarding_Service::is_test_mode_enabled() ? 'test' : 'live' ] ) ); + if ( $redirect_to_settings_page ) { + $this->redirect_service->redirect_to_settings_page( + WC_Payments_Onboarding_Service::FROM_WPCOM_CONNECTION, + [ + 'source' => $onboarding_source, + 'wcpay-connect-jetpack-error' => '1', + ] + ); + } + $this->redirect_service->redirect_to_connect_page( sprintf( /* translators: %s: WooPayments */ @@ -1343,15 +1364,23 @@ public function maybe_handle_onboarding() { && $this->has_working_jetpack_connection() && $this->is_stripe_account_valid() ) { + $params = [ + 'source' => $onboarding_source, + // Carry over some parameters as they may be used by our frontend logic. + 'wcpay-connection-success' => ! empty( $_GET['wcpay-connection-success'] ) ? '1' : false, + 'wcpay-sandbox-success' => ! empty( $_GET['wcpay-sandbox-success'] ) ? 'true' : false, + 'test_drive_error' => ! empty( $_GET['test_drive_error'] ) ? 'true' : false, + ]; + if ( $redirect_to_settings_page ) { + $this->redirect_service->redirect_to_settings_page( + $from, + $params + ); + return; + } $this->redirect_service->redirect_to_overview_page( $from, - [ - 'source' => $onboarding_source, - // Carry over some parameters as they may be used by our frontend logic. - 'wcpay-connection-success' => ! empty( $_GET['wcpay-connection-success'] ) ? '1' : false, - 'wcpay-sandbox-success' => ! empty( $_GET['wcpay-sandbox-success'] ) ? 'true' : false, - 'test_drive_error' => ! empty( $_GET['test_drive_error'] ) ? 'true' : false, - ] + $params ); return; } @@ -1361,13 +1390,14 @@ public function maybe_handle_onboarding() { in_array( $from, [ - WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_SETTINGS, WC_Payments_Onboarding_Service::FROM_STRIPE, ], true ) // This is a weird case, but it is best to handle it. || ( WC_Payments_Onboarding_Service::FROM_ONBOARDING_WIZARD === $from && ! $this->has_working_jetpack_connection() ) + // Redirect merchants coming from settings page to the connect page only if $redirect_to_settings_page is false. + || ( WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_SETTINGS === $from && ! $redirect_to_settings_page ) ) { $this->redirect_service->redirect_to_connect_page( ! empty( $_GET['wcpay-connection-error'] ) ? sprintf( @@ -1410,7 +1440,7 @@ public function maybe_handle_onboarding() { 'auto_start_test_drive_onboarding' => $auto_start_test_drive_onboarding ? 'true' : false, 'from' => WC_Payments_Onboarding_Service::FROM_WPCOM_CONNECTION, 'source' => $onboarding_source, - + 'redirect_to_settings_page' => $redirect_to_settings_page ? 'true' : false, ], self::get_connect_url( $wcpay_connect_param ) // Instruct Jetpack to return here (connect link). ), @@ -1480,6 +1510,7 @@ public function maybe_handle_onboarding() { 'auto_start_test_drive_onboarding' => 'true', // This is critical. 'test_mode' => $should_onboard_in_test_mode ? 'true' : false, 'source' => $onboarding_source, + 'redirect_to_settings_page' => $redirect_to_settings_page ? 'true' : false, ] ); return; diff --git a/includes/class-wc-payments-redirect-service.php b/includes/class-wc-payments-redirect-service.php index 7863cf0aa6d..bae6299664e 100644 --- a/includes/class-wc-payments-redirect-service.php +++ b/includes/class-wc-payments-redirect-service.php @@ -182,6 +182,34 @@ public function redirect_to_onboarding_wizard( ?string $from = null, array $addi $this->redirect_to( admin_url( add_query_arg( $params, 'admin.php' ) ) ); } + /** + * Immediately redirect to the settings page. + * + * Note that this function immediately ends the execution. + * + * @param string|null $from Optional. Source of the redirect. + * @param array $additional_params Optional. Additional URL params to add to the redirect URL. + */ + public function redirect_to_settings_page( ?string $from = null, array $additional_params = [] ): void { + $params = [ + 'page' => 'wc-settings', + 'tab' => 'checkout', + ]; + + if ( count( $params ) === count( array_intersect_assoc( $_GET, $params ) ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended + // We are already in the settings page. Do nothing. + return; + } + + $params = array_merge( $params, $additional_params ); + + if ( ! empty( $from ) ) { + $params['from'] = $from; + } + + $this->redirect_to( admin_url( add_query_arg( $params, 'admin.php' ) ) ); + } + /** * Redirect to the overview page. * diff --git a/tests/unit/test-class-wc-payments-account.php b/tests/unit/test-class-wc-payments-account.php index 54ae7dede7e..f0087e3e966 100644 --- a/tests/unit/test-class-wc-payments-account.php +++ b/tests/unit/test-class-wc-payments-account.php @@ -257,6 +257,7 @@ public function test_maybe_handle_onboarding_connect_from_known_from( $has_working_jetpack_connection, $is_stripe_connected, $create_test_drive_account, + $redirect_to_settings_page, $expected_next_step ) { @@ -278,9 +279,10 @@ public function test_maybe_handle_onboarding_connect_from_known_from( $_GET['page'] = 'wc-admin'; $_GET['path'] = '/payments/some-bogus-page'; - $_GET['from'] = $onboarding_from; - $_GET['source'] = $onboarding_source; - $_GET['test_drive'] = $create_test_drive_account ? 'true' : null; + $_GET['from'] = $onboarding_from; + $_GET['source'] = $onboarding_source; + $_GET['test_drive'] = $create_test_drive_account ? 'true' : null; + $_GET['redirect_to_settings_page'] = $redirect_to_settings_page ? 'true' : null; $this->mock_jetpack_connection( $has_working_jetpack_connection ); @@ -398,6 +400,18 @@ public function test_maybe_handle_onboarding_connect_from_known_from( ) ); break; + case 'settings_page': + $this->mock_api_client + ->expects( $this->never() ) + ->method( 'start_server_connection' ); + $mock_redirect_service + ->expects( $this->once() ) + ->method( 'redirect_to' ) + ->with( + // It should redirect to settings page URL. + $this->stringContains( 'page=wc-settings&tab=checkout' ), + ); + break; default: $this->fail( 'Unexpected redirect type: ' . $expected_next_step ); break; @@ -431,6 +445,7 @@ public function provider_onboarding_known_froms() { false, true, false, + false, 'start_jetpack_connection', ], 'From Woo Payments task - Jetpack connection, Stripe not connected' => [ @@ -439,6 +454,7 @@ public function provider_onboarding_known_froms() { true, false, false, + false, 'onboarding_wizard', ], 'From Woo Payments task - Jetpack connection, Stripe connected' => [ @@ -447,6 +463,7 @@ public function provider_onboarding_known_froms() { true, true, false, + false, 'overview_page', ], 'From Connect page - no Jetpack connection, Stripe connected' => [ @@ -455,6 +472,7 @@ public function provider_onboarding_known_froms() { false, true, false, + false, 'start_jetpack_connection', ], 'From Connect page - Jetpack connection, Stripe not connected' => [ @@ -463,6 +481,7 @@ public function provider_onboarding_known_froms() { true, false, false, + false, 'onboarding_wizard', ], 'From Connect page - Jetpack connection, Stripe connected' => [ @@ -471,6 +490,7 @@ public function provider_onboarding_known_froms() { true, true, false, + false, 'overview_page', ], 'From Connect page - no Jetpack connection, Stripe connected - test-drive' => [ @@ -479,6 +499,7 @@ public function provider_onboarding_known_froms() { false, true, true, + false, 'start_jetpack_connection', ], 'From Connect page - Jetpack connection, Stripe not connected - test-drive' => [ @@ -487,6 +508,7 @@ public function provider_onboarding_known_froms() { true, false, true, + false, 'init_stripe_onboarding', ], 'From Connect page - Jetpack connection, Stripe connected - test-drive' => [ @@ -495,14 +517,25 @@ public function provider_onboarding_known_froms() { true, true, true, + false, 'overview_page', ], + 'From Connect page - Jetpack connection, Stripe connected - test-drive, redirect to settings' => [ + WC_Payments_Onboarding_Service::FROM_CONNECT_PAGE, + WC_Payments_Onboarding_Service::SOURCE_WCADMIN_SETTINGS_PAGE, // Some other original source. + true, + true, + true, + true, + 'settings_page', + ], 'From Woo Payments Settings - no Jetpack connection, Stripe connected' => [ WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_SETTINGS, WC_Payments_Onboarding_Service::SOURCE_WCADMIN_SETTINGS_PAGE, false, true, false, + false, 'connect_page', ], 'From Woo Payments Settings - Jetpack connection, Stripe not connected' => [ @@ -511,6 +544,7 @@ public function provider_onboarding_known_froms() { true, false, false, + false, 'connect_page', ], 'From Woo Payments Settings - Jetpack connection, Stripe connected' => [ @@ -519,6 +553,7 @@ public function provider_onboarding_known_froms() { true, true, false, + false, 'overview_page', ], 'From Incentive page - no Jetpack connection, Stripe connected' => [ @@ -527,6 +562,7 @@ public function provider_onboarding_known_froms() { false, true, false, + false, 'start_jetpack_connection', ], 'From Incentive page - Jetpack connection, Stripe not connected' => [ @@ -535,6 +571,7 @@ public function provider_onboarding_known_froms() { true, false, false, + false, 'onboarding_wizard', ], 'From Incentive page - Jetpack connection, Stripe connected' => [ @@ -543,6 +580,7 @@ public function provider_onboarding_known_froms() { true, true, false, + false, 'overview_page', ], // This is a weird scenario that should not happen under normal circumstances. @@ -552,6 +590,7 @@ public function provider_onboarding_known_froms() { false, false, false, + false, 'connect_page', ], 'From Onboarding wizard - Jetpack connection, Stripe not connected' => [ @@ -560,6 +599,7 @@ public function provider_onboarding_known_froms() { true, false, false, + false, 'init_stripe_onboarding', ], 'From Onboarding wizard - Jetpack connection, Stripe connected' => [ @@ -568,6 +608,7 @@ public function provider_onboarding_known_froms() { true, true, false, + false, 'overview_page', ], ]; diff --git a/tests/unit/test-class-wc-payments-redirect-service.php b/tests/unit/test-class-wc-payments-redirect-service.php index 931d0e18102..2d2a8173d9a 100644 --- a/tests/unit/test-class-wc-payments-redirect-service.php +++ b/tests/unit/test-class-wc-payments-redirect-service.php @@ -268,4 +268,15 @@ public function test_redirect_to_connect_page_redirects_with_additional_params() // Act. $this->redirect_service->redirect_to_connect_page( null, null, [ 'source' => 'some-source' ] ); } + + public function test_redirect_to_settings_page_redirects() { + // Assert. + $this->redirect_service + ->expects( $this->once() ) + ->method( 'redirect_to' ) + ->with( admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ); + + // Act. + $this->redirect_service->redirect_to_settings_page(); + } }