bluepay_hosted.php (10558B)
1 <?php 2 class ModelExtensionPaymentBluePayHosted 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 19 $this->db->query(" 20 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "bluepay_hosted_order_transaction` ( 21 `bluepay_hosted_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT, 22 `bluepay_hosted_order_id` INT(11) NOT NULL, 23 `date_added` DATETIME NOT NULL, 24 `type` ENUM('auth', 'payment', 'rebate', 'void') DEFAULT NULL, 25 `amount` DECIMAL( 10, 2 ) NOT NULL, 26 PRIMARY KEY (`bluepay_hosted_order_transaction_id`) 27 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 28 29 $this->db->query(" 30 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "bluepay_hosted_card` ( 31 `card_id` INT(11) NOT NULL AUTO_INCREMENT, 32 `customer_id` INT(11) NOT NULL, 33 `token` VARCHAR(50) NOT NULL, 34 `digits` VARCHAR(4) NOT NULL, 35 `expiry` VARCHAR(5) NOT NULL, 36 `type` VARCHAR(50) NOT NULL, 37 PRIMARY KEY (`card_id`) 38 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 39 } 40 41 public function uninstall() { 42 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "bluepay_hosted_order`;"); 43 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "bluepay_hosted_order_transaction`;"); 44 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "bluepay_hosted_card`;"); 45 } 46 47 public function void($order_id) { 48 $bluepay_hosted_order = $this->getOrder($order_id); 49 50 if (!empty($bluepay_hosted_order) && $bluepay_hosted_order['release_status'] == 1) { 51 52 $void_data = array(); 53 54 $void_data['MERCHANT'] = $this->config->get('payment_bluepay_hosted_account_id'); 55 $void_data["TRANSACTION_TYPE"] = 'VOID'; 56 $void_data["MODE"] = strtoupper($this->config->get('payment_bluepay_hosted_test')); 57 $void_data["RRNO"] = $bluepay_hosted_order['transaction_id']; 58 59 $void_data['APPROVED_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 60 $void_data['DECLINED_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 61 $void_data['MISSING_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 62 63 if (isset($this->request->server["REMOTE_ADDR"])) { 64 $void_data["REMOTE_IP"] = $this->request->server["REMOTE_ADDR"]; 65 } 66 67 $tamper_proof_data = $this->config->get('payment_bluepay_hosted_secret_key') . $void_data['MERCHANT'] . $void_data["TRANSACTION_TYPE"] . $void_data["RRNO"] . $void_data["MODE"]; 68 69 $void_data["TAMPER_PROOF_SEAL"] = md5($tamper_proof_data); 70 71 $this->logger('$void_data:\r\n' . print_r($void_data, 1)); 72 73 $response_data = $this->sendCurl('https://secure.bluepay.com/interfaces/bp10emu', $void_data); 74 75 return $response_data; 76 } else { 77 return false; 78 } 79 } 80 81 public function updateVoidStatus($bluepay_hosted_order_id, $status) { 82 $this->logger('$bluepay_hosted_order_id:\r\n' . print_r($bluepay_hosted_order_id, 1)); 83 $this->logger('$status:\r\n' . print_r($status, 1)); 84 $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `void_status` = '" . (int)$status . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'"); 85 } 86 87 public function release($order_id, $amount) { 88 $bluepay_hosted_order = $this->getOrder($order_id); 89 $total_released = $this->getTotalReleased($bluepay_hosted_order['bluepay_hosted_order_id']); 90 91 if (!empty($bluepay_hosted_order) && $bluepay_hosted_order['release_status'] == 0 && ($total_released + $amount <= $bluepay_hosted_order['total'])) { 92 $release_data = array(); 93 94 $release_data['MERCHANT'] = $this->config->get('payment_bluepay_hosted_account_id'); 95 $release_data["TRANSACTION_TYPE"] = 'CAPTURE'; 96 $release_data["MODE"] = strtoupper($this->config->get('payment_bluepay_hosted_test')); 97 $release_data["RRNO"] = $bluepay_hosted_order['transaction_id']; 98 99 $release_data['APPROVED_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 100 $release_data['DECLINED_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 101 $release_data['MISSING_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 102 103 if (isset($this->request->server["REMOTE_ADDR"])) { 104 $release_data["REMOTE_IP"] = $this->request->server["REMOTE_ADDR"]; 105 } 106 107 $tamper_proof_data = $this->config->get('payment_bluepay_hosted_secret_key') . $release_data['MERCHANT'] . $release_data["TRANSACTION_TYPE"] . $release_data["RRNO"] . $release_data["MODE"]; 108 109 $release_data["TAMPER_PROOF_SEAL"] = md5($tamper_proof_data); 110 111 $response_data = $this->sendCurl('https://secure.bluepay.com/interfaces/bp10emu', $release_data); 112 113 return $response_data; 114 } else { 115 return false; 116 } 117 } 118 119 public function updateReleaseStatus($bluepay_hosted_order_id, $status) { 120 $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `release_status` = '" . (int)$status . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'"); 121 } 122 123 public function rebate($order_id, $amount) { 124 $bluepay_hosted_order = $this->getOrder($order_id); 125 126 if (!empty($bluepay_hosted_order) && $bluepay_hosted_order['rebate_status'] != 1) { 127 $rebate_data = array(); 128 129 $rebate_data['MERCHANT'] = $this->config->get('payment_bluepay_hosted_account_id'); 130 $rebate_data["TRANSACTION_TYPE"] = 'REFUND'; 131 $rebate_data["MODE"] = strtoupper($this->config->get('payment_bluepay_hosted_test')); 132 $rebate_data["RRNO"] = $bluepay_hosted_order['transaction_id']; 133 $rebate_data["AMOUNT"] = $amount; 134 $rebate_data['APPROVED_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 135 $rebate_data['DECLINED_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 136 $rebate_data['MISSING_URL'] = HTTP_CATALOG . 'index.php?route=extension/payment/bluepay_hosted/adminCallback'; 137 138 if (isset($this->request->server["REMOTE_ADDR"])) { 139 $rebate_data["REMOTE_IP"] = $this->request->server["REMOTE_ADDR"]; 140 } 141 142 $tamper_proof_data = $this->config->get('payment_bluepay_hosted_secret_key') . $rebate_data['MERCHANT'] . $rebate_data["TRANSACTION_TYPE"] . $rebate_data['AMOUNT'] . $rebate_data["RRNO"] . $rebate_data["MODE"]; 143 144 $rebate_data["TAMPER_PROOF_SEAL"] = md5($tamper_proof_data); 145 146 $response_data = $this->sendCurl('https://secure.bluepay.com/interfaces/bp10emu', $rebate_data); 147 148 return $response_data; 149 } else { 150 return false; 151 } 152 } 153 154 public function updateRebateStatus($bluepay_hosted_order_id, $status) { 155 $this->db->query("UPDATE `" . DB_PREFIX . "bluepay_hosted_order` SET `rebate_status` = '" . (int)$status . "' WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'"); 156 } 157 158 public function updateTransactionId($bluepay_hosted_order_id, $transaction_id) { 159 $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 . "'"); 160 } 161 162 public function getOrder($order_id) { 163 164 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "bluepay_hosted_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 165 166 if ($qry->num_rows) { 167 $order = $qry->row; 168 $order['transactions'] = $this->getTransactions($order['bluepay_hosted_order_id']); 169 170 return $order; 171 } else { 172 return false; 173 } 174 } 175 176 private function getTransactions($bluepay_hosted_order_id) { 177 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "bluepay_hosted_order_transaction` WHERE `bluepay_hosted_order_id` = '" . (int)$bluepay_hosted_order_id . "'"); 178 179 if ($qry->num_rows) { 180 return $qry->rows; 181 } else { 182 return false; 183 } 184 } 185 186 public function addTransaction($bluepay_hosted_order_id, $type, $total) { 187 $this->logger('$type:\r\n' . print_r($type, 1)); 188 $this->logger('$total:\r\n' . print_r($total, 1)); 189 $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 . "'"); 190 } 191 192 public function getTotalReleased($bluepay_hosted_order_id) { 193 $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')"); 194 195 return (float)$query->row['total']; 196 } 197 198 public function getTotalRebated($bluepay_hosted_order_id) { 199 $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'"); 200 201 return (float)$query->row['total']; 202 } 203 204 public function sendCurl($url, $post_data) { 205 $curl = curl_init($url); 206 207 curl_setopt($curl, CURLOPT_PORT, 443); 208 curl_setopt($curl, CURLOPT_HEADER, 0); 209 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 210 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 211 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 212 curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); 213 curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); 214 curl_setopt($curl, CURLOPT_POST, 1); 215 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data)); 216 217 $response_data = curl_exec($curl); 218 219 curl_close($curl); 220 221 return json_decode($response_data, true); 222 } 223 224 public function adminCallback() { 225 $this->response->addHeader('Content-Type: application/json'); 226 $this->response->setOutput(json_encode($this->request->get)); 227 } 228 229 public function logger($message) { 230 if ($this->config->get('payment_bluepay_hosted_debug') == 1) { 231 $log = new Log('bluepay_hosted.log'); 232 $log->write($message); 233 } 234 } 235 }