Files
MiraviaConnector/connector-miravia/classes/shared/MiraviaSdk.php
Miravia Connector Bot 752600f337 Fix image upload structure for Miravia API compliance
🔧 Bug Fixes:
- Fixed product image structure to match Miravia API requirements
- Updated MiraviaProduct.php getData() method to wrap images in {"Image": [...]} format
- Updated MiraviaCombination.php getData() method to wrap SKU images properly
- Resolved error "[4224] The Main image of the product is required"

📋 Changes:
- Modified getData() methods to transform flat image arrays to nested structure
- Product images: images[] → Images: {"Image": [...]}
- SKU images: images[] → Images: {"Image": [...]}
- Maintains backward compatibility for empty image arrays

🎯 Impact:
- Product uploads will now pass Miravia's image validation
- Both product-level and SKU-level images properly formatted
- Complies with official Miravia API documentation structure

🤖 Generated with Claude Code (https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-21 11:34:59 +02:00

294 lines
9.9 KiB
PHP

<?php
/**
* Miravia SDK - Direct AliExpress API Implementation
* Replaces WeComm proxy with official AliExpress SDK
*/
// Include the simplified SDK
require_once __DIR__ . '/SimpleIopClient.php';
class MiraviaSdk
{
protected $app_key;
protected $secret_key;
protected $access_token;
protected $client;
public $last_error = '';
public function __construct()
{
$this->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');
$request->addApiParam('aeop_a_e_product', json_encode($productData));
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);
}
}