public function disable_test_account( string $location, string $from = '', ?string $source = self::SESSION_ENTRY_DEFAULT ): array {
$this->check_if_onboarding_action_is_acceptable();
// Ensure the payment gateways logic is initialized in case actions need to be taken on payment gateway changes.
WC()->payment_gateways();
$response = array(
'success' => true,
);
$event_props = array();
$source = $this->validate_onboarding_source( $source );
// Lock the onboarding to prevent concurrent actions.
$this->set_onboarding_lock();
try {
$has_test_account = $this->has_test_account();
$has_sandbox_account = $this->has_sandbox_account();
$event_props = array(
'account_type' => $has_test_account ? 'test_drive' : ( $has_sandbox_account ? 'sandbox' : 'unknown' ),
'source' => $source,
);
// First, check if we have a test account to disable.
if ( $has_test_account ) {
// Call the WooPayments API to disable the test account and prepare for the switch to live.
$response = $this->proxy->call_static(
Utils::class,
'rest_endpoint_post_request',
'/wc/v3/payments/onboarding/test_drive_account/disable',
array(
'from' => ! empty( $from ) ? esc_attr( $from ) : self::FROM_PAYMENT_SETTINGS,
'source' => $source,
)
);
} elseif ( $has_sandbox_account ) {
// Call the WooPayments API to reset onboarding.
$response = $this->proxy->call_static(
Utils::class,
'rest_endpoint_post_request',
'/wc/v3/payments/onboarding/reset',
array(
'from' => ! empty( $from ) ? esc_attr( $from ) : self::FROM_PAYMENT_SETTINGS,
'source' => $source,
)
);
}
} 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 disabling the test account.', '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();
// Make sure the onboarding mode is reset.
if ( class_exists( 'WC_Payments_Onboarding_Service' ) && defined( 'WC_Payments_Onboarding_Service::TEST_MODE_OPTION' ) ) {
$this->proxy->call_function( 'update_option', Constants::get_constant( 'WC_Payments_Onboarding_Service::TEST_MODE_OPTION' ), 'no' );
}
// Track the failure to disable the test account.
if ( is_wp_error( $response ) || ! is_array( $response ) || empty( $response['success'] ) ) {
$this->record_event(
self::EVENT_PREFIX . 'onboarding_test_account_disable_error',
$location,
array(
'source' => $source,
)
);
}
if ( is_wp_error( $response ) ) {
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 ) || empty( $response['success'] ) ) {
throw new ApiException(
'woocommerce_woopayments_onboarding_client_api_error',
esc_html__( 'Failed to disable the test account.', 'woocommerce' ),
(int) WP_Http::FAILED_DEPENDENCY
);
}
// For sanity, make sure the payment methods step is marked as completed.
// This is to avoid the user being prompted to set up payment methods again.
$this->mark_onboarding_step_completed( self::ONBOARDING_STEP_PAYMENT_METHODS, $location );
// For sanity, make sure the test account step is marked as completed and not blocked or failed.
// After disabling a test account, the user should be prompted to set up a live account.
$this->mark_onboarding_step_completed( self::ONBOARDING_STEP_TEST_ACCOUNT, $location );
$this->clear_onboarding_step_blocked( self::ONBOARDING_STEP_TEST_ACCOUNT, $location );
$this->clear_onboarding_step_failed( self::ONBOARDING_STEP_TEST_ACCOUNT, $location );
// Clear the NOX profile data for the business verification step sub-step data.
// This way the user will be prompted to complete ALL the business verification sub-steps.
$business_verification_sub_step_data = $this->get_nox_profile_onboarding_step_data_entry( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location, 'sub_steps', array() );
if ( ! empty( $business_verification_sub_step_data ) ) {
$this->save_nox_profile_onboarding_step_data_entry( self::ONBOARDING_STEP_BUSINESS_VERIFICATION, $location, 'sub_steps', array() );
}
// Record an event for the test account being disabled.
$this->record_event(
self::EVENT_PREFIX . 'onboarding_test_account_disabled',
$location,
$event_props
);
return $response;
}