🔧 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>
456 lines
17 KiB
PHP
456 lines
17 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(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 seller API gateway for personal tokens
|
|
$gateway_url = 'https://api.miravia.es/sync';
|
|
$this->client = new SimpleIopClient($gateway_url, $this->app_key, $this->secret_key);
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SDK: Using Miravia seller API 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 seller API for product creation
|
|
$request = new SimpleIopRequest('lazada.product.create');
|
|
|
|
// Convert to proper Miravia format
|
|
$miraviaProduct = $this->convertToMiraviaFormat($productData);
|
|
$request->addApiParam('payload', 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 (Miravia seller API format)
|
|
if(isset($response->data)) {
|
|
$result = $response->data;
|
|
if(isset($result->item_id) || isset($result->success)) {
|
|
return [
|
|
'success' => true,
|
|
'product_id' => $result->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;
|
|
}
|
|
}
|
|
|
|
// Check for response with code indicating success/failure
|
|
if(isset($response->code)) {
|
|
if($response->code === '0' || $response->code === 0) {
|
|
return [
|
|
'success' => true,
|
|
'product_id' => $response->data->item_id ?? null,
|
|
'data' => $response->data ?? $response
|
|
];
|
|
} else {
|
|
$this->last_error = $response->message ?? "API returned error code: {$response->code}";
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SDK: Product creation failed with code: " . $response->code);
|
|
}
|
|
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 basic connection - any method will return InvalidApiPath but confirms auth works
|
|
$request = new SimpleIopRequest('test.connection');
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SDK: Testing Miravia API authentication");
|
|
}
|
|
|
|
$response = $this->client->execute($request, $this->access_token);
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SDK: Miravia API 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}");
|
|
}
|
|
|
|
// InvalidApiPath with proper structure means authentication is working
|
|
if($error_code === 'InvalidApiPath') {
|
|
return ['success' => true, 'message' => 'Authentication successful (gateway confirmed working)'];
|
|
}
|
|
|
|
return ['success' => false, 'error' => "API Error ({$error_code}): {$error}"];
|
|
}
|
|
|
|
// Any response without error indicates successful connection
|
|
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/sync',
|
|
'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;
|
|
}
|
|
} |