first commit

This commit is contained in:
2024-11-11 18:46:54 +01:00
commit a630d17338
25634 changed files with 4923715 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
/**
* Class Attributes
* @package Emarketing\Action
*/
class Attributes
{
/**
* @param $postData
* @return array
*/
public function fetchAttributes($postData)
{
$idLang = $this->getIdLanguage($postData);
$serviceAttributes = new \Emarketing\Service\Attributes;
return $serviceAttributes->buildAttributeInformation($idLang);
}
/**
* @param $postData
* @return mixed
*/
private function getIdLanguage($postData)
{
if (!isset($postData['id_language']) || !is_numeric($postData['id_language'])) {
return \Context::getContext()->language->id;
}
return $postData['id_language'];
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
use Emarketing\ClientError;
/**
* Class Categories
* @package Emarketing\Action
*/
class Categories
{
/**
* @param $postData
* @return array
* @throws ClientError
*/
public function fetchCategory($postData)
{
$idCategory = $this->getIdCategory($postData);
$idLang = $this->getIdLanguage($postData);
$serviceCategory = new \Emarketing\Service\Category;
return $serviceCategory->buildCategoryInformation($idCategory, $idLang);
}
/**
* @param $postData
* @return string
* @throws ClientError
*/
private function getIdCategory($postData)
{
if (!isset($postData['id_category']) || !is_numeric($postData['id_category'])) {
throw new ClientError('Invalid Category ID');
}
if ($postData['id_category'] === 0) {
return \Configuration::get('PS_HOME_CATEGORY');
}
return $postData['id_category'];
}
/**
* @param $postData
* @return mixed
*/
private function getIdLanguage($postData)
{
if (!isset($postData['id_language']) || !is_numeric($postData['id_language'])) {
return \Context::getContext()->language->id;
}
return $postData['id_language'];
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
/**
* Class Countries
* @package Emarketing\Action
*/
class Countries
{
/**
* @return array
*/
public function fetchCountries()
{
$serviceCountries = new \Emarketing\Service\Countries;
return $serviceCountries->buildCountryInformation();
}
}

View File

@@ -0,0 +1,55 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
/**
* Class FacebookTracker
* @package Emarketing\Action
*/
class FacebookPixel
{
/**
* @param $postData
* @return bool|array
* @throws \Exception
*/
public function receivePixel($postData)
{
$serviceTracker = new \Emarketing\Service\FacebookPixel();
$globalTag = $this->getCode($postData, 'global_tag');
$viewContent = $this->getCode($postData, 'view_content_snippet');
$addToCart = $this->getCode($postData, 'add_to_cart_snippet');
$purchase = $this->getCode($postData, 'purchase_snippet');
return $serviceTracker->savePixel($globalTag, $viewContent, $addToCart, $purchase);
}
/**
* @param $postData
* @param $name
* @return string|null
*/
private function getCode($postData, $name)
{
if (!isset($postData[$name])) {
return null;
}
return $postData[$name];
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
/**
* Class Tracker
* @package Emarketing\Action
*/
class GoogleTracker
{
/**
* @param $postData
* @return bool|array
* @throws \Exception
*/
public function receiveTracker($postData)
{
$serviceTracker = new \Emarketing\Service\GoogleTracker();
$globalSiteTracker = $this->getCode($postData, 'global_site_tracker');
$conversionTracker = $this->getCode($postData, 'conversion_tracker');
return $serviceTracker->saveTracker($globalSiteTracker, $conversionTracker);
}
/**
* @param $postData
* @param $name
* @return string|null
*/
private function getCode($postData, $name)
{
if (!isset($postData[$name])) {
return null;
}
return $postData[$name];
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
use Emarketing\ClientError;
/**
* Class Products
* @package Emarketing\Action
*/
class Products
{
/**
* @param $postData
* @return array
* @throws ClientError
* @throws \Exception
*/
public function fetchProducts($postData)
{
$offset = $this->checkValidParam($postData, 'offset');
$limit = $this->checkValidParam($postData, 'limit');
$idLang = $this->getIdLanguage($postData);
$idCountry = $this->getIdCountry($postData);
$idCurrency = $this->getIdCurrency($postData);
$serviceProducts = new \Emarketing\Service\Products;
return $serviceProducts->buildProductsInformation($offset, $limit, $idLang, $idCountry, $idCurrency);
}
/**
* @param $postData
* @param $param
* @return mixed
* @throws ClientError
*/
private function checkValidParam($postData, $param)
{
if (!isset($postData[$param]) || !is_numeric($postData[$param]) || $postData[$param] < 0) {
throw new ClientError('Invalid ' . $param);
}
return $postData[$param];
}
/**
* @param $postData
* @return mixed
*/
private function getIdLanguage($postData)
{
if (!isset($postData['id_language']) || !is_numeric($postData['id_language'])) {
return \Context::getContext()->language->id;
}
return $postData['id_language'];
}
/**
* @param $postData
* @return string
*/
private function getIdCountry($postData)
{
if (!isset($postData['id_country']) || !is_numeric($postData['id_country'])) {
return \Configuration::get('PS_COUNTRY_DEFAULT');
}
return $postData['id_country'];
}
/**
* @param $postData
* @return string
*/
private function getIdCurrency($postData)
{
if (!isset($postData['id_currency']) || !is_numeric($postData['id_currency'])) {
return \Configuration::get('PS_CURRENCY_DEFAULT');
}
return $postData['id_currency'];
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Action;
use Emarketing\ClientError;
/**
* Class Verification
* @package Emarketing\Action
*/
class Verification
{
/**
* @param $postData
* @return bool
* @throws \Exception
*/
public function receiveTag($postData)
{
$serviceVerification = new \Emarketing\Service\Verification;
if (!isset($postData['tag'])) {
throw new ClientError('Missing tag');
}
return $serviceVerification->saveTag($postData['tag']);
}
}

View File

@@ -0,0 +1,11 @@
<?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;

View File

@@ -0,0 +1,23 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing;
/**
* Class ClientError
* @package Emarketing
*/
class ClientError extends \ErrorException
{
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing;
/**
* Class CurlRequest
* @package Emarketing
*/
class CurlRequest
{
/**
* @var false|resource|null
*/
private $handle = null;
/**
* CurlRequest constructor.
* @param $url
*/
public function __construct($url)
{
$this->handle = curl_init($url);
curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
}
/**
* @param $name
* @param $value
*/
public function setOption($name, $value)
{
curl_setopt($this->handle, $name, $value);
}
/**
* @return bool|string
*/
public function execute()
{
return curl_exec($this->handle);
}
/**
* @param $name
* @return mixed
*/
public function getInfo($name)
{
return curl_getinfo($this->handle, $name);
}
/**
*
*/
public function close()
{
curl_close($this->handle);
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing;
/**
* Class ErrorHandler
* @package Emarketing
*/
class ErrorHandler
{
/**
* @var array
*/
private $phpErrors = array();
/**
* @param \Exception $exception
* @return bool
*/
public function handleExceptions($exception)
{
$response = array(
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace()
);
// Catch PrestaShopException
if (!is_null($exception->getPrevious())) {
$response['trace'] = $exception->getPrevious()->getTrace();
}
if (get_class($exception) == 'Emarketing\ClientError') {
return $this->outputError(422, $response);
}
return $this->outputError(520, $response);
}
/**
* @param $errno
* @param $errstr
* @param $errfile
* @param $errline
*
* @throws \ErrorException
*/
public function handleErrors($errno, $errstr, $errfile, $errline)
{
if ($errno == E_ERROR || $errno == E_USER_ERROR) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
$this->phpErrors[] = array(
'message' => $errstr,
'file' => $errfile,
'line' => $errline,
'errno' => $errno
);
}
/**
* @return array
*/
public function getErrors()
{
return $this->phpErrors;
}
/**
* @param $httpCode
* @param $response
* @return bool
*/
public function outputError($httpCode, $response)
{
header('X-PHP-Response-Code: ' . $httpCode, true, $httpCode);
echo json_encode($response);
return true;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
/**
* Class Attributes
* @package Emarketing\Service
*/
class Attributes
{
/**
* @param $idLang
* @return array
*/
public function buildAttributeInformation($idLang)
{
$attrGroups = \AttributeGroup::getAttributesGroups($idLang);
$features = \Feature::getFeatures($idLang);
$mapData = array(
'attributes' => $attrGroups,
'features' => $features
);
return $mapData;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
use Emarketing\ClientError;
/**
* Class Category
* @package Emarketing\Service
*/
class Category
{
/**
* @param $idCategory
* @param $idLang
* @return array
* @throws ClientError
*/
public function buildCategoryInformation($idCategory, $idLang)
{
$category = new \Category($idCategory);
if (empty($category) || empty($category->id_category)) {
throw new ClientError('No category with this ID');
}
$categoryData = get_object_vars($category);
$categoryData['url'] = $category->getLink(null, $idLang);
$categoryData['children'] = $category->getSubCategories($idLang, true);
return $categoryData;
}
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
/**
* Class Countries
* @package Emarketing\Service
*/
class Countries
{
/**
* @return array
*/
public function buildCountryInformation()
{
$context = \Context::getContext();
$countryData = array(
'default' => array(
'id_country' => \Configuration::get('PS_COUNTRY_DEFAULT'),
'id_language' => \Configuration::get('PS_LANG_DEFAULT'),
'id_currency' => \Configuration::get('PS_CURRENCY_DEFAULT')
),
'active' => array(
'countries' => \Country::getCountries($context->language->id, true, false, false),
'languages' => \Language::getLanguages(true, $context->shop->id),
'currencies' => $this->getCurrencies($context->shop->id)
)
);
return $countryData;
}
/**
* @param $shopId
* @return array
*/
private function getCurrencies($shopId)
{
$currencyData = array();
$currencies = \Currency::getCurrenciesByIdShop($shopId);
foreach ($currencies as $currency) {
if ($currency['deleted'] == 1 || $currency['active'] == 0) {
continue;
}
$currencyData[] = array(
'id_currency' => (string)$currency['id_currency'],
'name' => $currency['name'],
'iso_code' => $currency['iso_code'],
'conversion_rate' => $currency['conversion_rate'],
'active' => $currency['active'],
'deleted' => $currency['deleted'],
'sign' => $currency['sign'],
'iso_code_num' => $currency['iso_code_num'],
'format' => $currency['format']
);
}
return $currencyData;
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
use Emarketing\Service\Snippets\Snippets;
use Emarketing\Service\Products\PriceCalculation;
/**
* Class FacebookPixel
* @package Emarketing\Service
*/
class FacebookPixel extends Snippets
{
/**
* @param $globalTag
* @param $viewContent
* @param $addToCart
* @param $purchase
* @return bool
* @throws \Exception
*/
public function savePixel($globalTag, $viewContent, $addToCart, $purchase)
{
$this->saveInConfiguration('FB_GLOBAL', $globalTag);
$this->saveInConfiguration('FB_VIEWCONTENT', $viewContent);
$this->saveInConfiguration('FB_ADDTOCART', $addToCart);
$this->saveInConfiguration('FB_PURCHASE', $purchase);
return true;
}
/**
* @return string
*/
public function getGlobalTagSnippet()
{
$snippet = $this->getFromConfiguration('FB_GLOBAL');
return $snippet;
}
/**
* @return string
*/
public function getViewContentSnippet()
{
$snippet = $this->getFromConfiguration('FB_VIEWCONTENT');
return $this->replaceProductPageVariables($snippet);
}
/**
* @return string
*/
public function getAddToCartSnippet()
{
$snippet = $this->getFromConfiguration('FB_ADDTOCART');
$snippet = preg_replace(
['/<script>/', '/<\/script>/'],
["<script>\ndocument.addEventListener('DOMContentLoaded', function(event) {\n document.querySelectorAll('.add-to-cart, #add_to_cart button, #add_to_cart a, #add_to_cart input').forEach(function(a){\na.addEventListener('click', function(){", "});});});\n</script>"],
$snippet
);
return $this->replaceProductPageVariables($snippet);
}
/**
* @return string
* @throws \Exception
*/
public function getPurchaseSnippet()
{
$snippet = $this->getFromConfiguration('FB_PURCHASE');
$order = new \Order(\Tools::getValue('id_order'));
$currency = new \Currency($order->id_currency);
$snippet = preg_replace(
['/\bREPLACE_VALUE\b/', '/\bCURRENCY\b/'],
[\Tools::ps_round($order->total_paid, 2), $currency->iso_code],
$snippet
);
return $snippet;
}
/**
* @param $snippet
* @return string
*/
private function replaceProductPageVariables($snippet)
{
$product = \Context::getContext()->controller->getProduct();
$category = new \Category($product->id_category_default);
$categoryName = is_array($category->name) ? reset($category->name) : $category->name;
$currentCurrency = \Context::getContext()->currency;
$priceCalculation = new PriceCalculation();
$price = $priceCalculation->getFinalPrice($currentCurrency->id, $product->id, true, \Tools::getValue('id_product_attribute'));
$snippet = preg_replace(
['/\bREPLACE_CONTENT\b/', '/\bREPLACE_CATEGORY\b/', '/\bREPLACE_CONTENT_ID\b/', '/\bREPLACE_VALUE\b/', '/\bCURRENCY\b/'],
[$product->name, $categoryName, $product->id, $price, $currentCurrency->iso_code],
$snippet
);
return $snippet;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
/**
* Class FrontendHeader
* @package Emarketing\Service
*/
class FrontendHeader
{
/**
* @return string
* @throws \Exception
*/
public function buildHtml()
{
$currentPage = \Context::getContext()->controller->php_self;
$html = "<!-- emarketing start -->\n";
$html .= $this->buildGoogle($currentPage);
$html .= $this->buildFacebook($currentPage);
$html .= "<!-- emarketing end -->";
return $html;
}
/**
* @param $currentPage
* @return string
* @throws \Exception
*/
private function buildGoogle($currentPage)
{
$serviceVerification = new Verification;
$html = $serviceVerification->getTag() . "\n";
$serviceTracker = new GoogleTracker();
$html .= $serviceTracker->getGlobalSiteTracker() . "\n";
if ($currentPage == 'order-confirmation') {
$html .= $serviceTracker->getConversionTracker() . "\n";
}
return $html;
}
/**
* @param $currentPage
* @return string
* @throws \Exception
*/
private function buildFacebook($currentPage)
{
$serviceTracker = new FacebookPixel();
$html = $serviceTracker->getGlobalTagSnippet() . "\n";
if ($currentPage == 'product') {
$html .= $serviceTracker->getViewContentSnippet() . "\n";
$html .= $serviceTracker->getAddToCartSnippet() . "\n";
}
if ($currentPage == 'order-confirmation') {
$html .= $serviceTracker->getPurchaseSnippet() . "\n";
}
return $html;
}
}

View File

@@ -0,0 +1,268 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
use Emarketing\CurlRequest;
/**
* Class Gateway
* @package Emarketing\Service
*/
class Gateway
{
/**
* @var string
*/
private $ecomUrl = "https://app.emarketing.com";
/**
* @var string
*/
private $ecomSignInUrl = "https://app.emarketing.com/?jwt=";
/**
* @var string
*/
private $gatewayUrl = "https://gateway.emarketing.com/gateway_api/sso";
/**
* @return string
*/
public function login()
{
$token = $this->getToken();
if (empty($token) || strpos($token, ':') === false) {
return $this->ecomUrl;
}
$jwt = $this->fetchJwt($token);
if (empty($jwt)) {
return $this->ecomUrl;
}
return $this->ecomSignInUrl . $jwt;
}
/**
* @return string
*/
public function signup()
{
$token = $this->register();
if (empty($token)) {
return $this->ecomUrl;
}
$jwt = $this->fetchJwt($token);
if (empty($jwt)) {
return $this->ecomUrl;
}
return $this->ecomSignInUrl . $jwt;
}
/**
* @return string
*/
public function linkAccount()
{
$token = $this->getToken();
if (!empty($token)) {
return $this->ecomUrl;
}
$jwt = $this->authorize();
if (empty($jwt)) {
return $this->ecomUrl;
}
return $this->ecomSignInUrl . $jwt;
}
/**
* @return string
*/
private function getToken()
{
return \Configuration::get('EMARKETING_SHOPTOKEN');
}
/**
* @param $token
* @return bool
*/
private function saveToken($token)
{
return \Configuration::updateValue('EMARKETING_SHOPTOKEN', $token);
}
/**
* @return bool|string
*/
private function register()
{
$data = array(
"partner_short_name" => "prestashop",
"authorizations" => ['datafeed_api'],
"language" => \Tools::strtolower(\Tools::substr(\Language::getIsoById(\Configuration::get('PS_LANG_DEFAULT')), 0, 2)),
"country" => \Tools::strtoupper(\Country::getIsoById(\Configuration::get('PS_COUNTRY_DEFAULT'))),
"email" => \Configuration::get('PS_SHOP_EMAIL'),
"version" => \Module::getInstanceByName('emarketing')->version
);
$data = $this->validateRegistrationData($data);
$header = "Origin: " . \Context::getContext()->shop->getBaseURL(true);
$token = $this->registerRequest($data, $header);
if (empty($token)) {
return false;
}
$this->saveToken($token);
return $token;
}
/**
* @param $data
* @return mixed
*/
private function validateRegistrationData($data)
{
$schema = json_decode(\Tools::file_get_contents('https://ecommerce-os.s3.eu-west-1.amazonaws.com/schema/partner.json'));
$languages = $schema->links[0]->schema->properties->language->enum;
$countries = $schema->links[0]->schema->properties->country->enum;
if (!in_array($data['language'], $languages)) {
$data['language'] = 'en';
}
if (!in_array($data['country'], $countries)) {
$data['country'] = 'DE';
}
return $data;
}
/**
* @param $data
* @param $header
* @return bool|string
*/
private function registerRequest($data, $header)
{
$response = $this->sendRequest('/signup', $header, $data);
if ($response['code'] == 400) {
unset($data['email']);
$response = $this->sendRequest('/signup', $header, $data);
}
if ($response['code'] !== 200 || empty($response['body']->token)) {
return false;
}
return $response['body']->token;
}
/**
* @return bool|string
*/
private function authorize()
{
$data = array(
"partner_short_name" => "prestashop",
'authorizations' => ['datafeed_api'],
"version" => \Module::getInstanceByName('emarketing')->version
);
$header = "Origin: " . \Context::getContext()->shop->getBaseURL(true);
$response = $this->sendRequest('/authorize', $header, $data);
if ($response['code'] !== 200 || empty($response['body']->uuid)) {
return false;
}
$this->saveToken($response['body']->uuid);
\Configuration::updateValue('EMARKETING_AUTHORIZE_JWT', $response['body']->jwt);
return $response['body']->jwt;
}
/**
* @param $token
* @return bool|string
*/
private function fetchJwt($token)
{
$header = "Authorization: " . $token;
$response = $this->sendRequest('/jwt', $header);
if (empty($response['body']->jwt)) {
return false;
}
return $response['body']->jwt;
}
/**
* @param $path
* @param string $additionalHeader
* @param array $data
* @return array
*/
private function sendRequest($path, $additionalHeader = "", $data = array())
{
$curl = new CurlRequest($this->gatewayUrl . $path);
$dataJson = "";
if (!empty($data)) {
$curl->setOption(CURLOPT_CUSTOMREQUEST, 'POST');
$dataJson = json_encode($data);
$curl->setOption(CURLOPT_POSTFIELDS, $dataJson);
}
$header = array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . \Tools::strlen($dataJson),
$additionalHeader
);
$curl->setOption(CURLOPT_HTTPHEADER, $header);
$response = $curl->execute();
$return = array(
'body' => json_decode($response),
'code' => $curl->getInfo(CURLINFO_HTTP_CODE)
);
$curl->close();
return $return;
}
}

View File

@@ -0,0 +1,68 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
use Emarketing\Service\Snippets\Snippets;
/**
* Class GoogleTracker
* @package Emarketing\Service
*/
class GoogleTracker extends Snippets
{
/**
* @param $globalSiteTracker
* @param $conversionTracker
* @return bool
* @throws \Exception
*/
public function saveTracker($globalSiteTracker, $conversionTracker)
{
$this->saveInConfiguration('GLOBAL_SITE_TRACKER', $globalSiteTracker);
$this->saveInConfiguration('CONVERSION_TRACKER', $conversionTracker);
return true;
}
/**
* @return string
*/
public function getGlobalSiteTracker()
{
$globalSiteTracker = $this->getFromConfiguration('GLOBAL_SITE_TRACKER');
return $globalSiteTracker;
}
/**
* @return string
* @throws \Exception
*/
public function getConversionTracker()
{
$conversionTracker = $this->getFromConfiguration('CONVERSION_TRACKER');
$order = new \Order(\Tools::getValue('id_order'));
$currency = new \Currency($order->id_currency);
$conversionTracker = preg_replace(
['/\'value\'\:[\s\S]+?,/', '/\'currency\'\:[\s\S]+?,/', '/\'transaction_id\'\:[\s\S]+?\'\'/'],
["'value': " . \Tools::ps_round($order->total_paid, 2) . ",", "'currency': '" . $currency->iso_code . "',", "'transaction_id': '" . $order->id . "',"],
$conversionTracker
);
return $conversionTracker;
}
}

View File

@@ -0,0 +1,191 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
use Emarketing\Service\Products\Variants;
use Emarketing\Service\Products\Images;
use Emarketing\Service\Products\Carriers;
use Emarketing\Service\Products\Features;
use Emarketing\Service\Products\PriceCalculation;
/**
* Class Products
* @package Emarketing\Service
*/
class Products
{
/**
* @var Carriers
*/
private $serviceCarriers;
/**
* @var Variants
*/
private $serviceVariants;
/**
* @var Images
*/
private $serviceImages;
/**
* @var Features
*/
private $serviceFeatures;
/**
* @var PriceCalculation
*/
private $priceCalculation;
/**
* @var \Context
*/
private $context;
/**
* Products constructor.
*/
public function __construct()
{
$this->serviceVariants = new Variants();
$this->serviceImages = new Images();
$this->serviceCarriers = new Carriers();
$this->serviceFeatures = new Features();
$this->priceCalculation = new PriceCalculation();
$this->context = \Context::getContext();
}
/**
* @param $offset
* @param $limit
* @param $idLang
* @param $idCountry
* @param $idCurrency
* @return array
* @throws \PrestaShopException
*/
public function buildProductsInformation($offset, $limit, $idLang, $idCountry, $idCurrency)
{
$productsData = array();
$productsData['settings'] = $this->getShopSettings();
$products = $this->getAllProducts($offset, $limit, $idLang);
foreach ($products as $product) {
$productData = $this->getProductDetails($product, $idLang, $idCountry, $idCurrency);
$productsData[$product['id_product']] = $productData;
}
return $productsData;
}
/**
* @param $offset
* @param $limit
* @param $idLang
* @return array
* @throws \Exception
*/
private function getAllProducts($offset, $limit, $idLang)
{
$products = \Product::getProducts($idLang, $offset, $limit, 'id_product', 'ASC', false, true);
if ($products === false) {
throw new \Exception('Error while fetching products.');
}
return $products;
}
/**
* @param $product
* @param $idLang
* @param $idCountry
* @return array
* @throws \PrestaShopException
*/
private function getProductDetails($product, $idLang, $idCountry, $idCurrency)
{
$productData = array();
$psProduct = new \Product($product['id_product'], false, $idLang);
$productData['details'] = $product;
$productData['additional'] = $this->getAdditionalInformation($psProduct, $idLang);
$productData['variants'] = $this->serviceVariants->buildVariantInformation($psProduct, $idLang, $idCurrency);
$productData['images'] = $this->serviceImages->buildImageInformation($psProduct, $idLang);
$productData['carriers'] = $this->serviceCarriers->buildCarrierInformation($idLang, $idCountry, $psProduct);
$productData['features'] = $this->serviceFeatures->buildFeatureInformation($psProduct, $idLang);
$productData['attributes'] = $psProduct->getAttributesGroups($idLang);
$productData['plugin_price'] = $this->priceCalculation->getFinalPrice($idCurrency, $psProduct->id, false);
$productData['plugin_sale_price'] = $this->priceCalculation->getFinalPrice($idCurrency, $psProduct->id, true);
return $productData;
}
/**
* @return array
*/
private function getShopSettings()
{
$settings = array();
$settings['dimension_unit'] = \Configuration::get('PS_DIMENSION_UNIT');
$settings['weight_unit'] = \Configuration::get('PS_WEIGHT_UNIT');
$settings['allow_ordering_oos'] = \Configuration::get('PS_ORDER_OUT_OF_STOCK');
return $settings;
}
/**
* @param \Product $psProduct
* @param $idLang
* @return array
* @throws \PrestaShopException
*/
public function getAdditionalInformation($psProduct, $idLang)
{
$additional = array();
$additional['categories'] = $psProduct->getCategories();
$additional['url'] = $this->context->link->getProductLink(
$psProduct->id,
null,
null,
null,
$idLang
);
$additional['quantity'] = \StockAvailable::getQuantityAvailableByProduct($psProduct->id);
$additional['availability'] = $psProduct->checkQty(1);
$additional['special_price'] = \SpecificPrice::getByProductId($psProduct->id);
return $additional;
}
}

View File

@@ -0,0 +1,120 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service\Products;
/**
* Class Carriers
* @package Emarketing\Service
*/
class Carriers
{
/**
* @param $idLang
* @param $idCountry
* @param \Product $psProduct
* @return array
* @throws \PrestaShopException
*/
public function buildCarrierInformation($idLang, $idCountry, $psProduct)
{
$idZone = \Country::getIdZone($idCountry);
$carriers = $this->getAllCarriers($idLang, $idZone);
foreach ($carriers as $carrierKey => $carrier) {
$carriers[$carrierKey]['price'] = $this->getShippingCost($idZone, $psProduct, $carrier);
}
return $carriers;
}
/**
* @param $idLang
* @param $idZone
* @return array
*/
private function getAllCarriers($idLang, $idZone)
{
return \Carrier::getCarriers($idLang, true, false, $idZone, null, \Carrier::ALL_CARRIERS);
}
/**
* @param $idZone
* @param \Product $psProduct
* @param $carrier
* @return bool|float
* @throws \PrestaShopException
*/
private function getShippingCost($idZone, $psProduct, $carrier)
{
$carrier = new \Carrier($carrier['id_carrier']);
$rangeTable = $carrier->getRangeTable();
if ($rangeTable == 'range_weight') {
$costs = $this->getCostsByWeight($carrier, $psProduct->weight, $idZone, $carrier->range_behavior);
}
if ($rangeTable == 'range_price') {
$costs = $this->getCostsByPrice($carrier, $psProduct->price, $idZone, $carrier->range_behavior);
}
if (empty($costs)) {
return false;
}
$address = \Address::initialize();
$carrierTax = $carrier->getTaxesRate($address);
if (!empty($carrierTax)) {
return $costs + ($costs * $carrierTax / 100);
}
return $costs;
}
/**
* @param \Carrier $carrier
* @param $productWeight
* @param $idZone
* @param $rangeBehaviour
* @return bool|float
*/
private function getCostsByWeight($carrier, $productWeight, $idZone, $rangeBehaviour)
{
$priceByWeightCheck = \Carrier::checkDeliveryPriceByWeight($carrier->id, $productWeight, $idZone);
if ($priceByWeightCheck || $rangeBehaviour == 0) {
return $carrier->getDeliveryPriceByWeight($productWeight, $idZone);
}
return false;
}
/**
* @param \Carrier $carrier
* @param $productPrice
* @param $idZone
* @param $rangeBehaviour
* @return bool|float
*/
private function getCostsByPrice($carrier, $productPrice, $idZone, $rangeBehaviour)
{
$priceByPriceCheck = \Carrier::checkDeliveryPriceByPrice($carrier->id, $productPrice, $idZone);
if ($priceByPriceCheck || $rangeBehaviour == 0) {
return $carrier->getDeliveryPriceByPrice($productPrice, $idZone);
}
return false;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service\Products;
/**
* Class Features
* @package Emarketing\Service
*/
class Features
{
/**
* @param \Product $psProduct
* @param $idLang
* @return array
* @throws \PrestaShopException
*/
public function buildFeatureInformation($psProduct, $idLang)
{
$features = $psProduct->getFeatures();
$featureData = array();
foreach ($features as $feature) {
$featureData[] = $this->getValue($feature['id_feature_value'], $idLang);
}
return $featureData;
}
/**
* @param $idValue
* @param $idLang
* @return \FeatureValue
* @throws \PrestaShopException
*/
private function getValue($idValue, $idLang)
{
$featureValue = new \FeatureValue($idValue, $idLang);
return get_object_vars($featureValue);
}
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service\Products;
/**
* Class Images
* @package Emarketing\Service
*/
class Images
{
/**
* @var \Context
*/
private $context;
/**
* Images constructor.
*/
public function __construct()
{
$this->context= \Context::getContext();
}
/**
* @param \Product $psProduct
* @param $idLang
* @return mixed
*/
public function buildImageInformation($psProduct, $idLang)
{
$images = array();
$productImages = $psProduct->getImages($idLang);
$images['product'] = $this->addUrls($productImages, $psProduct->link_rewrite);
$images['variants'] = array();
$variantImages = $psProduct->getCombinationImages($idLang);
if (empty($variantImages)) {
return $images;
}
foreach ($variantImages as $variantImage) {
$images['variants'][] = $this->addUrls($variantImage, $psProduct->link_rewrite);
}
return $images;
}
/**
* @param $images
* @param $linkRewrite
* @return array
*/
private function addUrls($images, $linkRewrite)
{
if (empty($images)) {
return array();
}
$completeImages = array();
foreach ($images as $image) {
$image['url'] = $this->context->link->getImageLink($linkRewrite, $image['id_image']);
$completeImages[] = $image;
}
return $completeImages;
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service\Products;
/**
* Class PriceCalculation
* @package Emarketing\Service
*/
class PriceCalculation
{
/**
* @var \Context
*/
private $context;
/**
* Images constructor.
*/
public function __construct()
{
$this->context= \Context::getContext();
}
/**
* @param $idCurrency
* @param $idProduct
* @param $saleIncluded
* @param $idVariant
* @return string
*/
public function getFinalPrice($idCurrency, $idProduct, $saleIncluded = false, $idVariant = false)
{
$specificPrice = \SpecificPrice::getSpecificPrice(
$idProduct,
$this->context->shop->id,
$idCurrency,
$this->context->country->id,
1,
1,
$idVariant
);
return \Product::priceCalculation(
$this->context->shop->id,
$idProduct,
$idVariant,
$this->context->country->id,
0,
0,
$idCurrency,
1,
1,
true,
2,
false,
$saleIncluded,
true,
$specificPrice,
true
);
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service\Products;
use Emarketing\Service\Products\PriceCalculation;
/**
* Class Variants
* @package Emarketing\Service
*/
class Variants
{
/**
* @var PriceCalculation
*/
private $priceCalculation;
/**
* @var \Context
*/
private $context;
/**
* Variants constructor.
*/
public function __construct()
{
$this->priceCalculation = new PriceCalculation();
$this->context = \Context::getContext();
}
/**
* @param \Product $psProduct
* @param $idLang
* @param $idCurrency
* @return mixed
* @throws \PrestaShopException
*/
public function buildVariantInformation($psProduct, $idLang, $idCurrency)
{
$variants = $psProduct->getAttributesResume($idLang);
if (empty($variants)) {
return array();
}
foreach ($variants as $variantKey => $variant) {
$variants[$variantKey]['url'] = $this->getUrl($psProduct->id, $idLang, $variant['id_product_attribute']);
$variants[$variantKey]['special_price'] = \SpecificPrice::getByProductId(
$psProduct->id,
$variant['id_product_attribute']
);
$variants[$variantKey]['availability'] = $psProduct->checkQty(1);
$variants[$variantKey]['plugin_price'] = $this->priceCalculation->getFinalPrice($idCurrency, $psProduct->id, false, $variant['id_product_attribute']);
$variants[$variantKey]['plugin_sale_price'] = $this->priceCalculation->getFinalPrice($idCurrency, $psProduct->id, true, $variant['id_product_attribute']);
}
return $variants;
}
/**
* @param $idProduct
* @param $idLang
* @param $idVariant
* @return string
* @throws \PrestaShopException
*/
private function getUrl($idProduct, $idLang, $idVariant)
{
return $this->context->link->getProductLink(
$idProduct,
null,
null,
null,
$idLang,
null,
$idVariant
);
}
}

View File

@@ -0,0 +1,11 @@
<?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;

View File

@@ -0,0 +1,55 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service\Snippets;
/**
* Class Snippets
* @package Emarketing\Service\Snippets
*/
class Snippets
{
/**
* @param $key
* @param $code
* @return bool
* @throws \Exception
*/
protected function saveInConfiguration($key, $code)
{
if (is_null($code) || !is_string($code)) {
return false;
}
$code = htmlentities($code, ENT_QUOTES);
$saved = \Configuration::updateValue('EMARKETING_' . $key, $code);
if (!$saved) {
throw new \Exception('Error while saving ' . $key);
}
return true;
}
/**
* @param $key
* @return string
*/
protected function getFromConfiguration($key)
{
$code = \Configuration::get('EMARKETING_' . $key);
return html_entity_decode($code, ENT_QUOTES);
}
}

View File

@@ -0,0 +1,11 @@
<?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;

View File

@@ -0,0 +1,62 @@
<?php
/**
* NOTICE OF LICENSE
*
* This file is licenced under the GNU General Public License, version 3 (GPL-3.0).
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* @author emarketing www.emarketing.com <integrations@emarketing.com>
* @copyright 2020 emarketing AG
* @license https://opensource.org/licenses/GPL-3.0 GNU General Public License version 3
*/
namespace Emarketing\Service;
/**
* Class Verification
* @package Emarketing\Service
*/
class Verification
{
/**
* @var string
*/
private $configName = 'EMARKETING_VERIFICATION_TAG';
/**
* @param $tag
* @return bool
* @throws \Exception
*/
public function saveTag($tag)
{
if (is_null($tag) || !is_string($tag)) {
$tag = "";
}
$tag = htmlentities($tag, ENT_QUOTES);
$saved = \Configuration::updateValue($this->configName, $tag);
if (!$saved) {
throw new \Exception('Error while saving tag.');
}
return true;
}
/**
* @return string
*/
public function getTag()
{
$tag = \Configuration::get($this->configName);
if (empty($tag)) {
return "";
}
return html_entity_decode($tag);
}
}

View File

@@ -0,0 +1,11 @@
<?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;

View File

@@ -0,0 +1,11 @@
<?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;