g2apay.php (5496B)
1 <?php 2 3 class ModelExtensionPaymentG2aPay extends Model { 4 5 public function install() { 6 $this->db->query(" 7 CREATE TABLE `" . DB_PREFIX . "g2apay_order` ( 8 `g2apay_order_id` INT(11) NOT NULL AUTO_INCREMENT, 9 `order_id` int(11) NOT NULL, 10 `g2apay_transaction_id` varchar(255) NOT NULL, 11 `date_added` DATETIME NOT NULL, 12 `modified` DATETIME NOT NULL, 13 `refund_status` INT(1) DEFAULT NULL, 14 `currency_code` CHAR(3) NOT NULL, 15 `total` DECIMAL( 10, 2 ) NOT NULL, 16 KEY `g2apay_transaction_id` (`g2apay_transaction_id`), 17 PRIMARY KEY `g2apay_order_id` (`g2apay_order_id`) 18 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci; 19 "); 20 21 $this->db->query(" 22 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "g2apay_order_transaction` ( 23 `g2apay_order_transaction_id` INT(11) NOT NULL AUTO_INCREMENT, 24 `g2apay_order_id` INT(11) NOT NULL, 25 `date_added` DATETIME NOT NULL, 26 `type` ENUM('payment', 'refund') DEFAULT NULL, 27 `amount` DECIMAL( 10, 2 ) NOT NULL, 28 PRIMARY KEY (`g2apay_order_transaction_id`) 29 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci; 30 "); 31 } 32 33 public function uninstall() { 34 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "g2apay_order`;"); 35 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "g2apay_order_transaction`;"); 36 } 37 38 public function getOrder($order_id) { 39 40 $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "g2apay_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 41 42 if ($qry->num_rows) { 43 $order = $qry->row; 44 $order['transactions'] = $this->getTransactions($order['g2apay_order_id'], $qry->row['currency_code']); 45 return $order; 46 } else { 47 return false; 48 } 49 } 50 51 public function getTotalReleased($g2apay_order_id) { 52 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "g2apay_order_transaction` WHERE `g2apay_order_id` = '" . (int)$g2apay_order_id . "' AND (`type` = 'payment' OR `type` = 'refund')"); 53 54 return (double)$query->row['total']; 55 } 56 57 public function refund($g2apay_order, $amount) { 58 if (!empty($g2apay_order) && $g2apay_order['refund_status'] != 1) { 59 if ($this->config->get('payment_g2apay_environment') == 1) { 60 $url = 'https://pay.g2a.com/rest/transactions/' . $g2apay_order['g2apay_transaction_id']; 61 } else { 62 $url = 'https://www.test.pay.g2a.com/rest/transactions/' . $g2apay_order['g2apay_transaction_id']; 63 } 64 65 $refunded_amount = round($amount, 2); 66 67 $string = $g2apay_order['g2apay_transaction_id'] . $g2apay_order['order_id'] . round($g2apay_order['total'], 2) . $refunded_amount . html_entity_decode($this->config->get('payment_g2apay_secret')); 68 $hash = hash('sha256', $string); 69 70 $fields = array( 71 'action' => 'refund', 72 'amount' => $refunded_amount, 73 'hash' => $hash, 74 ); 75 76 return $this->sendCurl($url, $fields); 77 } else { 78 return false; 79 } 80 } 81 82 public function updateRefundStatus($g2apay_order_id, $status) { 83 $this->db->query("UPDATE `" . DB_PREFIX . "g2apay_order` SET `refund_status` = '" . (int)$status . "' WHERE `g2apay_order_id` = '" . (int)$g2apay_order_id . "'"); 84 } 85 86 private function getTransactions($g2apay_order_id, $currency_code) { 87 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "g2apay_order_transaction` WHERE `g2apay_order_id` = '" . (int)$g2apay_order_id . "'"); 88 89 $transactions = array(); 90 if ($query->num_rows) { 91 foreach ($query->rows as $row) { 92 $row['amount'] = $this->currency->format($row['amount'], $currency_code, true, true); 93 $transactions[] = $row; 94 } 95 return $transactions; 96 } else { 97 return false; 98 } 99 } 100 101 public function addTransaction($g2apay_order_id, $type, $total) { 102 $this->db->query("INSERT INTO `" . DB_PREFIX . "g2apay_order_transaction` SET `g2apay_order_id` = '" . (int)$g2apay_order_id . "',`date_added` = now(), `type` = '" . $this->db->escape($type) . "', `amount` = '" . (double)$total . "'"); 103 } 104 105 public function getTotalRefunded($g2apay_order_id) { 106 $query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "g2apay_order_transaction` WHERE `g2apay_order_id` = '" . (int)$g2apay_order_id . "' AND 'refund'"); 107 108 return (double)$query->row['total']; 109 } 110 111 public function sendCurl($url, $fields) { 112 $curl = curl_init($url); 113 curl_setopt($curl, CURLOPT_URL, $url); 114 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 115 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); 116 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fields)); 117 118 $auth_hash = hash('sha256', $this->config->get('payment_g2apay_api_hash') . $this->config->get('payment_g2apay_username') . html_entity_decode($this->config->get('payment_g2apay_secret'))); 119 $authorization = $this->config->get('payment_g2apay_api_hash') . ";" . $auth_hash; 120 curl_setopt( 121 $curl, CURLOPT_HTTPHEADER, array( 122 "Authorization: " . $authorization 123 ) 124 ); 125 126 $response = json_decode(curl_exec($curl)); 127 128 curl_close($curl); 129 if (is_object($response)) { 130 return (string)$response->status; 131 } else { 132 return str_replace('"', "", $response); 133 } 134 } 135 136 public function logger($message) { 137 if ($this->config->get('payment_g2apay_debug') == 1) { 138 $log = new Log('g2apay.log'); 139 $backtrace = debug_backtrace(); 140 $log->write('Origin: ' . $backtrace[6]['class'] . '::' . $backtrace[6]['function']); 141 $log->write(print_r($message, 1)); 142 } 143 } 144 145 }