Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPayments

WooPaymentsService::get_onboarding_stepsprivateWC 1.0

Get the onboarding details for each step.

Method of the class: WooPaymentsService{}

No Hooks.

Returns

Array[]. The list of onboarding steps details.

Usage

// private - for code of main (parent) class only
$result = $this->get_onboarding_steps( $location, $rest_path, ?string $source ): array;
$location(string) (required)
The location for which we are onboarding. This is an ISO 3166-1 alpha-2 country code.
$rest_path(string) (required)
The REST API path to use for constructing REST API URLs.
?string $source
.
Default: self::SESSION_ENTRY_DEFAULT

WooPaymentsService::get_onboarding_steps() code WC 10.8.1

private function get_onboarding_steps( string $location, string $rest_path, ?string $source = self::SESSION_ENTRY_DEFAULT ): array {
	$steps = array();

	// Add the payment methods onboarding step details, but only if we have recommended payment methods.
	$recommended_pms = $this->get_onboarding_recommended_payment_methods( $location );
	if ( ! empty( $recommended_pms ) ) {
		$steps[] = $this->standardize_onboarding_step_details(
			array(
				'id'      => self::ONBOARDING_STEP_PAYMENT_METHODS,
				'context' => array(
					'recommended_pms' => $recommended_pms,
					'pms_state'       => $this->get_onboarding_payment_methods_state( $location, $recommended_pms ),
				),
				'actions' => array(
					'start'  => array(
						'type' => self::ACTION_TYPE_REST,
						'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_PAYMENT_METHODS . '/start' ),
					),
					'save'   => array(
						'type' => self::ACTION_TYPE_REST,
						'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_PAYMENT_METHODS . '/save' ),
					),
					'finish' => array(
						'type' => self::ACTION_TYPE_REST,
						'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_PAYMENT_METHODS . '/finish' ),
					),
				),
			),
			$location,
			$rest_path
		);
	}

	// Add the WPCOM connection onboarding step details.
	$wpcom_step = $this->standardize_onboarding_step_details(
		array(
			'id'      => self::ONBOARDING_STEP_WPCOM_CONNECTION,
			'context' => array(
				'connection_state' => $this->get_wpcom_connection_state(),
			),
		),
		$location,
		$rest_path
	);

	// If the WPCOM connection is already set up, we don't need to add anything more.
	if ( self::ONBOARDING_STEP_STATUS_COMPLETED !== $wpcom_step['status'] ) {
		// Craft the return URL.
		switch ( $source ) {
			case self::SESSION_ENTRY_LYS:
				// If the source is LYS, we return the user to the Launch Your Store flow.
				$return_url = $this->proxy->call_function(
					'admin_url',
					'admin.php?page=wc-admin&path=/launch-your-store' . self::ONBOARDING_PATH_BASE . '&sidebar=hub&content=payments'
				);
				break;
			default:
				// By default, we return the user to the onboarding modal in the Settings > Payments page.
				$return_url = $this->proxy->call_static(
					Utils::class,
					'wc_payments_settings_url',
					self::ONBOARDING_PATH_BASE
				);
				break;
		}

		// Add standardized query arguments to the return URL.
		$return_url = add_query_arg(
			array(
				// URL query flag so we can properly identify when the user returns
				// either by accepting or rejecting the WPCOM connection.
				self::WPCOM_CONNECTION_RETURN_PARAM => '1',
				// Keep the source.
				'source'                            => $source,
				// Attach the `from` parameter to more easily identify where the return request is coming from.
				'from'                              => self::FROM_WPCOM,
			),
			$return_url
		);

		// Try to generate the authorization URL.
		$wpcom_connection = $this->get_wpcom_connection_authorization( $return_url );
		if ( ! $wpcom_connection['success'] ) {
			// In case of errors, make sure we work with a list of error messages.
			$wpcom_step['errors'] = array_values( (array) ( $wpcom_connection['errors'] ?? array() ) );
		}
		$wpcom_step['actions'] = array(
			'start' => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_WPCOM_CONNECTION . '/start' ),
			),
			'auth'  => array(
				'type' => self::ACTION_TYPE_REDIRECT,
				'href' => $wpcom_connection['url'],
			),
		);
	}

	$steps[] = $wpcom_step;

	// Test account onboarding step is unavailable in UAE and Singapore.
	if ( ! in_array( $location, array( 'AE', 'SG' ), true ) ) {
		$test_account_step = $this->standardize_onboarding_step_details(
			array(
				'id' => self::ONBOARDING_STEP_TEST_ACCOUNT,
			),
			$location,
			$rest_path
		);

		// If the step is not completed, we need to add the actions.
		if ( self::ONBOARDING_STEP_STATUS_COMPLETED !== $test_account_step['status'] ) {
			$test_account_step['actions'] = array(
				'start'  => array(
					'type' => self::ACTION_TYPE_REST,
					'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_TEST_ACCOUNT . '/start' ),
				),
				'init'   => array(
					'type' => self::ACTION_TYPE_REST,
					'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_TEST_ACCOUNT . '/init' ),
				),
				'finish' => array(
					'type' => self::ACTION_TYPE_REST,
					'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_TEST_ACCOUNT . '/finish' ),
				),
			);
		}

		$test_account_step['actions']['reset'] = array(
			'type' => self::ACTION_TYPE_REST,
			'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_TEST_ACCOUNT . '/reset' ),
		);

		$steps[] = $test_account_step;
	}

	// Add the live account business verification onboarding step details.
	$business_verification_step_sub_steps = $this->get_nox_profile_onboarding_step_data_entry(
		self::ONBOARDING_STEP_BUSINESS_VERIFICATION,
		$location,
		'sub_steps',
		array()
	);
	// Sanity check: If there is no account connected, the sub-steps details should be forced empty.
	// This way we allow for the Transact Platform account reset to take effect and
	// allow the user to restart the business verification process, including the self-assessment business step.
	if ( ! $this->has_account() ) {
		$business_verification_step_sub_steps = array();
	}
	$business_verification_step = $this->standardize_onboarding_step_details(
		array(
			'id'      => self::ONBOARDING_STEP_BUSINESS_VERIFICATION,
			'context' => array(
				'fields'              => array(),
				'sub_steps'           => $business_verification_step_sub_steps,
				'self_assessment'     => $this->get_nox_profile_onboarding_step_data_entry( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location, 'self_assessment', array() ),
				'has_test_account'    => $this->has_test_account(),
				'has_sandbox_account' => $this->has_sandbox_account(),
			),
		),
		$location,
		$rest_path
	);

	// Try to get the pre-KYC fields, but only if the required step is completed.
	// This is because WooPayments needs a working WPCOM connection to be able to fetch the fields.
	if ( $this->check_onboarding_step_requirements( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location ) ) {
		try {
			$business_verification_step['context']['fields'] = $this->get_onboarding_kyc_fields( $location );
		} catch ( Exception $e ) {
			$business_verification_step['errors'][] = array(
				'code'    => 'fields_error',
				'message' => $e->getMessage(),
			);
		}
	}

	// If the step is not completed, we need to add the actions.
	if ( self::ONBOARDING_STEP_STATUS_COMPLETED !== $business_verification_step['status'] ) {
		$business_verification_step['actions'] = array(
			'start'                => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_BUSINESS_VERIFICATION . '/start' ),
			),
			'save'                 => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_BUSINESS_VERIFICATION . '/save' ),
			),
			'kyc_session'          => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_BUSINESS_VERIFICATION . '/kyc_session' ),
			),
			'kyc_session_finish'   => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_BUSINESS_VERIFICATION . '/kyc_session/finish' ),
			),
			'kyc_fallback'         => array(
				'type' => self::ACTION_TYPE_REDIRECT,
				'href' => $this->get_onboarding_kyc_fallback_url(),
			),
			'finish'               => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_BUSINESS_VERIFICATION . '/finish' ),
			),
			'test_account_disable' => array(
				'type' => self::ACTION_TYPE_REST,
				'href' => rest_url( trailingslashit( $rest_path ) . self::ONBOARDING_STEP_BUSINESS_VERIFICATION . '/test_account/disable' ),
			),
		);
	}

	$steps[] = $business_verification_step;

	// Do a complete list standardization, for safety.
	return $this->standardize_onboarding_steps_details( $steps, $location, $rest_path );
}