if ( defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ), true ) ) {
$connect_url = add_query_arg(
'calypso_env' => WOOCOMMERCE_CALYPSO_ENVIRONMENT,
'connectAction' => $connect_url,
* Finishes connecting to WooCommerce.com.
* @param object $rest_request Request details.
* @return \WP_Error|array Contains success status.
public function finish_wccom_connect( $rest_request ) {
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper.php';
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-api.php';
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-updater.php';
include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php';
if ( ! class_exists( 'WC_Helper_API' ) ) {
return new \WP_Error( 'woocommerce_rest_helper_not_active', __( 'There was an error loading the WooCommerce.com Helper API.', 'woocommerce' ), 404 );
// Obtain an access token.
$request = \WC_Helper_API::post(
'request_token' => wp_unslash( $rest_request['request_token'] ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
'home_url' => home_url(),
$code = wp_remote_retrieve_response_code( $request );
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
$access_token = json_decode( wp_remote_retrieve_body( $request ), true );
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
\WC_Helper_Options::update(
'access_token' => $access_token['access_token'],
'access_token_secret' => $access_token['access_token_secret'],
'site_id' => $access_token['site_id'],
'user_id' => get_current_user_id(),
if ( ! \WC_Helper::_flush_authentication_cache() ) {
\WC_Helper_Options::update( 'auth', array() );
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to WooCommerce.com. Please try again.', 'woocommerce' ), 500 );
delete_transient( '_woocommerce_helper_subscriptions' );
\WC_Helper_Updater::flush_updates_cache();
do_action( 'woocommerce_helper_connected' );
* Returns a URL that can be used to connect to Square.
* @return \WP_Error|array Connect URL.
public function connect_square() {
if ( ! class_exists( '\WooCommerce\Square\Handlers\Connection' ) ) {
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error connecting to Square.', 'woocommerce' ), 500 );
$has_cbd_industry = false;
if ( 'US' === WC()->countries->get_base_country() ) {
$profile = get_option( OnboardingProfile::DATA_OPTION, array() );
if ( ! empty( $profile['industry'] ) ) {
$has_cbd_industry = in_array( 'cbd-other-hemp-derived-products', array_column( $profile['industry'], 'slug' ), true );
if ( $has_cbd_industry ) {
$url = 'https://squareup.com/t/f_partnerships/d_referrals/p_woocommerce/c_general/o_none/l_us/dt_alldevice/pr_payments/?route=/solutions/cbd';
$url = \WooCommerce\Square\Handlers\Connection::CONNECT_URL_PRODUCTION;
$redirect_url = wp_nonce_url( wc_admin_url( '&task=payments&method=square&square-connect-finish=1' ), 'wc_square_connected' );
'redirect' => rawurlencode( rawurlencode( $redirect_url ) ),
$connect_url = add_query_arg( $args, $url );
'connectUrl' => $connect_url,
* Returns a URL that can be used to point the merchant to the WooPayments onboarding flow.
* @return \WP_Error|array Connect URL.
public function connect_wcpay() {
if ( ! class_exists( 'WC_Payments' ) ) {
return new \WP_Error( 'woocommerce_rest_helper_connect', __( 'There was an error communicating with the WooPayments plugin.', 'woocommerce' ), 500 );
// Use a WooPayments connect link to let the WooPayments plugin handle the connection flow.
'connectUrl' => add_query_arg(
'from' => 'WCADMIN_PAYMENT_TASK',
'_wpnonce' => wp_create_nonce( 'wcpay-connect' ),
* Get the schema, conforming to JSON Schema.
public function get_item_schema() {
'$schema' => 'http://json-schema.org/draft-04/schema#',
'description' => __( 'Plugin slug.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),
'description' => __( 'Plugin name.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),
'description' => __( 'Plugin status.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),
return $this->add_additional_fields_schema( $schema );
* Get the schema, conforming to JSON Schema.
public function get_connect_schema() {
$schema = $this->get_item_schema();
unset( $schema['properties']['status'] );
$schema['properties']['connectAction'] = array(
'description' => __( 'Action that should be completed to connect Jetpack.', 'woocommerce' ),
'context' => array( 'view', 'edit' ),