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
|
<?php
class ControllerApiVoucher extends Controller {
public function index() {
$this->load->language('api/voucher');
// Delete past voucher in case there is an error
unset($this->session->data['voucher']);
$json = array();
if (!isset($this->session->data['api_id'])) {
$json['error'] = $this->language->get('error_permission');
} else {
$this->load->model('extension/total/voucher');
if (isset($this->request->post['voucher'])) {
$voucher = $this->request->post['voucher'];
} else {
$voucher = '';
}
$voucher_info = $this->model_extension_total_voucher->getVoucher($voucher);
if ($voucher_info) {
$this->session->data['voucher'] = $this->request->post['voucher'];
$json['success'] = $this->language->get('text_success');
} else {
$json['error'] = $this->language->get('error_voucher');
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
public function add() {
$this->load->language('api/voucher');
$json = array();
if (!isset($this->session->data['api_id'])) {
$json['error']['warning'] = $this->language->get('error_permission');
} else {
// Add keys for missing post vars
$keys = array(
'from_name',
'from_email',
'to_name',
'to_email',
'voucher_theme_id',
'message',
'amount'
);
foreach ($keys as $key) {
if (!isset($this->request->post[$key])) {
$this->request->post[$key] = '';
}
}
if (isset($this->request->post['voucher'])) {
$this->session->data['vouchers'] = array();
foreach ($this->request->post['voucher'] as $voucher) {
if (isset($voucher['code']) && isset($voucher['to_name']) && isset($voucher['to_email']) && isset($voucher['from_name']) && isset($voucher['from_email']) && isset($voucher['voucher_theme_id']) && isset($voucher['message']) && isset($voucher['amount'])) {
$this->session->data['vouchers'][$voucher['code']] = array(
'code' => $voucher['code'],
'description' => sprintf($this->language->get('text_for'), $this->currency->format($this->currency->convert($voucher['amount'], $this->session->data['currency'], $this->config->get('config_currency')), $this->session->data['currency']), $voucher['to_name']),
'to_name' => $voucher['to_name'],
'to_email' => $voucher['to_email'],
'from_name' => $voucher['from_name'],
'from_email' => $voucher['from_email'],
'voucher_theme_id' => $voucher['voucher_theme_id'],
'message' => $voucher['message'],
'amount' => $this->currency->convert($voucher['amount'], $this->session->data['currency'], $this->config->get('config_currency'))
);
}
}
$json['success'] = $this->language->get('text_cart');
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
} else {
// Add a new voucher if set
if ((utf8_strlen($this->request->post['from_name']) < 1) || (utf8_strlen($this->request->post['from_name']) > 64)) {
$json['error']['from_name'] = $this->language->get('error_from_name');
}
if ((utf8_strlen($this->request->post['from_email']) > 96) || !filter_var($this->request->post['from_email'], FILTER_VALIDATE_EMAIL)) {
$json['error']['from_email'] = $this->language->get('error_email');
}
if ((utf8_strlen($this->request->post['to_name']) < 1) || (utf8_strlen($this->request->post['to_name']) > 64)) {
$json['error']['to_name'] = $this->language->get('error_to_name');
}
if ((utf8_strlen($this->request->post['to_email']) > 96) || !filter_var($this->request->post['to_email'], FILTER_VALIDATE_EMAIL)) {
$json['error']['to_email'] = $this->language->get('error_email');
}
if (($this->request->post['amount'] < $this->config->get('config_voucher_min')) || ($this->request->post['amount'] > $this->config->get('config_voucher_max'))) {
$json['error']['amount'] = sprintf($this->language->get('error_amount'), $this->currency->format($this->config->get('config_voucher_min'), $this->session->data['currency']), $this->currency->format($this->config->get('config_voucher_max'), $this->session->data['currency']));
}
if (!$json) {
$code = mt_rand();
$this->session->data['vouchers'][$code] = array(
'code' => $code,
'description' => sprintf($this->language->get('text_for'), $this->currency->format($this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')), $this->session->data['currency']), $this->request->post['to_name']),
'to_name' => $this->request->post['to_name'],
'to_email' => $this->request->post['to_email'],
'from_name' => $this->request->post['from_name'],
'from_email' => $this->request->post['from_email'],
'voucher_theme_id' => $this->request->post['voucher_theme_id'],
'message' => $this->request->post['message'],
'amount' => $this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency'))
);
$json['success'] = $this->language->get('text_cart');
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
}
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
|