Skip to content

Commit

Permalink
Allow redirect to the settings page from WCPay connect (#9827)
Browse files Browse the repository at this point in the history
Co-authored-by: oaratovskyi <[email protected]>
Co-authored-by: Daniel Mallory <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent e47c67a commit 0827530
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 26 deletions.
1 change: 1 addition & 0 deletions assets/images/illustrations/setup.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Allow redirect to the settings page from WCPay connect
18 changes: 11 additions & 7 deletions client/connect-account-page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -52,18 +53,19 @@ const TestDriveLoader: React.FunctionComponent< {
progress: number;
} > = ( { progress } ) => (
<Loader className="connect-account-page__preloader">
<img src={ WooPaymentsLogo } alt="" />
<img className="logo" src={ WooLogo } alt="" />
<Loader.Layout>
<Loader.Illustration>
<img src={ SetupImg } alt="setup" />
</Loader.Illustration>

<Loader.Title>
{ __(
'Creating your sandbox account',
'woocommerce-payments'
) }
{ __( 'Finishing payments setup', 'woocommerce-payments' ) }
</Loader.Title>
<Loader.ProgressBar progress={ progress ?? 0 } />
<Loader.Sequence interval={ 0 }>
{ __(
'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."
) }
</Loader.Sequence>
</Loader.Layout>
Expand Down Expand Up @@ -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 );
Expand Down
8 changes: 4 additions & 4 deletions client/connect-account-page/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
55 changes: 43 additions & 12 deletions includes/class-wc-payments-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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'] ) &&
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down Expand Up @@ -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).
),
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions includes/class-wc-payments-redirect-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading

0 comments on commit 0827530

Please sign in to comment.