laybuy.php (44967B)
1 <?php 2 class ControllerExtensionPaymentLaybuy extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->model('setting/setting'); 7 8 $this->load->model('extension/payment/laybuy'); 9 10 $this->load->language('extension/payment/laybuy'); 11 12 $this->document->setTitle($this->language->get('heading_title')); 13 14 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 15 unset($this->request->post['laybuy_cron_url'], $this->request->post['laybuy_cron_time']); 16 17 $this->model_setting_setting->editSetting('payment_laybuy', $this->request->post); 18 19 $this->session->data['success'] = $this->language->get('text_success'); 20 21 $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)); 22 } 23 24 $data['breadcrumbs'] = array(); 25 26 $data['breadcrumbs'][] = array( 27 'text' => $this->language->get('text_home'), 28 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 29 ); 30 31 $data['breadcrumbs'][] = array( 32 'text' => $this->language->get('text_extension'), 33 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true) 34 ); 35 36 $data['breadcrumbs'][] = array( 37 'text' => $this->language->get('heading_title'), 38 'href' => $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'], true) 39 ); 40 41 $data['action'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'], true); 42 43 $data['fetch'] = $this->url->link('extension/payment/laybuy/fetch', 'user_token=' . $this->session->data['user_token'] . '#reportstab', true); 44 45 $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true); 46 47 if (isset($this->request->post['payment_laybuys_membership_id'])) { 48 $data['payment_laybuys_membership_id'] = $this->request->post['payment_laybuys_membership_id']; 49 } else { 50 $data['payment_laybuys_membership_id'] = $this->config->get('payment_laybuys_membership_id'); 51 } 52 53 if (isset($this->request->post['payment_laybuy_token'])) { 54 $data['payment_laybuy_token'] = $this->request->post['payment_laybuy_token']; 55 } elseif ($this->config->has('payment_laybuy_token')) { 56 $data['payment_laybuy_token'] = $this->config->get('payment_laybuy_token'); 57 } else { 58 $data['payment_laybuy_token'] = md5(time()); 59 } 60 61 if (isset($this->request->post['payment_laybuy_min_deposit'])) { 62 $data['payment_laybuy_min_deposit'] = $this->request->post['payment_laybuy_min_deposit']; 63 } elseif ($this->config->get('payment_laybuy_min_deposit')) { 64 $data['payment_laybuy_min_deposit'] = $this->config->get('payment_laybuy_min_deposit'); 65 } else { 66 $data['payment_laybuy_min_deposit'] = '20'; 67 } 68 69 if (isset($this->request->post['payment_laybuy_max_deposit'])) { 70 $data['payment_laybuy_max_deposit'] = $this->request->post['payment_laybuy_max_deposit']; 71 } elseif ($this->config->get('payment_laybuy_max_deposit')) { 72 $data['payment_laybuy_max_deposit'] = $this->config->get('payment_laybuy_max_deposit'); 73 } else { 74 $data['payment_laybuy_max_deposit'] = '50'; 75 } 76 77 if (isset($this->request->post['payment_laybuy_max_months'])) { 78 $data['payment_laybuy_max_months'] = $this->request->post['payment_laybuy_max_months']; 79 } elseif ($this->config->get('payment_laybuy_max_months')) { 80 $data['payment_laybuy_max_months'] = $this->config->get('payment_laybuy_max_months'); 81 } else { 82 $data['payment_laybuy_max_months'] = '3'; 83 } 84 85 if (isset($this->request->post['payment_laybuy_category'])) { 86 $data['payment_laybuy_category'] = $this->request->post['payment_laybuy_category']; 87 } elseif ($this->config->get('payment_laybuy_category')) { 88 $data['payment_laybuy_category'] = $this->config->get('payment_laybuy_category'); 89 } else { 90 $data['payment_laybuy_category'] = array(); 91 } 92 93 $data['categories'] = array(); 94 95 $this->load->model('catalog/category'); 96 97 foreach ($data['payment_laybuy_category'] as $category_id) { 98 $category_info = $this->model_catalog_category->getCategory($category_id); 99 100 if ($category_info) { 101 $data['categories'][] = array( 102 'category_id' => $category_info['category_id'], 103 'name' => ($category_info['path']) ? $category_info['path'] . ' > ' . $category_info['name'] : $category_info['name'] 104 ); 105 } 106 } 107 108 if (isset($this->request->post['payment_laybuy_xproducts'])) { 109 $data['payment_laybuy_xproducts'] = $this->request->post['payment_laybuy_xproducts']; 110 } else { 111 $data['payment_laybuy_xproducts'] = $this->config->get('payment_laybuy_xproducts'); 112 } 113 114 if (isset($this->request->post['payment_laybuy_customer_group'])) { 115 $data['payment_laybuy_customer_group'] = $this->request->post['payment_laybuy_customer_group']; 116 } elseif ($this->config->get('payment_laybuy_customer_group')) { 117 $data['payment_laybuy_customer_group'] = $this->config->get('payment_laybuy_customer_group'); 118 } else { 119 $data['payment_laybuy_customer_group'] = array(); 120 } 121 122 $data['customer_groups'] = array(); 123 124 $this->load->model('customer/customer_group'); 125 126 foreach ($data['payment_laybuy_customer_group'] as $customer_group_id) { 127 $customer_group_info = $this->model_customer_customer_group->getCustomerGroup($customer_group_id); 128 129 if ($customer_group_info) { 130 $data['customer_groups'][] = array( 131 'customer_group_id' => $customer_group_info['customer_group_id'], 132 'name' => $customer_group_info['name'] 133 ); 134 } 135 } 136 137 if (isset($this->request->post['payment_laybuy_logging'])) { 138 $data['payment_laybuy_logging'] = $this->request->post['payment_laybuy_logging']; 139 } else { 140 $data['payment_laybuy_logging'] = $this->config->get('payment_laybuy_logging'); 141 } 142 143 if (isset($this->request->post['payment_laybuy_total'])) { 144 $data['payment_laybuy_total'] = $this->request->post['payment_laybuy_total']; 145 } else { 146 $data['payment_laybuy_total'] = $this->config->get('payment_laybuy_total'); 147 } 148 149 if (isset($this->request->post['payment_laybuy_order_status_id_pending'])) { 150 $data['payment_laybuy_order_status_id_pending'] = $this->request->post['payment_laybuy_order_status_id_pending']; 151 } elseif ($this->config->get('payment_laybuy_order_status_id_pending')) { 152 $data['payment_laybuy_order_status_id_pending'] = $this->config->get('payment_laybuy_order_status_id_pending'); 153 } else { 154 $data['payment_laybuy_order_status_id_pending'] = '1'; 155 } 156 157 if (isset($this->request->post['payment_laybuy_order_status_id_canceled'])) { 158 $data['payment_laybuy_order_status_id_canceled'] = $this->request->post['payment_laybuy_order_status_id_canceled']; 159 } elseif ($this->config->get('payment_laybuy_order_status_id_canceled')) { 160 $data['payment_laybuy_order_status_id_canceled'] = $this->config->get('payment_laybuy_order_status_id_canceled'); 161 } else { 162 $data['payment_laybuy_order_status_id_canceled'] = '7'; 163 } 164 165 if (isset($this->request->post['payment_laybuy_order_status_id_processing'])) { 166 $data['payment_laybuy_order_status_id_processing'] = $this->request->post['payment_laybuy_order_status_id_processing']; 167 } elseif ($this->config->get('payment_laybuy_order_status_id_processing')) { 168 $data['payment_laybuy_order_status_id_processing'] = $this->config->get('payment_laybuy_order_status_id_processing'); 169 } else { 170 $data['payment_laybuy_order_status_id_processing'] = '2'; 171 } 172 173 if (isset($this->request->post['payment_laybuy_gateway_url'])) { 174 $data['payment_laybuy_gateway_url'] = $this->request->post['payment_laybuy_gateway_url']; 175 } elseif ($this->config->get('payment_laybuy_gateway_url')) { 176 $data['payment_laybuy_gateway_url'] = $this->config->get('payment_laybuy_gateway_url'); 177 } else { 178 $data['payment_laybuy_gateway_url'] = 'http://lay-buys.com/gateway/'; 179 } 180 181 if (isset($this->request->post['payment_laybuy_api_url'])) { 182 $data['payment_laybuy_api_url'] = $this->request->post['payment_laybuy_api_url']; 183 } elseif ($this->config->get('payment_laybuy_api_url')) { 184 $data['payment_laybuy_api_url'] = $this->config->get('payment_laybuy_api_url'); 185 } else { 186 $data['payment_laybuy_api_url'] = 'https://lay-buys.com/report/'; 187 } 188 189 if (isset($this->request->post['payment_laybuy_geo_zone_id'])) { 190 $data['payment_laybuy_geo_zone_id'] = $this->request->post['payment_laybuy_geo_zone_id']; 191 } else { 192 $data['payment_laybuy_geo_zone_id'] = $this->config->get('payment_laybuy_geo_zone_id'); 193 } 194 195 if (isset($this->request->post['payment_laybuy_status'])) { 196 $data['payment_laybuy_status'] = $this->request->post['payment_laybuy_status']; 197 } else { 198 $data['payment_laybuy_status'] = $this->config->get('payment_laybuy_status'); 199 } 200 201 if (isset($this->request->post['payment_laybuy_sort_order'])) { 202 $data['payment_laybuy_sort_order'] = $this->request->post['payment_laybuy_sort_order']; 203 } else { 204 $data['payment_laybuy_sort_order'] = $this->config->get('payment_laybuy_sort_order'); 205 } 206 207 $data['laybuy_cron_url'] = HTTPS_CATALOG . 'index.php?route=extension/payment/laybuy/cron&token=' . $data['payment_laybuy_token']; 208 209 if ($this->config->get('laybuy_cron_time')) { 210 $data['laybuy_cron_time'] = date($this->language->get('datetime_format'), strtotime($this->config->get('laybuy_cron_time'))); 211 } else { 212 $data['laybuy_cron_time'] = $this->language->get('text_no_cron_time'); 213 } 214 215 if (isset($this->error['warning'])) { 216 $data['error_warning'] = $this->error['warning']; 217 } else { 218 $data['error_warning'] = ''; 219 } 220 221 if (isset($this->error['laybuys_membership_id'])) { 222 $data['error_laybuys_membership_id'] = $this->error['laybuys_membership_id']; 223 } else { 224 $data['error_laybuys_membership_id'] = ''; 225 } 226 227 if (isset($this->error['laybuy_token'])) { 228 $data['error_laybuy_token'] = $this->error['laybuy_token']; 229 } else { 230 $data['error_laybuy_token'] = ''; 231 } 232 233 if (isset($this->error['laybuy_min_deposit'])) { 234 $data['error_laybuy_min_deposit'] = $this->error['laybuy_min_deposit']; 235 } else { 236 $data['error_laybuy_min_deposit'] = ''; 237 } 238 239 $this->load->model('localisation/order_status'); 240 241 $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses(); 242 243 $this->load->model('localisation/geo_zone'); 244 245 $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones(); 246 247 /* Reports tab */ 248 if (isset($this->request->get['filter_order_id'])) { 249 $filter_order_id = $this->request->get['filter_order_id']; 250 } else { 251 $filter_order_id = ''; 252 } 253 254 if (isset($this->request->get['filter_customer'])) { 255 $filter_customer = $this->request->get['filter_customer']; 256 } else { 257 $filter_customer = ''; 258 } 259 260 if (isset($this->request->get['filter_dp_percent'])) { 261 $filter_dp_percent = $this->request->get['filter_dp_percent']; 262 } else { 263 $filter_dp_percent = ''; 264 } 265 266 if (isset($this->request->get['filter_months'])) { 267 $filter_months = $this->request->get['filter_months']; 268 } else { 269 $filter_months = ''; 270 } 271 272 if (isset($this->request->get['filter_status'])) { 273 $filter_status = $this->request->get['filter_status']; 274 } else { 275 $filter_status = ''; 276 } 277 278 if (isset($this->request->get['filter_date_added'])) { 279 $filter_date_added = $this->request->get['filter_date_added']; 280 } else { 281 $filter_date_added = ''; 282 } 283 284 if (isset($this->request->get['sort'])) { 285 $sort = $this->request->get['sort']; 286 } else { 287 $sort = 'lt.order_id'; 288 } 289 290 if (isset($this->request->get['order'])) { 291 $order = $this->request->get['order']; 292 } else { 293 $order = 'DESC'; 294 } 295 296 if (isset($this->request->get['page'])) { 297 $page = $this->request->get['page']; 298 } else { 299 $page = 1; 300 } 301 302 $data['reports'] = array(); 303 304 $filter_data = array( 305 'filter_order_id' => $filter_order_id, 306 'filter_customer' => $filter_customer, 307 'filter_dp_percent' => $filter_dp_percent, 308 'filter_months' => $filter_months, 309 'filter_status' => $filter_status, 310 'filter_date_added' => $filter_date_added, 311 'sort' => $sort, 312 'order' => $order, 313 'start' => ($page - 1) * $this->config->get('config_limit_admin'), 314 'limit' => $this->config->get('config_limit_admin') 315 ); 316 317 $report_total = $this->model_extension_payment_laybuy->getTotalTransactions($filter_data); 318 319 $results = $this->model_extension_payment_laybuy->getTransactions($filter_data); 320 321 foreach ($results as $result) { 322 $customer_url = false; 323 324 $customer_id = $this->model_extension_payment_laybuy->getCustomerIdByOrderId($result['order_id']); 325 326 if ($customer_id) { 327 $customer_url = $this->url->link('customer/customer/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . (int)$customer_id, true); 328 } 329 330 $data['reports'][] = array( 331 'id' => $result['laybuy_transaction_id'], 332 'order_id' => $result['order_id'], 333 'order_url' => $this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . (int)$result['order_id'], true), 334 'customer_name' => $result['firstname'] . ' ' . $result['lastname'], 335 'customer_url' => $customer_url, 336 'amount' => $this->currency->format($result['amount'], $result['currency']), 337 'dp_percent' => $result['downpayment'], 338 'months' => $result['months'], 339 'dp_amount' => $this->currency->format($result['downpayment_amount'], $result['currency']), 340 'first_payment' => date($this->language->get('date_format_short'), strtotime($result['first_payment_due'])), 341 'last_payment' => date($this->language->get('date_format_short'), strtotime($result['last_payment_due'])), 342 'status' => $this->model_extension_payment_laybuy->getStatusLabel($result['status']), 343 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])), 344 'view' => $this->url->link('extension/payment/laybuy/transaction', 'user_token=' . $this->session->data['user_token'] . '&id=' . (int)$result['laybuy_transaction_id'], true) 345 ); 346 } 347 348 if (isset($this->request->post['selected'])) { 349 $data['selected'] = (array)$this->request->post['selected']; 350 } else { 351 $data['selected'] = array(); 352 } 353 354 $url = ''; 355 356 if (isset($this->request->get['filter_order_id'])) { 357 $url .= '&filter_order_id=' . urlencode(html_entity_decode($this->request->get['filter_order_id'], ENT_QUOTES, 'UTF-8')); 358 } 359 360 if (isset($this->request->get['filter_customer'])) { 361 $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8')); 362 } 363 364 if (isset($this->request->get['filter_dp_percent'])) { 365 $url .= '&filter_dp_percent=' . urlencode(html_entity_decode($this->request->get['filter_dp_percent'], ENT_QUOTES, 'UTF-8')); 366 } 367 368 if (isset($this->request->get['filter_months'])) { 369 $url .= '&filter_months=' . $this->request->get['filter_months']; 370 } 371 372 if (isset($this->request->get['filter_status'])) { 373 $url .= '&filter_status=' . $this->request->get['filter_status']; 374 } 375 376 if (isset($this->request->get['filter_date_added'])) { 377 $url .= '&filter_date_added=' . $this->request->get['filter_date_added']; 378 } 379 380 if ($order == 'ASC') { 381 $url .= '&order=DESC'; 382 } else { 383 $url .= '&order=ASC'; 384 } 385 386 if (isset($this->request->get['page'])) { 387 $url .= '&page=' . $this->request->get['page']; 388 } 389 390 $data['sort_order_id'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.order_id' . $url . '#reportstab', true); 391 $data['sort_customer'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=customer' . $url . '#reportstab', true); 392 $data['sort_amount'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.amount' . $url . '#reportstab', true); 393 $data['sort_dp_percent'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.downpayment' . $url . '#reportstab', true); 394 $data['sort_months'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.months' . $url . '#reportstab', true); 395 $data['sort_dp_amount'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.downpayment_amount' . $url . '#reportstab', true); 396 $data['sort_first_payment'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.first_payment_due' . $url . '#reportstab', true); 397 $data['sort_last_payment'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.last_payment_due' . $url . '#reportstab', true); 398 $data['sort_status'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.status' . $url . '#reportstab', true); 399 $data['sort_date_added'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '&sort=lt.date_added' . $url . '#reportstab', true); 400 401 $url = ''; 402 403 if (isset($this->request->get['filter_order_id'])) { 404 $url .= '&filter_order_id=' . $this->request->get['filter_order_id']; 405 } 406 407 if (isset($this->request->get['filter_customer'])) { 408 $url .= '&filter_customer=' . $this->request->get['filter_customer']; 409 } 410 411 if (isset($this->request->get['filter_dp_percent'])) { 412 $url .= '&filter_dp_percent=' . $this->request->get['filter_dp_percent']; 413 } 414 415 if (isset($this->request->get['filter_months'])) { 416 $url .= '&filter_months=' . $this->request->get['filter_months']; 417 } 418 419 if (isset($this->request->get['filter_status'])) { 420 $url .= '&filter_status=' . $this->request->get['filter_status']; 421 } 422 423 if (isset($this->request->get['filter_date_added'])) { 424 $url .= '&filter_date_added=' . $this->request->get['filter_date_added']; 425 } 426 427 if (isset($this->request->get['sort'])) { 428 $url .= '&sort=' . $this->request->get['sort']; 429 } 430 431 if (isset($this->request->get['order'])) { 432 $url .= '&order=' . $this->request->get['order']; 433 } 434 435 $pagination = new Pagination(); 436 $pagination->total = $report_total; 437 $pagination->page = $page; 438 $pagination->limit = $this->config->get('config_limit_admin'); 439 $pagination->url = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}#reportstab', true); 440 441 $data['pagination'] = $pagination->render(); 442 443 $data['results'] = sprintf($this->language->get('text_pagination'), ($report_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($report_total - $this->config->get('config_limit_admin'))) ? $report_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $report_total, ceil($report_total / $this->config->get('config_limit_admin'))); 444 445 $data['filter_order_id'] = $filter_order_id; 446 $data['filter_customer'] = $filter_customer; 447 $data['filter_dp_percent'] = $filter_dp_percent; 448 $data['filter_months'] = $filter_months; 449 $data['filter_status'] = $filter_status; 450 $data['filter_date_added'] = $filter_date_added; 451 452 $data['sort'] = $sort; 453 $data['order'] = $order; 454 455 $data['transaction_statuses'] = $this->model_extension_payment_laybuy->getTransactionStatuses(); 456 /* End of Reports Tab */ 457 458 $data['user_token'] = $this->session->data['user_token']; 459 460 if (isset($this->error['warning'])) { 461 $data['error_warning'] = $this->error['warning']; 462 } else { 463 $data['error_warning'] = ''; 464 } 465 466 if (isset($this->session->data['success'])) { 467 $data['success'] = $this->session->data['success']; 468 469 unset($this->session->data['success']); 470 } else { 471 $data['success'] = ''; 472 } 473 474 $data['header'] = $this->load->controller('common/header'); 475 $data['column_left'] = $this->load->controller('common/column_left'); 476 $data['footer'] = $this->load->controller('common/footer'); 477 478 $this->response->setOutput($this->load->view('extension/payment/laybuy', $data)); 479 } 480 481 public function fetch() { 482 $this->load->model('extension/payment/laybuy'); 483 484 $this->model_extension_payment_laybuy->log('Fetching transactions'); 485 486 if ($this->user->hasPermission('modify', 'extension/payment/laybuy')) { 487 $this->load->language('extension/payment/laybuy'); 488 489 $json = array(); 490 491 $fetched = 0; 492 493 $paypal_profile_id_array = $this->model_extension_payment_laybuy->getPayPalProfileIds(); 494 495 if ($paypal_profile_id_array) { 496 $paypal_profile_ids = ''; 497 498 foreach ($paypal_profile_id_array as $profile_id) { 499 $paypal_profile_ids .= $profile_id['paypal_profile_id'] . ','; 500 } 501 502 $paypal_profile_ids = rtrim($paypal_profile_ids, ','); 503 504 $data_string = 'mid=' . $this->config->get('payment_laybuys_membership_id') . '&' . 'profileIds=' . $paypal_profile_ids; 505 506 $this->model_extension_payment_laybuy->log('Data String: ' . $data_string); 507 508 $this->model_extension_payment_laybuy->log('API URL: ' . $this->config->get('payment_laybuy_api_url')); 509 510 $ch = curl_init(); 511 curl_setopt($ch, CURLOPT_URL, $this->config->get('payment_laybuy_api_url')); 512 curl_setopt($ch, CURLOPT_POST, true); 513 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 514 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 515 curl_setopt($ch, CURLOPT_HEADER, false); 516 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 517 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 518 $result = curl_exec($ch); 519 if (curl_errno($ch)) { 520 $this->model_extension_payment_laybuy->log('cURL error: ' . curl_errno($ch)); 521 } 522 curl_close($ch); 523 524 $results = json_decode($result, true); 525 526 $this->model_extension_payment_laybuy->log('Response: ' . print_r($results, true)); 527 528 if ($results) { 529 foreach ($results as $laybuy_ref_id => $reports) { 530 $status = $reports['status']; 531 532 $report = $reports['report']; 533 534 $transaction = array(); 535 536 $transaction = $this->model_extension_payment_laybuy->getTransactionByLayBuyRefId($laybuy_ref_id); 537 538 $order_id = $transaction['order_id']; 539 540 $paypal_profile_id = $transaction['paypal_profile_id']; 541 542 $months = $transaction['months']; 543 544 $report_content = array(); 545 546 $pending_flag = false; 547 548 $next_payment_status = $this->language->get('text_status_1'); 549 550 foreach ($report as $month => $payment) { 551 $payment['paymentDate'] = date('Y-m-d h:i:s', strtotime(str_replace('/', '-', $payment['paymentDate']))); 552 $date = date($this->language->get('date_format_short'), strtotime($payment['paymentDate'])); 553 $next_payment_date = $payment['paymentDate']; 554 555 if ($payment['type'] == 'd') { 556 $report_content[] = array( 557 'instalment' => 0, 558 'amount' => $this->currency->format($payment['amount'], $transaction['currency']), 559 'date' => $date, 560 'pp_trans_id' => $payment['txnID'], 561 'status' => $payment['paymentStatus'] 562 ); 563 } elseif ($payment['type'] == 'p') { 564 $pending_flag = true; 565 566 $report_content[] = array( 567 'instalment' => $month, 568 'amount' => $this->currency->format($payment['amount'], $transaction['currency']), 569 'date' => $date, 570 'pp_trans_id' => $payment['txnID'], 571 'status' => $payment['paymentStatus'] 572 ); 573 574 $next_payment_status = $payment['paymentStatus']; 575 } 576 } 577 578 if ($pending_flag) { 579 $start_index = $month + 1; 580 } else { 581 $start_index = $month + 2; 582 } 583 584 if ($month < $months) { 585 for ($month = 1; $month <= $months; $month++) { 586 $next_payment_date = date("Y-m-d h:i:s", strtotime($next_payment_date . " +1 month")); 587 $date = date($this->language->get('date_format_short'), strtotime($next_payment_date)); 588 589 $report_content[] = array( 590 'instalment' => $month, 591 'amount' => $this->currency->format($transaction['payment_amounts'], $transaction['currency']), 592 'date' => $date, 593 'pp_trans_id' => '', 594 'status' => $next_payment_status 595 ); 596 } 597 } 598 599 $report_content = json_encode($report_content); 600 601 switch ($status) { 602 case -1: // Cancel 603 $this->model_extension_payment_laybuy->log('Transaction #' . $transaction['laybuy_transaction_id'] . ' canceled'); 604 $this->model_extension_payment_laybuy->updateOrderStatus($order_id, $this->config->get('payment_laybuy_order_status_id_canceled'), $this->language->get('text_comment')); 605 $this->model_extension_payment_laybuy->updateTransaction($transaction['laybuy_transaction_id'], '7', $report_content, $start_index); 606 $fetched++; 607 break; 608 case 0: // Pending 609 $this->model_extension_payment_laybuy->log('Transaction #' . $transaction['laybuy_transaction_id'] . ' still pending'); 610 $this->model_extension_payment_laybuy->updateTransaction($transaction['laybuy_transaction_id'], $transaction['status'], $report_content, $start_index); 611 $fetched++; 612 break; 613 case 1: // Paid 614 $this->model_extension_payment_laybuy->log('Transaction #' . $transaction['laybuy_transaction_id'] . ' paid'); 615 $this->model_extension_payment_laybuy->updateOrderStatus($order_id, $this->config->get('payment_laybuy_order_status_id_processing'), $this->language->get('text_comment')); 616 $this->model_extension_payment_laybuy->updateTransaction($transaction['laybuy_transaction_id'], '5', $report_content, $start_index); 617 $fetched++; 618 break; 619 } 620 } 621 } 622 623 if ($fetched) { 624 $this->session->data['success'] = sprintf($this->language->get('text_fetched_some'), $fetched); 625 } else { 626 $this->session->data['success'] = $this->language->get('text_fetched_none'); 627 } 628 629 $this->response->redirect($this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'], true)); 630 } else { 631 $this->model_extension_payment_laybuy->log('No PayPal Profile IDs to update'); 632 633 $this->session->data['success'] = $this->language->get('text_fetched_none'); 634 635 $this->response->redirect($this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'], true)); 636 } 637 } else { 638 $this->model_extension_payment_laybuy->log('User does not have permission'); 639 } 640 } 641 642 public function install() { 643 if ($this->user->hasPermission('modify', 'marketplace/extension')) { 644 $this->load->model('extension/payment/laybuy'); 645 646 $this->model_extension_payment_laybuy->install(); 647 } 648 } 649 650 public function uninstall() { 651 if ($this->user->hasPermission('modify', 'marketplace/extension')) { 652 $this->load->model('extension/payment/laybuy'); 653 654 $this->model_extension_payment_laybuy->uninstall(); 655 } 656 } 657 658 public function transaction($order_page = false) { 659 $this->load->model('extension/payment/laybuy'); 660 661 $this->load->language('extension/payment/laybuy'); 662 663 if (isset($this->request->get['id'])) { 664 $id = (int)$this->request->get['id']; 665 } else { 666 $id = 0; 667 } 668 669 $data['id'] = $id; 670 671 if (!$order_page) { 672 $this->document->setTitle($this->language->get('heading_transaction_title')); 673 } 674 675 $data['breadcrumbs'] = array(); 676 677 $data['breadcrumbs'][] = array( 678 'text' => $this->language->get('text_home'), 679 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 680 ); 681 682 $data['breadcrumbs'][] = array( 683 'text' => $this->language->get('text_extension'), 684 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'], true) 685 ); 686 687 $data['breadcrumbs'][] = array( 688 'text' => $this->language->get('heading_title'), 689 'href' => $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '#reportstab', true) 690 ); 691 692 $data['breadcrumbs'][] = array( 693 'text' => $this->language->get('heading_transaction_title'), 694 'href' => $this->url->link('extension/payment/laybuy/transaction', 'user_token=' . $this->session->data['user_token'] . '&id=' . $id, true) 695 ); 696 697 $data['heading_title'] = $this->language->get('heading_transaction_title'); 698 699 $data['button_cancel'] = $this->language->get('button_cancel'); 700 701 $data['cancel'] = $this->url->link('extension/payment/laybuy', 'user_token=' . $this->session->data['user_token'] . '#reportstab', true); 702 703 $transaction_info = $this->model_extension_payment_laybuy->getTransaction($id); 704 705 if ($transaction_info) { 706 $data['initial_payments'] = $this->model_extension_payment_laybuy->getInitialPayments(); 707 708 $data['months'] = $this->model_extension_payment_laybuy->getMonths(); 709 710 $data['currency_symbol_left'] = $this->currency->getSymbolLeft($transaction_info['currency']); 711 712 $data['currency_symbol_right'] = $this->currency->getSymbolRight($transaction_info['currency']); 713 714 $data['store_url'] = HTTPS_CATALOG; 715 716 $data['api_key'] = $this->getApiKey(); 717 718 $this->load->model('sale/order'); 719 720 $order = $this->model_sale_order->getOrder($transaction_info['order_id']); 721 722 $data['order_info'] = array( 723 'currency_value' => $order['currency_value'] 724 ); 725 726 $data['total'] = $this->model_extension_payment_laybuy->getRemainingAmount($transaction_info['amount'], $transaction_info['downpayment_amount'], $transaction_info['payment_amounts'], $transaction_info['transaction']); 727 728 $data['transaction'] = array( 729 'paypal_profile_id' => $transaction_info['paypal_profile_id'], 730 'laybuy_ref_no' => $transaction_info['laybuy_ref_no'], 731 'order_id' => $transaction_info['order_id'], 732 'firstname' => $transaction_info['firstname'], 733 'lastname' => $transaction_info['lastname'], 734 'email' => $transaction_info['email'], 735 'address' => $transaction_info['address'], 736 'suburb' => $transaction_info['suburb'], 737 'state' => $transaction_info['state'], 738 'country' => $transaction_info['country'], 739 'postcode' => $transaction_info['postcode'], 740 'status_id' => $transaction_info['status'], 741 'status' => $this->model_extension_payment_laybuy->getStatusLabel($transaction_info['status']), 742 'amount' => $this->currency->format($transaction_info['amount'], $transaction_info['currency']), 743 'remaining' => $this->currency->format($this->model_extension_payment_laybuy->getRemainingAmount($transaction_info['amount'], $transaction_info['downpayment_amount'], $transaction_info['payment_amounts'], $transaction_info['transaction']), $transaction_info['currency']), 744 'downpayment' => $transaction_info['downpayment'], 745 'months' => $transaction_info['months'], 746 'downpayment_amount' => $this->currency->format($transaction_info['downpayment_amount'], $transaction_info['currency']), 747 'payment_amounts' => $this->currency->format($transaction_info['payment_amounts'], $transaction_info['currency']), 748 'first_payment_due' => date($this->language->get('date_format_short'), strtotime($transaction_info['first_payment_due'])), 749 'last_payment_due' => date($this->language->get('date_format_short'), strtotime($transaction_info['last_payment_due'])), 750 'report' => json_decode($transaction_info['report'], true) 751 ); 752 } else { 753 $data['transaction'] = array(); 754 755 $data['text_not_found'] = $this->language->get('text_not_found'); 756 } 757 758 $data['user_token'] = $this->session->data['user_token']; 759 760 if (isset($this->session->data['success'])) { 761 $data['success'] = $this->session->data['success']; 762 763 unset($this->session->data['success']); 764 } else { 765 $data['success'] = ''; 766 } 767 768 if (isset($this->session->data['error_warning'])) { 769 $data['error_warning'] = $this->session->data['error_warning']; 770 771 unset($this->session->data['error_warning']); 772 } else { 773 $data['error_warning'] = ''; 774 } 775 776 $data['header'] = $this->load->controller('common/header'); 777 $data['column_left'] = $this->load->controller('common/column_left'); 778 $data['footer'] = $this->load->controller('common/footer'); 779 780 if ($order_page) { 781 return $data; 782 } 783 784 $this->response->setOutput($this->load->view('extension/payment/laybuy_transaction', $data)); 785 } 786 787 public function cancel() { 788 $this->load->model('extension/payment/laybuy'); 789 790 $this->model_extension_payment_laybuy->log('Canceling transaction'); 791 792 if ($this->request->get['source'] == 'order') { 793 $this->model_extension_payment_laybuy->log('Called from order page'); 794 } else { 795 $this->model_extension_payment_laybuy->log('Called from extension page'); 796 } 797 798 if ($this->user->hasPermission('modify', 'extension/payment/laybuy')) { 799 $this->load->language('extension/payment/laybuy'); 800 801 $json = array(); 802 803 $id = (int)$this->request->get['id']; 804 805 $transaction_info = $this->model_extension_payment_laybuy->getTransaction($id); 806 807 $cancel = false; 808 809 if (!$transaction_info['paypal_profile_id']) { 810 $this->model_extension_payment_laybuy->log('Transaction has no paypal_profile_id'); 811 812 $cancel = true; 813 } 814 815 if (!$cancel) { 816 $data_string = 'mid=' . $this->config->get('payment_laybuys_membership_id') . '&' . 'paypal_profile_id=' . $transaction_info['paypal_profile_id']; 817 818 $this->model_extension_payment_laybuy->log('Data String: ' . $data_string); 819 820 $ch = curl_init(); 821 $url = 'https://lay-buys.com/vtmob/deal5cancel.php'; 822 curl_setopt($ch, CURLOPT_URL, $url); 823 curl_setopt($ch, CURLOPT_POST, true); 824 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 825 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 826 curl_setopt($ch, CURLOPT_HEADER, false); 827 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 828 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 829 $result = curl_exec($ch); 830 if (curl_errno($ch)) { 831 $this->model_extension_payment_laybuy->log('cURL error: ' . curl_errno($ch)); 832 } 833 curl_close($ch); 834 835 $this->model_extension_payment_laybuy->log('Response: ' . $result); 836 837 if ($result == 'success') { 838 $this->model_extension_payment_laybuy->log('Success'); 839 840 $cancel = true; 841 } else { 842 $this->model_extension_payment_laybuy->log('Failure'); 843 } 844 } 845 846 if ($cancel) { 847 $this->model_extension_payment_laybuy->log('Transaction canceled'); 848 849 $report_content = json_decode($transaction_info['report'], true); 850 851 foreach ($report_content as &$array) { 852 $array['status'] = str_replace('Pending', 'Canceled', $array['status']); 853 } 854 855 $report_content = json_encode($report_content); 856 857 $this->model_extension_payment_laybuy->updateTransaction($transaction_info['laybuy_transaction_id'], '7', $report_content, $transaction_info['transaction']); 858 859 $json['success'] = $this->language->get('text_cancel_success'); 860 861 $json['order_id'] = $transaction_info['order_id']; 862 863 $json['order_status_id'] = $this->config->get('payment_laybuy_order_status_id_canceled'); 864 865 $json['comment'] = sprintf($this->language->get('text_comment_canceled'), $transaction_info['paypal_profile_id']); 866 } else { 867 $json['error'] = $this->language->get('text_cancel_failure'); 868 } 869 870 if ($this->request->get['source'] == 'order') { 871 $json['reload'] = $this->url->link('sale/order/info', 'order_id=' . (int)$transaction_info['order_id'] . '&user_token=' . $this->session->data['user_token'], true); 872 } else { 873 $json['reload'] = $this->url->link('extension/payment/laybuy/transaction', 'user_token=' . $this->session->data['user_token'] . '&id=' . $id, true); 874 } 875 876 $this->response->setOutput(json_encode($json)); 877 } else { 878 $this->model_extension_payment_laybuy->log('User does not have permission'); 879 } 880 } 881 882 public function revise() { 883 $this->load->model('extension/payment/laybuy'); 884 885 $this->model_extension_payment_laybuy->log('Revising transaction'); 886 887 if ($this->request->get['source'] == 'order') { 888 $this->model_extension_payment_laybuy->log('Called from order page'); 889 } else { 890 $this->model_extension_payment_laybuy->log('Called from extension page'); 891 } 892 893 if ($this->user->hasPermission('modify', 'extension/payment/laybuy')) { 894 if ($this->request->server['REQUEST_METHOD'] == 'POST') { 895 $this->load->language('extension/payment/laybuy'); 896 897 $json = array(); 898 899 $payment_type = $this->request->post['payment_type']; 900 901 $amount = $this->request->post['amount']; 902 903 $initial = $this->request->post['INIT']; 904 905 $months = $this->request->post['MONTHS']; 906 907 $id = $this->request->get['id']; 908 909 $transaction_info = $this->model_extension_payment_laybuy->getTransaction($id); 910 911 $original = $new = $transaction_info; 912 913 $original['transaction_id'] = $new['transaction_id'] = $transaction_info['laybuy_transaction_id']; 914 915 $original['payment_type'] = $new['payment_type'] = $payment_type; 916 917 $original['type'] = 'Original'; 918 919 $new['type'] = 'New'; 920 $new['status'] = '50'; 921 $new['amount'] = $amount; 922 $new['downpayment'] = $initial; 923 $new['months'] = $months; 924 925 $collection = $this->model_extension_payment_laybuy->getRevisedTransactions($id); 926 927 $this->model_extension_payment_laybuy->log('Collection: ' . print_r($collection, true)); 928 929 if (count($collection) == 2) { 930 $this->model_extension_payment_laybuy->log('Collection == 2'); 931 932 foreach ($collection as $request) { 933 $this->model_extension_payment_laybuy->log('request: ' . print_r($request, true)); 934 935 if ($request['type'] == 'Original') { 936 $this->model_extension_payment_laybuy->log('Original: ' . print_r($original, true)); 937 938 $this->model_extension_payment_laybuy->updateRevisedTransaction($id, $original); 939 } elseif ($request['type'] == 'New') { 940 $this->model_extension_payment_laybuy->log('New: ' . print_r($new, true)); 941 942 $this->model_extension_payment_laybuy->updateRevisedTransaction($id, $new); 943 944 $revised_transaction = $this->model_extension_payment_laybuy->getRevisedTransaction($id); 945 } 946 } 947 } else { 948 $this->model_extension_payment_laybuy->log('Collection != 2'); 949 950 $this->model_extension_payment_laybuy->addRevisedTransaction($original); 951 952 $laybuy_revise_request_id = $this->model_extension_payment_laybuy->addRevisedTransaction($new); 953 954 $this->model_extension_payment_laybuy->log('$laybuy_revise_request_id: ' . $laybuy_revise_request_id); 955 956 $revised_transaction = $this->model_extension_payment_laybuy->getRevisedTransaction($laybuy_revise_request_id); 957 } 958 959 $this->model_extension_payment_laybuy->log('Revised transaction: ' . print_r($revised_transaction, true)); 960 961 if ($revised_transaction['payment_type'] == '1') { 962 $pp = '1'; 963 $pplan = '1'; 964 } else { 965 $pp = '0'; 966 $pplan = '0'; 967 } 968 969 $data = array(); 970 971 $data['mid'] = $this->config->get('payment_laybuys_membership_id'); 972 $data['eml'] = $revised_transaction['email']; 973 $data['prc'] = $revised_transaction['amount']; 974 $data['curr'] = $revised_transaction['currency']; 975 $data['pp'] = $pp; 976 $data['pplan'] = $pplan; 977 $data['init'] = $initial; 978 $data['mnth'] = $months; 979 $data['convrate'] = '1'; 980 $data['id'] = $revised_transaction['laybuy_revise_request_id'] . '-' . $revised_transaction['order_id'] . ':' . md5($this->config->get('payment_laybuy_token')); 981 $data['RETURNURL'] = HTTPS_CATALOG . 'index.php?route=extension/payment/laybuy/reviseCallback'; 982 $data['CANCELURL'] = HTTPS_CATALOG . 'index.php?route=extension/payment/laybuy/reviseCancel'; 983 984 $data_string = ''; 985 986 foreach ($data as $param => $value) { 987 $data_string .= $param . '=' . $value . '&'; 988 } 989 990 $data_string = rtrim($data_string, '&'); 991 992 $this->model_extension_payment_laybuy->log('Data String: ' . $data_string); 993 994 $ch = curl_init(); 995 $url = 'https://lay-buys.com/vtmob/deal5.php'; 996 curl_setopt($ch, CURLOPT_URL, $url); 997 curl_setopt($ch, CURLOPT_POST, true); 998 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 999 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 1000 curl_setopt($ch, CURLOPT_HEADER, false); 1001 curl_setopt($ch, CURLOPT_TIMEOUT, 30); 1002 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 1003 $result = curl_exec($ch); 1004 if (curl_errno($ch)) { 1005 $this->model_extension_payment_laybuy->log('cURL error: ' . curl_errno($ch)); 1006 } 1007 curl_close($ch); 1008 1009 if ($result == 'success') { 1010 $this->model_extension_payment_laybuy->log('Success'); 1011 1012 $this->model_extension_payment_laybuy->updateTransactionStatus($id, '50'); 1013 1014 $json['success'] = $this->language->get('text_revise_success'); 1015 } else { 1016 $this->model_extension_payment_laybuy->log('Failure'); 1017 1018 $this->model_extension_payment_laybuy->log('Response: ' . print_r($result, true)); 1019 1020 $json['error'] = $this->language->get('text_revise_failure'); 1021 } 1022 1023 if ($this->request->get['source'] == 'order') { 1024 $json['reload'] = $this->url->link('sale/order/info', 'order_id=' . (int)$transaction_info['order_id'] . '&user_token=' . $this->session->data['user_token'], true); 1025 } else { 1026 $json['reload'] = $this->url->link('extension/payment/laybuy/transaction', 'user_token=' . $this->session->data['user_token'] . '&id=' . $id, true); 1027 } 1028 1029 $this->response->setOutput(json_encode($json)); 1030 } else { 1031 $this->model_extension_payment_laybuy->log('No $_POST data'); 1032 } 1033 } else { 1034 $this->model_extension_payment_laybuy->log('User does not have permission'); 1035 } 1036 } 1037 1038 public function autocomplete() { 1039 $json = array(); 1040 1041 if (isset($this->request->get['filter_customer_group'])) { 1042 $this->load->model('customer/customer_group'); 1043 1044 $results = $this->model_customer_customer_group->getCustomerGroups(); 1045 1046 foreach ($results as $result) { 1047 $json[] = array( 1048 'customer_group_id' => $result['customer_group_id'], 1049 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')) 1050 ); 1051 } 1052 } 1053 1054 $this->response->addHeader('Content-Type: application/json'); 1055 $this->response->setOutput(json_encode($json)); 1056 } 1057 1058 public function order() { 1059 if ($this->config->get('payment_laybuy_status')) { 1060 $this->load->model('extension/payment/laybuy'); 1061 1062 $this->load->language('extension/payment/laybuy'); 1063 1064 $order_id = $this->request->get['order_id']; 1065 1066 $transaction_info = $this->model_extension_payment_laybuy->getTransactionByOrderId($order_id); 1067 1068 $laybuy_transaction_id = $transaction_info['laybuy_transaction_id']; 1069 1070 $this->request->get['id'] = $laybuy_transaction_id; 1071 1072 $data = $this->transaction(true); 1073 1074 $data['store_url'] = HTTPS_CATALOG; 1075 1076 $data['api_key'] = $this->getApiKey(); 1077 1078 return $this->load->view('extension/payment/laybuy_order', $data); 1079 } 1080 } 1081 1082 private function getApiKey() { 1083 $this->load->model('extension/payment/laybuy'); 1084 1085 $this->model_extension_payment_laybuy->log('Getting API key'); 1086 1087 $this->load->model('user/api'); 1088 1089 $api_info = $this->model_user_api->getApi($this->config->get('config_api_id')); 1090 1091 if ($api_info) { 1092 $this->model_extension_payment_laybuy->log('API key: ' . $api_info['key']); 1093 1094 return $api_info['key']; 1095 } else { 1096 $this->model_extension_payment_laybuy->log('No API info'); 1097 1098 return; 1099 } 1100 } 1101 1102 protected function validate() { 1103 if (!$this->user->hasPermission('modify', 'extension/payment/laybuy')) { 1104 $this->error['warning'] = $this->language->get('error_permission'); 1105 } 1106 1107 if (!$this->request->post['payment_laybuys_membership_id']) { 1108 $this->error['laybuys_membership_id'] = $this->language->get('error_membership_id'); 1109 } 1110 1111 if (!$this->request->post['payment_laybuy_token']) { 1112 $this->error['laybuy_token'] = $this->language->get('error_token'); 1113 } 1114 1115 if ($this->request->post['payment_laybuy_min_deposit'] > $this->request->post['payment_laybuy_max_deposit']) { 1116 $this->error['laybuy_min_deposit'] = $this->language->get('error_min_deposit'); 1117 } 1118 1119 return !$this->error; 1120 } 1121 }