before log

This commit is contained in:
dimitar committed 2025-01-27 22:35:52 +01:00
1 parent c8fc93ecf3
commit 9c2c174a43
11 files changed
+444

No files matched your search

Binary file not shown.
+106
View File
@@ -0,0 +1,106 @@
<?php
class EcomZoneAPI
{
private $apiKey;
private $apiUrl;
private $lastRequestTime;
private $requestCount = 0;
private const RATE_LIMIT = 120; // requests per minute
private const RATE_WINDOW = 60; // seconds
public function __construct()
{
$this->apiKey = Configuration::get('ECOMZONE_API_KEY');
$this->apiUrl = Configuration::get('ECOMZONE_API_URL');
$this->lastRequestTime = time();
}
private function checkRateLimit()
{
$currentTime = time();
if ($currentTime - $this->lastRequestTime >= self::RATE_WINDOW) {
$this->requestCount = 0;
$this->lastRequestTime = $currentTime;
}
if ($this->requestCount >= self::RATE_LIMIT) {
throw new Exception('Rate limit exceeded. Please wait before making more requests.');
}
$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 = [])
{
try {
$curl = curl_init();
$options = [
CURLOPT_URL => $url . ($method === 'GET' && !empty($params) ? '?' . http_build_query($params) : ''),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->apiKey,
'Accept: application/json',
'Content-Type: application/json'
],
];
if ($method === 'POST') {
$options[CURLOPT_POSTFIELDS] = json_encode($params);
}
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$err = curl_error($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($err) {
throw new Exception("cURL Error: " . $err);
}
if ($httpCode >= 400) {
throw new Exception("HTTP Error: " . $httpCode . " Response: " . $response);
}
return json_decode($response, true);
} catch (Exception $e) {
PrestaShopLogger::addLog(
'EcomZone API Error: ' . $e->getMessage(),
3,
null,
'EcomZone'
);
throw $e;
}
}
}
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>ecomzone</name>
<displayName><![CDATA[EcomZone Integration]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Integrates PrestaShop with EcomZone Dropshipping API]]></description>
<author><![CDATA[Your Name]]></author>
<tab><![CDATA[market_place]]></tab>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
+157
View File
@@ -0,0 +1,157 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class EcomZone extends Module
{
private $errors = [];
private $api;
public function __construct()
{
$this->name = 'ecomzone';
$this->tab = 'market_place';
$this->version = '1.0.0';
$this->author = 'Your Name';
$this->need_instance = 0;
$this->bootstrap = true;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => _PS_VERSION_
];
parent::__construct();
$this->displayName = $this->l('EcomZone Integration');
$this->description = $this->l('Integrates PrestaShop with EcomZone Dropshipping API');
// Initialize API
require_once(dirname(__FILE__) . '/classes/EcomZoneAPI.php');
$this->api = new EcomZoneAPI();
}
public function install()
{
if (!parent::install()) {
$this->errors[] = $this->l('Could not install the module');
return false;
}
if (!$this->registerHook('actionProductUpdate')) {
$this->errors[] = $this->l('Could not register hooks');
return false;
}
if (!Configuration::updateValue('ECOMZONE_API_KEY', '') ||
!Configuration::updateValue('ECOMZONE_API_URL', 'https://dropship.ecomzone.eu/api')) {
$this->errors[] = $this->l('Could not set default configuration');
return false;
}
return true;
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submitEcomZone')) {
$apiKey = Tools::getValue('ECOMZONE_API_KEY');
if (!$apiKey) {
$output .= $this->displayError($this->l('API Key is required'));
} else {
Configuration::updateValue('ECOMZONE_API_KEY', $apiKey);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output . $this->displayForm();
}
public function displayForm()
{
$fields_form[0]['form'] = [
'legend' => [
'title' => $this->l('Settings'),
],
'input' => [
[
'type' => 'text',
'label' => $this->l('API Key'),
'name' => 'ECOMZONE_API_KEY',
'required' => true,
'size' => 50
],
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
]
];
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = Configuration::get('PS_LANG_DEFAULT');
$helper->fields_value['ECOMZONE_API_KEY'] = Configuration::get('ECOMZONE_API_KEY');
return $helper->generateForm($fields_form);
}
public function getErrors()
{
return $this->errors;
}
public function hookActionProductUpdate($params)
{
try {
$product = $params['product'];
$productId = $product->id;
// Get product reference (SKU)
$reference = $product->reference;
if (!empty($reference)) {
// Get product data from EcomZone
$ecomZoneProduct = $this->api->getProduct($reference);
if ($ecomZoneProduct && isset($ecomZoneProduct['data'])) {
// Update stock
if (isset($ecomZoneProduct['data']['stock'])) {
StockAvailable::setQuantity($productId, 0, (int)$ecomZoneProduct['data']['stock']);
}
// Update price
if (isset($ecomZoneProduct['data']['product_price'])) {
$product->price = (float)$ecomZoneProduct['data']['product_price'];
$product->update();
}
PrestaShopLogger::addLog(
sprintf('EcomZone: Updated product %s (ID: %d)', $reference, $productId),
1, // Notice level
null,
'Product',
$productId,
true
);
}
}
} catch (Exception $e) {
PrestaShopLogger::addLog(
sprintf('EcomZone: Error updating product %d: %s', $productId, $e->getMessage()),
3, // Error level
null,
'Product',
$productId,
true
);
}
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
+8
View File
@@ -0,0 +1,8 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
@@ -0,0 +1,20 @@
<form method="post" action="{$current|escape:'html':'UTF-8'}&token={$token|escape:'html':'UTF-8'}">
<div class="panel">
<div class="panel-heading">
<i class="icon-cogs"></i> {l s='EcomZone Settings' mod='ecomzone'}
</div>
<div class="form-wrapper">
<div class="form-group">
<label class="control-label col-lg-3">{l s='API Key' mod='ecomzone'}</label>
<div class="col-lg-9">
<input type="text" name="ECOMZONE_API_KEY" value="{$ECOMZONE_API_KEY|escape:'html':'UTF-8'}" />
</div>
</div>
</div>
<div class="panel-footer">
<button type="submit" name="submitEcomZone" class="btn btn-default pull-right">
<i class="process-icon-save"></i> {l s='Save' mod='ecomzone'}
</button>
</div>
</div>
</form>