diff --git a/changelog/add-6924-migrate-test-drive-capabilities b/changelog/add-6924-migrate-test-drive-capabilities new file mode 100644 index 00000000000..7b280af4d92 --- /dev/null +++ b/changelog/add-6924-migrate-test-drive-capabilities @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Migrate active capabilities from test-drive account when switching to live account. diff --git a/includes/class-wc-payments-account.php b/includes/class-wc-payments-account.php index 85b0f336263..611c3a8acd2 100644 --- a/includes/class-wc-payments-account.php +++ b/includes/class-wc-payments-account.php @@ -30,6 +30,7 @@ class WC_Payments_Account implements MultiCurrencyAccountInterface { const ONBOARDING_STARTED_TRANSIENT = 'wcpay_on_boarding_started'; const ONBOARDING_STATE_TRANSIENT = 'wcpay_stripe_onboarding_state'; const WOOPAY_ENABLED_BY_DEFAULT_TRANSIENT = 'woopay_enabled_by_default'; + const ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT = 'test_drive_account_settings_for_live_account'; const EMBEDDED_KYC_IN_PROGRESS_OPTION = 'wcpay_onboarding_embedded_kyc_in_progress'; const ERROR_MESSAGE_TRANSIENT = 'wcpay_error_message'; const INSTANT_DEPOSITS_REMINDER_ACTION = 'wcpay_instant_deposit_reminder'; @@ -1317,6 +1318,7 @@ public function maybe_handle_onboarding() { } $this->cleanup_on_account_reset(); + delete_transient( self::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ); // When we reset the account and want to go back to the settings page - redirect immediately! if ( $redirect_to_settings_page ) { @@ -1342,6 +1344,10 @@ public function maybe_handle_onboarding() { // in the "everything OK" scenario). if ( WC_Payments_Onboarding_Service::is_test_mode_enabled() ) { try { + // If we're in test mode and dealing with a test-drive account, + // we need to collect the test drive settings before we delete the test-drive account, + // and apply those settings to the live account. + $this->save_test_drive_settings(); // Delete the currently connected Stripe account. $this->payments_api_client->delete_account( true ); } catch ( API_Exception $e ) { @@ -1426,7 +1432,6 @@ public function maybe_handle_onboarding() { if ( ! $collect_payout_requirements && $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. @@ -2149,13 +2154,11 @@ private function finalize_connection( string $state, string $mode, array $additi // If we get this parameter, but we have a valid state, it means the merchant left KYC early and didn't finish it. // While we do have an account, it is not yet valid. We need to redirect them back to the connect page. $params['wcpay-connection-error'] = '1'; - $this->redirect_service->redirect_to_connect_page( '', WC_Payments_Onboarding_Service::FROM_STRIPE, $params ); return; } $params['wcpay-connection-success'] = '1'; - $this->redirect_service->redirect_to_overview_page( WC_Payments_Onboarding_Service::FROM_STRIPE, $params ); } @@ -2582,4 +2585,42 @@ public function get_lifetime_total_payment_volume(): int { $account = $this->get_cached_account_data(); return (int) ! empty( $account ) && isset( $account['lifetime_total_payment_volume'] ) ? $account['lifetime_total_payment_volume'] : 0; } + + /** + * Extract the test drive settings from the account data that we want to store for the live account. + * ATM we only store the enabled payment methods. + * + * @return array The test drive settings for the live account. + */ + private function get_test_drive_settings_for_live_account(): array { + $gateway = WC_Payments::get_gateway(); + + $capabilities = []; + foreach ( $gateway->get_upe_enabled_payment_method_ids() as $payment_method_id ) { + $capabilities[ $payment_method_id . '_payments' ] = [ 'requested' => 'true' ]; + } + + return [ 'capabilities' => $capabilities ]; + } + + /** + * If we're in test mode and dealing with a test-drive account, + * we need to collect the test drive settings before we delete the test-drive account, + * and apply those settings to the live account. + * + * @return void + */ + private function save_test_drive_settings(): void { + $account = $this->get_cached_account_data(); + + if ( ! empty( $account['is_test_drive'] ) && true === $account['is_test_drive'] ) { + $test_drive_account_data = $this->get_test_drive_settings_for_live_account(); + + // Store the test drive settings for the live account in a transient, + // We don't passing the data around, as the merchant might cancel and start + // the onboarding from scratch. In this case, we won't have the test drive + // account anymore to collect the settings. + set_transient( self::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT, $test_drive_account_data, HOUR_IN_SECONDS ); + } + } } diff --git a/includes/class-wc-payments-onboarding-service.php b/includes/class-wc-payments-onboarding-service.php index bce87bcbf0c..ce558ac2faf 100644 --- a/includes/class-wc-payments-onboarding-service.php +++ b/includes/class-wc-payments-onboarding-service.php @@ -109,6 +109,7 @@ public function __construct( WC_Payments_API_Client $payments_api_client, Databa */ public function init_hooks() { add_filter( 'admin_body_class', [ $this, 'add_admin_body_classes' ] ); + add_filter( 'wc_payments_get_onboarding_data_args', [ $this, 'maybe_add_test_drive_settings_to_new_account_request' ] ); } /** @@ -902,4 +903,27 @@ public static function get_source( ?string $referer = null, ?array $get_params = // Default to an unknown source. return self::SOURCE_UNKNOWN; } + + /** + * If settings are collected from the test-drive account, + * include them in the existing arguments when creating the new account. + * + * @param array $args The request args to create new account. + * + * @return array The request args, possible updated with the test drive account settings, used to create new account. + */ + public function maybe_add_test_drive_settings_to_new_account_request( array $args ): array { + if ( + get_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ) && + is_array( get_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ) ) + ) { + $args['account_data'] = array_merge( + $args['account_data'], + get_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ) + ); + delete_transient( WC_Payments_Account::ONBOARDING_TEST_DRIVE_SETTINGS_FOR_LIVE_ACCOUNT ); + } + + return $args; + } }