PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/payment/bluepay_hosted.php

https://gitlab.com/shapcy/opencart
PHP | 235 lines | 182 code | 53 blank | 0 comment | 22 complexity | ac9ff5fa8e6cc177513398310a043e8b MD5 | raw file
  1. <?php
  2. class ModelPaymentBluePayHosted extends Model {
  3. public function install() {
  4. $this->db->query("
  5. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "bluepay_hosted_order` (
  6. `bluepay_hosted_order_id` INT(11) NOT NULL AUTO_INCREMENT,
  7. `order_id` INT(11) NOT NULL,
  8. `transaction_id` VARCHAR(50),
  9. `date_added` DATETIME NOT NULL,
  10. `date_modified` DATETIME NOT NULL,
  11. `release_status` INT(1) DEFAULT 0,
  12. `void_status` INT(1) DEFAULT 0,
  13. `rebate_status` INT(1) DEFAULT 0,
  14. `currency_code` CHAR(3) NOT NULL,
  15. `total` DECIMAL( 10, 2 ) NOT NULL,
  16. PRIMARY KEY (`bluepay_hosted_order_id`)
  17. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  18. $this->db->query("
  19. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "bluepay_hosted_order_transaction` (
  20. `bluepay_hosted_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
  21. `bluepay_hosted_order_id` INT(11) NOT NULL,
  22. `date_added` DATETIME NOT NULL,
  23. `type` ENUM('auth', 'payment', 'rebate', 'void') DEFAULT NULL,
  24. `amount` DECIMAL( 10, 2 ) NOT NULL,
  25. PRIMARY KEY (`bluepay_hosted_order_transaction_id`)
  26. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  27. $this->db->query("
  28. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "bluepay_hosted_card` (
  29. `card_id` INT(11) NOT NULL AUTO_INCREMENT,
  30. `customer_id` INT(11) NOT NULL,
  31. `token` VARCHAR(50) NOT NULL,
  32. `digits` VARCHAR(4) NOT NULL,
  33. `expiry` VARCHAR(5) NOT NULL,
  34. `type` VARCHAR(50) NOT NULL,
  35. PRIMARY KEY (`card_id`)
  36. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  37. }
  38. public function uninstall() {
  39. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "bluepay_hosted_order`;");
  40. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "bluepay_hosted_order_transaction`;");
  41. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "bluepay_hosted_card`;");
  42. }
  43. public function void($order_id) {
  44. $bluepay_hosted_order = $this->getOrder($order_id);
  45. if (!empty($bluepay_hosted_order) && $bluepay_hosted_order['release_status'] == 1) {
  46. $void_data = array();
  47. $void_data['MERCHANT'] = $this->config->get('bluepay_hosted_account_id');
  48. $void_data["TRANSACTION_TYPE"] = 'VOID';
  49. $void_data["MODE"] = strtoupper($this->config->get('bluepay_hosted_test'));
  50. $void_data["RRNO"] = $bluepay_hosted_order['transaction_id'];
  51. $void_data['APPROVED_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  52. $void_data['DECLINED_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  53. $void_data['MISSING_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  54. if (isset($this->request->server["REMOTE_ADDR"])) {
  55. $void_data["REMOTE_IP"] = $this->request->server["REMOTE_ADDR"];
  56. }
  57. $tamper_proof_data = $this->config->get('bluepay_hosted_secret_key') . $void_data['MERCHANT'] . $void_data["TRANSACTION_TYPE"] . $void_data["RRNO"] . $void_data["MODE"];
  58. $void_data["TAMPER_PROOF_SEAL"] = md5($tamper_proof_data);
  59. $this->logger('$void_data:\r\n' . print_r($void_data, 1));
  60. $response_data = $this->sendCurl('https://secure.bluepay.com/interfaces/bp10emu', $void_data);
  61. return $response_data;
  62. } else {
  63. return false;
  64. }
  65. }
  66. public function updateVoidStatus($bluepay_hosted_order_id, $status) {
  67. $this->logger('$bluepay_hosted_order_id:\r\n' . print_r($bluepay_hosted_order_id, 1));
  68. $this->logger('$status:\r\n' . print_r($status, 1));
  69. $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `void_status` = '" . (int)$status . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'");
  70. }
  71. public function release($order_id, $amount) {
  72. $bluepay_hosted_order = $this->getOrder($order_id);
  73. $total_released = $this->getTotalReleased($bluepay_hosted_order['bluepay_hosted_order_id']);
  74. if (!empty($bluepay_hosted_order) && $bluepay_hosted_order['release_status'] == 0 && ($total_released + $amount <= $bluepay_hosted_order['total'])) {
  75. $release_data = array();
  76. $release_data['MERCHANT'] = $this->config->get('bluepay_hosted_account_id');
  77. $release_data["TRANSACTION_TYPE"] = 'CAPTURE';
  78. $release_data["MODE"] = strtoupper($this->config->get('bluepay_hosted_test'));
  79. $release_data["RRNO"] = $bluepay_hosted_order['transaction_id'];
  80. $release_data['APPROVED_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  81. $release_data['DECLINED_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  82. $release_data['MISSING_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  83. if (isset($this->request->server["REMOTE_ADDR"])) {
  84. $release_data["REMOTE_IP"] = $this->request->server["REMOTE_ADDR"];
  85. }
  86. $tamper_proof_data = $this->config->get('bluepay_hosted_secret_key') . $release_data['MERCHANT'] . $release_data["TRANSACTION_TYPE"] . $release_data["RRNO"] . $release_data["MODE"];
  87. $release_data["TAMPER_PROOF_SEAL"] = md5($tamper_proof_data);
  88. $response_data = $this->sendCurl('https://secure.bluepay.com/interfaces/bp10emu', $release_data);
  89. return $response_data;
  90. } else {
  91. return false;
  92. }
  93. }
  94. public function updateReleaseStatus($bluepay_hosted_order_id, $status) {
  95. $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `release_status` = '" . (int)$status . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'");
  96. }
  97. public function rebate($order_id, $amount) {
  98. $bluepay_hosted_order = $this->getOrder($order_id);
  99. if (!empty($bluepay_hosted_order) && $bluepay_hosted_order['rebate_status'] != 1) {
  100. $rebate_data = array();
  101. $rebate_data['MERCHANT'] = $this->config->get('bluepay_hosted_account_id');
  102. $rebate_data["TRANSACTION_TYPE"] = 'REFUND';
  103. $rebate_data["MODE"] = strtoupper($this->config->get('bluepay_hosted_test'));
  104. $rebate_data["RRNO"] = $bluepay_hosted_order['transaction_id'];
  105. $rebate_data["AMOUNT"] = $amount;
  106. $rebate_data['APPROVED_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  107. $rebate_data['DECLINED_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  108. $rebate_data['MISSING_URL'] = HTTP_CATALOG . 'index.php?route=payment/bluepay_hosted/adminCallback';
  109. if (isset($this->request->server["REMOTE_ADDR"])) {
  110. $rebate_data["REMOTE_IP"] = $this->request->server["REMOTE_ADDR"];
  111. }
  112. $tamper_proof_data = $this->config->get('bluepay_hosted_secret_key') . $rebate_data['MERCHANT'] . $rebate_data["TRANSACTION_TYPE"] . $rebate_data['AMOUNT'] . $rebate_data["RRNO"] . $rebate_data["MODE"];
  113. $rebate_data["TAMPER_PROOF_SEAL"] = md5($tamper_proof_data);
  114. $response_data = $this->sendCurl('https://secure.bluepay.com/interfaces/bp10emu', $rebate_data);
  115. return $response_data;
  116. } else {
  117. return false;
  118. }
  119. }
  120. public function updateRebateStatus($bluepay_hosted_order_id, $status) {
  121. $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `rebate_status` = '" . (int)$status . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'");
  122. }
  123. public function updateTransactionId($bluepay_hosted_order_id, $transaction_id) {
  124. $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `transaction_id` = '" . (int)$transaction_id . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'");
  125. }
  126. public function getOrder($order_id) {
  127. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "bluepay_hosted_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
  128. if ($qry->num_rows) {
  129. $order = $qry->row;
  130. $order['transactions'] = $this->getTransactions($order['bluepay_hosted_order_id']);
  131. return $order;
  132. } else {
  133. return false;
  134. }
  135. }
  136. private function getTransactions($bluepay_hosted_order_id) {
  137. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "bluepay_hosted_order_transaction` WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'");
  138. if ($qry->num_rows) {
  139. return $qry->rows;
  140. } else {
  141. return false;
  142. }
  143. }
  144. public function addTransaction($bluepay_hosted_order_id, $type, $total) {
  145. $this->logger('$type:\r\n' . print_r($type, 1));
  146. $this->logger('$total:\r\n' . print_r($total, 1));
  147. $this->db->query("INSERT INTO `" . DB_PREFIX . "bluepay_hosted_order_transaction` SET `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'");
  148. }
  149. public function getTotalReleased($bluepay_hosted_order_id) {
  150. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "bluepay_hosted_order_transaction` WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "' AND (`type` = 'payment' OR `type` = 'rebate')");
  151. return (float)$query->row['total'];
  152. }
  153. public function getTotalRebated($bluepay_hosted_order_id) {
  154. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "bluepay_hosted_order_transaction` WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "' AND 'rebate'");
  155. return (float)$query->row['total'];
  156. }
  157. public function sendCurl($url, $post_data) {
  158. $curl = curl_init($url);
  159. curl_setopt($curl, CURLOPT_PORT, 443);
  160. curl_setopt($curl, CURLOPT_HEADER, 0);
  161. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  162. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  163. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  164. curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
  165. curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
  166. curl_setopt($curl, CURLOPT_POST, 1);
  167. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
  168. $response_data = curl_exec($curl);
  169. curl_close($curl);
  170. return json_decode($response_data, true);
  171. }
  172. public function adminCallback() {
  173. $this->response->addHeader('Content-Type: application/json');
  174. $this->response->setOutput(json_encode($this->request->get));
  175. }
  176. public function logger($message) {
  177. if ($this->config->get('bluepay_hosted_debug') == 1) {
  178. $log = new Log('bluepay_hosted.log');
  179. $log->write($message);
  180. }
  181. }
  182. }