prefix . 'odoo_crm_logs'; if (is_wp_error($result)) { $error_msg = $result->get_error_message(); if ($retry_count < $max_retries) { // Schedule retry in the future wp_schedule_single_event(time() + ($retry_interval * MINUTE_IN_SECONDS), 'odoo_crm_process_queue_event', array($payload, $retry_count + 1, $log_id)); if ($log_id > 0) { $wpdb->update( $table_name, array('response_msg' => $error_msg, 'retry_count' => $retry_count + 1), array('id' => $log_id), array('%s', '%d'), array('%d') ); } } else { // Max retries reached, update to failed if ($log_id > 0) { $wpdb->update( $table_name, array('status' => 'failed', 'response_msg' => $error_msg, 'retry_count' => $retry_count), array('id' => $log_id), array('%s', '%s', '%d'), array('%d') ); } else { self::log_failed_lead($payload, $error_msg); } } } else { // Success if ($log_id > 0) { $wpdb->update( $table_name, array('status' => 'success', 'response_msg' => 'Lead successfully sent to Odoo.', 'retry_count' => $retry_count), array('id' => $log_id), array('%s', '%s', '%d'), array('%d') ); } } } public static function send_to_odoo($payload) { $options = get_option('odoo_crm_settings'); $url = !empty($options['odoo_url']) ? rtrim($options['odoo_url'], '/') : ''; $token = !empty($options['odoo_token']) ? $options['odoo_token'] : ''; $secret_key = !empty($options['odoo_secret_key']) ? $options['odoo_secret_key'] : ''; if (empty($url) || empty($token)) { return new WP_Error('config_error', 'Odoo URL or Token is not configured.'); } $endpoint = $url . '/api/ms/contact'; $json_payload = wp_json_encode($payload); $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ); if (!empty($secret_key)) { $headers['X-Odoo-Signature'] = hash_hmac('sha256', $json_payload, $secret_key); } $args = array( 'body' => $json_payload, 'headers' => $headers, 'timeout' => 15, ); $response = wp_remote_post($endpoint, $args); if (is_wp_error($response)) { return $response; } $status_code = wp_remote_retrieve_response_code($response); if ($status_code !== 200) { $body = wp_remote_retrieve_body($response); return new WP_Error('http_error', sprintf('HTTP %d: %s', $status_code, $body)); } return true; } private static function log_failed_lead($payload, $error_msg) { global $wpdb; $table_name = $wpdb->prefix . 'odoo_crm_logs'; $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'email' => isset($payload['email']) ? $payload['email'] : '', 'payload' => wp_json_encode($payload), 'response_msg' => $error_msg, 'status' => 'failed', 'retry_count' => 3 ), array('%s', '%s', '%s', '%s', '%s', '%d') ); } public static function get_odoo_teams() { $options = get_option('odoo_crm_settings'); $url = !empty($options['odoo_url']) ? rtrim($options['odoo_url'], '/') : ''; $token = !empty($options['odoo_token']) ? $options['odoo_token'] : ''; $secret_key = !empty($options['odoo_secret_key']) ? $options['odoo_secret_key'] : ''; $cache_duration = isset($options['teams_cache_duration']) ? absint($options['teams_cache_duration']) : 60; if (empty($url) || empty($token)) { return array(); } $transient_key = 'odoo_crm_teams_cache'; $cached_teams = get_transient($transient_key); if (false !== $cached_teams) { return $cached_teams; } $endpoint = $url . '/api/ms/teams'; $headers = array( 'Authorization' => 'Bearer ' . $token, ); if (!empty($secret_key)) { $headers['X-Odoo-Signature'] = hash_hmac('sha256', '', $secret_key); } $args = array( 'headers' => $headers, 'timeout' => 10, ); $response = wp_remote_get($endpoint, $args); if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { return array(); } $body = json_decode(wp_remote_retrieve_body($response), true); $teams = isset($body['teams']) ? $body['teams'] : array(); if (!empty($teams) && $cache_duration > 0) { set_transient($transient_key, $teams, $cache_duration * MINUTE_IN_SECONDS); } return $teams; } }