Skip to content

Commit 0222692

Browse files
committed
Added logic to validate the public and private keys upon saving.
1 parent 822a231 commit 0222692

Some content is hidden

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

55 files changed

+4377
-15
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,41 @@ Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
4141
</FilesMatch>
4242
```
4343
After the last line.
44-
7. Install the Paylike module from modules -> payment in the admin
45-
8. Insert the app key and your public key in the settings and enable the plugin
44+
7. In: `admin/modules.php`
45+
7.1.Add:
46+
```
47+
require_once('includes/modules/payment/paylike/errors.php');
48+
```
49+
Before 'require('includes/application_top.php');' line(__);
4650
51+
7.2.Add:
52+
```
53+
require_once('includes/modules/payment/paylike/validate.php');
54+
```
55+
After 'reset($HTTP_POST_VARS['configuration']);' line(__);
56+
57+
7.3.Replace 'tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");' line(__) with:
58+
```
59+
if(sizeof($errors) === 0 || array_search($key, $validation_keys) === FALSE){
60+
tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '" . $value . "' where configuration_key = '" . $key . "'");
61+
}
62+
```
63+
64+
7.4.Add:
65+
```
66+
if(sizeof($errors)){
67+
tep_redirect(tep_href_link(FILENAME_MODULES, 'set=' . $set . '&module=' . $HTTP_GET_VARS['module'] . '&action=edit'));
68+
}
69+
```
70+
Before 'tep_redirect(tep_href_link(FILENAME_MODULES, 'set=' . $set . '&module=' . $HTTP_GET_VARS['module']));' line(__);
71+
72+
7.5.Add:
73+
```
74+
if(isset($errorHandler))$errorHandler->display();
75+
```
76+
After 'case 'edit':' line(__).
77+
8. Install the Paylike module from modules -> payment in the admin
78+
9. Insert the app key and your public key in the settings and enable the plugin
4779
4880
## Updating settings
4981

README_Phoenix.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# osCommerce plugin for Paylike
2+
3+
This plugin is *not* developed or maintained by Paylike but kindly made
4+
available by a user.
5+
6+
Released under the GPL V3 license: https://opensource.org/licenses/GPL-3.0
7+
8+
9+
## Supported osCommerce versions
10+
11+
*The plugin has been tested with osCommerce v.2.3.4.1 and osCommerce CE Phoenix
12+
13+
## Installation
14+
15+
Once you have installed osCommerce, follow these simple steps:
16+
1. Signup at [paylike.io](https://paylike.io) (it’s free)
17+
2. Create a live account
18+
3. Create an app key for your osCommerce website
19+
4. Upload the files in the `upload` folder to root of your osCommerce store.
20+
5. In: `includes/template_top.php` add:
21+
```
22+
<?php
23+
if ( basename( $PHP_SELF ) == 'checkout_confirmation.php' ) {
24+
?>
25+
<script src="https://sdk.paylike.io/3.js"></script>
26+
<script src= "includes/javascript/paylike.js"></script>
27+
<?php
28+
}
29+
?>
30+
```
31+
Anywhere betwen the `head` tags.
32+
6. In: `includes/.htaccess` add:
33+
```
34+
<FilesMatch "paylike\.php$">
35+
<IfModule mod_authz_core.c>
36+
Require all granted
37+
</IfModule>
38+
<IfModule !mod_authz_core.c>
39+
Allow from all
40+
</IfModule>
41+
</FilesMatch>
42+
```
43+
After the last line.
44+
7. Install the Paylike module from modules -> payment in the admin
45+
8. Insert the app key and your public key in the settings and enable the plugin
46+
47+
48+
## Updating settings
49+
50+
Under the Paylike settings, you can:
51+
* Update the title that shows up in the payment popup
52+
* Add test/live keys
53+
* Set payment mode (test/live)
54+
* Change the capture type (Instant/Delayed)
55+
56+
## How to
57+
58+
1. Capture
59+
* In Instant mode, the orders are captured automatically
60+
* In delayed mode you can capture funds only from the paylike dashboard.
61+
2. Refund
62+
* To refund an order you can use the paylike dashboard.
63+
3. Void
64+
* To void an order you can use the paylike dashboard.
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/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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 (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+
$this->errors = json_decode($_COOKIE[$this->cookieName], true);
45+
}
46+
}
47+
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.validation_error {
2+
background: pink;
3+
width: 100%;
4+
margin-bottom: 10px;
5+
}
6+
7+
.validation_error ul {
8+
padding: 0px 10px;
9+
display: inline-block;
10+
}
11+
12+
.validation_error ul li {
13+
list-style: none;
14+
padding: 0;
15+
}
16+
17+
*:disabled, .disabled {
18+
opacity: 0.4;
19+
pointer-events: none;
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
$(function() {
2+
/* Onready trigger for toggleFields function */
3+
$(document).ready(function() {
4+
/* Scroll to top of the page in order to see the errors on any device */
5+
setTimeout(function() {
6+
window.scrollTo(0, 0);
7+
}, 100);
8+
toggleFields($('input[name="configuration[MODULE_PAYMENT_PAYLIKE_TRANSACTION_MODE]"]:checked').val());
9+
});
10+
11+
/* Onchange trigger for toggleFields function */
12+
$(document).on('change', 'input[name="configuration[MODULE_PAYMENT_PAYLIKE_TRANSACTION_MODE]"]', function(e) {
13+
toggleFields($(this).val())
14+
});
15+
})
16+
17+
/** Enable/Disable live and test fields based on selected value of the 'Paylike transaction mode' select */
18+
function toggleFields(mode) {
19+
if (mode == 'Live') {
20+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_APP_KEY]').removeAttr("disabled");
21+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_KEY]').removeAttr("disabled");
22+
23+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_TEST_APP_KEY]').attr("disabled", "disabled");
24+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_TEST_KEY]').attr("disabled", "disabled");
25+
} else {
26+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_APP_KEY]').attr("disabled", "disabled");
27+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_KEY]').attr("disabled", "disabled");
28+
29+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_TEST_APP_KEY]').removeAttr("disabled");
30+
$('input[name="configuration[MODULE_PAYMENT_PAYLIKE_TEST_KEY]').removeAttr("disabled");
31+
}
32+
}

0 commit comments

Comments
 (0)