shop.balmet.com

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

cardconnect.php (6805B)


      1 <?php
      2 class ModelExtensionPaymentCardConnect extends Model {
      3 	public function getMethod($address, $total) {
      4 		$this->load->language('extension/payment/cardconnect');
      5 
      6 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$this->config->get('cardconnect_geo_zone') . "' AND `country_id` = '" . (int)$address['country_id'] . "' AND (`zone_id` = '" . (int)$address['zone_id'] . "' OR `zone_id` = '0')");
      7 
      8 		if ($this->config->get('cardconnect_total') > 0 && $this->config->get('cardconnect_total') > $total) {
      9 			$status = false;
     10 		} elseif (!$this->config->get('cardconnect_geo_zone')) {
     11 			$status = true;
     12 		} elseif ($query->num_rows) {
     13 			$status = true;
     14 		} else {
     15 			$status = false;
     16 		}
     17 
     18 		$method_data = array();
     19 
     20 		if ($status) {
     21 			$method_data = array(
     22 				'code'			=> 'cardconnect',
     23 				'title'			=> $this->language->get('text_title'),
     24 				'terms'			=> '',
     25 				'sort_order'	=> $this->config->get('cardconnect_sort_order')
     26 			);
     27 		}
     28 
     29 		return $method_data;
     30 	}
     31 
     32 	public function getCardTypes() {
     33 		$cards = array();
     34 
     35 		$cards[] = array(
     36 			'text'  => 'Visa',
     37 			'value' => 'VISA'
     38 		);
     39 
     40 		$cards[] = array(
     41 			'text'  => 'MasterCard',
     42 			'value' => 'MASTERCARD'
     43 		);
     44 
     45 		$cards[] = array(
     46 			'text'  => 'Discover Card',
     47 			'value' => 'DISCOVER'
     48 		);
     49 
     50 		$cards[] = array(
     51 			'text'  => 'American Express',
     52 			'value' => 'AMEX'
     53 		);
     54 
     55 		return $cards;
     56 	}
     57 
     58 	public function getMonths() {
     59 		$months = array();
     60 
     61 		for ($i = 1; $i <= 12; $i++) {
     62 			$months[] = array(
     63 				'text'  => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)),
     64 				'value' => sprintf('%02d', $i)
     65 			);
     66 		}
     67 
     68 		return $months;
     69 	}
     70 
     71 	public function getYears() {
     72 		$years = array();
     73 
     74 		$today = getdate();
     75 
     76 		for ($i = $today['year']; $i < $today['year'] + 11; $i++) {
     77 			$years[] = array(
     78 				'text'  => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
     79 				'value' => strftime('%y', mktime(0, 0, 0, 1, 1, $i))
     80 			);
     81 		}
     82 
     83 		return $years;
     84 	}
     85 
     86 	public function getCard($token, $customer_id) {
     87 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardconnect_card` WHERE `token` = '" . $this->db->escape($token) . "' AND `customer_id` = '" . (int)$customer_id . "'");
     88 
     89 		if ($query->num_rows) {
     90 			return $query->row;
     91 		} else {
     92 			return false;
     93 		}
     94 	}
     95 
     96 	public function getCards($customer_id) {
     97 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardconnect_card` WHERE `customer_id` = '" . (int)$customer_id . "'");
     98 
     99 		return $query->rows;
    100 	}
    101 
    102 	public function addCard($cardconnect_order_id, $customer_id, $profileid, $token, $type, $account, $expiry) {
    103 		$this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_card` SET `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "', `customer_id` = '" . (int)$customer_id . "', `profileid` = '" . $this->db->escape($profileid) . "', `token` = '" . $this->db->escape($token) . "', `type` = '" . $this->db->escape($type) . "', `account` = '" . $this->db->escape($account) . "', `expiry` = '" . $this->db->escape($expiry) . "', `date_added` = NOW()");
    104 	}
    105 
    106 	public function deleteCard($token, $customer_id) {
    107 		$this->db->query("DELETE FROM `" . DB_PREFIX . "cardconnect_card` WHERE `token` = '" . $this->db->escape($token) . "' AND `customer_id` = '" . (int)$customer_id . "'");
    108 	}
    109 
    110 	public function addOrder($order_info, $payment_method) {
    111 		$this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_order` SET `order_id` = '" . (int)$order_info['order_id'] . "', `customer_id` = '" . (int)$this->customer->getId() . "', `payment_method` = '" . $this->db->escape($payment_method) . "', `retref` = '" . $this->db->escape($order_info['retref']) . "', `authcode` = '" . $this->db->escape($order_info['authcode']) . "', `currency_code` = '" . $this->db->escape($order_info['currency_code']) . "', `total` = '" . $this->currency->format($order_info['total'], $order_info['currency_code'], false, false) . "', `date_added` = NOW()");
    112 
    113 		return $this->db->getLastId();
    114 	}
    115 
    116 	public function addTransaction($cardconnect_order_id, $type, $status, $order_info) {
    117 		$this->db->query("INSERT INTO `" . DB_PREFIX . "cardconnect_order_transaction` SET `cardconnect_order_id` = '" . (int)$cardconnect_order_id . "', `type` = '" . $this->db->escape($type) . "', `retref` = '" . $this->db->escape($order_info['retref']) . "', `amount` = '" . (float)$this->currency->format($order_info['total'], $order_info['currency_code'], false, false) . "', `status` = '" . $this->db->escape($status) . "', `date_modified` = NOW(), `date_added` = NOW()");
    118 	}
    119 
    120 	public function getSettlementStatuses($merchant_id, $date) {
    121 		$this->log('Getting settlement statuses from CardConnect');
    122 
    123 		$url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/settlestat?merchid=' . $merchant_id . '&date=' . $date;
    124 
    125 		$header = array();
    126 
    127 		$header[] = 'Content-type: application/json';
    128 		$header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password'));
    129 
    130 		$this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true));
    131 
    132 		$this->model_extension_payment_cardconnect->log('URL: ' . $url);
    133 
    134 		$ch = curl_init();
    135 		curl_setopt($ch, CURLOPT_URL, $url);
    136 		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    137 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    138 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    139 		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    140 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    141 		$response_data = curl_exec($ch);
    142 		if (curl_errno($ch)) {
    143 			$this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch));
    144 		}
    145 		curl_close($ch);
    146 
    147 		$response_data = json_decode($response_data, true);
    148 
    149 		$this->log('Response: ' . print_r($response_data, true));
    150 
    151 		return $response_data;
    152 	}
    153 
    154 	public function updateTransactionStatusByRetref($retref, $status) {
    155 		$this->db->query("UPDATE `" . DB_PREFIX . "cardconnect_order_transaction` SET `status` = '" . $this->db->escape($status) . "', `date_modified` = NOW() WHERE `retref` = '" . $this->db->escape($retref) . "'");
    156 	}
    157 
    158 	public function updateCronRunTime() {
    159 		$this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE `key` = 'cardconnect_cron_time'");
    160 
    161 		$this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `store_id` = '0', `code` = 'cardconnect', `key` = 'cardconnect_cron_time', `value` = NOW(), `serialized` = '0'");
    162 	}
    163 
    164 	public function log($data) {
    165 		if ($this->config->get('cardconnect_logging')) {
    166 			$log = new Log('cardconnect.log');
    167 
    168 			$log->write($data);
    169 		}
    170 	}
    171 }