Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PW-1556 execute notifications #21

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 95 additions & 37 deletions adyen.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,16 @@

class Adyen extends \PaymentModule
{
const TEST = 'test';
const LIVE = 'live';
const CHECKOUT_COMPONENT_JS_TEST = 'https://checkoutshopper-test.adyen.com/checkoutshopper/sdk/3.0.0/adyen.js';
const CHECKOUT_COMPONENT_JS_LIVE = 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/3.0.0/adyen.js';
const CHECKOUT_COMPONENT_CSS_TEST = 'https://checkoutshopper-test.adyen.com/checkoutshopper/sdk/3.0.0/adyen.css';
const CHECKOUT_COMPONENT_CSS_LIVE = 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/3.0.0/adyen.css';
const VERSION = '1.0.1';
const MODULE_NAME = 'adyen-prestashop';


/**
* Adyen constructor.
*
* @throws \Adyen\AdyenException
*/
public function __construct()
{
$this->name = 'adyen';
$this->tab = 'payments_gateways';
$this->version = self::VERSION;
$this->version = \Adyen\PrestaShop\service\Configuration::VERSION;
$this->author = 'Adyen';
$this->bootstrap = true;
$this->display = 'view';
Expand Down Expand Up @@ -80,12 +75,13 @@ public function __construct()
*/
public function install()
{

if (version_compare(_PS_VERSION_, '1.5', '<')) {
$this->_errors[] = $this->l('Sorry, this module is not compatible with your version.');
return false;
}

$this->updateCronJobToken();

if ($this->helper_data->isPrestashop16()) {
// Version 1.6 requires a different set of hooks
if (
Expand All @@ -99,6 +95,7 @@ public function install()
&& $this->registerHook('moduleRoutes')
// the table for notifications from Adyen needs to be both in install and upgrade
&& $this->createAdyenNotificationTable()
&& $this->installTab()
) {
return true;
} else {
Expand All @@ -109,6 +106,7 @@ public function install()

// install hooks for version 1.7 or higher
return parent::install()
&& $this->installTab()
&& $this->registerHook('header')
&& $this->registerHook('orderConfirmation')
&& $this->registerHook('paymentOptions')
Expand All @@ -119,6 +117,22 @@ public function install()
&& $this->createAdyenNotificationTable();
}

/**
* @param string $token
* @return bool
*/
public function updateCronJobToken($token = '')
{
if (empty($token)) {
$token = $this->helper_data->encrypt(\Tools::getShopDomainSsl().time());
}

return \Configuration::updateValue('ADYEN_CRONJOB_TOKEN', $this->helper_data->encrypt($token));
}

/**
* @return bool
*/
public function createAdyenNotificationTable()
{
$db = Db::getInstance();
Expand Down Expand Up @@ -146,9 +160,7 @@ public function createAdyenNotificationTable()
KEY `ADYEN_NOTIFICATION_MERCHANT_REFERENCE_EVENT_CODE` (`merchant_reference`,`event_code`)
) ENGINE=' . _MYSQL_ENGINE_ . ' AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT=\'Adyen Notifications\'';

$db->execute($query);

return true;
return $db->execute($query);
}

/**
Expand All @@ -161,10 +173,45 @@ public function uninstall()
// TODO: delete adyen configurations (api-key)
$db = Db::getInstance();
/** @noinspection SqlWithoutWhere SqlResolve */
$db->execute('DELETE FROM `' . _DB_PREFIX_ . 'adyen_notification`');
$db->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'adyen_notification`');

return parent::uninstall();
return parent::uninstall() && $this->uninstallTab();
}

/**
* @return mixed
*/
public function installTab()
{
$tab = new Tab();
// invisible tab
$tab->id_parent = -1;
$tab->active = 1;

$tab->name = array();
foreach (\Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'Adyen Prestashop Cron';
}

$tab->class_name = 'AdminAdyenPrestashopCron';
$tab->module = $this->name;

return $tab->add();
}

/**
* @return bool
*/
public function uninstallTab()
{
$id_tab = (int)\Tab::getIdFromClassName('AdminAdyenPrestashopCron');

if ($id_tab) {
$tab = new \Tab($id_tab);
return $tab->delete();
}

return false;
}


Expand All @@ -183,11 +230,11 @@ public function getContent()
$notification_username = (string)\Tools::getValue('ADYEN_NOTI_USERNAME');
$notification_password = (string)\Tools::getValue('ADYEN_NOTI_PASSWORD');
$notification_hmac = (string)\Tools::getValue('ADYEN_NOTI_HMAC');
$cron_job_token = \Tools::getValue('ADYEN_CRONJOB_TOKEN');
$api_key_test = \Tools::getValue('ADYEN_APIKEY_TEST');
$api_key_live = \Tools::getValue('ADYEN_APIKEY_LIVE');
$live_endpoint_url_prefix = (string)\Tools::getValue('ADYEN_LIVE_ENDPOINT_URL_PREFIX');


// validating the input
if (!$merchant_account || empty($merchant_account) || !\Validate::isGenericName($merchant_account)) {
$output .= $this->displayError($this->l('Invalid Configuration value for Merchant Account'));
Expand All @@ -201,19 +248,20 @@ public function getContent()
$output .= $this->displayError($this->l('Invalid Configuration value for Notification Password'));
}

if (empty($notification_hmac) || !\Validate::isGenericName($notification_hmac)) {
$output .= $this->displayError($this->l('Invalid Configuration value for Notification HMAC Key'));
}


if ($output == null) {

\Configuration::updateValue('ADYEN_MERCHANT_ACCOUNT', $merchant_account);
\Configuration::updateValue('ADYEN_MODE', $mode);
\Configuration::updateValue('ADYEN_NOTI_USERNAME', $notification_username);
\Configuration::updateValue('ADYEN_NOTI_PASSWORD', $notification_password);
\Configuration::updateValue('ADYEN_NOTI_HMAC', $notification_hmac);
\Configuration::updateValue('ADYEN_LIVE_ENDPOINT_URL_PREFIX', $live_endpoint_url_prefix);

if (!empty($notification_hmac)) {
\Configuration::updateValue('ADYEN_NOTI_HMAC', $notification_hmac);
}
if (!empty($cron_job_token)) {
\Configuration::updateValue('ADYEN_CRONJOB_TOKEN', $this->helper_data->encrypt($cron_job_token));
}
if (!empty($api_key_test)) {
\Configuration::updateValue('ADYEN_APIKEY_TEST', $this->helper_data->encrypt($api_key_test));
}
Expand Down Expand Up @@ -294,13 +342,21 @@ public function displayForm()
'hint' => $this->l('Must correspond to the notification password in the Adyen Backoffice under Settings => Notifications')
),
array(
'type' => 'text',
'type' => 'password',
'label' => $this->l('HMAC key for notifications'),
'name' => 'ADYEN_NOTI_HMAC',
'size' => 20,
'required' => true,
'required' => false,
'hint' => $this->l('Must correspond to the notification HMAC Key in the Adyen Backoffice under Settings => Notifications => Additional Settings => HMAC Key (HEX Encoded)')
),
array(
'type' => 'text',
cyattilakiss marked this conversation as resolved.
Show resolved Hide resolved
'desc' => $this->l('Your adyen cron job processor\'s url includes this secure token . Your URL looks like: ' . _PS_BASE_URL_ . '/' . basename(_PS_ADMIN_DIR_) . '/index.php?fc=module&controller=AdminAdyenPrestashopCron&token=' . $this->helper_data->decrypt(\Configuration::get('ADYEN_CRONJOB_TOKEN'))),
'label' => $this->l('Secure token for cron job'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the other options discuss wioth grant if we should remove the required label

'name' => 'ADYEN_CRONJOB_TOKEN',
'size' => 20,
'required' => false
),
array(
'type' => 'password',
'label' => $this->l('API key for Test'),
Expand Down Expand Up @@ -366,7 +422,8 @@ public function displayForm()
$mode = (string)\Tools::getValue('ADYEN_MODE');
$notification_username = (string)\Tools::getValue('ADYEN_NOTI_USERNAME');
$notification_password = (string)\Tools::getValue('ADYEN_NOTI_PASSWORD');
$notification_HMAC = (string)\Tools::getValue('ADYEN_NOTI_HMAC');
$notification_HMAC = $this->hashing->hash(\Tools::getValue('ADYEN_NOTI_HMAC', _COOKIE_KEY_));
$cron_job_token = \Tools::getValue('ADYEN_CRONJOB_TOKEN');
$live_endpoint_url_prefix = (string)\Tools::getValue('ADYEN_LIVE_ENDPOINT_URL_PREFIX');
$api_key_test = $this->hashing->hash(\Tools::getValue('ADYEN_APIKEY_TEST'), _COOKIE_KEY_);
$api_key_live = $this->hashing->hash(\Tools::getValue('ADYEN_APIKEY_LIVE'), _COOKIE_KEY_);
Expand All @@ -375,7 +432,8 @@ public function displayForm()
$mode = \Configuration::get('ADYEN_MODE');
$notification_username = \Configuration::get('ADYEN_NOTI_USERNAME');
$notification_password = \Configuration::get('ADYEN_NOTI_PASSWORD');
$notification_HMAC = \Configuration::get('ADYEN_NOTI_HMAC');
$notification_HMAC = $this->hashing->hash(\Configuration::get('ADYEN_NOTI_HMAC'), _COOKIE_KEY_);
$cron_job_token = $this->helper_data->decrypt(\Configuration::get('ADYEN_CRONJOB_TOKEN'));
$live_endpoint_url_prefix = \Configuration::get('ADYEN_LIVE_ENDPOINT_URL_PREFIX');
$api_key_test = $this->hashing->hash(\Configuration::get('ADYEN_APIKEY_TEST'),
_COOKIE_KEY_);;
Expand All @@ -389,6 +447,7 @@ public function displayForm()
$helper->fields_value['ADYEN_NOTI_USERNAME'] = $notification_username;
$helper->fields_value['ADYEN_NOTI_PASSWORD'] = $notification_password;
$helper->fields_value['ADYEN_NOTI_HMAC'] = $notification_HMAC;
$helper->fields_value['ADYEN_CRONJOB_TOKEN'] = $cron_job_token;
$helper->fields_value['ADYEN_APIKEY_TEST'] = $api_key_test;
$helper->fields_value['ADYEN_APIKEY_LIVE'] = $api_key_live;
$helper->fields_value['ADYEN_LIVE_ENDPOINT_URL_PREFIX'] = $live_endpoint_url_prefix;
Expand Down Expand Up @@ -417,13 +476,13 @@ public function hookHeader()
if ($this->helper_data->isDemoMode()) {

if (version_compare(_PS_VERSION_, '1.7', '<')) {
$this->context->controller->addJS(self::CHECKOUT_COMPONENT_JS_TEST);
$this->context->controller->addCSS(self::CHECKOUT_COMPONENT_CSS_TEST);
$this->context->controller->addJS(\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_JS_TEST);
$this->context->controller->addCSS(\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_CSS_TEST);
$this->context->controller->addJS($this->_path . 'views/js/threeds2-js-utils.js');
} else {
$this->context->controller->registerJavascript(
'component', // Unique ID
self::CHECKOUT_COMPONENT_JS_TEST, // JS path
\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_JS_TEST, // JS path
array('server' => 'remote', 'position' => 'bottom', 'priority' => 150) // Arguments
);
$this->context->controller->registerJavascript(
Expand All @@ -433,7 +492,7 @@ public function hookHeader()
);
$this->context->controller->registerStylesheet(
'stylecheckout', // Unique ID
self::CHECKOUT_COMPONENT_CSS_TEST, // CSS path
\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_CSS_TEST, // CSS path
array('server' => 'remote', 'position' => 'bottom', 'priority' => 150) // Arguments
);

Expand All @@ -443,13 +502,13 @@ public function hookHeader()
} else {

if (version_compare(_PS_VERSION_, '1.7', '<')) {
$this->context->controller->addJS(self::CHECKOUT_COMPONENT_JS_LIVE);
$this->context->controller->addCSS(self::CHECKOUT_COMPONENT_CSS_LIVE);
$this->context->controller->addJS(\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_JS_LIVE);
$this->context->controller->addCSS(\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_CSS_LIVE);
$this->context->controller->addJS($this->_path . 'views/js/threeds2-js-utils.js');
} else {
$this->context->controller->registerJavascript(
'component', // Unique ID
self::CHECKOUT_COMPONENT_JS_LIVE, // JS path
\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_JS_LIVE, // JS path
array('server' => 'remote', 'position' => 'bottom', 'priority' => 150) // Arguments
);
$this->context->controller->registerJavascript(
Expand All @@ -459,7 +518,7 @@ public function hookHeader()
);
$this->context->controller->registerStylesheet(
'stylecheckout', // Unique ID
self::CHECKOUT_COMPONENT_CSS_LIVE, // CSS path
\Adyen\PrestaShop\service\Configuration::CHECKOUT_COMPONENT_CSS_LIVE, // CSS path
array('server' => 'remote', 'position' => 'bottom', 'priority' => 150) // Arguments
);
}
Expand Down Expand Up @@ -567,5 +626,4 @@ public function hookPaymentReturn()
{
return;
}

}
5 changes: 3 additions & 2 deletions controllers/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace Adyen\PrestaShop\controllers;


use PrestaShopException;

abstract class FrontController extends \ModuleFrontController
Expand All @@ -41,9 +40,11 @@ abstract class FrontController extends \ModuleFrontController
protected function ajaxRender($value = null, $controller = null, $method = null)
{
if ($this->helperData->isPrestashop16()) {
$this->ajax = true;
parent::ajaxDie($value, $controller, $method);
} else {
parent::ajaxRender($value, $controller, $method);
exit;
}
}
}
}
67 changes: 67 additions & 0 deletions controllers/admin/AdminAdyenPrestashopCronController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen PrestaShop module
*
* Copyright (c) 2019 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*/

class AdminAdyenPrestashopCronController extends \ModuleAdminController
{
/**
* @var bool
*/
public $ssl = true;

/**
* AdminAdyenPrestashopCronController constructor.
*
* @throws \Adyen\AdyenException
*/
public function __construct()
{

$adyenHelperFactory = new \Adyen\PrestaShop\service\helper\DataFactory();
$this->helperData = $adyenHelperFactory->createAdyenHelperData(
\Configuration::get('ADYEN_MODE'),
_COOKIE_KEY_
);

if (\Tools::getValue('token') != $this->helperData->decrypt(\Configuration::get('ADYEN_CRONJOB_TOKEN'))) {
die('Invalid token');
}

$this->context = \Context::getContext();

parent::__construct();
$this->postProcess();
die();
}

/**
*
*/
public function postProcess()
{
$notificationProcessor = new \Adyen\PrestaShop\service\notification\NotificationProcessor(
$this->helperData,
Db::getInstance()

);
$notificationProcessor->doPostProcess();
}
}
Loading