diff --git a/changelog/account-migration-feature b/changelog/account-migration-feature new file mode 100644 index 00000000000..673d2362b31 --- /dev/null +++ b/changelog/account-migration-feature @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Make it possible to migrate a test account to a live account diff --git a/includes/admin/class-wc-rest-payments-onboarding-controller.php b/includes/admin/class-wc-rest-payments-onboarding-controller.php index 9062e43283f..6df13680dff 100644 --- a/includes/admin/class-wc-rest-payments-onboarding-controller.php +++ b/includes/admin/class-wc-rest-payments-onboarding-controller.php @@ -97,6 +97,12 @@ public function register_routes() { ], ], ], + 'migrate_to_live' => [ + 'required' => false, + 'description' => 'Whether to migrate the account to live.', + 'type' => 'boolean', + 'default' => false, + ], ], ] ); @@ -227,11 +233,13 @@ public function create_embedded_kyc_session( WP_REST_Request $request ) { $self_assessment_data = ! empty( $request->get_param( 'self_assessment' ) ) ? wc_clean( wp_unslash( $request->get_param( 'self_assessment' ) ) ) : []; $progressive = ! empty( $request->get_param( 'progressive' ) ) && filter_var( $request->get_param( 'progressive' ), FILTER_VALIDATE_BOOLEAN ); $capabilities = ! empty( $request->get_param( 'capabilities' ) ) ? wc_clean( wp_unslash( $request->get_param( 'capabilities' ) ) ) : []; + $migrate_to_live = ! empty( $request->get_param( 'migrate_to_live' ) ) && filter_var( $request->get_param( 'migrate_to_live' ), FILTER_VALIDATE_BOOLEAN ); $account_session = $this->onboarding_service->create_embedded_kyc_session( $self_assessment_data, $progressive, - $capabilities + $capabilities, + $migrate_to_live ); if ( $account_session ) { diff --git a/includes/class-wc-payments-account.php b/includes/class-wc-payments-account.php index d3320a59071..c9836fa1da7 100644 --- a/includes/class-wc-payments-account.php +++ b/includes/class-wc-payments-account.php @@ -2327,6 +2327,12 @@ public function get_cached_account_data( bool $force_refresh = false ) { return []; } + // Check if there's an ongoing migration to live payments. + if ( $this->onboarding_service->is_onboarding_init_in_progress() ) { + // Return empty array to indicate no account is connected yet. + return []; + } + $refreshed = false; $account = $this->database_cache->get_or_add( diff --git a/includes/class-wc-payments-onboarding-service.php b/includes/class-wc-payments-onboarding-service.php index 33c81b9e642..aa69d6081da 100644 --- a/includes/class-wc-payments-onboarding-service.php +++ b/includes/class-wc-payments-onboarding-service.php @@ -281,12 +281,13 @@ public function should_enable_woopay( bool $default_value, array $capabilities ) * @param array $capabilities Optional. List keyed by capabilities IDs (payment methods) with boolean values * indicating whether the capability should be requested when the account is created * and enabled in the settings. + * @param bool $migrate_to_live Whether to migrate the account to live. * * @return array Session data. * * @throws API_Exception|Exception */ - public function create_embedded_kyc_session( array $self_assessment_data, bool $progressive = false, array $capabilities = [] ): array { + public function create_embedded_kyc_session( array $self_assessment_data, bool $progressive = false, array $capabilities = [], bool $migrate_to_live = false ): array { if ( ! $this->payments_api_client->is_server_connected() ) { return []; } @@ -339,7 +340,8 @@ public function create_embedded_kyc_session( array $self_assessment_data, bool $ WC_Payments_Utils::array_filter_recursive( $account_data ), // nosemgrep: audit.php.lang.misc.array-filter-no-callback -- output of array_filter is escaped. $actioned_notes, $progressive, - $this->get_referral_code() + $this->get_referral_code(), + $migrate_to_live ); } catch ( API_Exception $e ) { $this->clear_onboarding_init_in_progress(); diff --git a/includes/wc-payment-api/class-wc-payments-api-client.php b/includes/wc-payment-api/class-wc-payments-api-client.php index 9d1233bd0e9..327c2fba1d1 100644 --- a/includes/wc-payment-api/class-wc-payments-api-client.php +++ b/includes/wc-payment-api/class-wc-payments-api-client.php @@ -1095,6 +1095,7 @@ public function get_onboarding_data( * @param array $actioned_notes Actioned notes to be sent. * @param bool $progressive Whether progressive onboarding should be enabled for this onboarding. * @param ?string $referral_code Referral code to be used for onboarding. + * @param ?bool $migrate_to_live Whether to migrate the account to live. * * @return array * @@ -1107,7 +1108,8 @@ public function initialize_onboarding_embedded_kyc( array $account_data = [], array $actioned_notes = [], bool $progressive = false, - ?string $referral_code = null + ?string $referral_code = null, + ?bool $migrate_to_live = false ): array { $request_args = apply_filters( 'wc_payments_get_onboarding_data_args', @@ -1118,6 +1120,7 @@ public function initialize_onboarding_embedded_kyc( 'actioned_notes' => $actioned_notes, 'create_live_account' => $live_account, 'progressive' => $progressive, + 'migrate_to_live' => $migrate_to_live, ] ); diff --git a/tests/unit/test-class-wc-payments-account.php b/tests/unit/test-class-wc-payments-account.php index 7d42386e11f..bbdf887a345 100644 --- a/tests/unit/test-class-wc-payments-account.php +++ b/tests/unit/test-class-wc-payments-account.php @@ -1154,7 +1154,7 @@ public function test_maybe_handle_onboarding_init_stripe_onboarding_another_onbo // There is another onboarding started. $this->mock_onboarding_service - ->expects( $this->once() ) + ->expects( $this->atLeastOnce() ) ->method( 'is_onboarding_init_in_progress' ) ->willReturn( true ); diff --git a/tests/unit/test-class-wc-payments-onboarding-service.php b/tests/unit/test-class-wc-payments-onboarding-service.php index d65bb02653d..d03eea595c6 100644 --- a/tests/unit/test-class-wc-payments-onboarding-service.php +++ b/tests/unit/test-class-wc-payments-onboarding-service.php @@ -31,14 +31,14 @@ class WC_Payments_Onboarding_Service_Test extends WCPAY_UnitTestCase { /** * Mock Database_Cache * - * @var MockObject + * @var Database_Cache|MockObject */ private $mock_database_cache; /** * Mock WC_Payments_Session_Service * - * @var MockObject + * @var WC_Payments_Session_Service|MockObject */ private $mock_session_service;