voucher.php (5906B)
1 <?php 2 class ControllerApiVoucher extends Controller { 3 public function index() { 4 $this->load->language('api/voucher'); 5 6 // Delete past voucher in case there is an error 7 unset($this->session->data['voucher']); 8 9 $json = array(); 10 11 if (!isset($this->session->data['api_id'])) { 12 $json['error'] = $this->language->get('error_permission'); 13 } else { 14 $this->load->model('extension/total/voucher'); 15 16 if (isset($this->request->post['voucher'])) { 17 $voucher = $this->request->post['voucher']; 18 } else { 19 $voucher = ''; 20 } 21 22 $voucher_info = $this->model_extension_total_voucher->getVoucher($voucher); 23 24 if ($voucher_info) { 25 $this->session->data['voucher'] = $this->request->post['voucher']; 26 27 $json['success'] = $this->language->get('text_success'); 28 } else { 29 $json['error'] = $this->language->get('error_voucher'); 30 } 31 } 32 33 $this->response->addHeader('Content-Type: application/json'); 34 $this->response->setOutput(json_encode($json)); 35 } 36 37 public function add() { 38 $this->load->language('api/voucher'); 39 40 $json = array(); 41 42 if (!isset($this->session->data['api_id'])) { 43 $json['error']['warning'] = $this->language->get('error_permission'); 44 } else { 45 // Add keys for missing post vars 46 $keys = array( 47 'from_name', 48 'from_email', 49 'to_name', 50 'to_email', 51 'voucher_theme_id', 52 'message', 53 'amount' 54 ); 55 56 foreach ($keys as $key) { 57 if (!isset($this->request->post[$key])) { 58 $this->request->post[$key] = ''; 59 } 60 } 61 62 if (isset($this->request->post['voucher'])) { 63 $this->session->data['vouchers'] = array(); 64 65 foreach ($this->request->post['voucher'] as $voucher) { 66 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'])) { 67 $this->session->data['vouchers'][$voucher['code']] = array( 68 'code' => $voucher['code'], 69 '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']), 70 'to_name' => $voucher['to_name'], 71 'to_email' => $voucher['to_email'], 72 'from_name' => $voucher['from_name'], 73 'from_email' => $voucher['from_email'], 74 'voucher_theme_id' => $voucher['voucher_theme_id'], 75 'message' => $voucher['message'], 76 'amount' => $this->currency->convert($voucher['amount'], $this->session->data['currency'], $this->config->get('config_currency')) 77 ); 78 } 79 } 80 81 $json['success'] = $this->language->get('text_cart'); 82 83 unset($this->session->data['shipping_method']); 84 unset($this->session->data['shipping_methods']); 85 unset($this->session->data['payment_method']); 86 unset($this->session->data['payment_methods']); 87 } else { 88 // Add a new voucher if set 89 if ((utf8_strlen($this->request->post['from_name']) < 1) || (utf8_strlen($this->request->post['from_name']) > 64)) { 90 $json['error']['from_name'] = $this->language->get('error_from_name'); 91 } 92 93 if ((utf8_strlen($this->request->post['from_email']) > 96) || !filter_var($this->request->post['from_email'], FILTER_VALIDATE_EMAIL)) { 94 $json['error']['from_email'] = $this->language->get('error_email'); 95 } 96 97 if ((utf8_strlen($this->request->post['to_name']) < 1) || (utf8_strlen($this->request->post['to_name']) > 64)) { 98 $json['error']['to_name'] = $this->language->get('error_to_name'); 99 } 100 101 if ((utf8_strlen($this->request->post['to_email']) > 96) || !filter_var($this->request->post['to_email'], FILTER_VALIDATE_EMAIL)) { 102 $json['error']['to_email'] = $this->language->get('error_email'); 103 } 104 105 if (($this->request->post['amount'] < $this->config->get('config_voucher_min')) || ($this->request->post['amount'] > $this->config->get('config_voucher_max'))) { 106 $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'])); 107 } 108 109 if (!$json) { 110 $code = mt_rand(); 111 112 $this->session->data['vouchers'][$code] = array( 113 'code' => $code, 114 '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']), 115 'to_name' => $this->request->post['to_name'], 116 'to_email' => $this->request->post['to_email'], 117 'from_name' => $this->request->post['from_name'], 118 'from_email' => $this->request->post['from_email'], 119 'voucher_theme_id' => $this->request->post['voucher_theme_id'], 120 'message' => $this->request->post['message'], 121 'amount' => $this->currency->convert($this->request->post['amount'], $this->session->data['currency'], $this->config->get('config_currency')) 122 ); 123 124 $json['success'] = $this->language->get('text_cart'); 125 126 unset($this->session->data['shipping_method']); 127 unset($this->session->data['shipping_methods']); 128 unset($this->session->data['payment_method']); 129 unset($this->session->data['payment_methods']); 130 } 131 } 132 } 133 134 $this->response->addHeader('Content-Type: application/json'); 135 $this->response->setOutput(json_encode($json)); 136 } 137 }