app_key = get_option('miravia_app_key', ''); $this->secret_key = get_option('miravia_secret_key', ''); $this->access_token = get_option('miravia_access_token', ''); if(!empty($this->app_key) && !empty($this->secret_key)) { $this->client = new SimpleIopClient('https://api-sg.aliexpress.com/sync', $this->app_key, $this->secret_key); } } /** * Create/Upload products to AliExpress/Miravia */ public function createProduct($productData) { if(!$this->client || empty($this->access_token)) { $this->last_error = 'SDK not configured properly'; return false; } try { $request = new SimpleIopRequest('aliexpress.offer.product.post'); // Convert Miravia product format to AliExpress format $aliexpressProduct = $this->convertToAliExpressFormat($productData); $request->addApiParam('aeop_a_e_product', json_encode($aliexpressProduct)); if(class_exists('LOG')) { LOG::add("DEBUG SDK: Creating product with AliExpress API"); LOG::add("DEBUG SDK: Product data: " . json_encode($productData)); } $response = $this->client->execute($request, $this->access_token); if(class_exists('LOG')) { LOG::add("DEBUG SDK: API Response: " . json_encode($response)); } if(isset($response->aliexpress_offer_product_post_response)) { $result = $response->aliexpress_offer_product_post_response; if(isset($result->result) && $result->result->success) { return [ 'success' => true, 'product_id' => $result->result->product_id ?? null, '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; } } /** * 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 */ public function testConnection() { if(!$this->client || empty($this->access_token)) { return ['success' => false, 'error' => 'SDK not configured properly']; } try { // Try to get a simple product list to test connection $result = $this->getProductList(['page_size' => 1]); if($result !== false) { return ['success' => true, 'message' => 'Connection successful']; } else { return ['success' => false, 'error' => $this->last_error]; } } catch (Exception $e) { return ['success' => false, 'error' => $e->getMessage()]; } } /** * Check if SDK is properly configured */ public function isConfigured() { return !empty($this->app_key) && !empty($this->secret_key) && !empty($this->access_token); } /** * Convert Miravia product format to AliExpress API format */ private function convertToAliExpressFormat($miraviaProduct) { $aliexpressProduct = [ 'subject' => $miraviaProduct['name'] ?? '', 'category_id' => $miraviaProduct['id_category'] ?? '', 'language' => 'en', 'currency_code' => 'EUR', 'package_type' => true, 'product_price' => $miraviaProduct['price'] ?? '0', 'product_unit' => 100, 'package_length' => $miraviaProduct['length'] ?? 1, 'package_width' => $miraviaProduct['width'] ?? 1, 'package_height' => $miraviaProduct['height'] ?? 1, 'gross_weight' => $miraviaProduct['weight'] ?? '0.1', 'detail' => $miraviaProduct['description'] ?? '', 'image_u_r_ls' => implode(';', $miraviaProduct['Images']['Image'] ?? []), 'package_quantity' => 1, 'ws_display' => 'N', 'ws_offline_date' => '', 'freight_template_id' => 0, 'product_status_type' => 'onSelling' ]; // Add SKU information if (!empty($miraviaProduct['sku'])) { $aliexpressProduct['aeop_ae_product_s_k_us'] = [ [ 'sku_code' => $miraviaProduct['sku'], 'sku_price' => $miraviaProduct['price'] ?? '0', 'sku_stock' => $miraviaProduct['quantity'] ?? '0', 'currency_code' => 'EUR', 'id' => '' ] ]; } if(class_exists('LOG')) { LOG::add("DEBUG SDK: Converted product format: " . json_encode($aliexpressProduct)); } return $aliexpressProduct; } }