get_api_key(); if ( empty( $api_key ) ) { return new WP_Error( 'missing_api_key', __( 'AIsoule API Key is not configured.', 'aisoule-connect' ) ); } $endpoint = $this->get_api_base_url() . '/integrations/woocommerce/events'; $body = array( 'event' => sanitize_key( $event ), 'organization_id' => $this->get_org_id(), 'site_url' => get_site_url(), 'timestamp' => current_time( 'timestamp' ), 'data' => $payload, ); $args = array( 'method' => 'POST', 'timeout' => 10, 'redirection' => 5, 'httpversion' => '1.1', 'blocking' => false, // Non-blocking during checkout to prevent slowdowns 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Bearer ' . $api_key, 'X-API-Key' => $api_key, 'X-Source' => 'aisoule-connect/' . AISOULE_VERSION, ), 'body' => wp_json_encode( $body ), 'sslverify' => true, ); return wp_remote_post( $endpoint, $args ); } /** * Test connection to AIsoule API. * * @param string $api_key Optional API key to test. * @return bool|WP_Error */ public function test_connection( $api_key = '' ) { if ( empty( $api_key ) ) { $api_key = $this->get_api_key(); } if ( empty( $api_key ) ) { return new WP_Error( 'empty_key', __( 'Please provide an API key.', 'aisoule-connect' ) ); } $endpoint = $this->get_api_base_url() . '/me'; $response = wp_remote_get( $endpoint, array( 'timeout' => 15, 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'X-API-Key' => $api_key, 'User-Agent' => 'AIsoule-Connect/' . AISOULE_VERSION, ), 'sslverify' => true, ) ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); if ( 200 === $code || 204 === $code ) { return true; } /* translators: %d: HTTP response status code */ return new WP_Error( 'invalid_response', sprintf( __( 'API returned HTTP code: %d', 'aisoule-connect' ), $code ) ); } }