public function get_onboarding_kyc_session( string $location, array $self_assessment = array(), ?string $source = self::SESSION_ENTRY_DEFAULT ): array {
$this->check_if_onboarding_step_action_is_acceptable( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location );
if ( empty( $self_assessment ) ) {
// Get the stored self-assessment data.
$self_assessment = (array) $this->get_nox_profile_onboarding_step_data_entry( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location, 'self_assessment' );
}
// Clear any previous failed status for the step.
$this->clear_onboarding_step_failed( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location );
// Get the selected payment methods from the NOX profile.
$selected_payment_methods = $this->get_nox_profile_onboarding_step_data_entry( self::ONBOARDING_STEP_PAYMENT_METHODS, $location, 'payment_methods', array() );
// Ensure the payment gateways logic is initialized in case actions need to be taken on payment gateway changes.
WC()->payment_gateways();
$source = $this->validate_onboarding_source( $source );
// Lock the onboarding to prevent concurrent actions.
$this->set_onboarding_lock();
try {
// Call the WooPayments API to get the KYC session.
$response = $this->proxy->call_static(
Utils::class,
'rest_endpoint_post_request',
'/wc/v3/payments/onboarding/kyc/session',
array(
'self_assessment' => $self_assessment,
'capabilities' => $selected_payment_methods,
)
);
} catch ( Exception $e ) {
// Catch any exceptions to allow for proper error handling and onboarding unlock.
$response = new WP_Error(
'woocommerce_woopayments_onboarding_client_api_exception',
esc_html__( 'An unexpected error happened while creating the KYC session.', 'woocommerce' ),
array(
'code' => $e->getCode(),
'message' => $e->getMessage(),
'trace' => $e->getTrace(),
)
);
}
// Unlock the onboarding after the API call finished or errored.
$this->clear_onboarding_lock();
if ( is_wp_error( $response ) ) {
// Mark the onboarding step as failed.
$this->mark_onboarding_step_failed(
self::ONBOARDING_STEP_BUSINESS_VERIFICATION,
$location,
array(
'code' => $response->get_error_code(),
'message' => $response->get_error_message(),
'context' => $response->get_error_data(),
)
);
throw new ApiException(
'woocommerce_woopayments_onboarding_client_api_error',
esc_html( $response->get_error_message() ),
(int) WP_Http::FAILED_DEPENDENCY,
map_deep( (array) $response->get_error_data(), 'esc_html' )
);
}
if ( ! is_array( $response ) ) {
// Mark the onboarding step as failed.
$this->mark_onboarding_step_failed(
self::ONBOARDING_STEP_BUSINESS_VERIFICATION,
$location,
array(
'code' => 'malformed_response',
'message' => esc_html__( 'Received an unexpected response from the platform.', 'woocommerce' ),
'context' => array(
'response' => $response,
),
)
);
throw new ApiException(
'woocommerce_woopayments_onboarding_client_api_error',
esc_html__( 'Failed to get the KYC session data.', 'woocommerce' ),
(int) WP_Http::FAILED_DEPENDENCY
);
}
// Add the user locale to the account session data to allow for localized KYC sessions.
$response['locale'] = $this->proxy->call_function( 'get_user_locale' );
// For sanity, make sure the test account step is marked as completed, if not already,
// since we are doing live account KYC.
$this->mark_onboarding_step_completed( self::ONBOARDING_STEP_TEST_ACCOUNT, $location, false, $source );
// Record an event for the KYC session being created.
$event_props = array(
'new_account_created' => $response['accountCreated'] ?? false,
'account_mode' => ( $response['isLive'] ?? false ) ? 'live' : 'test',
'source' => $source,
);
$this->record_event(
self::EVENT_PREFIX . 'onboarding_kyc_session_created',
$location,
$event_props
);
return $response;
}