PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/model/payment/firstdata.php

https://gitlab.com/reclamare/mao
PHP | 222 lines | 184 code | 37 blank | 1 comment | 17 complexity | 23b1ee31312aafc67be223d6d0892358 MD5 | raw file
  1. <?php
  2. class ModelPaymentFirstdata extends Model {
  3. public function install() {
  4. $this->db->query("
  5. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_order` (
  6. `firstdata_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. `tdate` DATETIME NOT NULL,
  13. `date_added` DATETIME NOT NULL,
  14. `date_modified` DATETIME NOT NULL,
  15. `capture_status` INT(1) DEFAULT NULL,
  16. `void_status` INT(1) DEFAULT NULL,
  17. `currency_code` CHAR(3) NOT NULL,
  18. `authcode` VARCHAR(30) NOT NULL,
  19. `account` VARCHAR(30) NOT NULL,
  20. `total` DECIMAL( 10, 2 ) NOT NULL,
  21. PRIMARY KEY (`firstdata_order_id`)
  22. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  23. $this->db->query("
  24. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_order_transaction` (
  25. `firstdata_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
  26. `firstdata_order_id` INT(11) NOT NULL,
  27. `date_added` DATETIME NOT NULL,
  28. `type` ENUM('auth', 'payment', 'void') DEFAULT NULL,
  29. `amount` DECIMAL( 10, 2 ) NOT NULL,
  30. PRIMARY KEY (`firstdata_order_transaction_id`)
  31. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  32. $this->db->query("
  33. CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "firstdata_card` (
  34. `firstdata_card_id` INT(11) NOT NULL AUTO_INCREMENT,
  35. `customer_id` INT(11) NOT NULL,
  36. `date_added` DATETIME NOT NULL,
  37. `digits` CHAR(25) NOT NULL,
  38. `expire_month` INT(2) NOT NULL,
  39. `expire_year` INT(2) NOT NULL,
  40. `token` CHAR(64) NOT NULL,
  41. PRIMARY KEY (`firstdata_card_id`)
  42. ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
  43. }
  44. public function uninstall() {
  45. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_order`;");
  46. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_order_transaction`;");
  47. $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "firstdata_card`;");
  48. }
  49. public function void($order_id) {
  50. $firstdata_order = $this->getOrder($order_id);
  51. if (!empty($firstdata_order)) {
  52. $timestamp = strftime("%Y%m%d%H%M%S");
  53. $merchant_id = $this->config->get('firstdata_merchant_id');
  54. $secret = $this->config->get('firstdata_secret');
  55. $this->logger('Void hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . . . ');
  56. $tmp = $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . . . ';
  57. $hash = sha1($tmp);
  58. $tmp = $hash . ' . ' . $secret;
  59. $hash = sha1($tmp);
  60. $xml = '';
  61. $xml .= '<request type="void" timestamp="' . $timestamp . '">';
  62. $xml .= '<merchantid>' . $merchant_id . '</merchantid>';
  63. $xml .= '<account>' . $firstdata_order['account'] . '</account>';
  64. $xml .= '<orderid>' . $firstdata_order['order_ref'] . '</orderid>';
  65. $xml .= '<pasref>' . $firstdata_order['pasref'] . '</pasref>';
  66. $xml .= '<authcode>' . $firstdata_order['authcode'] . '</authcode>';
  67. $xml .= '<sha1hash>' . $hash . '</sha1hash>';
  68. $xml .= '</request>';
  69. $this->logger('Void XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
  70. $ch = curl_init();
  71. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  72. curl_setopt($ch, CURLOPT_POST, 1);
  73. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
  74. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  75. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  77. $response = curl_exec ($ch);
  78. curl_close ($ch);
  79. return simplexml_load_string($response);
  80. } else {
  81. return false;
  82. }
  83. }
  84. public function updateVoidStatus($firstdata_order_id, $status) {
  85. $this->db->query("UPDATE `" . DB_PREFIX . "firstdata_order` SET `void_status` = '" . (int)$status . "' WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "'");
  86. }
  87. public function capture($order_id, $amount) {
  88. $firstdata_order = $this->getOrder($order_id);
  89. if (!empty($firstdata_order) && $firstdata_order['capture_status'] == 0) {
  90. $timestamp = strftime("%Y%m%d%H%M%S");
  91. $merchant_id = $this->config->get('firstdata_merchant_id');
  92. $secret = $this->config->get('firstdata_secret');
  93. if ($firstdata_order['settle_type'] == 2) {
  94. $this->logger('Capture hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ');
  95. $tmp = $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ';
  96. $hash = sha1($tmp);
  97. $tmp = $hash . ' . ' . $secret;
  98. $hash = sha1($tmp);
  99. $settle_type = 'multisettle';
  100. $xml_amount = '<amount currency="' . (string)$firstdata_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
  101. } else {
  102. //$this->logger('Capture hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . . . ');
  103. $this->logger('Capture hash construct: ' . $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ');
  104. $tmp = $timestamp . ' . ' . $merchant_id . ' . ' . $firstdata_order['order_ref'] . ' . ' . (int)round($amount*100) . ' . ' . (string)$firstdata_order['currency_code'] . ' . ';
  105. $hash = sha1($tmp);
  106. $tmp = $hash . ' . ' . $secret;
  107. $hash = sha1($tmp);
  108. $settle_type = 'settle';
  109. $xml_amount = '<amount currency="' . (string)$firstdata_order['currency_code'] . '">' . (int)round($amount*100) . '</amount>';
  110. }
  111. $xml = '';
  112. $xml .= '<request type="' . $settle_type . '" timestamp="' . $timestamp . '">';
  113. $xml .= '<merchantid>' . $merchant_id . '</merchantid>';
  114. $xml .= '<account>' . $firstdata_order['account'] . '</account>';
  115. $xml .= '<orderid>' . $firstdata_order['order_ref'] . '</orderid>';
  116. $xml .= $xml_amount;
  117. $xml .= '<pasref>' . $firstdata_order['pasref'] . '</pasref>';
  118. $xml .= '<autosettle flag="1" />';
  119. $xml .= '<authcode>' . $firstdata_order['authcode'] . '</authcode>';
  120. $xml .= '<sha1hash>' . $hash . '</sha1hash>';
  121. $xml .= '</request>';
  122. $this->logger('Settle XML request:\r\n' . print_r(simplexml_load_string($xml), 1));
  123. $ch = curl_init();
  124. curl_setopt($ch, CURLOPT_URL, "https://epage.payandshop.com/epage-remote.cgi");
  125. curl_setopt($ch, CURLOPT_POST, 1);
  126. curl_setopt($ch, CURLOPT_USERAGENT, "OpenCart " . VERSION);
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  128. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  129. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  130. $response = curl_exec ($ch);
  131. curl_close ($ch);
  132. return simplexml_load_string($response);
  133. } else {
  134. return false;
  135. }
  136. }
  137. public function updateCaptureStatus($firstdata_order_id, $status) {
  138. $this->db->query("UPDATE `" . DB_PREFIX . "firstdata_order` SET `capture_status` = '" . (int)$status . "' WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "'");
  139. }
  140. public function getOrder($order_id) {
  141. $this->logger('getOrder - ' . $order_id);
  142. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "firstdata_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
  143. if ($qry->num_rows) {
  144. $order = $qry->row;
  145. $order['transactions'] = $this->getTransactions($order['firstdata_order_id']);
  146. $this->logger(print_r($order, 1));
  147. return $order;
  148. } else {
  149. return false;
  150. }
  151. }
  152. private function getTransactions($firstdata_order_id) {
  153. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "firstdata_order_transaction` WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "'");
  154. if ($qry->num_rows) {
  155. return $qry->rows;
  156. } else {
  157. return false;
  158. }
  159. }
  160. public function addTransaction($firstdata_order_id, $type, $total) {
  161. $this->db->query("INSERT INTO `" . DB_PREFIX . "firstdata_order_transaction` SET `firstdata_order_id` = '" . (int)$firstdata_order_id . "', `date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (float)$total . "'");
  162. }
  163. public function logger($message) {
  164. if ($this->config->get('firstdata_debug') == 1) {
  165. $log = new Log('firstdata.log');
  166. $log->write($message);
  167. }
  168. }
  169. public function getTotalCaptured($firstdata_order_id) {
  170. $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "firstdata_order_transaction` WHERE `firstdata_order_id` = '" . (int)$firstdata_order_id . "' AND (`type` = 'payment' OR `type` = 'refund')");
  171. return (float)$query->row['total'];
  172. }
  173. public function mapCurrency($code) {
  174. $currency = array(
  175. 'GBP' => 826,
  176. 'USD' => 840,
  177. 'EUR' => 978,
  178. );
  179. if (array_key_exists($code, $currency)) {
  180. return $currency[$code];
  181. } else {
  182. return false;
  183. }
  184. }
  185. }