pilibaba.php (14239B)
1 <?php 2 class ControllerExtensionPaymentPilibaba extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->model('setting/setting'); 7 8 $this->load->model('extension/payment/pilibaba'); 9 10 $this->load->language('extension/payment/pilibaba'); 11 12 $this->document->setTitle($this->language->get('heading_title')); 13 14 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 15 $this->model_setting_setting->editSetting('payment_pilibaba', $this->request->post); 16 17 if ($this->request->post['payment_pilibaba_status']) { 18 $this->model_extension_payment_pilibaba->enablePiliExpress(); 19 } else { 20 $this->model_extension_payment_pilibaba->disablePiliExpress(); 21 } 22 23 $this->session->data['success'] = $this->language->get('text_success'); 24 25 $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)); 26 } 27 28 $data['breadcrumbs'] = array(); 29 30 $data['breadcrumbs'][] = array( 31 'text' => $this->language->get('text_home'), 32 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 33 ); 34 35 $data['breadcrumbs'][] = array( 36 'text' => $this->language->get('text_extension'), 37 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true) 38 ); 39 40 $data['breadcrumbs'][] = array( 41 'text' => $this->language->get('heading_title'), 42 'href' => $this->url->link('extension/payment/pilibaba', 'user_token=' . $this->session->data['user_token'], true) 43 ); 44 45 $data['action'] = $this->url->link('extension/payment/pilibaba', 'user_token=' . $this->session->data['user_token'], true); 46 47 $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true); 48 49 if (isset($this->request->post['payment_pilibaba_merchant_number'])) { 50 $data['payment_pilibaba_merchant_number'] = $this->request->post['payment_pilibaba_merchant_number']; 51 } else { 52 $data['payment_pilibaba_merchant_number'] = $this->config->get('payment_pilibaba_merchant_number'); 53 } 54 55 if (isset($this->request->post['payment_pilibaba_secret_key'])) { 56 $data['payment_pilibaba_secret_key'] = $this->request->post['payment_pilibaba_secret_key']; 57 } else { 58 $data['payment_pilibaba_secret_key'] = $this->config->get('payment_pilibaba_secret_key'); 59 } 60 61 if (isset($this->request->post['payment_pilibaba_environment'])) { 62 $data['payment_pilibaba_environment'] = $this->request->post['payment_pilibaba_environment']; 63 } else { 64 $data['payment_pilibaba_environment'] = $this->config->get('payment_pilibaba_environment'); 65 } 66 67 if (isset($this->request->post['payment_pilibaba_shipping_fee'])) { 68 $data['payment_pilibaba_shipping_fee'] = $this->request->post['payment_pilibaba_shipping_fee']; 69 } else { 70 $data['payment_pilibaba_shipping_fee'] = $this->config->get('payment_pilibaba_shipping_fee'); 71 } 72 73 if (isset($this->request->post['payment_pilibaba_order_status_id'])) { 74 $data['payment_pilibaba_order_status_id'] = $this->request->post['payment_pilibaba_order_status_id']; 75 } elseif ($this->config->has('payment_pilibaba_order_status_id')) { 76 $data['payment_pilibaba_order_status_id'] = $this->config->get('payment_pilibaba_order_status_id'); 77 } else { 78 $data['payment_pilibaba_order_status_id'] = '2'; 79 } 80 81 if (isset($this->request->post['payment_pilibaba_status'])) { 82 $data['payment_pilibaba_status'] = $this->request->post['payment_pilibaba_status']; 83 } else { 84 $data['payment_pilibaba_status'] = $this->config->get('payment_pilibaba_status'); 85 } 86 87 if (isset($this->request->post['payment_pilibaba_logging'])) { 88 $data['payment_pilibaba_logging'] = $this->request->post['payment_pilibaba_logging']; 89 } else { 90 $data['payment_pilibaba_logging'] = $this->config->get('payment_pilibaba_logging'); 91 } 92 93 if (isset($this->request->post['payment_pilibaba_sort_order'])) { 94 $data['payment_pilibaba_sort_order'] = $this->request->post['payment_pilibaba_sort_order']; 95 } else { 96 $data['payment_pilibaba_sort_order'] = $this->config->get('payment_pilibaba_sort_order'); 97 } 98 99 if (isset($this->request->post['payment_pilibaba_email_address'])) { 100 $data['payment_pilibaba_email_address'] = $this->request->post['payment_pilibaba_email_address']; 101 } elseif ($this->config->has('payment_pilibaba_email_address')) { 102 $data['payment_pilibaba_email_address'] = $this->config->get('payment_pilibaba_email_address'); 103 } else { 104 $data['payment_pilibaba_email_address'] = ''; 105 } 106 107 if (isset($this->error['warning'])) { 108 $data['error_warning'] = $this->error['warning']; 109 } else { 110 $data['error_warning'] = ''; 111 } 112 113 if (isset($this->session->data['success'])) { 114 $data['success'] = $this->session->data['success']; 115 116 unset($this->session->data['success']); 117 } else { 118 $data['success'] = ''; 119 } 120 121 if (isset($this->error['pilibaba_merchant_number'])) { 122 $data['error_pilibaba_merchant_number'] = $this->error['pilibaba_merchant_number']; 123 } else { 124 $data['error_pilibaba_merchant_number'] = ''; 125 } 126 127 if (isset($this->error['pilibaba_secret_key'])) { 128 $data['error_pilibaba_secret_key'] = $this->error['pilibaba_secret_key']; 129 } else { 130 $data['error_pilibaba_secret_key'] = ''; 131 } 132 133 if (isset($this->error['pilibaba_shipping_fee'])) { 134 $data['error_pilibaba_shipping_fee'] = $this->error['pilibaba_shipping_fee']; 135 } else { 136 $data['error_pilibaba_shipping_fee'] = ''; 137 } 138 139 if ($data['pilibaba_merchant_number'] && $data['payment_pilibaba_secret_key']) { 140 $data['show_register'] = false; 141 142 $data['currencies'] = $data['warehouses'] = $data['countries'] = array(); 143 } else { 144 $data['show_register'] = true; 145 146 $data['currencies'] = $this->model_extension_payment_pilibaba->getCurrencies(); 147 148 $data['warehouses'] = $this->model_extension_payment_pilibaba->getWarehouses(); 149 150 $this->load->model('localisation/country'); 151 152 $data['countries'] = $this->model_localisation_country->getCountries(); 153 } 154 155 $this->load->model('localisation/order_status'); 156 157 $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses(); 158 159 if ($this->config->get('config_weight_class_id') != '2') { 160 $data['error_weight'] = sprintf($this->language->get('error_weight'), $this->url->link('setting/setting', 'user_token=' . $this->session->data['user_token'], true)); 161 } else { 162 $data['error_weight'] = ''; 163 } 164 165 if ($this->config->has('payment_pilibaba_email_address') && $this->config->get('payment_pilibaba_email_address')) { 166 $data['notice_email'] = sprintf($this->language->get('text_email'), $this->config->get('payment_pilibaba_email_address')); 167 } else { 168 $data['notice_email'] = ''; 169 } 170 171 $data['user_token'] = $this->session->data['user_token']; 172 173 $data['header'] = $this->load->controller('common/header'); 174 $data['column_left'] = $this->load->controller('common/column_left'); 175 $data['footer'] = $this->load->controller('common/footer'); 176 177 $this->response->setOutput($this->load->view('extension/payment/pilibaba', $data)); 178 } 179 180 public function install() { 181 if ($this->user->hasPermission('modify', 'marketplace/extension')) { 182 $this->load->model('extension/payment/pilibaba'); 183 184 $this->model_extension_payment_pilibaba->install(); 185 } 186 } 187 188 public function uninstall() { 189 if ($this->user->hasPermission('modify', 'marketplace/extension')) { 190 $this->load->model('extension/payment/pilibaba'); 191 192 $this->model_extension_payment_pilibaba->uninstall(); 193 } 194 } 195 196 public function register() { 197 $this->load->language('extension/payment/pilibaba'); 198 199 $json = array(); 200 201 if (isset($this->request->post['email_address']) && isset($this->request->post['password']) && isset($this->request->post['currency']) && isset($this->request->post['warehouse']) && isset($this->request->post['country']) && isset($this->request->post['environment'])) { 202 if (utf8_strlen($this->request->post['email_address']) < 1) { 203 $json['error'] = $this->language->get('error_email_address'); 204 } else if (!filter_var($this->request->post['email_address'], FILTER_VALIDATE_EMAIL)) { 205 $json['error'] = $this->language->get('error_email_invalid'); 206 } else if (utf8_strlen($this->request->post['password']) < 8) { 207 $json['error'] = $this->language->get('error_password'); 208 } else if (utf8_strlen($this->request->post['currency']) < 1) { 209 $json['error'] = $this->language->get('error_currency'); 210 } else if (utf8_strlen($this->request->post['warehouse']) < 1) { 211 $json['error'] = $this->language->get('error_warehouse'); 212 } else if ($this->request->post['warehouse'] == 'other' && utf8_strlen($this->request->post['country']) < 1) { 213 $json['error'] = $this->language->get('error_country'); 214 } else { 215 $this->load->model('extension/payment/pilibaba'); 216 217 $response = $this->model_extension_payment_pilibaba->register($this->request->post['email_address'], $this->request->post['password'], $this->request->post['currency'], $this->request->post['warehouse'], $this->request->post['country'], $this->request->post['environment']); 218 219 if (isset($response['code']) && isset($response['message'])) { 220 if ($response['code'] == '0') { 221 $this->load->model('setting/setting'); 222 223 $this->model_setting_setting->editSetting('payment_pilibaba', array('pilibaba_merchant_number' => $response['data']['merchantNo'], 'pilibaba_secret_key' => $response['data']['privateKey'], 'pilibaba_email_address' => $this->request->post['email_address'], 'payment_pilibaba_environment' => $this->request->post['environment']), 0); 224 225 $this->session->data['success'] = $this->language->get('text_register_success'); 226 227 $json['redirect'] = $this->url->link('extension/payment/pilibaba', 'user_token=' . $this->session->data['user_token'], true); 228 } else { 229 $json['error'] = $response['message']; 230 } 231 } else { 232 $json['error'] = $this->language->get('error_bad_response'); 233 } 234 } 235 } else { 236 $json['error'] = $this->language->get('error_data_missing'); 237 } 238 239 $this->response->addHeader('Content-Type: application/json'); 240 $this->response->setOutput(json_encode($json)); 241 } 242 243 public function order() { 244 if ($this->config->get('payment_pilibaba_status')) { 245 $this->load->model('extension/payment/pilibaba'); 246 247 $order_id = $this->request->get['order_id']; 248 249 $pilibaba_order = $this->model_extension_payment_pilibaba->getOrder($this->request->get['order_id']); 250 251 if ($pilibaba_order) { 252 $this->load->language('extension/payment/pilibaba'); 253 254 $order_info['order_id'] = $pilibaba_order['order_id']; 255 256 $order_info['amount'] = '¥' . $pilibaba_order['amount']; 257 258 $order_info['fee'] = '¥' . $pilibaba_order['fee']; 259 260 $order_info['status'] = 'Success'; 261 262 $order_info['date_added'] = date($this->language->get('datetime_format'), strtotime($pilibaba_order['date_added'])); 263 264 $order_info['tracking'] = $pilibaba_order['tracking']; 265 266 $data['pilibaba_order'] = $order_info; 267 268 $data['barcode'] = $this->url->link('extension/payment/pilibaba/barcode', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $this->request->get['order_id'], true); 269 270 $data['order_id'] = $this->request->get['order_id']; 271 272 $data['user_token'] = $this->request->get['user_token']; 273 274 return $this->load->view('extension/payment/pilibaba_order', $data); 275 } 276 } 277 } 278 279 public function tracking() { 280 $this->load->language('extension/payment/pilibaba'); 281 282 $json = array(); 283 284 if ($this->config->get('payment_pilibaba_status')) { 285 if (isset($this->request->post['order_id']) && isset($this->request->post['tracking'])) { 286 if (utf8_strlen($this->request->post['tracking']) > 0 && utf8_strlen($this->request->post['tracking']) <= 50) { 287 $this->load->model('extension/payment/pilibaba'); 288 289 $this->model_extension_payment_pilibaba->updateTrackingNumber($this->request->post['order_id'], $this->request->post['tracking'], $this->config->get('payment_pilibaba_merchant_number')); 290 291 $json['success'] = $this->language->get('text_tracking_success'); 292 } else { 293 $json['error'] = $this->language->get('error_tracking_length'); 294 } 295 } else { 296 $json['error'] = $this->language->get('error_data_missing'); 297 } 298 } else { 299 $json['error'] = $this->language->get('error_not_enabled'); 300 } 301 302 $this->response->addHeader('Content-Type: application/json'); 303 $this->response->setOutput(json_encode($json)); 304 } 305 306 public function barcode() { 307 if ($this->config->get('payment_pilibaba_status')) { 308 if (isset($this->request->get['order_id'])) { 309 if ($this->config->get('payment_pilibaba_environment') == 'live') { 310 $url = 'https://www.pilibaba.com/pilipay/barCode'; 311 } else { 312 $url = 'http://pre.pilibaba.com/pilipay/barCode'; 313 } 314 315 echo '<img src="' . $url . '?orderNo=' . $this->request->get['order_id'] . '&merchantNo=' . $this->config->get('payment_pilibaba_merchant_number') . '">'; 316 } 317 } 318 } 319 320 protected function validate() { 321 if (!$this->user->hasPermission('modify', 'extension/payment/pilibaba')) { 322 $this->error['warning'] = $this->language->get('error_permission'); 323 } 324 325 if (!$this->request->post['payment_pilibaba_merchant_number']) { 326 $this->error['pilibaba_merchant_number'] = $this->language->get('error_merchant_number'); 327 } 328 329 if (!$this->request->post['payment_pilibaba_secret_key']) { 330 $this->error['pilibaba_secret_key'] = $this->language->get('error_secret_key'); 331 } 332 333 if ($this->request->post['payment_pilibaba_shipping_fee'] != '' && strpos($this->request->post['payment_pilibaba_shipping_fee'], '.') === false) { 334 $this->error['pilibaba_shipping_fee'] = $this->language->get('error_shipping_fee'); 335 } 336 337 if ($this->error && !isset($this->error['warning'])) { 338 $this->error['warning'] = $this->language->get('error_warning'); 339 } 340 341 return !$this->error; 342 } 343 }