app_key = get_option('miravia_app_key', ''); $this->secret_key = get_option('miravia_secret_key', ''); $this->access_token = get_option('miravia_access_token', ''); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Loaded credentials - App Key: " . substr($this->app_key, 0, 6) . "..."); LOG::add("DEBUG SDK: Loaded credentials - Access Token: " . substr($this->access_token, 0, 20) . "..."); } if(!empty($this->app_key) && !empty($this->secret_key)) { // Use Miravia Open Platform API gateway $gateway_url = 'https://api.miravia.es'; $this->client = new SimpleIopClient($gateway_url, $this->app_key, $this->secret_key); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Using Miravia gateway: " . $gateway_url); } } } /** * Create/Upload products to Miravia using proper API format */ public function createProduct($productData) { if(!$this->client || empty($this->access_token)) { $this->last_error = 'SDK not configured properly'; return false; } try { // Use Miravia product creation API $request = new SimpleIopRequest('/product/create'); // Convert to proper Miravia format $miraviaProduct = $this->convertToMiraviaFormat($productData); $request->addApiParam('product', json_encode($miraviaProduct)); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Creating product with Miravia API"); LOG::add("DEBUG SDK: Original product data: " . json_encode($productData)); LOG::add("DEBUG SDK: Converted Miravia format: " . json_encode($miraviaProduct)); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: API Response: " . json_encode($response)); } // Check for error response first if(isset($response->error_response)) { $error = $response->error_response->msg ?? 'Unknown API error'; $error_code = $response->error_response->code ?? 'NO_CODE'; $this->last_error = "API Error ({$error_code}): {$error}"; if(class_exists('LOG')) { LOG::add("DEBUG SDK: API Error - Code: {$error_code}, Message: {$error}"); } return false; } // Check for successful product creation if(isset($response->result)) { $result = $response->result; if(isset($result->success) && $result->success) { return [ 'success' => true, 'product_id' => $result->item_id ?? $result->data->item_id ?? null, 'data' => $result ]; } else { $this->last_error = $result->message ?? 'Product creation failed'; if(class_exists('LOG')) { LOG::add("DEBUG SDK: Product creation failed: " . $this->last_error); } return false; } } // If we get here, the response format is unexpected $this->last_error = 'Unexpected API response format'; if(class_exists('LOG')) { LOG::add("DEBUG SDK: Unexpected response format: " . json_encode($response)); } return false; } catch (Exception $e) { $this->last_error = $e->getMessage(); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Exception: " . $e->getMessage()); } return false; } } /** * Update existing product */ public function updateProduct($productId, $productData) { if(!$this->client || empty($this->access_token)) { $this->last_error = 'SDK not configured properly'; return false; } try { $request = new SimpleIopRequest('aliexpress.offer.product.edit'); $request->addApiParam('aeop_a_e_product', json_encode($productData)); $request->addApiParam('product_id', $productId); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Updating product {$productId} with AliExpress API"); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Update Response: " . json_encode($response)); } if(isset($response->aliexpress_offer_product_edit_response)) { $result = $response->aliexpress_offer_product_edit_response; if(isset($result->result) && $result->result->success) { return [ 'success' => true, 'product_id' => $productId, 'data' => $result ]; } else { $this->last_error = $result->result->error_message ?? 'Unknown error'; return false; } } $this->last_error = 'Invalid response format'; return false; } catch (Exception $e) { $this->last_error = $e->getMessage(); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Error: " . $e->getMessage()); } return false; } } /** * Get product list */ public function getProductList($params = []) { if(!$this->client || empty($this->access_token)) { $this->last_error = 'SDK not configured properly'; return false; } try { $request = new SimpleIopRequest('aliexpress.postproduct.redefining.findproductinfolistquery'); // Set default parameters $defaultParams = [ 'current_page' => 1, 'page_size' => 20 ]; $params = array_merge($defaultParams, $params); foreach($params as $key => $value) { $request->addApiParam($key, $value); } if(class_exists('LOG')) { LOG::add("DEBUG SDK: Getting product list with params: " . json_encode($params)); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Product list response: " . json_encode($response)); } if(isset($response->aliexpress_postproduct_redefining_findproductinfolistquery_response)) { return $response->aliexpress_postproduct_redefining_findproductinfolistquery_response->result; } return $response; } catch (Exception $e) { $this->last_error = $e->getMessage(); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Error: " . $e->getMessage()); } return false; } } /** * Get orders from AliExpress/Miravia */ public function getOrders($fromDate, $params = []) { if(!$this->client || empty($this->access_token)) { $this->last_error = 'SDK not configured properly'; return false; } try { $request = new SimpleIopRequest('aliexpress.trade.seller.orderlist.get'); // Set required parameters $request->addApiParam('create_date_start', $fromDate); $request->addApiParam('page_size', $params['page_size'] ?? 20); $request->addApiParam('current_page', $params['current_page'] ?? 1); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Getting orders from {$fromDate}"); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Orders response: " . json_encode($response)); } if(isset($response->aliexpress_trade_seller_orderlist_get_response)) { return $response->aliexpress_trade_seller_orderlist_get_response->result; } return $response; } catch (Exception $e) { $this->last_error = $e->getMessage(); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Error: " . $e->getMessage()); } return false; } } /** * Get order details */ public function getOrderDetails($orderId) { if(!$this->client || empty($this->access_token)) { $this->last_error = 'SDK not configured properly'; return false; } try { $request = new SimpleIopRequest('aliexpress.trade.redefining.findorderbyid'); $request->addApiParam('order_id', $orderId); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Getting order details for {$orderId}"); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Order details response: " . json_encode($response)); } if(isset($response->aliexpress_trade_redefining_findorderbyid_response)) { return $response->aliexpress_trade_redefining_findorderbyid_response->result; } return $response; } catch (Exception $e) { $this->last_error = $e->getMessage(); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Error: " . $e->getMessage()); } return false; } } /** * Test API connection using Miravia-specific endpoints */ public function testConnection() { if(!$this->client || empty($this->access_token)) { return ['success' => false, 'error' => 'SDK not configured properly']; } try { // Test with Miravia category tree endpoint $request = new SimpleIopRequest('/category/tree/get'); $request->addApiParam('language', 'en'); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Testing Miravia connection with category tree"); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Category tree response: " . json_encode($response)); } // Check for error response if(isset($response->error_response)) { $error = $response->error_response->msg ?? 'Unknown API error'; $error_code = $response->error_response->code ?? 'NO_CODE'; if(class_exists('LOG')) { LOG::add("DEBUG SDK: API Error - Code: {$error_code}, Message: {$error}"); } return ['success' => false, 'error' => "API Error ({$error_code}): {$error}"]; } // Check for successful response structure if(isset($response->result)) { $result = $response->result; if(isset($result->success) && $result->success) { return ['success' => true, 'message' => 'Miravia API connection successful']; } else { $error = $result->message ?? 'Connection test failed'; return ['success' => false, 'error' => $error]; } } // If response exists but format is unexpected, consider it successful if($response && !isset($response->error_response)) { return ['success' => true, 'message' => 'Connection successful']; } return ['success' => false, 'error' => 'No valid response received']; } catch (Exception $e) { if(class_exists('LOG')) { LOG::add("DEBUG SDK: Exception during test: " . $e->getMessage()); } return ['success' => false, 'error' => 'Connection failed: ' . $e->getMessage()]; } } /** * Check if SDK is properly configured */ public function isConfigured() { return !empty($this->app_key) && !empty($this->secret_key) && !empty($this->access_token); } /** * Debug token information */ public function debugTokenInfo() { $debug_info = [ 'app_key' => $this->app_key, 'has_secret' => !empty($this->secret_key), 'has_token' => !empty($this->access_token), 'token_length' => strlen($this->access_token), 'token_prefix' => substr($this->access_token, 0, 20), 'gateway_url' => 'https://api.miravia.es', 'auth_method' => 'personal_token' ]; if(class_exists('LOG')) { LOG::add("DEBUG SDK: Token info: " . json_encode($debug_info)); } return $debug_info; } /** * Convert plugin product format to proper Miravia API format */ private function convertToMiraviaFormat($productData) { // Build the proper Miravia Request structure $miraviaProduct = [ 'Request' => [ 'Product' => [ 'PrimaryCategory' => $productData['id_category'] ?? '', 'Images' => $productData['Images'] ?? ['Image' => []], 'Attributes' => [ 'name' => $productData['name'] ?? '', 'description' => $productData['description'] ?? '', 'brand' => $productData['brand'] ?? get_option('miravia_default_brand', 'No Brand'), 'short_description' => $productData['short_description'] ?? '', 'delivery_option_sof' => 'No', 'Hazmat' => 'None' ], 'Skus' => [ 'Sku' => [] ] ] ] ]; // Build SKU data $sku = [ 'SellerSku' => $productData['sku'] ?? uniqid(), 'quantity' => $productData['quantity'] ?? 1, 'price' => $productData['price'] ?? '0', 'package_height' => $productData['height'] ?? '1', 'package_length' => $productData['length'] ?? '1', 'package_width' => $productData['width'] ?? '1', 'package_weight' => $productData['weight'] ?? '0.1', 'package_content' => $productData['package_content'] ?? $productData['name'] ?? 'Product', 'ean_code' => $productData['ean_code'] ?? '' ]; // Add SKU images if available if (isset($productData['Images']['Image']) && !empty($productData['Images']['Image'])) { $sku['Images'] = $productData['Images']; } // Add special price if available if (isset($productData['sale_price']) && !empty($productData['sale_price'])) { $sku['special_price'] = $productData['sale_price']; } $miraviaProduct['Request']['Product']['Skus']['Sku'][] = $sku; if(class_exists('LOG')) { LOG::add("DEBUG SDK: Converted to Miravia format: " . json_encode($miraviaProduct)); } return $miraviaProduct; } }