PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/payment/bluepay_hosted.php

https://gitlab.com/reclamare/mao
PHP | 231 lines | 179 code | 52 blank | 0 comment | 22 complexity | e4d29699ef405a6d5b6a9a7ead86176c 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"] = 'VOID';
  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 getOrder($order_id) {
  124. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "bluepay_hosted_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
  125. if ($qry->num_rows) {
  126. $order = $qry->row;
  127. $order['transactions'] = $this->getTransactions($order['bluepay_hosted_order_id']);
  128. return $order;
  129. } else {
  130. return false;
  131. }
  132. }
  133. private function getTransactions($bluepay_hosted_order_id) {
  134. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "bluepay_hosted_order_transaction` WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'");
  135. if ($qry->num_rows) {
  136. return $qry->rows;
  137. } else {
  138. return false;
  139. }
  140. }
  141. public function addTransaction($bluepay_hosted_order_id, $type, $total) {
  142. $this->logger('$type:\r\n' . print_r($type, 1));
  143. $this->logger('$total:\r\n' . print_r($total, 1));
  144. $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 . "'");
  145. }
  146. public function getTotalReleased($bluepay_hosted_order_id) {
  147. $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')");
  148. return (float)$query->row['total'];
  149. }
  150. public function getTotalRebated($bluepay_hosted_order_id) {
  151. $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'");
  152. return (float)$query->row['total'];
  153. }
  154. public function sendCurl($url, $post_data) {
  155. $curl = curl_init($url);
  156. curl_setopt($curl, CURLOPT_PORT, 443);
  157. curl_setopt($curl, CURLOPT_HEADER, 0);
  158. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  159. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  160. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  161. curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
  162. curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
  163. curl_setopt($curl, CURLOPT_POST, 1);
  164. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
  165. $response_data = curl_exec($curl);
  166. curl_close($curl);
  167. return json_decode($response_data, true);
  168. }
  169. public function adminCallback() {
  170. $this->response->addHeader('Content-Type: application/json');
  171. $this->response->setOutput(json_encode($this->request->get));
  172. }
  173. public function logger($message) {
  174. if ($this->config->get('bluepay_hosted_debug') == 1) {
  175. $log = new Log('bluepay_hosted.log');
  176. $log->write($message);
  177. }
  178. }
  179. }