1: | <?php
|
2: | namespace Opencart\Catalog\Controller\Api\Sale;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Voucher extends \Opencart\System\Engine\Controller {
|
9: |
|
10: | |
11: | |
12: |
|
13: | public function index(): void {
|
14: | $this->load->language('api/sale/voucher');
|
15: |
|
16: | $json = [];
|
17: |
|
18: | if (isset($this->request->post['voucher'])) {
|
19: | $voucher = (string)$this->request->post['voucher'];
|
20: | } else {
|
21: | $voucher = '';
|
22: | }
|
23: |
|
24: | if ($voucher) {
|
25: | $this->load->model('checkout/voucher');
|
26: |
|
27: | $voucher_info = $this->model_checkout_voucher->getVoucher($voucher);
|
28: |
|
29: | if (!$voucher_info) {
|
30: | $json['error'] = $this->language->get('error_voucher');
|
31: | }
|
32: | }
|
33: |
|
34: | if (!$json) {
|
35: | if ($voucher) {
|
36: | $this->session->data['voucher'] = $this->request->post['voucher'];
|
37: |
|
38: | $json['success'] = $this->language->get('text_success');
|
39: | } else {
|
40: | unset($this->session->data['voucher']);
|
41: |
|
42: | $json['success'] = $this->language->get('text_remove');
|
43: | }
|
44: | }
|
45: |
|
46: | $this->response->addHeader('Content-Type: application/json');
|
47: | $this->response->setOutput(json_encode($json));
|
48: | }
|
49: |
|
50: | |
51: | |
52: | |
53: | |
54: |
|
55: | public function add(): void {
|
56: | $this->load->language('api/sale/voucher');
|
57: |
|
58: | $json = [];
|
59: |
|
60: |
|
61: | $keys = [
|
62: | 'from_name',
|
63: | 'from_email',
|
64: | 'to_name',
|
65: | 'to_email',
|
66: | 'voucher_theme_id',
|
67: | 'message',
|
68: | 'amount'
|
69: | ];
|
70: |
|
71: | foreach ($keys as $key) {
|
72: | if (!isset($this->request->post[$key])) {
|
73: | $this->request->post[$key] = '';
|
74: | }
|
75: | }
|
76: |
|
77: |
|
78: | if ((oc_strlen($this->request->post['from_name']) < 1) || (oc_strlen($this->request->post['from_name']) > 64)) {
|
79: | $json['error']['from_name'] = $this->language->get('error_from_name');
|
80: | }
|
81: |
|
82: | if ((oc_strlen($this->request->post['from_email']) > 96) || !filter_var($this->request->post['from_email'], FILTER_VALIDATE_EMAIL)) {
|
83: | $json['error']['from_email'] = $this->language->get('error_email');
|
84: | }
|
85: |
|
86: | if ((oc_strlen($this->request->post['to_name']) < 1) || (oc_strlen($this->request->post['to_name']) > 64)) {
|
87: | $json['error']['to_name'] = $this->language->get('error_to_name');
|
88: | }
|
89: |
|
90: | if ((oc_strlen($this->request->post['to_email']) > 96) || !filter_var($this->request->post['to_email'], FILTER_VALIDATE_EMAIL)) {
|
91: | $json['error']['to_email'] = $this->language->get('error_email');
|
92: | }
|
93: |
|
94: | if (($this->request->post['amount'] < $this->config->get('config_voucher_min')) || ($this->request->post['amount'] > $this->config->get('config_voucher_max'))) {
|
95: | $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']));
|
96: | }
|
97: |
|
98: | if (!$json) {
|
99: | $code = oc_token();
|
100: |
|
101: | $this->session->data['vouchers'][] = [
|
102: | 'code' => $code,
|
103: | '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']),
|
104: | 'to_name' => $this->request->post['to_name'],
|
105: | 'to_email' => $this->request->post['to_email'],
|
106: | 'from_name' => $this->request->post['from_name'],
|
107: | 'from_email' => $this->request->post['from_email'],
|
108: | 'voucher_theme_id' => $this->request->post['voucher_theme_id'],
|
109: | 'message' => $this->request->post['message'],
|
110: | 'amount' => $this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency'))
|
111: | ];
|
112: |
|
113: | $json['success'] = $this->language->get('text_cart');
|
114: | }
|
115: |
|
116: | $this->response->addHeader('Content-Type: application/json');
|
117: | $this->response->setOutput(json_encode($json));
|
118: | }
|
119: |
|
120: | |
121: | |
122: | |
123: | |
124: |
|
125: | public function remove(): void {
|
126: | $this->load->language('api/sale/cart');
|
127: |
|
128: | $json = [];
|
129: |
|
130: | if (isset($this->request->post['key'])) {
|
131: | $key = (int)$this->request->post['key'];
|
132: | } else {
|
133: | $key = '';
|
134: | }
|
135: |
|
136: | if (!isset($this->session->data['vouchers'][$key])) {
|
137: | $json['error'] = $this->language->get('error_voucher');
|
138: | }
|
139: |
|
140: |
|
141: | if (!$json) {
|
142: | $json['success'] = $this->language->get('text_success');
|
143: |
|
144: | unset($this->session->data['vouchers'][$key]);
|
145: | unset($this->session->data['reward']);
|
146: | }
|
147: |
|
148: | $this->response->addHeader('Content-Type: application/json');
|
149: | $this->response->setOutput(json_encode($json));
|
150: | }
|
151: | }
|
152: | |