diff --git a/connector-miravia/classes/shared/MiraviaLink.php b/connector-miravia/classes/shared/MiraviaLink.php index 3853a9a..32f8e3f 100644 --- a/connector-miravia/classes/shared/MiraviaLink.php +++ b/connector-miravia/classes/shared/MiraviaLink.php @@ -5,17 +5,37 @@ class MiraviaLink protected $api_url = 'https://miravia.wecomm.es'; protected $api_key = ''; + protected $direct_api = false; public $last_error = ''; public $request_id = ''; public $insecure_mode = true; public $sandbox_mode = false; + protected $app_key = ''; + protected $secret_key = ''; public function __construct($api_key = '') { $this->api_key = $api_key; - if( strpos($api_key, '_f6649cb881216ce050bd0e3') ){ - $this->sandbox_mode = true; - $this->api_url = 'https://sandbox.miravia.wecomm.es'; + + // Check if direct API mode is enabled + $this->direct_api = get_option('miravia_direct_api', '0') === '1'; + + if($this->direct_api) { + // Use AliExpress API for direct access + $access_token = get_option('miravia_access_token', ''); + $this->app_key = get_option('miravia_app_key', ''); + $this->secret_key = get_option('miravia_secret_key', ''); + if(!empty($access_token)) { + $this->api_key = $access_token; + } + // AliExpress API URL for Miravia + $this->api_url = 'https://api-sg.aliexpress.com'; + } else { + // Original WeComm proxy logic + if( strpos($api_key, '_f6649cb881216ce050bd0e3') ){ + $this->sandbox_mode = true; + $this->api_url = 'https://sandbox.miravia.wecomm.es'; + } } } @@ -266,25 +286,45 @@ class MiraviaLink public function getFeedInfo($id) { - $url = $this->api_url . '/feed/' . $id . '/get' ; - $ret = $this->CallAPI($url); + if($this->direct_api) { + // AliExpress API endpoint for batch status + $url = $this->api_url . '/ae/batch/product/status'; + } else { + // WeComm proxy endpoint + $url = $this->api_url . '/feed/' . $id . '/get'; + } + + $ret = $this->CallAPI($url, 'GET', ['batch_id' => $id]); if($ret === false){ return false; } $resp = json_decode($ret, true); - if(isset($resp['feed_result'])) { - $response = []; - if(isset($resp['response'])) { - $response = $resp['response']; + + if($this->direct_api) { + // Handle AliExpress API response format + if(isset($resp['aliexpress_solution_batch_product_status_response'])) { + $response = $resp['aliexpress_solution_batch_product_status_response']; + if(isset($response['status'])) { + return ['success' => true, 'result' => ['processing_status' => $response['status']]]; + } } - $resp = $resp['feed_result']; - $resp['response'] = $response; - }else{ - if(isset($resp['code'])){ - $this->last_error = $resp['code'] . ': ' . - @$resp['message'] ?: ''; + } else { + // Handle WeComm proxy response format + if(isset($resp['feed_result'])) { + $response = []; + if(isset($resp['response'])) { + $response = $resp['response']; + } + $resp = $resp['feed_result']; + $resp['response'] = $response; + }else{ + if(isset($resp['code'])){ + $this->last_error = $resp['code'] . ': ' . + @$resp['message'] ?: ''; + } } } + if(!isset($resp['success']) || !$resp['success']){ return false; } @@ -311,19 +351,42 @@ class MiraviaLink public function sendFeed($data, $type = 'create') { - $url = $this->api_url . '/feed/' . $type; + if($this->direct_api) { + // AliExpress API endpoints for Miravia + if($type === 'create') { + $url = $this->api_url . '/ae/item/create'; + } else { + $url = $this->api_url . '/ae/item/update'; + } + } else { + // WeComm proxy endpoints + $url = $this->api_url . '/feed/' . $type; + } + $ret = $this->CallAPI($url, 'POST', $data); if($ret === false){ return false; } $resp = json_decode($ret, true); - if(!isset($resp['success'])){ - return false; - }else if(!$resp['success']){ - // $this->last_error = $resp['message']; - if(isset($resp['detail'])){ - foreach ($resp['detail'] as $det){ - $this->last_error .= "\r" . $det['field'] . ': ' . $det['message']; + + if($this->direct_api) { + // Handle AliExpress API response format + if(isset($resp['aliexpress_solution_batch_product_upload_response'])) { + $response = $resp['aliexpress_solution_batch_product_upload_response']; + if(isset($response['batch_id'])) { + return ['success' => true, 'feed_result' => ['result' => $response['batch_id']]]; + } + } + } else { + // Handle WeComm proxy response format + if(!isset($resp['success'])){ + return false; + }else if(!$resp['success']){ + // $this->last_error = $resp['message']; + if(isset($resp['detail'])){ + foreach ($resp['detail'] as $det){ + $this->last_error .= "\r" . $det['field'] . ': ' . $det['message']; + } } } } @@ -534,11 +597,53 @@ class MiraviaLink } if(!empty($this->api_key)){ - curl_setopt($curl, CURLOPT_HTTPHEADER, array( - 'Api-Token: ' . $this->api_key - )); + if($this->direct_api) { + // AliExpress API requires signature authentication + $path = parse_url($url, PHP_URL_PATH); + $query = parse_url($url, PHP_URL_QUERY); + parse_str($query ?: '', $queryParams); + + // Parse POST data if present + $postParams = []; + if($data && $method === 'POST') { + if(is_string($data)) { + $postParams = json_decode($data, true) ?: []; + } else { + $postParams = (array)$data; + } + } + + $allParams = array_merge($queryParams, $postParams); + $signature = $this->generateSignature($path, $allParams, $method); + + // Add signature to URL parameters + $signedParams = [ + 'app_key' => $this->app_key, + 'access_token' => $this->api_key, + 'timestamp' => time() * 1000, + 'sign_method' => 'sha256', + 'format' => 'json', + 'v' => '1.0', + 'sign' => $signature + ]; + + $finalParams = array_merge($allParams, $signedParams); + $url = $this->api_url . $path . '?' . http_build_query($finalParams); + curl_setopt($curl, CURLOPT_URL, $url); + + curl_setopt($curl, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'Accept: application/json' + )); + } else { + // WeComm proxy uses Api-Token header + curl_setopt($curl, CURLOPT_HTTPHEADER, array( + 'Api-Token: ' . $this->api_key + )); + } if(class_exists('LOG')) { - LOG::add("DEBUG API: Using API token: " . substr($this->api_key, 0, 10) . "..."); + $auth_method = $this->direct_api ? 'AliExpress API Signature' : 'WeComm Api-Token'; + LOG::add("DEBUG API: Using {$auth_method}: " . substr($this->api_key, 0, 10) . "..."); } } @@ -551,7 +656,7 @@ class MiraviaLink if(class_exists('LOG')) { LOG::add("DEBUG API: Response time: {$response_time}ms, HTTP code: {$http_code}"); - if($result && strlen($result) < 2000) { + if($result && ($http_code >= 400 || strlen($result) < 2000)) { LOG::add("DEBUG API: Response: " . $result); } elseif($result) { LOG::add("DEBUG API: Response size: " . strlen($result) . " bytes"); @@ -568,4 +673,44 @@ class MiraviaLink return $result; } + /** + * Generate AliExpress API signature + */ + protected function generateSignature($path, $params, $method = 'POST') + { + if(!$this->direct_api) { + return ''; + } + + // Add common parameters + $commonParams = [ + 'app_key' => $this->app_key, + 'access_token' => $this->api_key, + 'timestamp' => time() * 1000, + 'sign_method' => 'sha256', + 'format' => 'json', + 'v' => '1.0' + ]; + + // Merge with request parameters + $allParams = array_merge($commonParams, $params); + + // Sort parameters + ksort($allParams); + + // Build signature string + $signString = $path; + foreach($allParams as $key => $value) { + if(is_array($value) || is_object($value)) { + $value = json_encode($value); + } + $signString .= $key . $value; + } + + // Generate signature + $signature = hash_hmac('sha256', $signString, $this->secret_key); + + return strtoupper($signature); + } + } diff --git a/connector-miravia/views/pages/configuration.php b/connector-miravia/views/pages/configuration.php index 7c45406..c6538de 100644 --- a/connector-miravia/views/pages/configuration.php +++ b/connector-miravia/views/pages/configuration.php @@ -22,6 +22,23 @@ if(isset($_POST['miravia_action_nonce'])) { }else{ update_option('miravia_only_stock', '0'); } + if(isset($_POST['miravia_direct_api']) and sanitize_text_field($_POST['miravia_direct_api']) == '1') { + update_option('miravia_direct_api', '1'); + }else{ + update_option('miravia_direct_api', '0'); + } + if(isset($_POST['miravia_personal_token'])) { + update_option('miravia_personal_token', sanitize_text_field($_POST['miravia_personal_token'])); + } + if(isset($_POST['miravia_app_key'])) { + update_option('miravia_app_key', sanitize_text_field($_POST['miravia_app_key'])); + } + if(isset($_POST['miravia_secret_key'])) { + update_option('miravia_secret_key', sanitize_text_field($_POST['miravia_secret_key'])); + } + if(isset($_POST['miravia_access_token'])) { + update_option('miravia_access_token', sanitize_text_field($_POST['miravia_access_token'])); + } } @@ -30,6 +47,11 @@ $defaultUnit = get_option('miravia_default_unit', 'units'); $defaultUnitValue = get_option('miravia_default_unit_value', '1'); $timeDelay = get_option('miravia_delay_time', 300); $debugMode = get_option('miravia_debug_mode', '0'); +$directApi = get_option('miravia_direct_api', '0'); +$personalToken = get_option('miravia_personal_token', ''); +$appKey = get_option('miravia_app_key', ''); +$secretKey = get_option('miravia_secret_key', ''); +$accessToken = get_option('miravia_access_token', ''); $transportMode = get_option('miravia_transport_mode', 'dbm'); $defaultBrand = get_option('miravia_default_brand', 'No Brand'); $statuses = wc_get_order_statuses(); @@ -71,6 +93,38 @@ $categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] ); /> + + +

+ + + /> + + + + +

+ + + + + + + +

+ + + + + + + +

+ + + + +