Skip to content

Commit

Permalink
Merge pull request #3 from Invertus/SMAR-16/ajax-error-handling
Browse files Browse the repository at this point in the history
SMAR-16/ Ajax controller error handling improvements
  • Loading branch information
GytisZum authored Nov 17, 2023
2 parents 5b4fe7a + e289f75 commit 08c2124
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 55 deletions.
152 changes: 101 additions & 51 deletions controllers/admin/AdminSmartsuppAjax.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,144 @@
/**
* Smartsupp Live Chat integration module.
*
* @package Smartsupp
* @author Smartsupp <[email protected]>
* @link http://www.smartsupp.com
* @copyright 2016 Smartsupp.com
* @license GPL-2.0+
* @package Smartsupp
* @link http://www.smartsupp.com
*
* Plugin Name: Smartsupp Live Chat
* Plugin URI: http://www.smartsupp.com
* Description: Adds Smartsupp Live Chat code to PrestaShop.
* Version: 2.2.0
* Text Domain: smartsupp
* Author: Smartsupp
* Author URI: http://www.smartsupp.com
* Text Domain: smartsupp
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

use \Smartsupp\Auth\Api;
use Smartsupp\Auth\Api;
use Smartsupp\LiveChat\Validator\UserCredentialsValidator;

class AdminSmartsuppAjaxController extends ModuleAdminController
{
const FILE_NAME = 'AdminSmartsuppAjaxController';
public $ssl = true;

private $partnerKey = 'h4w6t8hln9';
private $response = [];
private $api;

const LOGIN_ACTION = 'login';
const CREATE_ACTION = 'create';
const DEACTIVATE_ACTION = 'deactivate';

public function init()
{
$api = new Api();

switch (Tools::getValue('action')) {
case 'login':
$this->response = $api->login([
'email' => Tools::getValue('email'),
'password' => Tools::getValue('password'),
'platform' => 'Prestashop ' . _PS_VERSION_,
]);
$this->updateCredentials();
break;
case 'create':
$this->response = $api->create([
'email' => Tools::getValue('email'),
'password' => Tools::getValue('password'),
'partnerKey' => $this->partnerKey,
'consentTerms' => 1,
'platform' => 'Prestashop ' . _PS_VERSION_,
]);
$this->updateCredentials();
break;
case 'deactivate':
Configuration::updateValue('SMARTSUPP_KEY', '');
Configuration::updateValue('SMARTSUPP_EMAIL', '');
break;
$validator = new UserCredentialsValidator($this->module);

$validator->validate(Tools::getAllValues());
$action = Tools::getValue('action');

if ($validator->getError() && $action !== self::DEACTIVATE_ACTION) {
$this->handleError($validator->getMessage(), $validator->getError());
}

header('Content-Type: application/json');
try {
$this->api = new Api();

$responseData = [
'key' => Configuration::get('SMARTSUPP_KEY'),
'email' => Configuration::get('SMARTSUPP_EMAIL'),
'error' => isset($this->response['error']) ? $this->response['error'] : null,
'message' => isset($this->response['message']) ? $this->response['message'] : null,
];
switch ($action) {
case self::LOGIN_ACTION:
$this->handleLoginAction();
break;
case self::CREATE_ACTION:
$this->handleCreateAction();
break;
case self::DEACTIVATE_ACTION:
$this->handleDeactivateAction();
break;
default:
throw new Exception('Invalid action');
}
} catch (Exception $e) {
$this->handleError($e->getMessage());
}

$responseData = array_filter($responseData, function ($val) {
return $val !== null;
});
$this->sendResponse();
}

die(json_encode($responseData));
private function handleLoginAction()
{
$this->response = $this->api->login([
'email' => Tools::getValue('email'),
'password' => Tools::getValue('password'),
'platform' => 'Prestashop ' . _PS_VERSION_,
]);

$this->updateCredentials();
}

private function handleCreateAction()
{
$this->response = $this->api->create([
'email' => Tools::getValue('email'),
'password' => Tools::getValue('password'),
'partnerKey' => $this->partnerKey,
'consentTerms' => 1,
'platform' => 'Prestashop ' . _PS_VERSION_,
]);

$this->updateCredentials();
}

private function handleDeactivateAction()
{
Configuration::updateValue('SMARTSUPP_KEY', '');
Configuration::updateValue('SMARTSUPP_EMAIL', '');

$this->sendResponse();
}

/**
* @return void
*/
private function updateCredentials()
{
if (isset($this->response['account']['key'])) {
Configuration::updateValue('SMARTSUPP_KEY', $this->response['account']['key']);
Configuration::updateValue('SMARTSUPP_EMAIL', Tools::getValue('email'));

return;
}

$this->response['error'] = isset($this->response['error']) ? $this->response['error'] : $this->module->l('Unknown Error Occurred', self::FILE_NAME);
$this->response['message'] = isset($this->response['message']) ? $this->response['message'] : $this->module->l('Unknown Error Occurred', self::FILE_NAME);
if (isset($this->response['error'])) {
$this->sendResponse();
}

Configuration::updateValue('SMARTSUPP_KEY', '');
Configuration::updateValue('SMARTSUPP_EMAIL', '');
$this->handleError($this->module->l('Unknown error occurred while processing your request.', self::FILE_NAME));
}

private function handleError($message, $error = 'error')
{
$this->response['key'] = Configuration::get('SMARTSUPP_KEY');
$this->response['email'] = Configuration::get('SMARTSUPP_EMAIL');

$this->response['error'] = $error;
$this->response['message'] = $message;

die(json_encode($this->response));
}

private function sendResponse()
{
header('Content-Type: application/json');

$responseData = [
'key' => Configuration::get('SMARTSUPP_KEY'),
'email' => Configuration::get('SMARTSUPP_EMAIL'),
'error' => isset($this->response['error']) ? $this->response['error'] : null,
'message' => isset($this->response['message']) ? $this->response['message'] : null,
];

$responseData = array_filter($responseData, function ($val) {
return $val !== null;
});

die(json_encode($responseData));
}
}
}
5 changes: 1 addition & 4 deletions smartsupp.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public function __construct()
$this->displayName = $this->l('Smartsupp Live Chat & AI Chatbots');
$this->description = $this->l('Smartsupp is your personal online shopping assistant, built to increase conversion rates and sales via visitor engagement in real-time, at the right time.');

$confirm = $this->l('Are you sure you want to uninstall Smartsupp Live Chat? ');
$confirm .= $this->l('You will lose all the data related to this module.');

$this->confirmUninstall = $this->l($confirm);
$this->confirmUninstall = $this->l('Are you sure you want to uninstall Smartsupp Live Chat? You will lose all the data related to this module.');

if (version_compare(_PS_VERSION_, '1.5', '<')) {
include _PS_MODULE_DIR_ . $this->name . '/backward_compatibility/backward.php';
Expand Down
112 changes: 112 additions & 0 deletions src/Validator/UserCredentialsValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Smartsupp Live Chat integration module.
*
* @package Smartsupp
* @author Smartsupp <[email protected]>
* @link http://www.smartsupp.com
* @copyright 2016 Smartsupp.com
* @license GPL-2.0+
*
* Plugin Name: Smartsupp Live Chat
* Plugin URI: http://www.smartsupp.com
* Description: Adds Smartsupp Live Chat code to PrestaShop.
* Version: 2.2.0
* Author: Smartsupp
* Author URI: http://www.smartsupp.com
* Text Domain: smartsupp
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

namespace Smartsupp\LiveChat\Validator;

class UserCredentialsValidator
{
const FILE_NAME = 'UserCredentialsValidator';

/**
* @var \Smartsupp $module
*/
private $module;

/**
* @var string
*/
private $error = '';

/**
* @var string
*/
private $message = '';

public function __construct($module)
{
$this->module = $module;
}

/**
* @param array $data
*
* @return void
*/
public function validate($data)
{
$email = isset($data['email']) ? $data['email'] : '';
$password = isset($data['password']) ? $data['password'] : '';

if (empty($email) || empty($password)) {
$this->error = $this->module->l('Empty values provided', self::FILE_NAME);
$this->message = $this->module->l('Email and password fields can not be empty', self::FILE_NAME);

return;
}

if (!$this->validateEmail($email)) {
$this->error = $this->module->l('Invalid email format', self::FILE_NAME);
$this->message = $this->module->l('Invalid email address', self::FILE_NAME);

return;
}

// Validate password
if (!$this->validatePassword($password)) {
$this->error = $this->module->l('Password length is invalid', self::FILE_NAME);
$this->message = $this->module->l('Password must be between 6-255 characters long', self::FILE_NAME);
}
}

/**
* @return string
*/
public function getError()
{
return $this->error;
}

/**
* @return string
*/
public function getMessage()
{
return $this->message;
}

/**
* @param string $email
* @return array|false
*/
private function validateEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}

/**
* @param string $password
* @return bool
*/
private function validatePassword($password)
{
return strlen($password) >= 6 && strlen($password) <= 255;
}
}

0 comments on commit 08c2124

Please sign in to comment.