PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/model/payment/globalpay_remote.php

https://gitlab.com/shapcy/opencart
PHP | 260 lines | 213 code | 46 blank | 1 comment | 24 complexity | d68a0ef2050e04ee76e98058c20fe780 MD5 | raw file
  1. <?php
  2. class ModelPaymentGlobalpayRemote extends Model {
  3. public function install() {
  4. $this->db->query("
  5. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "globalpay_remote_order` (
  6. `globalpay_remote_order_id` INT(11) NOT NULL AUTO_INCREMENT,
  7. `order_id` INT(11) NOT NULL,
  8. `order_ref` CHAR(50) NOT NULL,
  9. `order_ref_previous` CHAR(50) NOT NULL,
  10. `pasref` VARCHAR(50) NOT NULL,
  11. `pasref_previous` VARCHAR(50) NOT NULL,
  12. `date_added` DATETIME NOT NULL,
  13. `date_modified` DATETIME NOT NULL,
  14. `capture_status` INT(1) DEFAULT NULL,
  15. `void_status` INT(1) DEFAULT NULL,
  16. `settle_type` INT(1) DEFAULT NULL,
  17. `rebate_status` INT(1) DEFAULT NULL,
  18. `currency_code` CHAR(3) NOT NULL,
  19. `authcode` VARCHAR(30) NOT NULL,
  20. `account` VARCHAR(30) NOT NULL,
  21. `total` DECIMAL( 10, 2 ) NOT NULL,
  22. PRIMARY KEY (`globalpay_remote_order_id`)
  23. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  24. $this->db->query("
  25. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "globalpay_remote_order_transaction` (
  26. `globalpay_remote_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
  27. `globalpay_remote_order_id` INT(11) NOT NULL,
  28. `date_added` DATETIME NOT NULL,
  29. `type` ENUM('auth', 'payment', 'rebate', 'void') DEFAULT NULL,
  30. `amount` DECIMAL( 10, 2 ) NOT NULL,
  31. PRIMARY KEY (`globalpay_remote_order_transaction_id`)
  32. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  33. }
  34. public function void($order_id) {
  35. $globalpay_order = $this->getOrder($order_id);
  36. if (!empty($globalpay_order)) {
  37. $timestamp = strftime("%Y%m%d%H%M%S");
  38. $merchant_id = $this->config->get('globalpay_remote_merchant_id');
  39. $secret = $this->config->get('globalpay_remote_secret');
  40. $this->logger('Void hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '...');
  41. $tmp = $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '...';
  42. $hash = sha1($tmp);
  43. $tmp = $hash . '.' . $secret;
  44. $hash = sha1($tmp);
  45. $xml = '';
  46. $xml .= '<request type="void" timestamp="' . $timestamp . '">';
  47. $xml .= '<merchantid>' . $merchant_id . '</merchantid>';
  48. $xml .= '<account>' . $globalpay_order['account'] . '</account>';
  49. $xml .= '<orderid>' . $globalpay_order['order_ref'] . '</orderid>';
  50. $xml .= '<pasref>' . $globalpay_order['pasref'] . '</pasref>';
  51. $xml .= '<authcode>' . $globalpay_order['authcode'] . '</authcode>';
  52. $xml .= '<sha1hash>' . $hash . '</sha1hash>';
  53. $xml .= '</request>';
  54. $this->logger('Void XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  57. curl_setopt($ch, CURLOPT_POST, 1);
  58. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  60. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  61. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  62. $response = curl_exec ($ch);
  63. curl_close ($ch);
  64. return simplexml_load_string($response);
  65. } else {
  66. return false;
  67. }
  68. }
  69. public function updateVoidStatus($globalpay_remote_order_id, $status) {
  70. $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_remote_order` SET `void_status` = '" . (int)$status . "' WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "'");
  71. }
  72. public function capture($order_id, $amount) {
  73. $globalpay_order = $this->getOrder($order_id);
  74. if (!empty($globalpay_order) && $globalpay_order['capture_status'] == 0) {
  75. $timestamp = strftime("%Y%m%d%H%M%S");
  76. $merchant_id = $this->config->get('globalpay_remote_merchant_id');
  77. $secret = $this->config->get('globalpay_remote_secret');
  78. if ($globalpay_order['settle_type'] == 2) {
  79. $this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.');
  80. $tmp = $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.';
  81. $hash = sha1($tmp);
  82. $tmp = $hash . '.' . $secret;
  83. $hash = sha1($tmp);
  84. $settle_type = 'multisettle';
  85. $xml_amount = '<amount currency="' . (string)$globalpay_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
  86. } else {
  87. //$this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '...');
  88. $this->logger('Capture hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.');
  89. $tmp = $timestamp . '.' . $merchant_id . '.' . $globalpay_order['order_ref'] . '.' . (int)round($amount*100) . '.' . (string)$globalpay_order['currency_code'] . '.';
  90. $hash = sha1($tmp);
  91. $tmp = $hash . '.' . $secret;
  92. $hash = sha1($tmp);
  93. $settle_type = 'settle';
  94. $xml_amount = '<amount currency="' . (string)$globalpay_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
  95. }
  96. $xml = '';
  97. $xml .= '<request type="' . $settle_type . '" timestamp="' . $timestamp . '">';
  98. $xml .= '<merchantid>' . $merchant_id . '</merchantid>';
  99. $xml .= '<account>' . $globalpay_order['account'] . '</account>';
  100. $xml .= '<orderid>' . $globalpay_order['order_ref'] . '</orderid>';
  101. $xml .= $xml_amount;
  102. $xml .= '<pasref>' . $globalpay_order['pasref'] . '</pasref>';
  103. $xml .= '<authcode>' . $globalpay_order['authcode'] . '</authcode>';
  104. $xml .= '<sha1hash>' . $hash . '</sha1hash>';
  105. $xml .= '</request>';
  106. $this->logger('Settle XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
  107. $ch = curl_init();
  108. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  109. curl_setopt($ch, CURLOPT_POST, 1);
  110. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  112. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  113. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  114. $response = curl_exec ($ch);
  115. curl_close ($ch);
  116. return simplexml_load_string($response);
  117. } else {
  118. return false;
  119. }
  120. }
  121. public function updateCaptureStatus($globalpay_remote_order_id, $status) {
  122. $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_remote_order` SET `capture_status` = '" . (int)$status . "' WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "'");
  123. }
  124. public function updateForRebate($globalpay_remote_order_id, $pas_ref, $order_ref) {
  125. $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_remote_order` SET `order_ref_previous` = '_multisettle_" . $this->db->escape($order_ref) . "', `pasref_previous` = '" . $this->db->escape($pas_ref) . "' WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "' LIMIT 1");
  126. }
  127. public function rebate($order_id, $amount) {
  128. $globalpay_order = $this->getOrder($order_id);
  129. if (!empty($globalpay_order) && $globalpay_order['rebate_status'] != 1) {
  130. $timestamp = strftime("%Y%m%d%H%M%S");
  131. $merchant_id = $this->config->get('globalpay_remote_merchant_id');
  132. $secret = $this->config->get('globalpay_remote_secret');
  133. if ($globalpay_order['settle_type'] == 2) {
  134. $order_ref = '_multisettle_' . $globalpay_order['order_ref'];
  135. if (empty($globalpay_order['pasref_previous'])) {
  136. $pas_ref = $globalpay_order['pasref'];
  137. } else {
  138. $pas_ref = $globalpay_order['pasref_previous'];
  139. }
  140. } else {
  141. $order_ref = $globalpay_order['order_ref'];
  142. $pas_ref = $globalpay_order['pasref'];
  143. }
  144. $this->logger('Rebate hash construct: ' . $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $globalpay_order['currency_code'] . '.');
  145. $tmp = $timestamp . '.' . $merchant_id . '.' . $order_ref . '.' . (int)round($amount*100) . '.' . $globalpay_order['currency_code'] . '.';
  146. $hash = sha1($tmp);
  147. $tmp = $hash . '.' . $secret;
  148. $hash = sha1($tmp);
  149. $rebatehash = sha1($this->config->get('globalpay_remote_rebate_password'));
  150. $xml = '';
  151. $xml .= '<request type="rebate" timestamp="' . $timestamp . '">';
  152. $xml .= '<merchantid>' . $merchant_id . '</merchantid>';
  153. $xml .= '<account>' . $globalpay_order['account'] . '</account>';
  154. $xml .= '<orderid>' . $order_ref . '</orderid>';
  155. $xml .= '<pasref>' . $pas_ref . '</pasref>';
  156. $xml .= '<authcode>' . $globalpay_order['authcode'] . '</authcode>';
  157. $xml .= '<amount currency="' . (string)$globalpay_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
  158. $xml .= '<refundhash>' . $rebatehash . '</refundhash>';
  159. $xml .= '<sha1hash>' . $hash . '</sha1hash>';
  160. $xml .= '</request>';
  161. $this->logger('Rebate XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
  162. $ch = curl_init();
  163. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  164. curl_setopt($ch, CURLOPT_POST, 1);
  165. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
  166. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  167. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  168. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  169. $response = curl_exec ($ch);
  170. curl_close ($ch);
  171. return simplexml_load_string($response);
  172. } else {
  173. return false;
  174. }
  175. }
  176. public function updateRebateStatus($globalpay_remote_order_id, $status) {
  177. $this->db->query("UPDATE `" . DB_PREFIX . "globalpay_remote_order` SET `rebate_status` = '" . (int)$status . "' WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "'");
  178. }
  179. public function getOrder($order_id) {
  180. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "globalpay_remote_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
  181. if ($qry->num_rows) {
  182. $order = $qry->row;
  183. $order['transactions'] = $this->getTransactions($order['globalpay_remote_order_id']);
  184. return $order;
  185. } else {
  186. return false;
  187. }
  188. }
  189. private function getTransactions($globalpay_remote_order_id) {
  190. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "globalpay_remote_order_transaction` WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "'");
  191. if ($qry->num_rows) {
  192. return $qry->rows;
  193. } else {
  194. return false;
  195. }
  196. }
  197. public function addTransaction($globalpay_remote_order_id, $type, $total) {
  198. $this->db->query("INSERT INTO `" . DB_PREFIX . "globalpay_remote_order_transaction` SET `globalpay_remote_order_id` = '" . (int)$globalpay_remote_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'");
  199. }
  200. public function logger($message) {
  201. if ($this->config->get('globalpay_remote_debug') == 1) {
  202. $log = new Log('globalpay_remote.log');
  203. $log->write($message);
  204. }
  205. }
  206. public function getTotalCaptured($globalpay_order_id) {
  207. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "globalpay_remote_order_transaction` WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_order_id . "' AND (`type` = 'payment' OR `type` = 'rebate')");
  208. return (float)$query->row['total'];
  209. }
  210. public function getTotalRebated($globalpay_order_id) {
  211. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "globalpay_remote_order_transaction` WHERE `globalpay_remote_order_id` = '" . (int)$globalpay_order_id . "' AND 'rebate'");
  212. return (double)$query->row['total'];
  213. }
  214. }