PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/admin/model/payment/realex.php

https://gitlab.com/m.mirsamie/opencart
PHP | 265 lines | 216 code | 48 blank | 1 comment | 24 complexity | e9bbf7cd0ff40f1e3778ad16cf6a7deb MD5 | raw file
  1. <?php
  2. class ModelPaymentRealex extends Model {
  3. public function install() {
  4. $this->db->query("
  5. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "realex_order` (
  6. `realex_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. `created` DATETIME NOT NULL,
  13. `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 (`realex_order_id`)
  23. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  24. $this->db->query("
  25. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "realex_order_transaction` (
  26. `realex_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
  27. `realex_order_id` INT(11) NOT NULL,
  28. `created` DATETIME NOT NULL,
  29. `type` ENUM('auth', 'payment', 'rebate', 'void') DEFAULT NULL,
  30. `amount` DECIMAL( 10, 2 ) NOT NULL,
  31. PRIMARY KEY (`realex_order_transaction_id`)
  32. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  33. }
  34. public function void($order_id) {
  35. $realex_order = $this->getOrder($order_id);
  36. if (!empty($realex_order)) {
  37. $timestamp = strftime("%Y%m%d%H%M%S");
  38. $merchant_id = $this->config->get('realex_merchant_id');
  39. $secret = $this->config->get('realex_secret');
  40. $this->logger('Void hash construct: '.$timestamp.'.'.$merchant_id.'.'.$realex_order['order_ref'].'...');
  41. $tmp = $timestamp.'.'.$merchant_id.'.'.$realex_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>'.$realex_order['account'].'</account>';
  49. $xml .= '<orderid>'.$realex_order['order_ref'].'</orderid>';
  50. $xml .= '<pasref>'.$realex_order['pasref'].'</pasref>';
  51. $xml .= '<authcode>'.$realex_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($realex_order_id, $status) {
  70. $this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `void_status` = '".(int)$status."' WHERE `realex_order_id` = '" . (int)$realex_order_id . "'");
  71. }
  72. public function capture($order_id, $amount) {
  73. $realex_order = $this->getOrder($order_id);
  74. if (!empty($realex_order) && $realex_order['capture_status'] == 0) {
  75. $timestamp = strftime("%Y%m%d%H%M%S");
  76. $merchant_id = $this->config->get('realex_merchant_id');
  77. $secret = $this->config->get('realex_secret');
  78. if ($realex_order['settle_type'] == 2) {
  79. $this->logger('Capture hash construct: '.$timestamp.'.'.$merchant_id.'.'.$realex_order['order_ref'].'.'.(int)round($amount*100).'.'.(string)$realex_order['currency_code'].'.');
  80. $tmp = $timestamp.'.'.$merchant_id.'.'.$realex_order['order_ref'].'.'.(int)round($amount*100).'.'.(string)$realex_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)$realex_order['currency_code'].'">'.(int)round($amount*100).'</amount>';
  86. } else {
  87. //$this->logger('Capture hash construct: '.$timestamp.'.'.$merchant_id.'.'.$realex_order['order_ref'].'...');
  88. $this->logger('Capture hash construct: '.$timestamp.'.'.$merchant_id.'.'.$realex_order['order_ref'].'.'.(int)round($amount*100).'.'.(string)$realex_order['currency_code'].'.');
  89. $tmp = $timestamp.'.'.$merchant_id.'.'.$realex_order['order_ref'].'.'.(int)round($amount*100).'.'.(string)$realex_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)$realex_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>'.$realex_order['account'].'</account>';
  100. $xml .= '<orderid>'.$realex_order['order_ref'].'</orderid>';
  101. $xml .= $xml_amount;
  102. $xml .= '<pasref>'.$realex_order['pasref'].'</pasref>';
  103. $xml .= '<autosettle flag="1" />';
  104. $xml .= '<authcode>'.$realex_order['authcode'].'</authcode>';
  105. $xml .= '<sha1hash>'.$hash.'</sha1hash>';
  106. $xml .= '</request>';
  107. $this->logger('Settle XML request:\r\n'.print_r(simplexml_load_string($xml), 1));
  108. $ch = curl_init();
  109. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  110. curl_setopt($ch, CURLOPT_POST, 1);
  111. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart ".VERSION);
  112. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  113. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  114. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  115. $response = curl_exec ($ch);
  116. curl_close ($ch);
  117. return simplexml_load_string($response);
  118. } else {
  119. return false;
  120. }
  121. }
  122. public function updateCaptureStatus($realex_order_id, $status) {
  123. $this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `capture_status` = '".(int)$status."' WHERE `realex_order_id` = '" . (int)$realex_order_id . "'");
  124. }
  125. public function updateForRebate($realex_order_id, $pas_ref, $order_ref) {
  126. $this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `order_ref_previous` = '_multisettle_".$this->db->escape($order_ref)."', `pasref_previous` = '".$this->db->escape($pas_ref)."' WHERE `realex_order_id` = '".(int)$realex_order_id."' LIMIT 1");
  127. }
  128. public function rebate($order_id, $amount) {
  129. $realex_order = $this->getOrder($order_id);
  130. if (!empty($realex_order) && $realex_order['rebate_status'] != 1) {
  131. $timestamp = strftime("%Y%m%d%H%M%S");
  132. $merchant_id = $this->config->get('realex_merchant_id');
  133. $secret = $this->config->get('realex_secret');
  134. if ($realex_order['settle_type'] == 2) {
  135. $order_ref = '_multisettle_'.$realex_order['order_ref'];
  136. if (empty($realex_order['pasref_previous'])) {
  137. $pas_ref = $realex_order['pasref'];
  138. } else {
  139. $pas_ref = $realex_order['pasref_previous'];
  140. }
  141. } else {
  142. $order_ref = $realex_order['order_ref'];
  143. $pas_ref = $realex_order['pasref'];
  144. }
  145. $this->logger('Rebate hash construct: '.$timestamp.'.'.$merchant_id.'.'.$order_ref.'.'.(int)round($amount*100).'.'.$realex_order['currency_code'].'.');
  146. $tmp = $timestamp.'.'.$merchant_id.'.'.$order_ref.'.'.(int)round($amount*100).'.'.$realex_order['currency_code'].'.';
  147. $hash = sha1($tmp);
  148. $tmp = $hash.'.'.$secret;
  149. $hash = sha1($tmp);
  150. $rebate_hash = sha1($this->config->get('realex_rebate_password'));
  151. $xml = '';
  152. $xml .= '<request type="rebate" timestamp="'.$timestamp.'">';
  153. $xml .= '<merchantid>'.$merchant_id.'</merchantid>';
  154. $xml .= '<account>'.$realex_order['account'].'</account>';
  155. $xml .= '<orderid>'.$order_ref.'</orderid>';
  156. $xml .= '<pasref>'.$pas_ref.'</pasref>';
  157. $xml .= '<authcode>'.$realex_order['authcode'].'</authcode>';
  158. $xml .= '<amount currency="'.(string)$realex_order['currency_code'].'">'.(int)round($amount*100).'</amount>';
  159. $xml .= '<refundhash>'.$rebate_hash.'</refundhash>';
  160. $xml .= '<sha1hash>'.$hash.'</sha1hash>';
  161. $xml .= '</request>';
  162. $this->logger('Rebate XML request:\r\n'.print_r(simplexml_load_string($xml), 1));
  163. $ch = curl_init();
  164. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  165. curl_setopt($ch, CURLOPT_POST, 1);
  166. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart ".VERSION);
  167. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  168. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  169. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  170. $response = curl_exec ($ch);
  171. curl_close ($ch);
  172. return simplexml_load_string($response);
  173. } else {
  174. return false;
  175. }
  176. }
  177. public function updateRebateStatus($realex_order_id, $status) {
  178. $this->db->query("UPDATE `" . DB_PREFIX . "realex_order` SET `rebate_status` = '".(int)$status."' WHERE `realex_order_id` = '" . (int)$realex_order_id . "'");
  179. }
  180. public function getOrder($order_id) {
  181. $this->logger('getOrder - '.$order_id);
  182. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "realex_order` WHERE `order_id` = '".(int)$order_id."' LIMIT 1");
  183. if ($qry->num_rows) {
  184. $order = $qry->row;
  185. $order['transactions'] = $this->getTransactions($order['realex_order_id']);
  186. $this->logger(print_r($order, 1));
  187. return $order;
  188. } else {
  189. return false;
  190. }
  191. }
  192. private function getTransactions($realex_order_id) {
  193. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "realex_order_transaction` WHERE `realex_order_id` = '" . (int)$realex_order_id . "'");
  194. if ($qry->num_rows) {
  195. return $qry->rows;
  196. } else {
  197. return false;
  198. }
  199. }
  200. public function addTransaction($realex_order_id, $type, $total) {
  201. $this->db->query("INSERT INTO `" . DB_PREFIX . "realex_order_transaction` SET `realex_order_id` = '".(int)$realex_order_id."', `created` = now(), `type` = '".$this->db->escape($type)."', `amount` = '".(double)$total."'");
  202. }
  203. public function logger($message) {
  204. if ($this->config->get('realex_debug') == 1) {
  205. $log = new Log('realex.log');
  206. $log->write($message);
  207. }
  208. }
  209. public function getTotalCaptured($realex_order_id) {
  210. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "realex_order_transaction` WHERE `realex_order_id` = '".(int)$realex_order_id."' AND (`type` = 'payment' OR `type` = 'rebate')");
  211. return (double)$query->row['total'];
  212. }
  213. public function getTotalRebated($realex_order_id) {
  214. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "realex_order_transaction` WHERE `realex_order_id` = '".(int)$realex_order_id."' AND 'rebate'");
  215. return (double)$query->row['total'];
  216. }
  217. }