forked from PrestaShopCorp/dotpay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdotpay.php
312 lines (275 loc) · 10.7 KB
/
dotpay.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
*
*
* 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 [email protected] 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 Piotr Karecki <[email protected]>
* @copyright dotpay
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*
*/
if (!defined('_PS_VERSION_'))
exit;
class dotpay extends PaymentModule
{
const DOTPAY_PAYMENTS_TEST_CUSTOMER = '701169';
const DOTPAY_PAYMENTS_TEST_CUSTOMER_PIN = 'CNiqWSUnfMaeEyWT3mwZch8xl2IbKv9U';
protected $config_form = false;
public function __construct()
{
$this->name = 'dotpay';
$this->tab = 'payments_gateways';
$this->version = '1.4.0';
$this->author = '[email protected]';
parent::__construct();
$this->displayName = $this->l('dotpay');
$this->description = $this->l('dotpay payment module');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall dotpay payment module?');
}
private function addNewOrderState($state, $names, $color)
{
$query='SELECT * FROM `'._DB_PREFIX_.'order_state` os LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = '.(int)$this->context->language->id.') WHERE module_name = "dotpay"';
if ($result = Db::getInstance()->ExecuteS($query))
foreach ($result as $row)
if ($row["name"] == $names[1] || $row["name"] == $names[0]) {
Configuration::updateValue($state, $row["id_order_state"]);
Db::getInstance()->execute("UPDATE " . _DB_PREFIX_ . 'order_state SET `deleted` = 0 WHERE `id_order_state` = ' . $row["id_order_state"]);
};
$order_status = new OrderState((int)Configuration::get($state), (int)$this->context->language->id);
if (Validate::isLoadedObject($order_status))
return true;
$order_state = new OrderState();
$order_state->name = array();
foreach (Language::getLanguages() as $language)
{
if (Tools::strtolower($language['iso_code']) == 'pl') $order_state->name[$language['id_lang']] = $names[1];
else $order_state->name[$language['id_lang']] = $names[0];
}
$order_state->send_email = false;
$order_state->invoice = false;
$order_state->unremovable = false;
$order_state->color = $color;
$order_state->module_name = $this->name;
if ($order_state->add() || Configuration::updateValue($state, $order_state->id)) return true;
else return false;
}
public function install()
{
Configuration::updateValue('DP_TEST', true);
Configuration::updateValue('DP_ID', self::DOTPAY_PAYMENTS_TEST_CUSTOMER);
Configuration::updateValue('DP_PIN', self::DOTPAY_PAYMENTS_TEST_CUSTOMER_PIN);
Configuration::updateValue('DOTPAY_CONFIGURATION_OK', true);
return
parent::install() &&
$this->registerHook('header') &&
$this->registerHook('backOfficeHeader') &&
$this->registerHook('payment') &&
$this->registerHook('paymentReturn') &&
$this->addNewOrderState('PAYMENT_DOTPAY_NEW_STATUS', array('Awaiting payment confirmation', 'Oczekuje potwierdzenia płatności'),'lightblue') &&
$this->addNewOrderState('PAYMENT_DOTPAY_COMPLAINT_STATUS', array('Complaint', 'Rozpatrzona reklamacja'),'darkred');
}
public function uninstall()
{
$sql = array(
"UPDATE " . _DB_PREFIX_ . 'order_state SET `deleted` = 1 WHERE `module_name` = "dotpay"',
"UPDATE " . _DB_PREFIX_ . "order_state SET `deleted` = 1 WHERE id_order_state = " . pSQL(Configuration::get('PAYMENT_DOTPAY_NEW_STATUS')),
"UPDATE " . _DB_PREFIX_ . "order_state SET `deleted` = 1 WHERE id_order_state = " . pSQL(Configuration::get('PAYMENT_DOTPAY_COMPLAINT_STATUS'))
);
foreach ($sql as $query)
Db::getInstance()->execute($query);
Configuration::deleteByName('DP_ID');
Configuration::deleteByName('DP_PIN');
Configuration::deleteByName('DP_TEST');
Configuration::deleteByName('PAYMENT_DOTPAY_NEW_STATUS');
Configuration::deleteByName('PAYMENT_DOTPAY_COMPLAINT_STATUS');
Configuration::deleteByName('DOTPAY_CONFIGURATION_OK');
return parent::uninstall();
}
/**
* Load the configuration form
*/
public function getContent()
{
$this->_postProcess();
$this->context->smarty->assign(array(
'module_dir' => $this->_path,
'DP_TEST' => Configuration::get('DP_TEST', false),
'DOTPAY_CONFIGURATION_OK' => Configuration::get('DOTPAY_CONFIGURATION_OK', false),
'DP_URLC' => $this->context->link->getModuleLink('dotpay', 'callback', array('ajax' => '1'))
));
$output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
return $output.$this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitDotpayModule';
$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' => $this->getConfigFormValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Test mode'),
'name' => 'DP_TEST',
'is_bool' => true,
'desc' => $this->l('Use this module in test environment'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => false,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'text',
'name' => 'DP_ID',
'label' => $this->l('ID'),
),
array(
'type' => 'text',
'name' => 'DP_PIN',
'label' => $this->l('PIN'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'DP_TEST' => Configuration::get('DP_TEST', false),
'DP_ID' => Configuration::get('DP_ID'),
'DP_PIN' => Configuration::get('DP_PIN'),
);
}
/**
* Save form data.
*/
protected function _postProcess()
{
if(Tools::getValue("submitDotpayModule", false) == false) return;
Configuration::updateValue('DOTPAY_CONFIGURATION_OK', false);
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key)
$values[$key] = Tools::getValue($key);
if(!is_numeric($values['DP_ID']) or empty($values['DP_PIN'])) return;
foreach ($values as $key => $value)
Configuration::updateValue($key, $value);
Configuration::updateValue('DOTPAY_CONFIGURATION_OK', true);
}
/**
* Add the CSS & JavaScript files you want to be loaded in the BO.
*/
public function hookBackOfficeHeader()
{
$this->context->controller->addJS($this->_path.'js/back.js');
$this->context->controller->addCSS($this->_path.'css/back.css');
}
/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader()
{
$this->context->controller->addJS($this->_path.'/js/front.js');
$this->context->controller->addCSS($this->_path.'/css/front.css');
}
public function hookPayment()
{
$this->smarty->assign(array('module_dir' => $this->_path));
if ($this->active && Configuration::get('DOTPAY_CONFIGURATION_OK'))
return $this->display(__FILE__, 'payment.tpl');
}
public function hookPaymentReturn($params)
{
if (!$this->active)
return;
$customer = new Customer($params['objOrder']->id_customer);
if (!Validate::isLoadedObject($customer))
return;
if ((bool)Context::getContext()->customer->is_guest) $form_url=$this->context->link->getPageLink('guest-tracking', true);
else $form_url=$this->context->link->getPageLink('history', true);
$param = array(
'order_reference' => $params['objOrder']->reference,
'email' => $customer->email,
'submitGuestTracking' => 1
);
$this->smarty->assign(array(
'params' => $param,
'module_dir' => $this->getPathUri(),
'form_url' => $form_url,
));
return $this->display(__FILE__, 'payment_return.tpl');
}
static public function check_urlc_legacy()
{
$signature =
Configuration::get('DP_PIN').":".
Configuration::get('DP_ID').":".
Tools::getValue('control').":".
Tools::getValue('t_id').":".
Tools::getValue('amount').":".
Tools::getValue('email').":".
Tools::getValue('service').":".
Tools::getValue('code').":".
Tools::getValue('username').":".
Tools::getValue('password').":".
Tools::getValue('t_status');
$signature=hash('md5', $signature);
return (Tools::getValue('md5') == $signature);
}
}