shop.balmet.com

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

eway.php (7002B)


      1 <?php
      2 class ModelExtensionPaymentEway extends Model {
      3 	public function getMethod($address, $total) {
      4 		$this->load->language('extension/payment/eway');
      5 
      6 		if ($this->config->get('payment_eway_status')) {
      7 			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_eway_standard_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
      8 			if (!$this->config->get('payment_eway_standard_geo_zone_id')) {
      9 				$status = true;
     10 			} elseif ($query->num_rows) {
     11 				$status = true;
     12 			} else {
     13 				$status = false;
     14 			}
     15 		} else {
     16 			$status = false;
     17 		}
     18 
     19 		$method_data = array();
     20 
     21 		if ($status) {
     22 			$method_data = array(
     23 				'code' => 'eway',
     24 				'title' => $this->language->get('text_title'),
     25 				'terms'      => '',
     26 				'sort_order' => $this->config->get('payment_eway_sort_order')
     27 			);
     28 		}
     29 
     30 		return $method_data;
     31 	}
     32 
     33 	public function addOrder($order_data) {
     34 
     35 		$cap = '';
     36 		if ($this->config->get('payment_eway_transaction_method') == 'payment') {
     37 			$cap = ",`capture_status` = '1'";
     38 		}
     39 		$this->db->query("INSERT INTO `" . DB_PREFIX . "eway_order` SET `order_id` = '" . (int)$order_data['order_id'] . "', `created` = NOW(), `modified` = NOW(), `debug_data` = '" . $this->db->escape($order_data['debug_data']) . "', `amount` = '" . $this->currency->format($order_data['amount'], $order_data['currency_code'], false, false) . "', `currency_code` = '" . $this->db->escape($order_data['currency_code']) . "', `transaction_id` = '" . $this->db->escape($order_data['transaction_id']) . "'{$cap}");
     40 
     41 		return $this->db->getLastId();
     42 	}
     43 
     44 	public function addTransaction($eway_order_id, $type, $transactionid, $order_info) {
     45 		$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($order_info['total'], $order_info['currency_code'], false, false) . "'");
     46 
     47 		return $this->db->getLastId();
     48 	}
     49 
     50 	public function getCards($customer_id) {
     51 
     52 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "eway_card WHERE customer_id = '" . (int)$customer_id . "'");
     53 
     54 		$card_data = array();
     55 
     56 		$this->load->model('account/address');
     57 
     58 		foreach ($query->rows as $row) {
     59 
     60 			$card_data[] = array(
     61 				'card_id' => $row['card_id'],
     62 				'customer_id' => $row['customer_id'],
     63 				'token' => $row['token'],
     64 				'digits' => '**** ' . $row['digits'],
     65 				'expiry' => $row['expiry'],
     66 				'type' => $row['type'],
     67 			);
     68 		}
     69 		return $card_data;
     70 	}
     71 
     72 	public function checkToken($token_id) {
     73 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "eway_card WHERE token_id = '" . (int)$token_id . "'");
     74 		if ($query->num_rows) {
     75 			return true;
     76 		} else {
     77 			return false;
     78 		}
     79 	}
     80 
     81 	public function addCard($order_id, $card_data) {
     82 		$this->db->query("INSERT into " . DB_PREFIX . "eway_card SET customer_id = '" . $this->db->escape($card_data['customer_id']) . "', order_id = '" . $this->db->escape($order_id) . "', digits = '" . $this->db->escape($card_data['Last4Digits']) . "', expiry = '" . $this->db->escape($card_data['ExpiryDate']) . "', type = '" . $this->db->escape($card_data['CardType']) . "'");
     83 	}
     84 
     85 	public function updateCard($order_id, $token) {
     86 		$this->db->query("UPDATE " . DB_PREFIX . "eway_card SET token = '" . $this->db->escape($token) . "' WHERE order_id = '" . (int)$order_id . "'");
     87 	}
     88 
     89 	public function updateFullCard($card_id, $token, $card_data) {
     90 		$this->db->query("UPDATE " . DB_PREFIX . "eway_card SET token = '" . $this->db->escape($token) . "', digits = '" . $this->db->escape($card_data['Last4Digits']) . "', expiry = '" . $this->db->escape($card_data['ExpiryDate']) . "', type = '" . $this->db->escape($card_data['CardType']) . "' WHERE card_id = '" . (int)$card_id . "'");
     91 	}
     92 
     93 	public function deleteCard($order_id) {
     94 		$this->db->query("DELETE FROM " . DB_PREFIX . "eway_card WHERE order_id = '" . (int)$order_id . "'");
     95 	}
     96 
     97 	public function getAccessCode($request) {
     98 		if ($this->config->get('payment_eway_test')) {
     99 			$url = 'https://api.sandbox.ewaypayments.com/AccessCodes';
    100 		} else {
    101 			$url = 'https://api.ewaypayments.com/AccessCodes';
    102 		}
    103 
    104 		$response = $this->sendCurl($url, $request);
    105 		$response = json_decode($response);
    106 
    107 		return $response;
    108 	}
    109 
    110 	public function getSharedAccessCode($request) {
    111 		if ($this->config->get('payment_eway_test')) {
    112 			$url = 'https://api.sandbox.ewaypayments.com/AccessCodesShared';
    113 		} else {
    114 			$url = 'https://api.ewaypayments.com/AccessCodesShared';
    115 		}
    116 
    117 		$response = $this->sendCurl($url, $request);
    118 		$response = json_decode($response);
    119 
    120 		return $response;
    121 	}
    122 
    123 	public function getAccessCodeResult($access_code) {
    124 		if ($this->config->get('payment_eway_test')) {
    125 			$url = 'https://api.sandbox.ewaypayments.com/AccessCode/' . $access_code;
    126 		} else {
    127 			$url = 'https://api.ewaypayments.com/AccessCode/' . $access_code;
    128 		}
    129 
    130 		$response = $this->sendCurl($url, '', false);
    131 		$response = json_decode($response);
    132 
    133 		return $response;
    134 	}
    135 
    136 	public function sendCurl($url, $data, $is_post=true) {
    137 		$ch = curl_init($url);
    138 
    139 		$eway_username = html_entity_decode($this->config->get('payment_eway_username'), ENT_QUOTES, 'UTF-8');
    140 		$eway_password = html_entity_decode($this->config->get('payment_eway_password'), ENT_QUOTES, 'UTF-8');
    141 
    142 		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    143 		curl_setopt($ch, CURLOPT_USERPWD, $eway_username . ":" . $eway_password);
    144 		if ($is_post) {
    145 		curl_setopt($ch, CURLOPT_POST, 1);
    146 			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    147 		} else {
    148 			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    149 		}
    150 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    151 		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    152 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    153 		curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    154 		curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    155 
    156 		$response = curl_exec($ch);
    157 
    158 		if (curl_errno($ch) != CURLE_OK) {
    159 			$response = new stdClass();
    160 			$response->Errors = "POST Error: " . curl_error($ch) . " URL: $url";
    161 			$this->log(array('error' => curl_error($ch), 'errno' => curl_errno($ch)), 'cURL failed');
    162 			$response = json_encode($response);
    163 		} else {
    164 			$info = curl_getinfo($ch);
    165 			if ($info['http_code'] != 200) {
    166 				$response = new stdClass();
    167 				if ($info['http_code'] == 401 || $info['http_code'] == 404 || $info['http_code'] == 403) {
    168 					$response->Errors = "Please check the API Key and Password";
    169 				} else {
    170 					$response->Errors = 'Error connecting to eWAY: ' . $info['http_code'];
    171 				}
    172 				$response = json_encode($response);
    173 			}
    174 		}
    175 
    176 		curl_close($ch);
    177 
    178 		return $response;
    179 	}
    180 
    181 }