up untill fetch 10 products

This commit is contained in:
dimitar 2025-01-27 23:08:03 +01:00
parent 9c2c174a43
commit 2eef682859
4 changed files with 148 additions and 28 deletions

Binary file not shown.

View File

@ -14,6 +14,14 @@ class EcomZoneAPI
$this->apiKey = Configuration::get('ECOMZONE_API_KEY'); $this->apiKey = Configuration::get('ECOMZONE_API_KEY');
$this->apiUrl = Configuration::get('ECOMZONE_API_URL'); $this->apiUrl = Configuration::get('ECOMZONE_API_URL');
$this->lastRequestTime = time(); $this->lastRequestTime = time();
// Debug log the configuration
PrestaShopLogger::addLog(
sprintf('EcomZone API initialized with URL: %s', $this->apiUrl),
1,
null,
'EcomZone'
);
} }
private function checkRateLimit() private function checkRateLimit()
@ -31,40 +39,35 @@ class EcomZoneAPI
$this->requestCount++; $this->requestCount++;
} }
public function getCatalog($perPage = 1000)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/catalog';
return $this->makeRequest('GET', $url, ['per_page' => $perPage]);
}
public function getProduct($sku)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/product/' . urlencode($sku);
return $this->makeRequest('GET', $url);
}
public function createOrder($orderData)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/ordering';
return $this->makeRequest('POST', $url, $orderData);
}
private function makeRequest($method, $url, $params = []) private function makeRequest($method, $url, $params = [])
{ {
try { try {
// Debug log the request
PrestaShopLogger::addLog(
sprintf('Making request: %s %s', $method, $url),
1,
null,
'EcomZone'
);
$curl = curl_init(); $curl = curl_init();
// Add token as URL parameter AND header for maximum compatibility
$urlWithToken = $url . (strpos($url, '?') === false ? '?' : '&') . 'token=' . $this->apiKey;
if ($method === 'GET' && !empty($params)) {
$urlWithToken .= '&' . http_build_query($params);
}
$options = [ $options = [
CURLOPT_URL => $url . ($method === 'GET' && !empty($params) ? '?' . http_build_query($params) : ''), CURLOPT_URL => $urlWithToken,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '', CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10, CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30, CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method, CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_SSL_VERIFYPEER => false, // For testing only, remove in production
CURLOPT_SSL_VERIFYHOST => false, // For testing only, remove in production
CURLOPT_HTTPHEADER => [ CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->apiKey, 'Authorization: Bearer ' . $this->apiKey,
'Accept: application/json', 'Accept: application/json',
@ -78,10 +81,33 @@ class EcomZoneAPI
curl_setopt_array($curl, $options); curl_setopt_array($curl, $options);
// Debug log the curl options
PrestaShopLogger::addLog(
sprintf('Curl options: %s', json_encode([
'url' => $urlWithToken,
'headers' => $options[CURLOPT_HTTPHEADER]
])),
1,
null,
'EcomZone'
);
$response = curl_exec($curl); $response = curl_exec($curl);
$err = curl_error($curl); $err = curl_error($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Debug log the response
PrestaShopLogger::addLog(
sprintf('Response Code: %d, Error: %s, Response: %s',
$httpCode,
$err ?: 'none',
substr($response, 0, 1000)
),
1,
null,
'EcomZone'
);
curl_close($curl); curl_close($curl);
if ($err) { if ($err) {
@ -103,4 +129,25 @@ class EcomZoneAPI
throw $e; throw $e;
} }
} }
public function getCatalog($perPage = 10)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/catalog';
return $this->makeRequest('GET', $url, ['per_page' => $perPage]);
}
public function getProduct($sku)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/product/' . urlencode($sku);
return $this->makeRequest('GET', $url);
}
public function createOrder($orderData)
{
$this->checkRateLimit();
$url = $this->apiUrl . '/ordering';
return $this->makeRequest('POST', $url, $orderData);
}
} }

View File

@ -44,7 +44,8 @@ class EcomZone extends Module
return false; return false;
} }
if (!Configuration::updateValue('ECOMZONE_API_KEY', '') || // Set default API key from your working configuration
if (!Configuration::updateValue('ECOMZONE_API_KEY', 'klRyAdrXaxL0s6PEUp7LDlH6T8aPSCtBY8NiEHsHiWpc6646K2TZPi5KMxUg') ||
!Configuration::updateValue('ECOMZONE_API_URL', 'https://dropship.ecomzone.eu/api')) { !Configuration::updateValue('ECOMZONE_API_URL', 'https://dropship.ecomzone.eu/api')) {
$this->errors[] = $this->l('Could not set default configuration'); $this->errors[] = $this->l('Could not set default configuration');
return false; return false;
@ -56,18 +57,54 @@ class EcomZone extends Module
public function getContent() public function getContent()
{ {
$output = ''; $output = '';
$debugOutput = '';
if (Tools::isSubmit('submitEcomZone')) { if (Tools::isSubmit('submitEcomZone')) {
$apiKey = Tools::getValue('ECOMZONE_API_KEY'); $apiKey = trim(Tools::getValue('ECOMZONE_API_KEY'));
if (!$apiKey) { if (!$apiKey) {
$output .= $this->displayError($this->l('API Key is required')); $output .= $this->displayError($this->l('API Key is required'));
} else { } else {
Configuration::updateValue('ECOMZONE_API_KEY', $apiKey); Configuration::updateValue('ECOMZONE_API_KEY', $apiKey);
$debugOutput .= "API Key saved: " . substr($apiKey, 0, 10) . "...\n";
$output .= $this->displayConfirmation($this->l('Settings updated')); $output .= $this->displayConfirmation($this->l('Settings updated'));
} }
} }
// Handle manual fetch
if (Tools::isSubmit('fetchProducts')) {
try {
$debugOutput .= "Starting product fetch...\n";
// Get first 10 products for testing
$catalog = $this->api->getCatalog(10);
if (isset($catalog['data']) && is_array($catalog['data'])) {
foreach ($catalog['data'] as $product) {
$debugOutput .= sprintf(
"Found product: %s - %s (Price: %s EUR, Stock: %s)\n",
$product['sku'],
$product['product_name'],
$product['product_price'],
$product['stock']
);
}
$debugOutput .= sprintf("\nTotal products fetched: %d\n", count($catalog['data']));
} else {
$debugOutput .= "No products found or invalid response format\n";
}
$output .= $this->displayConfirmation($this->l('Products fetched successfully'));
} catch (Exception $e) {
$debugOutput .= "Error: " . $e->getMessage() . "\n";
$output .= $this->displayError($this->l('Error fetching products'));
}
}
// Store debug output in smarty variable
$this->context->smarty->assign('debug_output', $debugOutput);
return $output . $this->displayForm(); return $output . $this->displayForm();
} }
@ -86,6 +123,15 @@ class EcomZone extends Module
'size' => 50 'size' => 50
], ],
], ],
'buttons' => [
[
'type' => 'submit',
'title' => $this->l('Fetch Products'),
'icon' => 'process-icon-download',
'name' => 'fetchProducts',
'class' => 'btn btn-primary pull-left'
]
],
'submit' => [ 'submit' => [
'title' => $this->l('Save'), 'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right' 'class' => 'btn btn-default pull-right'
@ -99,8 +145,21 @@ class EcomZone extends Module
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name; $helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = Configuration::get('PS_LANG_DEFAULT'); $helper->default_form_language = Configuration::get('PS_LANG_DEFAULT');
$helper->fields_value['ECOMZONE_API_KEY'] = Configuration::get('ECOMZONE_API_KEY'); $helper->fields_value['ECOMZONE_API_KEY'] = Configuration::get('ECOMZONE_API_KEY');
return $helper->generateForm($fields_form); // Add debug console after form
$form = $helper->generateForm($fields_form);
$debug_console = '<div class="panel">
<div class="panel-heading">
<i class="icon-list"></i> ' . $this->l('Debug Console') . '
</div>
<div class="panel-content">
<pre id="debug-console" style="max-height: 300px; overflow-y: auto; background: #f5f5f5; padding: 10px;">
' . $this->context->smarty->getTemplateVars('debug_output') . '
</pre>
</div>
</div>';
return $form . $debug_console;
} }
public function getErrors() public function getErrors()

View File

@ -15,6 +15,20 @@
<button type="submit" name="submitEcomZone" class="btn btn-default pull-right"> <button type="submit" name="submitEcomZone" class="btn btn-default pull-right">
<i class="process-icon-save"></i> {l s='Save' mod='ecomzone'} <i class="process-icon-save"></i> {l s='Save' mod='ecomzone'}
</button> </button>
<button type="submit" name="fetchProducts" class="btn btn-primary pull-left">
<i class="process-icon-download"></i> {l s='Fetch Products' mod='ecomzone'}
</button>
</div> </div>
</div> </div>
</form> </form>
<div class="panel">
<div class="panel-heading">
<i class="icon-list"></i> {l s='Debug Console' mod='ecomzone'}
</div>
<div class="panel-content">
<pre id="debug-console" style="max-height: 300px; overflow-y: auto; background: #f5f5f5; padding: 10px;">
{$debug_output|escape:'html':'UTF-8'}
</pre>
</div>
</div>