|
| 1 | +<?php |
| 2 | + |
| 3 | +class ControllerPaymentG2APay extends Controller { |
| 4 | + |
| 5 | + private $error = array(); |
| 6 | + |
| 7 | + public function index() { |
| 8 | + $this->load->language('payment/g2apay'); |
| 9 | + |
| 10 | + $this->document->setTitle($this->language->get('heading_title')); |
| 11 | + |
| 12 | + $this->load->model('setting/setting'); |
| 13 | + |
| 14 | + if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { |
| 15 | + $this->model_setting_setting->editSetting('g2apay', $this->request->post); |
| 16 | + |
| 17 | + $this->session->data['complete'] = $this->language->get('text_complete'); |
| 18 | + |
| 19 | + $this->response->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL')); |
| 20 | + } |
| 21 | + |
| 22 | + $data['heading_title'] = $this->language->get('heading_title'); |
| 23 | + |
| 24 | + $data['entry_username'] = $this->language->get('entry_username'); |
| 25 | + $data['entry_secret'] = $this->language->get('entry_secret'); |
| 26 | + $data['entry_api_hash'] = $this->language->get('entry_api_hash'); |
| 27 | + $data['entry_environment'] = $this->language->get('entry_environment'); |
| 28 | + $data['entry_secret_token'] = $this->language->get('entry_secret_token'); |
| 29 | + $data['entry_ipn_url'] = $this->language->get('entry_ipn_url'); |
| 30 | + $data['entry_total'] = $this->language->get('entry_total'); |
| 31 | + $data['entry_order_status'] = $this->language->get('entry_order_status'); |
| 32 | + $data['entry_geo_zone'] = $this->language->get('entry_geo_zone'); |
| 33 | + $data['entry_status'] = $this->language->get('entry_status'); |
| 34 | + $data['entry_sort_order'] = $this->language->get('entry_sort_order'); |
| 35 | + $data['entry_debug'] = $this->language->get('entry_debug'); |
| 36 | + |
| 37 | + $data['entry_complete_status'] = $this->language->get('entry_complete_status'); |
| 38 | + $data['entry_rejected_status'] = $this->language->get('entry_rejected_status'); |
| 39 | + $data['entry_cancelled_status'] = $this->language->get('entry_cancelled_status'); |
| 40 | + $data['entry_refunded_status'] = $this->language->get('entry_refunded_status'); |
| 41 | + $data['entry_partially_refunded_status'] = $this->language->get('entry_partially_refunded_status'); |
| 42 | + |
| 43 | + $data['g2apay_environment_live'] = $this->language->get('g2apay_environment_live'); |
| 44 | + $data['g2apay_environment_test'] = $this->language->get('g2apay_environment_test'); |
| 45 | + |
| 46 | + $data['text_edit'] = $this->language->get('text_edit'); |
| 47 | + $data['text_enabled'] = $this->language->get('text_enabled'); |
| 48 | + $data['text_disabled'] = $this->language->get('text_disabled'); |
| 49 | + $data['text_all_zones'] = $this->language->get('text_all_zones'); |
| 50 | + |
| 51 | + $data['help_username'] = $this->language->get('help_username'); |
| 52 | + $data['help_secret_token'] = $this->language->get('help_secret_token'); |
| 53 | + $data['help_ipn_url'] = $this->language->get('help_ipn_url'); |
| 54 | + $data['help_total'] = $this->language->get('help_total'); |
| 55 | + $data['help_debug'] = $this->language->get('help_debug'); |
| 56 | + |
| 57 | + $data['tab_settings'] = $this->language->get('tab_settings'); |
| 58 | + $data['tab_order_status'] = $this->language->get('tab_order_status'); |
| 59 | + |
| 60 | + $data['button_save'] = $this->language->get('button_save'); |
| 61 | + $data['button_cancel'] = $this->language->get('button_cancel'); |
| 62 | + |
| 63 | + if (isset($this->error['warning'])) { |
| 64 | + $data['error_warning'] = $this->error['warning']; |
| 65 | + } else { |
| 66 | + $data['error_warning'] = ''; |
| 67 | + } |
| 68 | + |
| 69 | + if (isset($this->error['username'])) { |
| 70 | + $data['error_username'] = $this->error['username']; |
| 71 | + } else { |
| 72 | + $data['error_username'] = ''; |
| 73 | + } |
| 74 | + |
| 75 | + if (isset($this->error['secret'])) { |
| 76 | + $data['error_secret'] = $this->error['secret']; |
| 77 | + } else { |
| 78 | + $data['error_secret'] = ''; |
| 79 | + } |
| 80 | + |
| 81 | + if (isset($this->error['api_hash'])) { |
| 82 | + $data['error_api_hash'] = $this->error['api_hash']; |
| 83 | + } else { |
| 84 | + $data['error_api_hash'] = ''; |
| 85 | + } |
| 86 | + |
| 87 | + if (isset($this->request->post['g2apay_order_status_id'])) { |
| 88 | + $data['g2apay_order_status_id'] = $this->request->post['g2apay_order_status_id']; |
| 89 | + } else { |
| 90 | + $data['g2apay_order_status_id'] = $this->config->get('g2apay_order_status_id'); |
| 91 | + } |
| 92 | + |
| 93 | + if (isset($this->request->post['g2apay_complete_status_id'])) { |
| 94 | + $data['g2apay_complete_status_id'] = $this->request->post['g2apay_complete_status_id']; |
| 95 | + } else { |
| 96 | + $data['g2apay_complete_status_id'] = $this->config->get('g2apay_complete_status_id'); |
| 97 | + } |
| 98 | + |
| 99 | + if (isset($this->request->post['g2apay_rejected_status_id'])) { |
| 100 | + $data['g2apay_rejected_status_id'] = $this->request->post['g2apay_rejected_status_id']; |
| 101 | + } else { |
| 102 | + $data['g2apay_rejected_status_id'] = $this->config->get('g2apay_rejected_status_id'); |
| 103 | + } |
| 104 | + |
| 105 | + if (isset($this->request->post['g2apay_cancelled_status_id'])) { |
| 106 | + $data['g2apay_cancelled_status_id'] = $this->request->post['g2apay_cancelled_status_id']; |
| 107 | + } else { |
| 108 | + $data['g2apay_cancelled_status_id'] = $this->config->get('g2apay_cancelled_status_id'); |
| 109 | + } |
| 110 | + |
| 111 | + if (isset($this->request->post['g2apay_refunded_status_id'])) { |
| 112 | + $data['g2apay_refunded_status_id'] = $this->request->post['g2apay_refunded_status_id']; |
| 113 | + } else { |
| 114 | + $data['g2apay_refunded_status_id'] = $this->config->get('g2apay_refunded_status_id'); |
| 115 | + } |
| 116 | + |
| 117 | + if (isset($this->request->post['g2apay_partially_refunded_status_id'])) { |
| 118 | + $data['g2apay_partially_refunded_status_id'] = $this->request->post['g2apay_partially_refunded_status_id']; |
| 119 | + } else { |
| 120 | + $data['g2apay_partially_refunded_status_id'] = $this->config->get('g2apay_partially_refunded_status_id'); |
| 121 | + } |
| 122 | + |
| 123 | + $data['breadcrumbs'] = array(); |
| 124 | + |
| 125 | + $data['breadcrumbs'][] = array( |
| 126 | + 'text' => $this->language->get('text_home'), |
| 127 | + 'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL') |
| 128 | + ); |
| 129 | + |
| 130 | + $data['breadcrumbs'][] = array( |
| 131 | + 'text' => $this->language->get('text_payment'), |
| 132 | + 'href' => $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL') |
| 133 | + ); |
| 134 | + |
| 135 | + $data['breadcrumbs'][] = array( |
| 136 | + 'text' => $this->language->get('heading_title'), |
| 137 | + 'href' => $this->url->link('payment/g2apay', 'token=' . $this->session->data['token'], 'SSL') |
| 138 | + ); |
| 139 | + |
| 140 | + $this->load->model('localisation/order_status'); |
| 141 | + $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses(); |
| 142 | + |
| 143 | + $data['action'] = $this->url->link('payment/g2apay', 'token=' . $this->session->data['token'], 'SSL'); |
| 144 | + |
| 145 | + $data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'); |
| 146 | + |
| 147 | + if (isset($this->request->post['g2apay_username'])) { |
| 148 | + $data['g2apay_username'] = $this->request->post['g2apay_username']; |
| 149 | + } else { |
| 150 | + $data['g2apay_username'] = $this->config->get('g2apay_username'); |
| 151 | + } |
| 152 | + |
| 153 | + if (isset($this->request->post['g2apay_secret'])) { |
| 154 | + $data['g2apay_secret'] = $this->request->post['g2apay_secret']; |
| 155 | + } else { |
| 156 | + $data['g2apay_secret'] = $this->config->get('g2apay_secret'); |
| 157 | + } |
| 158 | + |
| 159 | + if (isset($this->request->post['g2apay_api_hash'])) { |
| 160 | + $data['g2apay_api_hash'] = $this->request->post['g2apay_api_hash']; |
| 161 | + } else { |
| 162 | + $data['g2apay_api_hash'] = $this->config->get('g2apay_api_hash'); |
| 163 | + } |
| 164 | + |
| 165 | + if (isset($this->request->post['g2apay_environment'])) { |
| 166 | + $data['g2apay_environment'] = $this->request->post['g2apay_environment']; |
| 167 | + } else { |
| 168 | + $data['g2apay_environment'] = $this->config->get('g2apay_environment'); |
| 169 | + } |
| 170 | + |
| 171 | + if (isset($this->request->post['g2apay_total'])) { |
| 172 | + $data['g2apay_total'] = $this->request->post['g2apay_total']; |
| 173 | + } else { |
| 174 | + $data['g2apay_total'] = $this->config->get('g2apay_total'); |
| 175 | + } |
| 176 | + |
| 177 | + if (isset($this->request->post['g2apay_secret_token'])) { |
| 178 | + $data['g2apay_secret_token'] = $this->request->post['g2apay_secret_token']; |
| 179 | + } elseif ($this->config->get('g2apay_secret_token')) { |
| 180 | + $data['g2apay_secret_token'] = $this->config->get('g2apay_secret_token'); |
| 181 | + } else { |
| 182 | + $data['g2apay_secret_token'] = sha1(uniqid(mt_rand(), 1)); |
| 183 | + } |
| 184 | + |
| 185 | + $data['g2apay_ipn_url'] = HTTPS_CATALOG . 'index.php?route=payment/g2apay/ipn&token=' . $data['g2apay_secret_token']; |
| 186 | + |
| 187 | + if (isset($this->request->post['g2apay_ipn_uri'])) { |
| 188 | + $data['g2apay_ipn_uri'] = $this->request->post['g2apay_ipn_uri']; |
| 189 | + } else { |
| 190 | + $data['g2apay_ipn_uri'] = $this->config->get('g2apay_ipn_uri'); |
| 191 | + } |
| 192 | + |
| 193 | + if (isset($this->request->post['g2apay_order_status_id'])) { |
| 194 | + $data['g2apay_order_status_id'] = $this->request->post['g2apay_order_status_id']; |
| 195 | + } else { |
| 196 | + $data['g2apay_order_status_id'] = $this->config->get('g2apay_order_status_id'); |
| 197 | + } |
| 198 | + |
| 199 | + $this->load->model('localisation/order_status'); |
| 200 | + |
| 201 | + $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses(); |
| 202 | + |
| 203 | + if (isset($this->request->post['g2apay_geo_zone_id'])) { |
| 204 | + $data['g2apay_geo_zone_id'] = $this->request->post['g2apay_geo_zone_id']; |
| 205 | + } else { |
| 206 | + $data['g2apay_geo_zone_id'] = $this->config->get('g2apay_geo_zone_id'); |
| 207 | + } |
| 208 | + |
| 209 | + $this->load->model('localisation/geo_zone'); |
| 210 | + |
| 211 | + $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones(); |
| 212 | + |
| 213 | + if (isset($this->request->post['g2apay_status'])) { |
| 214 | + $data['g2apay_status'] = $this->request->post['g2apay_status']; |
| 215 | + } else { |
| 216 | + $data['g2apay_status'] = $this->config->get('g2apay_status'); |
| 217 | + } |
| 218 | + |
| 219 | + if (isset($this->request->post['g2apay_debug'])) { |
| 220 | + $data['g2apay_debug'] = $this->request->post['g2apay_debug']; |
| 221 | + } else { |
| 222 | + $data['g2apay_debug'] = $this->config->get('g2apay_debug'); |
| 223 | + } |
| 224 | + |
| 225 | + if (isset($this->request->post['g2apay_sort_order'])) { |
| 226 | + $data['g2apay_sort_order'] = $this->request->post['g2apay_sort_order']; |
| 227 | + } else { |
| 228 | + $data['g2apay_sort_order'] = $this->config->get('g2apay_sort_order'); |
| 229 | + } |
| 230 | + |
| 231 | + $data['header'] = $this->load->controller('common/header'); |
| 232 | + $data['column_left'] = $this->load->controller('common/column_left'); |
| 233 | + $data['footer'] = $this->load->controller('common/footer'); |
| 234 | + |
| 235 | + $this->response->setOutput($this->load->view('payment/g2apay.tpl', $data)); |
| 236 | + } |
| 237 | + |
| 238 | + public function action() { |
| 239 | + |
| 240 | + if ($this->config->get('g2apay_status')) { |
| 241 | + |
| 242 | + $this->load->model('payment/g2apay'); |
| 243 | + |
| 244 | + $g2apay_order = $this->model_payment_g2apay->getOrder($this->request->get['order_id']); |
| 245 | + |
| 246 | + if (!empty($g2apay_order)) { |
| 247 | + $this->load->language('payment/g2apay'); |
| 248 | + |
| 249 | + $g2apay_order['total_released'] = $this->model_payment_g2apay->getTotalReleased($g2apay_order['g2apay_order_id']); |
| 250 | + |
| 251 | + $g2apay_order['total_formatted'] = $this->currency->format($g2apay_order['total'], $g2apay_order['currency_code'], false); |
| 252 | + $g2apay_order['total_released_formatted'] = $this->currency->format($g2apay_order['total_released'], $g2apay_order['currency_code'], false); |
| 253 | + |
| 254 | + $data['g2apay_order'] = $g2apay_order; |
| 255 | + |
| 256 | + $data['text_payment_info'] = $this->language->get('text_payment_info'); |
| 257 | + $data['text_order_ref'] = $this->language->get('text_order_ref'); |
| 258 | + $data['text_order_total'] = $this->language->get('text_order_total'); |
| 259 | + $data['text_total_released'] = $this->language->get('text_total_released'); |
| 260 | + $data['text_refund_status'] = $this->language->get('text_refund_status'); |
| 261 | + $data['text_transactions'] = $this->language->get('text_transactions'); |
| 262 | + $data['text_yes'] = $this->language->get('text_yes'); |
| 263 | + $data['text_no'] = $this->language->get('text_no'); |
| 264 | + $data['text_column_amount'] = $this->language->get('text_column_amount'); |
| 265 | + $data['text_column_type'] = $this->language->get('text_column_type'); |
| 266 | + $data['text_column_date_added'] = $this->language->get('text_column_date_added'); |
| 267 | + $data['btn_refund'] = $this->language->get('btn_refund'); |
| 268 | + $data['text_confirm_refund'] = $this->language->get('text_confirm_refund'); |
| 269 | + |
| 270 | + $data['order_id'] = $this->request->get['order_id']; |
| 271 | + $data['token'] = $this->request->get['token']; |
| 272 | + |
| 273 | + return $this->load->view('payment/g2apay_order.tpl', $data); |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + public function refund() { |
| 279 | + $this->load->language('payment/g2apay'); |
| 280 | + $json = array(); |
| 281 | + |
| 282 | + if (isset($this->request->post['order_id']) && !empty($this->request->post['order_id'])) { |
| 283 | + $this->load->model('payment/g2apay'); |
| 284 | + |
| 285 | + $g2apay_order = $this->model_payment_g2apay->getOrder($this->request->post['order_id']); |
| 286 | + |
| 287 | + $refund_response = $this->model_payment_g2apay->refund($g2apay_order, $this->request->post['amount']); |
| 288 | + |
| 289 | + $this->model_payment_g2apay->logger($refund_response); |
| 290 | + |
| 291 | + if ($refund_response == 'ok') { |
| 292 | + $this->model_payment_g2apay->addTransaction($g2apay_order['g2apay_order_id'], 'refund', $this->request->post['amount'] * -1); |
| 293 | + |
| 294 | + $total_refunded = $this->model_payment_g2apay->getTotalRefunded($g2apay_order['g2apay_order_id']); |
| 295 | + $total_released = $this->model_payment_g2apay->getTotalReleased($g2apay_order['g2apay_order_id']); |
| 296 | + |
| 297 | + if ($total_released <= 0 && $g2apay_order['release_status'] == 1) { |
| 298 | + $this->model_payment_g2apay->updateRefundStatus($g2apay_order['g2apay_order_id'], 1); |
| 299 | + $refund_status = 1; |
| 300 | + $json['msg'] = $this->language->get('text_refund_ok_order'); |
| 301 | + } else { |
| 302 | + $refund_status = 0; |
| 303 | + $json['msg'] = $this->language->get('text_refund_ok'); |
| 304 | + } |
| 305 | + |
| 306 | + $json['data'] = array(); |
| 307 | + $json['data']['date_added'] = date("Y-m-d H:i:s"); |
| 308 | + $json['data']['amount'] = $this->currency->format(($this->request->post['amount'] * -1), $g2apay_order['currency_code'], false); |
| 309 | + $json['data']['total_released'] = (float)$total_released; |
| 310 | + $json['data']['total_refunded'] = (float)$total_refunded; |
| 311 | + $json['data']['refund_status'] = $refund_status; |
| 312 | + $json['error'] = false; |
| 313 | + } else { |
| 314 | + $json['error'] = true; |
| 315 | + $json['msg'] = 'Unable to refund: ' . $refund_response; |
| 316 | + } |
| 317 | + } else { |
| 318 | + $json['error'] = true; |
| 319 | + $json['msg'] = 'Missing data'; |
| 320 | + } |
| 321 | + |
| 322 | + $this->response->addHeader('Content-Type: application/json'); |
| 323 | + $this->response->setOutput(json_encode($json)); |
| 324 | + } |
| 325 | + |
| 326 | + public function install() { |
| 327 | + $this->load->model('payment/g2apay'); |
| 328 | + $this->model_payment_g2apay->install(); |
| 329 | + } |
| 330 | + |
| 331 | + public function uninstall() { |
| 332 | + $this->load->model('payment/g2apay'); |
| 333 | + $this->model_payment_g2apay->uninstall(); |
| 334 | + } |
| 335 | + |
| 336 | + protected function validate() { |
| 337 | + if (!$this->user->hasPermission('modify', 'payment/g2apay')) { |
| 338 | + $this->error['warning'] = $this->language->get('error_permission'); |
| 339 | + } |
| 340 | + |
| 341 | + if (!$this->request->post['g2apay_username']) { |
| 342 | + $this->error['username'] = $this->language->get('error_username'); |
| 343 | + } |
| 344 | + |
| 345 | + if (!$this->request->post['g2apay_secret']) { |
| 346 | + $this->error['secret'] = $this->language->get('error_secret'); |
| 347 | + } |
| 348 | + |
| 349 | + if (!$this->request->post['g2apay_api_hash']) { |
| 350 | + $this->error['api_hash'] = $this->language->get('error_api_hash'); |
| 351 | + } |
| 352 | + |
| 353 | + return !$this->error; |
| 354 | + } |
| 355 | + |
| 356 | +} |
0 commit comments