public function get_details( WC_Payment_Gateway $gateway, int $order = 0, string $country_code = '' ): array {
$details = parent::get_details( $gateway, $order, $country_code );
$has_test_account = $this->has_test_account();
$has_sandbox_account = $this->has_sandbox_account();
// Switch the onboarding type to native.
$details['onboarding']['type'] = self::ONBOARDING_TYPE_NATIVE;
// Add the test [drive] account details to the onboarding state.
$details['onboarding']['state']['test_drive_account'] = $has_test_account;
// Add WPCOM/Jetpack connection details to the onboarding state.
$details['onboarding']['state'] = array_merge( $details['onboarding']['state'], $this->get_wpcom_connection_state() );
// If the WooPayments installed version is less than minimum required version,
// we can't use the in-context onboarding flows.
if ( Constants::is_defined( 'WCPAY_VERSION_NUMBER' ) &&
version_compare( Constants::get_constant( 'WCPAY_VERSION_NUMBER' ), WooPaymentsService::EXTENSION_MINIMUM_VERSION, '<' ) ) {
return $details;
}
// Switch the onboarding type to native in-context.
$details['onboarding']['type'] = self::ONBOARDING_TYPE_NATIVE_IN_CONTEXT;
// Provide the native, in-context onboarding URL instead of the external one.
// This is a catch-all URL that should start or continue the onboarding process.
$details['onboarding']['_links']['onboard'] = array(
'href' => Utils::wc_payments_settings_url( '/woopayments/onboarding', array( 'from' => Payments::FROM_PAYMENTS_SETTINGS ) ),
);
try {
/**
* The WooPayments REST controller instance.
*
* @var WooPaymentsRestController $rest_controller
*/
$rest_controller = wc_get_container()->get( WooPaymentsRestController::class );
// Add disable test account URL to onboarding links, if the current account is a test or sandbox account.
if ( $has_test_account || $has_sandbox_account ) {
$details['onboarding']['_links']['disable_test_account'] = array(
'href' => rest_url( $rest_controller->get_rest_url_path( 'onboarding/test_account/disable' ) ),
);
}
// Add reset account/onboarding URL to onboarding links.
$details['onboarding']['_links']['reset'] = array(
'href' => rest_url( $rest_controller->get_rest_url_path( 'onboarding/reset' ) ),
);
} catch ( \Throwable $e ) {
// If the REST controller is not available, we can't generate the REST API endpoint URLs.
// This is not a critical error, so we just ignore it.
// Log so we can investigate.
SafeGlobalFunctionProxy::wc_get_logger()->error(
'Failed to get the WooPayments REST controller instance: ' . $e->getMessage(),
array(
'source' => 'settings-payments',
)
);
}
// Override the onboarding state with the entries provided by the WooPayments service.
if ( ! empty( $country_code ) ) {
try {
/**
* The WooPayments service instance.
*
* @var WooPaymentsService $service
*/
$service = wc_get_container()->get( WooPaymentsService::class );
// Ensure we have a valid rest_controller from the earlier try block.
if ( ! isset( $rest_controller ) ) {
throw new \RuntimeException( 'WooPayments REST controller not available' );
}
$onboarding_details = $service->get_onboarding_details( $country_code, $rest_controller->get_rest_url_path( 'onboarding' ) );
// Merge the onboarding state with the one provided by the service.
if ( ! empty( $onboarding_details['state'] ) && is_array( $onboarding_details['state'] ) ) {
$details['onboarding']['state'] = array_merge(
$details['onboarding']['state'],
$onboarding_details['state']
);
}
// Merge any messages provided by the service.
if ( ! empty( $onboarding_details['messages'] ) && is_array( $onboarding_details['messages'] ) ) {
if ( ! isset( $details['onboarding']['messages'] ) || ! is_array( $details['onboarding']['messages'] ) ) {
$details['onboarding']['messages'] = array();
}
$details['onboarding']['messages'] = array_merge(
$details['onboarding']['messages'],
$onboarding_details['messages']
);
}
// The steps provided by the service override any existing steps.
if ( ! empty( $onboarding_details['steps'] ) && is_array( $onboarding_details['steps'] ) ) {
$details['onboarding']['steps'] = $onboarding_details['steps'];
}
// Merge any context provided by the service.
if ( ! empty( $onboarding_details['context'] ) && is_array( $onboarding_details['context'] ) ) {
if ( ! isset( $details['onboarding']['context'] ) || ! is_array( $details['onboarding']['context'] ) ) {
$details['onboarding']['context'] = array();
}
$details['onboarding']['context'] = array_merge(
$details['onboarding']['context'],
$onboarding_details['context']
);
}
} catch ( \Throwable $e ) {
// If the service is not available, we can't impose the more specific logic.
// This is not a critical error, so we just ignore it.
// Log so we can investigate.
SafeGlobalFunctionProxy::wc_get_logger()->error(
'Failed to get the WooPayments service instance: ' . $e->getMessage(),
array(
'source' => 'settings-payments',
)
);
}
}
return $details;
}