PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/catalog/model/extension/payment/cardconnect.php

https://gitlab.com/dadangnh/sb1-bon
PHP | 171 lines | 129 code | 42 blank | 0 comment | 11 complexity | 639da1c514b044bbc4ecf21f7f5da847 MD5 | raw file
  1. <?php
  2. class ModelExtensionPaymentCardConnect extends Model {
  3. public function getMethod($address, $total) {
  4. $this->load->language('extension/payment/cardconnect');
  5. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$this->config->get('cardconnect_geo_zone') . "' AND `country_id` = '" . (int)$address['country_id'] . "' AND (`zone_id` = '" . (int)$address['zone_id'] . "' OR `zone_id` = '0')");
  6. if ($this->config->get('cardconnect_total') > 0 && $this->config->get('cardconnect_total') > $total) {
  7. $status = false;
  8. } elseif (!$this->config->get('cardconnect_geo_zone')) {
  9. $status = true;
  10. } elseif ($query->num_rows) {
  11. $status = true;
  12. } else {
  13. $status = false;
  14. }
  15. $method_data = array();
  16. if ($status) {
  17. $method_data = array(
  18. 'code' => 'cardconnect',
  19. 'title' => $this->language->get('text_title'),
  20. 'terms' => '',
  21. 'sort_order' => $this->config->get('cardconnect_sort_order')
  22. );
  23. }
  24. return $method_data;
  25. }
  26. public function getCardTypes() {
  27. $cards = array();
  28. $cards[] = array(
  29. 'text' => 'Visa',
  30. 'value' => 'VISA'
  31. );
  32. $cards[] = array(
  33. 'text' => 'MasterCard',
  34. 'value' => 'MASTERCARD'
  35. );
  36. $cards[] = array(
  37. 'text' => 'Discover Card',
  38. 'value' => 'DISCOVER'
  39. );
  40. $cards[] = array(
  41. 'text' => 'American Express',
  42. 'value' => 'AMEX'
  43. );
  44. return $cards;
  45. }
  46. public function getMonths() {
  47. $months = array();
  48. for ($i = 1; $i <= 12; $i++) {
  49. $months[] = array(
  50. 'text' => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)),
  51. 'value' => sprintf('%02d', $i)
  52. );
  53. }
  54. return $months;
  55. }
  56. public function getYears() {
  57. $years = array();
  58. $today = getdate();
  59. for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
  60. $years[] = array(
  61. 'text' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
  62. 'value' => strftime('%y', mktime(0, 0, 0, 1, 1, $i))
  63. );
  64. }
  65. return $years;
  66. }
  67. public function getCard($token, $customer_id) {
  68. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardconnect_card` WHERE `token` = '" . $this->db->escape($token) . "' AND `customer_id` = '" . (int)$customer_id . "'");
  69. if ($query->num_rows) {
  70. return $query->row;
  71. } else {
  72. return false;
  73. }
  74. }
  75. public function getCards($customer_id) {
  76. $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardconnect_card` WHERE `customer_id` = '" . (int)$customer_id . "'");
  77. return $query->rows;
  78. }
  79. public function addCard($cardconnect_order_id, $customer_id, $profileid, $token, $type, $account, $expiry) {
  80. $this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_card` SET `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "', `customer_id` = '" . (int)$customer_id . "', `profileid` = '" . $this->db->escape($profileid) . "', `token` = '" . $this->db->escape($token) . "', `type` = '" . $this->db->escape($type) . "', `account` = '" . $this->db->escape($account) . "', `expiry` = '" . $this->db->escape($expiry) . "', `date_added` = NOW()");
  81. }
  82. public function deleteCard($token, $customer_id) {
  83. $this->db->query("DELETE FROM `" . DB_PREFIX . "cardconnect_card` WHERE `token` = '" . $this->db->escape($token) . "' AND `customer_id` = '" . (int)$customer_id . "'");
  84. }
  85. public function addOrder($order_info, $payment_method) {
  86. $this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_order` SET `order_id` = '" . (int)$order_info['order_id'] . "', `customer_id` = '" . (int)$this->customer->getId() . "', `payment_method` = '" . $this->db->escape($payment_method) . "', `retref` = '" . $this->db->escape($order_info['retref']) . "', `authcode` = '" . $this->db->escape($order_info['authcode']) . "', `currency_code` = '" . $this->db->escape($order_info['currency_code']) . "', `total` = '" . $this->currency->format($order_info['total'], $order_info['currency_code'], false, false) . "', `date_added` = NOW()");
  87. return $this->db->getLastId();
  88. }
  89. public function addTransaction($cardconnect_order_id, $type, $status, $order_info) {
  90. $this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_order_transaction` SET `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "', `type` = '" . $this->db->escape($type) . "', `retref` = '" . $this->db->escape($order_info['retref']) . "', `amount` = '" . (float)$this->currency->format($order_info['total'], $order_info['currency_code'], false, false) . "', `status` = '" . $this->db->escape($status) . "', `date_modified` = NOW(), `date_added` = NOW()");
  91. }
  92. public function getSettlementStatuses($merchant_id, $date) {
  93. $this->log('Getting settlement statuses from CardConnect');
  94. $url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/settlestat?merchid=' . $merchant_id . '&date=' . $date;
  95. $header = array();
  96. $header[] = 'Content-type: application/json';
  97. $header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password'));
  98. $this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true));
  99. $this->model_extension_payment_cardconnect->log('URL: ' . $url);
  100. $ch = curl_init();
  101. curl_setopt($ch, CURLOPT_URL, $url);
  102. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  103. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  105. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  106. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  107. $response_data = curl_exec($ch);
  108. if (curl_errno($ch)) {
  109. $this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch));
  110. }
  111. curl_close($ch);
  112. $response_data = json_decode($response_data, true);
  113. $this->log('Response: ' . print_r($response_data, true));
  114. return $response_data;
  115. }
  116. public function updateTransactionStatusByRetref($retref, $status) {
  117. $this->db->query("UPDATE `" . DB_PREFIX . "cardconnect_order_transaction` SET `status` = '" . $this->db->escape($status) . "', `date_modified` = NOW() WHERE `retref` = '" . $this->db->escape($retref) . "'");
  118. }
  119. public function updateCronRunTime() {
  120. $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE `key` = 'cardconnect_cron_time'");
  121. $this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `store_id` = '0', `code` = 'cardconnect', `key` = 'cardconnect_cron_time', `value` = NOW(), `serialized` = '0'");
  122. }
  123. public function log($data) {
  124. if ($this->config->get('cardconnect_logging')) {
  125. $log = new Log('cardconnect.log');
  126. $log->write($data);
  127. }
  128. }
  129. }