Skip to content

Commit 0bf7ee0

Browse files
committed
CE-Phoenix distribution: Added logic to validate the public and private keys upon saving.
1 parent 49d9d45 commit 0bf7ee0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4321
-14
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
4141
```
4242
require_once('includes/modules/payment/paylike/errors.php');
4343
```
44-
Before 'require('includes/application_top.php');' line;
44+
Before require('includes/application_top.php'); line;
4545
4646
7.2.Add:
4747
```
4848
require_once('includes/modules/payment/paylike/validate.php');
4949
```
50-
After 'reset($HTTP_POST_VARS['configuration']);' line;
50+
After reset($HTTP_POST_VARS['configuration']); line;
5151
52-
7.3.Replace 'tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");' line with:
52+
7.3.Replace tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'"); line with:
5353
```
5454
if(sizeof($errors) === 0 || array_search($key, $validation_keys) === FALSE){
5555
tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");
@@ -62,13 +62,13 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
6262
tep_redirect(tep_href_link(FILENAME_MODULES, 'set=' . $set . '&module=' . $HTTP_GET_VARS['module'] . '&action=edit'));
6363
}
6464
```
65-
Before 'tep_redirect(tep_href_link(FILENAME_MODULES, 'set=' . $set . '&module=' . $HTTP_GET_VARS['module']));' line;
65+
Before tep_redirect(tep_href_link(FILENAME_MODULES, 'set=' . $set . '&module=' . $HTTP_GET_VARS['module'])); line;
6666
6767
7.5.Add:
6868
```
6969
if(isset($errorHandler))$errorHandler->display();
7070
```
71-
After 'case 'edit':' line.
71+
After case 'edit': line.
7272
8. Install the Paylike module from modules -> payment in the admin
7373
9. Insert the app key and your public key in the settings and enable the plugin
7474

README_CE-Phoenix-2341-Frozen.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
4242
```
4343
require_once('includes/modules/payment/paylike/errors.php');
4444
```
45-
Before 'require('includes/application_top.php');' line;
45+
Before require('includes/application_top.php'); line;
4646
4747
7.2.Add:
4848
```
4949
require_once('includes/modules/payment/paylike/validate.php');
5050
```
51-
After 'case 'save':' line;
51+
After case 'save': line;
5252
5353
7.3.Replace 'tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");' line with:
5454
```
@@ -63,13 +63,13 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
6363
tep_redirect(tep_href_link('modules.php', 'set=' . $set . '&module=' . $HTTP_GET_VARS['module'] . '&action=edit'));
6464
}
6565
```
66-
Before 'tep_redirect(tep_href_link('modules.php', 'set=' . $set . '&module=' . $_GET['module']));' line;
66+
Before tep_redirect(tep_href_link('modules.php', 'set=' . $set . '&module=' . $_GET['module'])); line;
6767
6868
7.5.Add:
6969
```
7070
if(isset($errorHandler))$errorHandler->display();
7171
```
72-
After 'case 'edit':' line.
72+
After case 'edit': line.
7373
8. Install the Paylike module from modules -> payment in the admin
7474
9. Insert the app key and your public key in the settings and enable the plugin
7575

README_Phoenix.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ available by a user.
55

66
Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
77

8-
98
## Supported osCommerce versions
109

1110
*The plugin has been tested with osCommerce v.2.3.4.1 and osCommerce CE Phoenix
@@ -25,8 +24,8 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
2524
<script src="https://sdk.paylike.io/3.js"></script>
2625
<script src= "includes/javascript/paylike.js"></script>
2726
<?php
28-
}
29-
?>
27+
}
28+
?>
3029
```
3130
Anywhere betwen the `head` tags.
3231
6. In: `includes/.htaccess` add:
@@ -37,9 +36,41 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
3736
3837
```
3938
After the last line.
40-
7. Install the Paylike module from modules -> payment in the admin
41-
8. Insert the app key and your public key in the settings and enable the plugin
39+
7. In: `admin/modules.php`
40+
7.1.Add:
41+
```
42+
require_once('includes/modules/payment/paylike/errors.php');
43+
```
44+
Before require 'includes/application_top.php'; line;
45+
46+
7.2.Add:
47+
```
48+
require_once('includes/modules/payment/paylike/validate.php');
49+
```
50+
After case 'save': line;
51+
52+
7.3.Replace tep_db_query("UPDATE configuration SET configuration_value = '" . tep_db_input($value) . "' WHERE configuration_key = '" . tep_db_input($key) . "'"); line with:
53+
```
54+
if(sizeof($errors) === 0 || array_search($key, $validation_keys) === FALSE){
55+
tep_db_query("UPDATE configuration SET configuration_value = '" . tep_db_input($value) . "' WHERE configuration_key = '" . tep_db_input($key) . "'");
56+
}
57+
```
4258
59+
7.4.Add:
60+
```
61+
if(sizeof($errors)){
62+
tep_redirect(tep_href_link('modules.php', 'set=' . $set . '&module=' . $_GET['module'] . '&action=edit'));
63+
}
64+
```
65+
Before tep_redirect(tep_href_link('modules.php', 'set=' . $set . '&module=' . $_GET['module'])); line;
66+
67+
7.5.Add:
68+
```
69+
<?php if(isset($errorHandler))$errorHandler->display(); ?>
70+
```
71+
Before <div class="row no-gutters"> line.
72+
8. Install the Paylike module from modules -> payment in the admin
73+
9. Insert the app key and your public key in the settings and enable the plugin
4374
4475
## Updating settings
4576
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "esparks/module-paylike",
3+
"description": "Paylike integration with payment gateway prestashop",
4+
"type": "prestashop-module",
5+
"version": "1.0.6",
6+
"license": [
7+
"GPL-3.0-or-later"
8+
],
9+
"authors": [
10+
{
11+
"name": "Ionut Calara",
12+
"email": "ionut.calara@gmail.com"
13+
}
14+
],
15+
"require": {
16+
"paylike/php-api": "^1.0.3"
17+
}
18+
}

upload_ce_phoenix/admin/includes/modules/payment/paylike/composer.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
/* Initialize errors object */
3+
require_once('helpers/Paylike_Errors.php');
4+
$errorHandler = new PaylikeErrors();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/* Security class check */
3+
if (! class_exists('PaylikeErrors')) :
4+
/**
5+
* Helper class that handles errors via cookies
6+
*/
7+
class PaylikeErrors
8+
{
9+
public $errors = '';
10+
private $cookieName = 'validation_errors';
11+
12+
public function __construct()
13+
{
14+
/* Load current cookie value */
15+
$this->loadCookieErrors();
16+
}
17+
/**
18+
* Dispay errors as HTML list
19+
*/
20+
public function display()
21+
{
22+
if ($this->errors && sizeof($this->errors)) {
23+
echo '<div class="validation_error">';
24+
echo '<ul>';
25+
echo '<li>' . implode('</li><li>', $this->errors) . '</li>';
26+
echo '</ul></div>';
27+
}
28+
}
29+
/**
30+
* Set cookie with list of given values
31+
*
32+
* @param array $list - the list o errors to be stored
33+
*/
34+
public function setCookieErrors($list)
35+
{
36+
setcookie($this->cookieName, json_encode($list), time()+3600);
37+
}
38+
39+
/**
40+
* Read and store cookie value
41+
*/
42+
public function loadCookieErrors()
43+
{
44+
if(isset($_COOKIE[$this->cookieName])){
45+
$this->errors = json_decode($_COOKIE[$this->cookieName], true);
46+
setcookie($this->cookieName, '', time()-3600);
47+
}
48+
}
49+
}
50+
endif; /* End if class_exists. */
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/* Security class check */
3+
if (! class_exists('PaylikeValidator')) :
4+
/**
5+
* Helper class that validates module keys via Paylike API
6+
*/
7+
class PaylikeValidator
8+
{
9+
public $validationPublicKeys = array('Live'=>array(),'Test'=>array());
10+
/**
11+
* Validate the App key.
12+
*
13+
* @param string $value - the value of the input.
14+
* @param string $mode - the transaction mode 'test' | 'live'.
15+
*
16+
* @return string - the error message
17+
*/
18+
public function validateAppKeyField($value, $mode)
19+
{
20+
/** Check if the key value is empty **/
21+
if (! $value) {
22+
return sprintf(ERROR_APP_KEY, $mode);
23+
}
24+
/** Load the client from API**/
25+
$paylikeClient = new \Paylike\Paylike($value);
26+
try {
27+
/** Load the identity from API**/
28+
$identity = $paylikeClient->apps()->fetch();
29+
} catch (\Paylike\Exception\ApiException $exception) {
30+
$this->logMessage(sprintf(ERROR_APP_KEY_INVALID, $mode));
31+
return sprintf(ERROR_APP_KEY_INVALID, $mode);
32+
}
33+
34+
try {
35+
/** Load the merchants public keys list corresponding for current identity **/
36+
$merchants = $paylikeClient->merchants()->find($identity['id']);
37+
if ($merchants) {
38+
foreach ($merchants as $merchant) {
39+
/** Check if the key mode is the same as the transaction mode **/
40+
if (($mode == 'Test' && $merchant['test']) || ($mode != 'Test' && !$merchant['test'])) {
41+
$this->validationPublicKeys[$mode][] = $merchant['key'];
42+
}
43+
}
44+
}
45+
} catch (\Paylike\Exception\ApiException $exception) {
46+
$this->logMessage(sprintf(ERROR_APP_KEY_INVALID, $mode));
47+
}
48+
/** Check if public keys array for the current mode is populated **/
49+
if (empty($this->validationPublicKeys[$mode])) {
50+
/** Generate the error based on the current mode **/
51+
$error = sprintf(ERROR_APP_KEY_INVALID_MODE, $mode, array_values(array_diff(array_keys($this->validationPublicKeys), array($mode)))[0]);
52+
$this->logMessage($error);
53+
return $error;
54+
}
55+
}
56+
57+
/**
58+
* Validate the Public key.
59+
*
60+
* @param string $value - the value of the input.
61+
* @param string $mode - the transaction mode 'test' | 'live'.
62+
*
63+
* @return string - the error message
64+
*/
65+
public function validatePublicKeyField($value, $mode)
66+
{
67+
/** Check if the key value is not empty **/
68+
if (! $value) {
69+
return sprintf(ERROR_PUBLIC_KEY, $mode);
70+
}
71+
/** Check if the local stored public keys array is empty OR the key is not in public keys list **/
72+
if (empty($this->validationPublicKeys[$mode]) || ! in_array($value, $this->validationPublicKeys[$mode])) {
73+
$error = sprintf(ERROR_PUBLIC_KEY_INVALID, $mode);
74+
$this->logMessage($error);
75+
return $error;
76+
}
77+
}
78+
79+
/**
80+
* log message to default logger
81+
*
82+
* @param string $message
83+
*
84+
*/
85+
public static function logMessage($message)
86+
{
87+
error_log('[Paylike] ' . $message);
88+
}
89+
}
90+
endif; /* End if class_exists. */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.validation_error {
2+
background: pink;
3+
width: 100%;
4+
margin-bottom: 10px;
5+
}
6+
7+
.validation_error ul {
8+
padding: 5px 10px;
9+
display: inline-block;
10+
margin-bottom: 0;
11+
}
12+
13+
.validation_error ul li {
14+
list-style: none;
15+
padding: 0;
16+
}
17+
18+
*:disabled, .disabled {
19+
opacity: 0.4;
20+
pointer-events: none;
21+
}

0 commit comments

Comments
 (0)