This repository has been archived by the owner on Mar 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
desjardins.php
executable file
·258 lines (227 loc) · 10 KB
/
desjardins.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
class Desjardins extends PaymentModule
{
public $limited_countries = array('ca');
public function __construct()
{
$this->name = 'desjardins';
$this->tab = 'payments_gateways';
$this->version = '0.3.16';
$this->author = 'PrestaShop';
$this->need_instance = 0;
$this->bootstrap = true;
$this->display = 'view';
$this->meta_title = $this->l('Desjardins');
parent::__construct();
$this->displayName = $this->l('Desjardins');
$this->description = $this->l('Accept payments by Credit Card with Desjardins (Visa, Mastercard and Amex)');
}
public function install()
{
return parent::install() && $this->registerHook('payment') && $this->registerHook('orderConfirmation')
&& $this->registerHook('moduleRoutes') && Configuration::updateValue('DESJARDINS_MODE', 0);
}
public function uninstall()
{
return Configuration::deleteByName('DESJARDINS_TPE') && Configuration::deleteByName('DESJARDINS_CLE')
&& Configuration::deleteByName('DESJARDINS_CODE_SOCIETE') && Configuration::deleteByName('DESJARDINS_MODE') && parent::uninstall();
}
public function getContent()
{
/* Loading CSS and JS files */
$this->context->controller->addCSS(array($this->_path.'/css/desjardins.css'));
$this->context->controller->addJS($this->_path.'js/desjardins.js');
$output = '';
if (Tools::isSubmit('submitDesjardins'))
{
if (!Tools::getValue('DESJARDINS_TPE') || !Tools::getValue('DESJARDINS_CLE') || !Tools::getValue('DESJARDINS_CODE_SOCIETE'))
$output .= $this->displayError($this->l('Please fill the required fields to update your configuration.'));
elseif (Tools::getValue('DESJARDINS_TPE') && Configuration::updateValue('DESJARDINS_TPE', Tools::getValue('DESJARDINS_TPE'))
&& Configuration::updateValue('DESJARDINS_CLE', Tools::getValue('DESJARDINS_CLE'))
&& Configuration::updateValue('DESJARDINS_CODE_SOCIETE', Tools::getValue('DESJARDINS_CODE_SOCIETE'))
&& Configuration::updateValue('DESJARDINS_MODE', Tools::getValue('DESJARDINS_MODE'))
&& Configuration::updateValue('DESJARDINS_VERSION', '3.0'))
$output .= $this->displayConfirmation($this->l('Congratulations, your configuration was updated successfully'));
}
$this->smarty->assign(array('admin_lang' => Context::getContext()->language->iso_code,
'desjardins_url' => $this->context->link->getModuleLink('desjardins', 'validation', array(), false)));
return $this->display(__FILE__, 'views/templates/admin/configuration.tpl').$output.$this->renderForm();
}
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Configuration'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Live mode'),
'name' => 'DESJARDINS_MODE',
'is_bool' => true,
'desc' => $this->l('Production mode should be used only if your store is live'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Production')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Test')
)
),
),
array(
'type' => 'text',
'label' => $this->l('TPE'),
'name' => 'DESJARDINS_TPE',
'required' => true,
'class' => 'fixed-width-xxl',
'desc' => $this->l('Your Desjardins Electronic Payment Terminal ID'),
),
array(
'type' => 'text',
'label' => $this->l('Key'),
'name' => 'DESJARDINS_CLE',
'required' => true,
'class' => 'fixed-width-xxl',
'desc' => $this->l('Your Desjardins Secret Key'),
),
array(
'type' => 'text',
'label' => $this->l('Company code'),
'name' => 'DESJARDINS_CODE_SOCIETE',
'required' => true,
'class' => 'fixed-width-xxl',
'desc' => $this->l('Your Desjardins Company Code'),
),
),
'submit' => array(
'title' => $this->l('Save settings'),
'class' => 'btn btn-default pull-right')
)
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitDesjardins';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => Configuration::getMultiple(array('DESJARDINS_TPE', 'DESJARDINS_CLE', 'DESJARDINS_MODE', 'DESJARDINS_CODE_SOCIETE')),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
/*
* * The seal used in the payment form is calculated using the following encryption hashing function
* * We combine data with the secret key in accordance with RFC 2104 specifications
*/
public function generateHash($params)
{
return Tools::strtolower(hash_hmac('sha1',
$params['TPE'].'*'.$params['date'].'*'.$params['montant'].'*'.$params['reference'].'*'.
$params['texte-libre'].'*'.$params['version'].'*'.$params['lgue'].'*'.$params['societe'].'*'.$params['mail'].'**********',
$this->getKey()));
}
public function getKey()
{
$hex_str_key = Tools::substr(Configuration::get('DESJARDINS_CLE'), 0, 38);
$hex_final = Tools::substr(Configuration::get('DESJARDINS_CLE'), 38, 2).'00';
$cca0 = ord($hex_final);
if ($cca0 > 70 && $cca0 < 97)
$hex_str_key .= Tools::chr($cca0 - 23).Tools::substr($hex_final, 1, 1);
else
$hex_str_key .= (Tools::substr($hex_final, 1, 1) == 'M') ? Tools::substr($hex_final,
0, 1).'0' : Tools::substr($hex_final, 0, 2);
return pack('H*', $hex_str_key);
}
public function hookPayment()
{
$params = array();
$params['version'] = Configuration::get('DESJARDINS_VERSION');
$params['TPE'] = Configuration::get('DESJARDINS_TPE');
$params['date'] = date('d/m/Y:H:i:s');
$params['montant'] = number_format($this->context->cart->getOrderTotal(), 2, '.', '').Tools::strtoupper($this->context->currency->iso_code);
$params['reference'] = (int)$this->context->cart->id;
$params['texte-libre'] = 'PrestaShop';
$params['mail'] = $this->context->customer->email;
$params['lgue'] = in_array(Tools::strtoupper($this->context->language->iso_code), array('EN', 'FR')) ? Tools::strtoupper($this->context->language->iso_code) : 'EN';
$params['societe'] = Configuration::get('DESJARDINS_CODE_SOCIETE');
$params['url_retour'] = $this->context->link->getPageLink('order');
$params['url_retour_ok'] = version_compare(_PS_VERSION_, '1.5', '<') ? (Configuration::get('PS_SSL_ENABLED') ?
Tools::getShopDomainSsl(true) : Tools::getShopDomain(true)).__PS_BASE_URI__.'order-confirmation.php?id_cart='.
(int)$this->context->cart->id.'&id_module='.(int)$this->id.'&key='.$this->context->customer->secure_key :
$this->context->link->getPageLink('order-confirmation.php', null, null,
array('id_cart' => (int)$this->context->cart->id, 'key' => $this->context->customer->secure_key, 'id_module' => $this->id));
$params['url_retour_err'] = $params['url_retour_ok'];
$params['MAC'] = $this->generateHash($params);
$params['api_url'] = Configuration::get('DESJARDINS_MODE') ? 'https://p.monetico-services.com/paiement.cgi' :
'https://p.monetico-services.com/test/paiement.cgi';
$this->smarty->assign('desjardins_params', $params);
if (version_compare(_PS_VERSION_, '1.6', '<'))
return $this->display(__FILE__, 'views/templates/hook/payment.tpl');
return $this->display(__FILE__, 'views/templates/hook/payment_16.tpl');
}
public function displayAdminHomeQuickLinks()
{
return $this->display(__FILE__, 'views/templates/hook/back-office-home.tpl');
}
public function hookModuleRoutes()
{
/* The IPN sent by Desjardins can take a few seconds to create the order */
if ((Tools::getIsset('controller') && Tools::getValue('controller') == 'order-confirmation')
&& (Tools::getIsset('id_module') && Tools::getValue('id_module') == $this->id))
sleep(3);
}
public function hookOrderConfirmation($params)
{
if (!isset($params['objOrder']) || ($params['objOrder']->module != $this->name))
return false;
if ($params['objOrder'] && Validate::isLoadedObject($params['objOrder']) && isset($params['objOrder']->valid)) {
$this->smarty->assign('desjardins_order',
array(
'reference' => isset($params['objOrder']->reference) ? $params['objOrder']->reference : '#' . sprintf('%06d',
$params['objOrder']->id), 'valid' => $params['objOrder']->valid)
);
}
return $this->display(__FILE__, 'order-confirmation.tpl');
}
}