-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglewallet.php
411 lines (347 loc) · 16.3 KB
/
singlewallet.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
include dirname(__FILE__)."/vendor/autoload.php";
if(!class_exists('DBHelper')){
include _PS_MODULE_DIR_."singlewallet/helper/DBHelper.php";
}
if (!defined('_PS_VERSION_')) {
exit;
}
use singlewallet\helper\DBHelper;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
class SingleWallet extends PaymentModule {
public function __construct(){
$this->name = 'singlewallet';
$this->tab = 'payments_gateways';
$this->version = '1.0.0';
$this->author = 'SingleWallet';
$this->need_instance = 1;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => '8.99.99',
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('SingleWallet', []);
$this->description = $this->trans('Accept Tether in all popular blockchains with SingleWallet.', []);
$this->confirmUninstall = $this->trans('Are you sure you want to uninstall?', []);
}
public function install(){
if(!parent::install() || !$this->registerHook('paymentOptions') || !$this->registerHook('paymentReturn')){
return false;
}
$this->registerHook('displayOrderDetail');
// setup configurations
Configuration::updateValue('singlewallet_title', 'Pay with cryptocurrency');
Configuration::updateValue('singlewallet_description', 'Pay with Tether anonymously');
Configuration::updateValue('singlewallet_api_key', '');
Configuration::updateValue('singlewallet_secret', '');
Configuration::updateValue('singlewallet_language', 'en');
Configuration::updateValue('singlewallet_minimum_amount', 5);
Configuration::updateValue('singlewallet_ttl', 60);
Configuration::updateValue('singlewallet_send_customer_email', false);
Configuration::updateValue('singlewallet_order_state', 2);
// create db tables
DBHelper::createOrdersTable();
DBHelper::createPaymentsTable();
return true;
}
public function hookPaymentOptions($params){
if (!$this->active || !empty($this->warning)) {
return;
}
if(Configuration::get('singlewallet_minimum_amount') > $this->context->cart->getOrderTotal()){
return;
}
$externalOption = new PaymentOption();
$externalOption->setModuleName($this->name);
$externalOption->setCallToActionText($this->trans(Configuration::get('singlewallet_title')));
$externalOption->setAction($this->context->link->getModuleLink($this->name, 'payment', ['option' => 'external'], true));
$this->context->smarty->assign([
'description'=>Configuration::get('singlewallet_description'),
]);
$externalOption->setAdditionalInformation($this->context->smarty->fetch($this->local_path.'views/templates/front/payment-option.tpl'));
return [$externalOption];
}
public function hookDisplayOrderDetail($order){
if($order['order']->module == 'singlewallet'){
$orderId = Tools::getValue('id_order');
return $this->orderDetailTemplate($orderId);
}
return '';
}
public function orderDetailTemplate($orderId){
$payments = DBHelper::getOrderPayments($orderId);
$isUnderpaid = DBHelper::isUnderpaid($orderId);
$this->context->smarty->assign(array(
'pay_url' => $this->context->link->getModuleLink($this->name, 'redirect', [
'order_id'=>$orderId,
]),
'payments'=>$payments,
'is_underpaid'=>$isUnderpaid,
));
return $this->context->smarty->fetch($this->local_path.'views/templates/front/order-detail.tpl');
}
public function generateForm(){
$this->context->smarty->assign(array(
'action' => $this->context->link->getModuleLink($this->name, 'redirect', array(), true),
'description' => Configuration::get('singlewallet_description'),
));
return $this->context->smarty->fetch($this->local_path.'views/templates/front/payment_form.tpl');
}
public function uninstall(){
// keep db
return parent::uninstall();
}
public function getContent(){
$output = '';
if (Tools::isSubmit('submit' . $this->name)) {
$title = (string) Tools::getValue('singlewallet_title');
$description = (string) Tools::getValue('singlewallet_description');
$apiKey = (string) Tools::getValue('singlewallet_api_key');
$secret = (string) Tools::getValue('singlewallet_secret');
$language = (string) Tools::getValue('singlewallet_language');
$minimumAmount = (int) Tools::getValue('singlewallet_minimum_amount');
$ttl = (int) Tools::getValue('singlewallet_ttl');
$sendCustomerEmail = (bool) Tools::getValue('singlewallet_send_customer_email','off');
$orderStateId = (int) Tools::getValue('singlewallet_order_state',2);
$error = false;
if(strlen($title) < 5){
$output .= $this->displayError($this->trans('title must have 5 characters at least'));
$error = true;
}
if(!empty($apiKey)){
if(strlen($apiKey) == 16){
$sw = new \SingleWallet\SingleWallet($apiKey, $secret);
try{
$sw->getAccountInfo();
}catch(Throwable $e){
$output .= $this->displayError($this->trans($e->getMessage()));
$error = true;
}
}else{
$output .= $this->displayError($this->trans('invalid api key'));
$error = true;
}
}
if(!empty($secret) && !$error){
if(!preg_match("/^([a-f0-9]{64})$/", $secret)){
$output .= $this->displayError($this->trans('Invalid Configuration value'));
$error = true;
}
}
if($language != 'en'){
if(empty($apiKey)){
$apiKey = Configuration::get('singlewallet_api_key');
$secret = '';
}
if(strlen($apiKey) == 16 && !$error){
$sw = new \SingleWallet\SingleWallet($apiKey, $secret);
try{
$isExists = array_filter($sw->getLanguageList(), function($el) use($language){
return $el->getCode() === $language;
});
if(count($isExists) == 0){
$output .= $this->displayError($this->trans('invalid language code'));
$error = true;
}
}catch(Throwable $e){
$output .= $this->displayError($this->trans('please set a valid api key to change language'));
$error = true;
}
}else{
$output .= $this->displayError($this->trans('please set a valid api key to change language'));
$error = true;
}
}
if(!Validate::isPositiveInt($minimumAmount)){
if($minimumAmount < 3){
$output .= $this->displayError($this->trans('minimum amount is 3'));
$error = true;
}
}
if(!Validate::isPositiveInt($ttl)){
if($ttl < 15){
$output .= $this->displayError($this->trans('minimum invoice expire time is 15'));
$error = true;
}else if($ttl > 10080){
$output .= $this->displayError($this->trans('maximum invoice expire time is 10080'));
$error = true;
}
}
if(!Validate::isBool($sendCustomerEmail)){
$output .= $this->displayError($this->trans('send customer email must be boolean'));
$error = true;
}
if(!Validate::isPositiveInt($orderStateId)){
if($this->checkOrderState($orderStateId) == 0){
$output .= $this->displayError($this->trans('invalid order state'));
$error = true;
}
}
if(!$error){
if(!empty($apiKey)){
Configuration::updateValue('singlewallet_api_key', $apiKey);
}
if(!empty($secret)){
Configuration::updateValue('singlewallet_secret', $secret);
}
Configuration::updateValue('singlewallet_title', $title);
Configuration::updateValue('singlewallet_description', $description);
Configuration::updateValue('singlewallet_language', $language);
Configuration::updateValue('singlewallet_minimum_amount', $minimumAmount);
Configuration::updateValue('singlewallet_ttl', $ttl);
Configuration::updateValue('singlewallet_send_customer_email', $sendCustomerEmail);
Configuration::updateValue('singlewallet_order_state', $orderStateId);
$output = $this->displayConfirmation($this->trans('Settings updated'));
}
}
return $output . $this->displayForm();
}
public function displayForm(){
$form = [
'form' => [
'legend' => [
'title' => $this->trans('Settings'),
],
'input' => [
[
'type' => 'text',
'label' => $this->trans('Gateway name'),
'name' => 'singlewallet_title',
'description'=>'This name will be displayed for the customer on the checkout page.',
'required' => true,
],
[
'type' => 'text',
'label' => $this->trans('Description'),
'name' => 'singlewallet_description',
'description'=>'This description will be displayed for the customer on the checkout page.',
'required' => true,
],
[
'type' => 'text',
'label' => $this->trans('API Key'),
'name' => 'singlewallet_api_key',
'required' => true,
],
[
'type' => 'text',
'label' => $this->trans('Secret'),
'name' => 'singlewallet_secret',
'required' => true,
],
[
'type' => 'text',
'label' => $this->trans('Minimum amount'),
'name' => 'singlewallet_minimum_amount',
'required' => true,
],
[
'type' => 'text',
'label' => $this->trans('Invoice Expire Time (in minutes)'),
'name' => 'singlewallet_ttl',
'required' => true,
],
[
'type' => 'select',
'label' => $this->trans('Invoice Language'),
'name' => 'singlewallet_language',
'options' => array(
'query' => $this->languagesList(),
'id' => 'code',
'name' => 'name',
),
'required' => true,
],
[
'type' => 'select',
'label' => $this->trans('Order State'),
'name' => 'singlewallet_order_state',
'desc' => $this->trans('When customer pay the invoice, What the order state should be?'),
'options' => [
'query' => $this->getOrderStates(),
'id' => 'id',
'name' => 'name',
],
],
[
'type' => 'switch',
'label' => $this->trans('Add customer email to invoice'),
'name' => 'singlewallet_send_customer_email',
'desc' => "Customer will get updates about the invoice from SingleWallet, customer emails will never be shared with third party",
'required' => false,
'is_bool' => true,
'values'=>[
[
'id'=>'enabled',
'value'=>true,
'label' => $this->trans('Enabled')
],
[
'id'=>'disabled',
'value'=>false,
'label' => $this->trans('Disabled')
],
],
],
],
'submit' => [
'title' => 'Save',
'class' => 'btn btn-default pull-right',
],
],
];
$helper = new HelperForm();
$helper->table = $this->table;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&' . http_build_query(['configure' => $this->name]);
$helper->submit_action = 'submit' . $this->name;
$helper->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');
$helper->fields_value['singlewallet_title'] = Tools::getValue('singlewallet_title', Configuration::get('singlewallet_title'));
$helper->fields_value['singlewallet_description'] = Tools::getValue('singlewallet_description', Configuration::get('singlewallet_description'));
$helper->fields_value['singlewallet_api_key'] = '';// Tools::getValue('singlewallet_api_key', Configuration::get('singlewallet_api_key'));
$helper->fields_value['singlewallet_secret'] = '';// Tools::getValue('singlewallet_secret', Configuration::get('singlewallet_secret'));
$helper->fields_value['singlewallet_minimum_amount'] = Tools::getValue('singlewallet_minimum_amount', Configuration::get('singlewallet_minimum_amount'));
$helper->fields_value['singlewallet_ttl'] = Tools::getValue('singlewallet_ttl', Configuration::get('singlewallet_ttl'));
$helper->fields_value['singlewallet_language'] = Tools::getValue('singlewallet_language', Configuration::get('singlewallet_language'));
$helper->fields_value['singlewallet_send_customer_email'] = Tools::getValue('singlewallet_send_customer_email', Configuration::get('singlewallet_send_customer_email'));
$helper->fields_value['singlewallet_order_state'] = Tools::getValue('singlewallet_order_state', Configuration::get('singlewallet_order_state'));
return $helper->generateForm([$form]);
}
public function languagesList(){
$output = [
[
'code'=>'ar',
'name'=>'Arabic',
],
[
'code'=>'en',
'name'=>'English',
],
[
'code'=>'fr',
'name'=>'French',
],
[
'code'=>'ru',
'name'=>'Russian',
],
];
return $output;
}
public function getOrderStates(){
$results = Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'order_state_lang ORDER BY name ASC');
$output = [];
foreach ($results as $i=>$row) {
$output[$i]['id'] = $row['id_order_state'];
$output[$i]['name'] = $row['name'];
}
return $output;
}
public function checkOrderState($id){
$id = intval($id);
$results = Db::getInstance()->executeS('SELECT count(*) FROM '._DB_PREFIX_."order_state_lang where id_order_state='$id'");
return count($results);
}
}