wp_json_encode( array( 'code' => $code, 'site_url' => defined( 'AGENTCART_SITE_URL_OVERRIDE' ) ? AGENTCART_SITE_URL_OVERRIDE : get_site_url(), 'consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret, 'store_name' => get_bloginfo( 'name' ), 'currency' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : 'USD', 'wc_version' => defined( 'WC_VERSION' ) ? WC_VERSION : '', ) ), 'headers' => array( 'Content-Type' => 'application/json' ), 'timeout' => 30, ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'request_failed', /* translators: %s: error message from wp_remote_post */ sprintf( __( 'Could not connect to AgentCart: %s', 'agentcart' ), $response->get_error_message() ) ); } $status_code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $status_code !== 200 || empty( $body['success'] ) ) { $error_msg = isset( $body['error'] ) ? $body['error'] : __( 'Pairing failed. Please try again.', 'agentcart' ); return new WP_Error( 'pairing_failed', $error_msg ); } update_option( 'agentcart_connected', true ); update_option( 'agentcart_merchant_id', sanitize_text_field( $body['merchant_id'] ) ); if ( ! empty( $body['ucp_slug'] ) ) { update_option( 'agentcart_ucp_slug', sanitize_text_field( $body['ucp_slug'] ) ); } return array( 'success' => true, 'merchant_id' => $body['merchant_id'], ); } /** * Disconnect the store from AgentCart. * * @return bool True on success. */ public static function disconnect() { $merchant_id = get_option( 'agentcart_merchant_id' ); $api_url = get_option( 'agentcart_api_url', 'https://app.agentcart.io' ); if ( $merchant_id ) { wp_remote_post( $api_url . '/api/woocommerce/disconnect', array( 'body' => wp_json_encode( array( 'merchant_id' => $merchant_id ) ), 'headers' => array( 'Content-Type' => 'application/json' ), 'timeout' => 10, ) ); } update_option( 'agentcart_connected', false ); update_option( 'agentcart_merchant_id', '' ); return true; } /** * Check if the store is connected. * * @return bool */ public static function is_connected() { return (bool) get_option( 'agentcart_connected', false ); } /** * Get the merchant ID. * * @return string */ public static function get_merchant_id() { return get_option( 'agentcart_merchant_id', '' ); } }