shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

eway.php (8607B)


      1 <?php
      2 
      3 class ModelExtensionPaymentEway extends Model {
      4 
      5 	public function install() {
      6 		$this->db->query("
      7 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "eway_order` (
      8 			  `eway_order_id` int(11) NOT NULL AUTO_INCREMENT,
      9 			  `order_id` int(11) NOT NULL,
     10 			  `created` DATETIME NOT NULL,
     11 			  `modified` DATETIME NOT NULL,
     12 			  `amount` DECIMAL( 10, 2 ) NOT NULL,
     13 			  `currency_code` CHAR(3) NOT NULL,
     14 			  `transaction_id` VARCHAR(24) NOT NULL,
     15 			  `debug_data` TEXT,
     16 			  `capture_status` INT(1) DEFAULT NULL,
     17 			  `void_status` INT(1) DEFAULT NULL,
     18 			  `refund_status` INT(1) DEFAULT NULL,
     19 			  PRIMARY KEY (`eway_order_id`)
     20 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     21 
     22 		$this->db->query("
     23 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "eway_transactions` (
     24 			  `eway_order_transaction_id` int(11) NOT NULL AUTO_INCREMENT,
     25 			  `eway_order_id` int(11) NOT NULL,
     26 			  `transaction_id` VARCHAR(24) NOT NULL,
     27 			  `created` DATETIME NOT NULL,
     28 			  `type` ENUM('auth', 'payment', 'refund', 'void') DEFAULT NULL,
     29 			  `amount` DECIMAL( 10, 2 ) NOT NULL,
     30 			  PRIMARY KEY (`eway_order_transaction_id`)
     31 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     32 
     33 		$this->db->query("
     34 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "eway_card` (
     35 			  `card_id` INT(11) NOT NULL AUTO_INCREMENT,
     36 			  `customer_id` INT(11) NOT NULL,
     37 			  `order_id` INT(11) NOT NULL,
     38 			  `token` VARCHAR(50) NOT NULL,
     39 			  `digits` VARCHAR(4) NOT NULL,
     40 			  `expiry` VARCHAR(5) NOT NULL,
     41 			  `type` VARCHAR(50) NOT NULL,
     42 			  PRIMARY KEY (`card_id`)
     43 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     44 	}
     45 
     46 	public function uninstall() {
     47 		//$this->model_setting_setting->deleteSetting($this->request->get['extension']);
     48 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "eway_order`;");
     49 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "eway_transactions`;");
     50 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "eway_card`;");
     51 	}
     52 
     53 	public function getOrder($order_id) {
     54 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "eway_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
     55 
     56 		if ($qry->num_rows) {
     57 			$order = $qry->row;
     58 			$order['transactions'] = $this->getTransactions($order['eway_order_id']);
     59 			return $order;
     60 		} else {
     61 			return false;
     62 		}
     63 	}
     64 
     65 	public function addRefundRecord($order, $result) {
     66 		$transaction_id = $result->TransactionID;
     67 		$total_amount = $result->Refund->TotalAmount / 100;
     68 		$refund_amount = $order['refund_amount'] + $total_amount;
     69 
     70 		if (isset($order['refund_transaction_id']) && !empty($order['refund_transaction_id'])) {
     71 			$order['refund_transaction_id'] .= ',';
     72 		}
     73 		$order['refund_transaction_id'] .= $transaction_id;
     74 
     75 		$this->db->query("UPDATE `" . DB_PREFIX . "eway_order` SET `modified` = NOW(), refund_amount = '" . (double)$refund_amount . "', `refund_transaction_id` = '" . $this->db->escape($order['refund_transaction_id']) . "' WHERE eway_order_id = '" . $order['eway_order_id'] . "'");
     76 	}
     77 
     78 	public function capture($order_id, $capture_amount, $currency) {
     79 		$eway_order = $this->getOrder($order_id);
     80 
     81 		if ($eway_order && $capture_amount > 0 ) {
     82 
     83 			$capture_data = new stdClass();
     84 			$capture_data->Payment = new stdClass();
     85 			$capture_data->Payment->TotalAmount = (int)number_format($capture_amount, 2, '.', '') * 100;
     86 			$capture_data->Payment->CurrencyCode = $currency;
     87 			$capture_data->TransactionID = $eway_order['transaction_id'];
     88 
     89 			if ($this->config->get('payment_eway_test')) {
     90 				$url = 'https://api.sandbox.ewaypayments.com/CapturePayment';
     91 			} else {
     92 				$url = 'https://api.ewaypayments.com/CapturePayment';
     93 			}
     94 
     95 			$response = $this->sendCurl($url, $capture_data);
     96 
     97 			return json_decode($response);
     98 
     99 		} else {
    100 			return false;
    101 		}
    102 	}
    103 
    104 	public function updateCaptureStatus($eway_order_id, $status) {
    105 		$this->db->query("UPDATE `" . DB_PREFIX . "eway_order` SET `capture_status` = '" . (int)$status . "' WHERE `eway_order_id` = '" . (int)$eway_order_id . "'");
    106 	}
    107 
    108 	public function updateTransactionId($eway_order_id, $transaction_id) {
    109 		$this->db->query("UPDATE `" . DB_PREFIX . "eway_order` SET `transaction_id` = '" . $transaction_id . "' WHERE `eway_order_id` = '" . (int)$eway_order_id . "'");
    110 	}
    111 
    112 	public function void($order_id) {
    113 		$eway_order = $this->getOrder($order_id);
    114 		if ($eway_order) {
    115 
    116 			$data = new stdClass();
    117 			$data->TransactionID = $eway_order['transaction_id'];
    118 
    119 			if ($this->config->get('payment_eway_test')) {
    120 				$url = 'https://api.sandbox.ewaypayments.com/CancelAuthorisation';
    121 			} else {
    122 				$url = 'https://api.ewaypayments.com/CancelAuthorisation';
    123 			}
    124 
    125 			$response = $this->sendCurl($url, $data);
    126 
    127 			return json_decode($response);
    128 
    129 		} else {
    130 			return false;
    131 		}
    132 	}
    133 
    134 	public function updateVoidStatus($eway_order_id, $status) {
    135 		$this->db->query("UPDATE `" . DB_PREFIX . "eway_order` SET `void_status` = '" . (int)$status . "' WHERE `eway_order_id` = '" . (int)$eway_order_id . "'");
    136 	}
    137 
    138 	public function refund($order_id, $refund_amount) {
    139 		$eway_order = $this->getOrder($order_id);
    140 
    141 		if ($eway_order && $refund_amount > 0) {
    142 
    143 			$refund_data = new stdClass();
    144 			$refund_data->Refund = new stdClass();
    145 			$refund_data->Refund->TotalAmount = (int)number_format($refund_amount, 2, '.', '') * 100;
    146 			$refund_data->Refund->TransactionID = $eway_order['transaction_id'];
    147 
    148 			if ($this->config->get('payment_eway_test')) {
    149 				$url = 'https://api.sandbox.ewaypayments.com/Transaction/' . $eway_order['transaction_id'] . '/Refund';
    150 			} else {
    151 				$url = 'https://api.ewaypayments.com/Transaction/' . $eway_order['transaction_id'] . '/Refund';
    152 			}
    153 
    154 			$response = $this->sendCurl($url, $refund_data);
    155 
    156 			return json_decode($response);
    157 		} else {
    158 			return false;
    159 		}
    160 	}
    161 
    162 	public function updateRefundStatus($eway_order_id, $status) {
    163 		$this->db->query("UPDATE `" . DB_PREFIX . "eway_order` SET `refund_status` = '" . (int)$status . "' WHERE `eway_order_id` = '" . (int)$eway_order_id . "'");
    164 	}
    165 
    166 	public function sendCurl($url, $data) {
    167 		$ch = curl_init($url);
    168 
    169 		$eway_username = html_entity_decode($this->config->get('payment_eway_username'), ENT_QUOTES, 'UTF-8');
    170 		$eway_password = html_entity_decode($this->config->get('payment_eway_password'), ENT_QUOTES, 'UTF-8');
    171 
    172 		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    173 		curl_setopt($ch, CURLOPT_USERPWD, $eway_username . ":" . $eway_password);
    174 		curl_setopt($ch, CURLOPT_POST, 1);
    175 		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    176 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    177 		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    178 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    179 
    180 		$response = curl_exec($ch);
    181 
    182 		if (curl_errno($ch) != CURLE_OK) {
    183 			$response = new stdClass();
    184 			$response->Errors = "POST Error: " . curl_error($ch) . " URL: $url";
    185 			$response = json_encode($response);
    186 		} else {
    187 			$info = curl_getinfo($ch);
    188 			if ($info['http_code'] == 401 || $info['http_code'] == 404) {
    189 				$response = new stdClass();
    190 				$response->Errors = "Please check the API Key and Password";
    191 				$response = json_encode($response);
    192 			}
    193 		}
    194 
    195 		curl_close($ch);
    196 
    197 		return $response;
    198 	}
    199 
    200 	private function getTransactions($eway_order_id) {
    201 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "eway_transactions` WHERE `eway_order_id` = '" . (int)$eway_order_id . "'");
    202 
    203 		if ($qry->num_rows) {
    204 			return $qry->rows;
    205 		} else {
    206 			return false;
    207 		}
    208 	}
    209 
    210 	public function addTransaction($eway_order_id, $transactionid, $type, $total, $currency) {
    211 		$this->db->query("INSERT INTO `" . DB_PREFIX . "eway_transactions` SET `eway_order_id` = '" . (int)$eway_order_id . "', `created` = NOW(), `transaction_id` = '" . $this->db->escape($transactionid) . "', `type` = '" . $this->db->escape($type) . "', `amount` = '" . $this->currency->format($total, $currency, false, false) . "'");
    212 	}
    213 
    214 	public function getTotalCaptured($eway_order_id) {
    215 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "eway_transactions` WHERE `eway_order_id` = '" . (int)$eway_order_id . "' AND `type` = 'payment' ");
    216 
    217 		return (double)$query->row['total'];
    218 	}
    219 
    220 	public function getTotalRefunded($eway_order_id) {
    221 		$query = $this->db->query("SELECT SUM(`amount`) AS `total` FROM `" . DB_PREFIX . "eway_transactions` WHERE `eway_order_id` = '" . (int)$eway_order_id . "' AND `type` = 'refund'");
    222 
    223 		return (double)$query->row['total'];
    224 	}
    225 
    226 }