Wtyczka PrestaShop

Wersja v1.0.0 · pełny, otwarty kod źródłowy poniżej.

Instalacja

  1. W panelu PrestaShop 1.7/8: Moduły → Menedżer modułów → Załaduj moduł, wybierz ZIP.
  2. Konfiguruj: wklej klucz i sekret API z panelu sklepu, wybierz status wyzwalający (domyślnie „Dostarczone”) i liczbę dni; kliknij Testuj połączenie.
  3. Włącz „Widżet w stopce” albo wstaw kod widżetu z panelu w szablonie.

Pliki

legalniwsieci/index.php (0,0 KB)

<?php
header('Location: ../');
exit;

legalniwsieci/legalniwsieci.php (6,5 KB)

<?php
/**
 * Legalni w Sieci — zweryfikowane opinie (PrestaShop 1.7 / 8.x)
 * Wysyła zamówienia o statusie "Dostarczone" (lub wybranym) do API Legalni w Sieci (HMAC) i wyświetla widżet gwiazdek.
 * @version 1.0.0
 * @license AFL-3.0
 */
if (!defined('_PS_VERSION_')) { exit; }

class Legalniwsieci extends Module
{
    const API = 'https://legalniwsieci.pl/api/v1/';

    public function __construct()
    {
        $this->name = 'legalniwsieci';
        $this->tab = 'advertising_marketing';
        $this->version = '1.0.0';
        $this->author = 'Legalni w Sieci';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);
        $this->bootstrap = true;
        parent::__construct();
        $this->displayName = 'Legalni w Sieci — zweryfikowane opinie';
        $this->description = 'Jednorazowe linki do opinii po zakupie i widżet zaufania.';
    }

    public function install()
    {
        return parent::install() && $this->registerHook('actionOrderStatusPostUpdate') && $this->registerHook('displayFooter')
            && Configuration::updateValue('LWS_DELAY_DAYS', 7) && Configuration::updateValue('LWS_ORDER_STATE', (int) Configuration::get('PS_OS_DELIVERED'));
    }

    public function uninstall()
    {
        foreach (array('LWS_API_KEY', 'LWS_API_SECRET', 'LWS_DELAY_DAYS', 'LWS_ORDER_STATE', 'LWS_WIDGET') as $k) { Configuration::deleteByName($k); }
        return parent::uninstall();
    }

    public function getContent()
    {
        $out = '';
        if (Tools::isSubmit('submitLws')) {
            Configuration::updateValue('LWS_API_KEY', pSQL(trim(Tools::getValue('LWS_API_KEY'))));
            if (Tools::getValue('LWS_API_SECRET')) { Configuration::updateValue('LWS_API_SECRET', pSQL(trim(Tools::getValue('LWS_API_SECRET')))); }
            Configuration::updateValue('LWS_DELAY_DAYS', max(0, min(60, (int) Tools::getValue('LWS_DELAY_DAYS'))));
            Configuration::updateValue('LWS_ORDER_STATE', (int) Tools::getValue('LWS_ORDER_STATE'));
            Configuration::updateValue('LWS_WIDGET', (int) Tools::getValue('LWS_WIDGET'));
            $out .= $this->displayConfirmation('Zapisano.');
        }
        if (Tools::isSubmit('testLws')) {
            $r = $this->request('ping', '');
            $out .= $r['code'] === 200 ? $this->displayConfirmation('Połączenie OK.') : $this->displayError('Błąd ' . (int) $r['code'] . ': ' . Tools::safeOutput($r['body']));
        }
        $states = array();
        foreach (OrderState::getOrderStates((int) $this->context->language->id) as $s) { $states[] = array('id' => $s['id_order_state'], 'name' => $s['name']); }
        $form = array('form' => array('legend' => array('title' => 'Legalni w Sieci v' . $this->version), 'input' => array(
            array('type' => 'text', 'label' => 'Klucz API', 'name' => 'LWS_API_KEY', 'desc' => 'Z panelu: https://legalniwsieci.pl/panel'),
            array('type' => 'password', 'label' => 'Sekret API', 'name' => 'LWS_API_SECRET', 'desc' => 'Pozostaw puste, aby nie zmieniać.'),
            array('type' => 'text', 'label' => 'Wyślij prośbę po (dniach)', 'name' => 'LWS_DELAY_DAYS'),
            array('type' => 'select', 'label' => 'Status zamówienia wyzwalający', 'name' => 'LWS_ORDER_STATE', 'options' => array('query' => $states, 'id' => 'id', 'name' => 'name')),
            array('type' => 'switch', 'label' => 'Widżet w stopce', 'name' => 'LWS_WIDGET', 'is_bool' => true, 'values' => array(array('id' => 'on', 'value' => 1, 'label' => 'Tak'), array('id' => 'off', 'value' => 0, 'label' => 'Nie'))),
        ), 'submit' => array('title' => 'Zapisz', 'name' => 'submitLws'), 'buttons' => array(array('type' => 'submit', 'title' => 'Testuj połączenie', 'name' => 'testLws', 'icon' => 'process-icon-refresh'))));
        $h = new HelperForm();
        $h->module = $this; $h->name_controller = $this->name; $h->token = Tools::getAdminTokenLite('AdminModules');
        $h->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name; $h->default_form_language = (int) $this->context->language->id;
        $h->fields_value = array('LWS_API_KEY' => Configuration::get('LWS_API_KEY'), 'LWS_API_SECRET' => '', 'LWS_DELAY_DAYS' => Configuration::get('LWS_DELAY_DAYS'), 'LWS_ORDER_STATE' => Configuration::get('LWS_ORDER_STATE'), 'LWS_WIDGET' => Configuration::get('LWS_WIDGET'));
        return $out . $h->generateForm(array($form));
    }

    /** Podpis: hex(HMAC-SHA256(secret, timestamp + "." + body)) */
    private function request($endpoint, $body)
    {
        $key = Configuration::get('LWS_API_KEY'); $secret = Configuration::get('LWS_API_SECRET');
        if (!$key || !$secret) { return array('code' => 0, 'body' => 'Brak klucza/sekretu'); }
        $ts = (string) time();
        $ch = curl_init(self::API . $endpoint);
        curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'X-LWS-Key: ' . $key, 'X-LWS-Timestamp: ' . $ts, 'X-LWS-Signature: ' . hash_hmac('sha256', $ts . '.' . $body, $secret), 'User-Agent: lws-prestashop/' . $this->version)));
        if ($body !== '') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); }
        $res = curl_exec($ch); $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
        return array('code' => $code, 'body' => (string) $res);
    }

    public function hookActionOrderStatusPostUpdate($params)
    {
        if (empty($params['newOrderStatus']) || (int) $params['newOrderStatus']->id !== (int) Configuration::get('LWS_ORDER_STATE')) { return; }
        $order = new Order((int) $params['id_order']); $customer = new Customer((int) $order->id_customer);
        if (!Validate::isLoadedObject($order) || !Validate::isLoadedObject($customer)) { return; }
        $payload = json_encode(array('order_id' => (string) $order->reference, 'email' => (string) $customer->email, 'customer_name' => (string) $customer->firstname, 'delay_days' => (int) Configuration::get('LWS_DELAY_DAYS')));
        $r = $this->request('orders', $payload);
        if ($r['code'] !== 200 && $r['code'] !== 201) { PrestaShopLogger::addLog('Legalni w Sieci: błąd ' . $r['code'] . ' dla zamówienia ' . $order->reference, 2); }
    }

    public function hookDisplayFooter()
    {
        $key = Configuration::get('LWS_API_KEY');
        if (!Configuration::get('LWS_WIDGET') || !$key) { return ''; }
        return '<div class="lws-widget" data-lws-key="' . htmlspecialchars($key, ENT_QUOTES) . '"></div><script src="https://legalniwsieci.pl/widget.js" async></script>';
    }
}