-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecpay.php
459 lines (379 loc) · 11.9 KB
/
ecpay.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
if (!defined('_PS_VERSION_'))
exit;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
class Ecpay extends PaymentModule
{
# Custom variables: POST error
private $postError = '';
# Custom variables: ECPay parameters
private $ecpayParams = array();
# Custom variables: ECPay log
private $ecpayLog = array();
public function __construct()
{
# The value MUST be the name of the module's folder.
$this->name = 'ecpay';
# The type of the module, displayed in the modules list.
$this->tab = 'payments_gateways';
# The version number for the module, displayed in the modules list.
$this->version = '1.0.0831';
# The auther for the module, displayed in the modules list.
$this->author = 'ECPay';
# Enable BootStrap
$this->bootstrap = true;
# Calling the parent constuctor method must be done after the creation of the $this->name variable and before any use of the $this->l() translation method.
parent::__construct();
# A name for the module, which will be displayed in the back-office's modules list.
$this->displayName = $this->l('ECPay Integration Payment');
# A description for the module, which will be displayed in the back-office's modules
$this->description = 'https://www.ecpay.com.tw/';
# A message, asking the administrator if he really does want to uninstall the module. To be used in the installation code.
$this->confirmUninstall = $this->l('Do you want to uninstall ECPay payment module?');
# Custom variables: ECPay parameters
$this->ecpayParams = array(
'ecpay_merchant_id'
, 'ecpay_hash_key'
, 'ecpay_hash_iv'
, 'ecpay_payment_credit'
, 'ecpay_payment_credit_03'
, 'ecpay_payment_credit_06'
, 'ecpay_payment_credit_12'
, 'ecpay_payment_credit_18'
, 'ecpay_payment_credit_24'
, 'ecpay_payment_webatm'
, 'ecpay_payment_atm'
, 'ecpay_payment_cvs'
, 'ecpay_payment_barcode'
);
# Custom variables: ECPay log
$this->ecpayLog = _PS_MODULE_DIR_ . $this->name . '/log/return_url.log';
if (!file_exists(dirname($this->ecpayLog)))
{
mkdir(dirname($this->ecpayLog));
}
}
# Perform checks and actions during the module's installation process
public function install()
{
# Register PrestaShop hooks
if (!parent::install() OR !$this->registerHook('paymentOptions') or !$this->registerHook('paymentReturn')) {
return false;
}
return true;
}
public function uninstall()
{
if (!parent::uninstall() or !$this->cleanEcpayConfig()) {
return false;
}
return true;
}
public function getContent()
{
$html_content = '';
# Update the settings
if (Tools::isSubmit('ecpay_submit'))
{
# Validate the POST parameters
$this->postValidation();
if (!empty($this->postError))
{
# Display the POST error
$html_content .= $this->displayError($this->postError);
}
else
{
$html_content .= $this->postProcess();
}
}
# Display the setting form
$html_content .= $this->displayForm();
return $html_content;
}
public function hookPaymentOptions($params)
{
if (!$this->active) {
return;
}
if (!$this->checkCurrency($params['cart'])) {
return;
}
$payment_options = [];
foreach ($this->getPaymentsDesc() as $payment_name => $payment_desc) {
if (Configuration::get('ecpay_payment_' . strtolower($payment_name)) == 'on') {
$payment_option = new PaymentOption();
$payment_option->setCallToActionText($payment_desc)
// ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/logo.png'))
->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true))
->setInputs([
'payment_type' => [
'name' =>'payment_type',
'type' =>'hidden',
'value' => $payment_name,
],
])
->setAdditionalInformation($this->context->smarty->fetch('module:ecpay/views/templates/front/payment_infos.tpl'));
$payment_options[] = $payment_option;
}
}
return $payment_options;
}
public function hookPaymentReturn($params)
{
if (!$this->active)
{
return;
}
}
public function checkCurrency($cart)
{
$currency_order = new Currency($cart->id_currency);
$currencies_module = $this->getCurrency($cart->id_currency);
if (is_array($currencies_module))
{
foreach ($currencies_module as $currency_module)
{
if ($currency_order->id == $currency_module['id_currency'])
{
return true;
}
}
}
return false;
}
# Public custom function
public function getPaymentsDesc()
{
$payments_desc = array(
'Credit' => $this->l('Credit')
, 'Credit_03' => $this->l('Credit Card(03 Installments)')
, 'Credit_06' => $this->l('Credit Card(06 Installments)')
, 'Credit_12' => $this->l('Credit Card(12 Installments)')
, 'Credit_18' => $this->l('Credit Card(18 Installments)')
, 'Credit_24' => $this->l('Credit Card(24 Installments)')
, 'WebATM' => $this->l('WebATM')
, 'ATM' => $this->l('ATM')
, 'CVS' => $this->l('CVS')
, 'BARCODE' => $this->l('BARCODE')
);
return $payments_desc;
}
public function getPaymentDesc($payment_name)
{
$payments_desc = $this->getPaymentsDesc();
if (!isset($payments_desc[$payment_name]))
{
return '';
}
else
{
return $payments_desc[$payment_name];
}
}
public function isTestMode($ecpay_merchant_id)
{
if ($ecpay_merchant_id == '2000132' or $ecpay_merchant_id == '2000214') {
return true;
} else {
return false;
}
}
public function invokeEcpayModule()
{
if (!class_exists('ECPay_AllInOne', false))
{
if (!include(_PS_MODULE_DIR_ . $this->name . '/lib/ECPay.Payment.Integration.php'))
{
return false;
}
}
return true;
}
public function formatOrderTotal($order_total)
{
return intval(round($order_total));
}
public function getCartOrderID($merchant_trade_no, $ecpay_merchant_id)
{
$cart_order_id = $merchant_trade_no;
if ($this->isTestMode($ecpay_merchant_id))
{
$cart_order_id = substr($merchant_trade_no, 14);
}
return $cart_order_id;
}
public function getOrderStatusID($status_name, $payment_type = '')
{
$order_status = array(
'created' => 1,
'succeeded' => 2,
'failed' => 8,
);
return $order_status[$status_name];
}
public function setOrderComments($order_id, $comments)
{
try {
$order = new Order($order_id);
$customer = new Customer($order->id_customer);
$id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($customer->email, $order->id);
if (!$id_customer_thread) {
$customer_thread = new CustomerThread();
$customer_thread->id_contact = 0;
$customer_thread->id_customer = (int)$order->id_customer;
$customer_thread->id_shop = (int)$this->context->shop->id;
$customer_thread->id_order = $order->id;
$customer_thread->id_lang = $this->context->language->id;
$customer_thread->email = $customer->email;
$customer_thread->status = 'open';
$customer_thread->token = Tools::passwdGen(12);
$customer_thread->add();
} else {
$customer_thread = new CustomerThread((int)$id_customer_thread);
}
$customer_message = new CustomerMessage();
$customer_message->id_customer_thread = $customer_thread->id;
$customer_message->id_employee = 0;
$customer_message->message = $comments;
$customer_message->private = 0;
$customer_message->system = 1;
$customer_message->add();
} catch(Exception $e) {
$error = $e->getMessage();
$result_message = '0|' . $error;
$this->module->logEcpayMessage('Order ' . $order_id . ' process result : ' . $result_message, true);
}
}
public function updateOrderStatus($order_id, $status_id, $send_mail = false)
{
# Update the order status
$order_history = new OrderHistory();
$order_history->id_order = (int)$order_id;
$order_history->changeIdOrderState((int)$status_id, (int)$order_id);
# Send a mail
if ($send_mail)
{
$order_history->addWithemail();
}
}
public function logEcpayMessage($message, $is_append = false)
{
if (!$is_append)
{
return file_put_contents($this->ecpayLog, date('Y-m-d H:i:s') . ' - ' . $message . "\n", LOCK_EX);
}
else
{
return file_put_contents($this->ecpayLog, date('Y-m-d H:i:s') . ' - ' . $message . "\n", FILE_APPEND | LOCK_EX);
}
}
# Private custom function
private function postValidation()
{
$required_fields = array(
'ecpay_merchant_id' => $this->l('Merchant ID')
, 'ecpay_hash_key' => $this->l('Hash Key')
, 'ecpay_hash_iv' => $this->l('Hash IV')
);
foreach ($required_fields as $field_name => $field_desc)
{
$tmp_field_value = Tools::getValue($field_name);
if (empty($tmp_field_value))
{
$this->postError = $field_desc . $this->l(' is required');
return;
}
}
}
private function displayForm()
{
# Set the payment methods options
$payment_methods = array();
$payments_desc = $this->getPaymentsDesc();
foreach ($payments_desc as $payment_name => $payment_desc) {
array_push($payment_methods, array('id_option' => strtolower($payment_name), 'name' => $payment_desc));
}
# Set the configurations for generating a setting form
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('ECPay configuration')
, 'image' => '../modules/ecpay/images/ecpay_setting_logo.png'
)
, 'input' => array(
array(
'type' => 'text'
, 'label' => $this->l('Merchant ID')
, 'name' => 'ecpay_merchant_id'
, 'required' => true
)
, array(
'type' => 'text'
, 'label' => $this->l('Hash Key')
, 'name' => 'ecpay_hash_key'
, 'required' => true
)
, array(
'type' => 'text'
, 'label' => $this->l('Hash IV')
, 'name' => 'ecpay_hash_iv'
, 'required' => true
)
, array(
'type' => 'checkbox'
, 'label' => $this->l('Payment Methods')
, 'name' => 'ecpay_payment'
, 'values' => array(
'query' => $payment_methods
, 'name' => 'name'
, 'id' => 'id_option'
)
)
)
, 'submit' => array(
'name' => 'ecpay_submit'
, 'title' => $this->l('Save')
, 'class' => 'button'
)
);
$helper = new HelperForm();
# Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
# Get the default language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
# Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
# Load the current settings
foreach ($this->ecpayParams as $param_name) {
$helper->fields_value[$param_name] = Configuration::get($param_name);
}
return $helper->generateForm($fields_form);
}
private function postProcess()
{
# Update ECPay parameters
foreach ($this->ecpayParams as $param_name)
{
if (!Configuration::updateValue($param_name, Tools::getValue($param_name)))
{
return $this->displayError($param_name . ' ' . $this->l('updated failed'));
}
}
return $this->displayConfirmation($this->l('Settings updated'));
}
private function cleanEcpayConfig()
{
foreach ($this->ecpayParams as $param_name)
{
if (!Configuration::deleteByName($param_name))
{
return false;
}
}
return true;
}
}