shop.balmet.com

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

securetrading_ws.php (5934B)


      1 <?php
      2 class ModelExtensionPaymentSecureTradingWs extends Model {
      3 	public function getMethod($address, $total) {
      4 		$this->load->language('extension/payment/securetrading_ws');
      5 
      6 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_securetrading_ws_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
      7 
      8 		if ($this->config->get('payment_securetrading_ws_total') > $total) {
      9 			$status = false;
     10 		} elseif (!$this->config->get('payment_securetrading_ws_geo_zone_id')) {
     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'       => 'securetrading_ws',
     23 				'title'      => $this->language->get('text_title'),
     24 				'terms'      => '',
     25 				'sort_order' => $this->config->get('payment_securetrading_ws_sort_order')
     26 			);
     27 		}
     28 
     29 		return $method_data;
     30 	}
     31 
     32 	public function call($data) {
     33 		$ch = curl_init();
     34 
     35 		$defaults = array(
     36 			CURLOPT_POST => 1,
     37 			CURLOPT_HEADER => 0,
     38 			CURLOPT_SSL_VERIFYPEER => 0,
     39 			CURLOPT_URL => 'https://webservices.securetrading.net/xml/',
     40 			CURLOPT_FRESH_CONNECT => 1,
     41 			CURLOPT_RETURNTRANSFER => 1,
     42 			CURLOPT_FORBID_REUSE => 1,
     43 			CURLOPT_TIMEOUT => 15,
     44 			CURLOPT_HTTPHEADER => array(
     45 				'User-Agent: OpenCart - Secure Trading WS',
     46 				'Content-Length: ' . strlen($data),
     47 				'Authorization: Basic ' . base64_encode($this->config->get('payment_securetrading_ws_username') . ':' . $this->config->get('payment_securetrading_ws_password')),
     48 			),
     49 			CURLOPT_POSTFIELDS => $data,
     50 		);
     51 
     52 		curl_setopt_array($ch, $defaults);
     53 
     54 		$response = curl_exec($ch);
     55 
     56 		if ($response === false) {
     57 			$this->log->write('Secure Trading WS CURL Error: (' . curl_errno($ch) . ') ' . curl_error($ch));
     58 		}
     59 
     60 		curl_close($ch);
     61 
     62 		return $response;
     63 	}
     64 
     65 	public function format($number, $currency, $value = '', $format = false) {
     66 
     67 		$decimal_place = $this->currency->getDecimalPlace($currency);
     68 
     69 		if (!$value) {
     70 			$value = $this->currency->getValue($currency);
     71 		}
     72 
     73 		$amount = $value ? (float)$number * $value : (float)$number;
     74 
     75 		$amount = number_format($amount, (int)$decimal_place);
     76 
     77 		if (!$format) {
     78 			return $amount;
     79 		}
     80 	}
     81 
     82 	public function getOrder($order_id) {
     83 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "securetrading_ws_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
     84 
     85 		return $qry->row;
     86 	}
     87 
     88 	public function addMd($order_id, $md) {
     89 		$this->db->query("INSERT INTO " . DB_PREFIX . "securetrading_ws_order SET order_id = " . (int)$order_id . ", md = '" . $this->db->escape($md) . "', `created` = now(), `modified` = now()");
     90 	}
     91 
     92 	public function removeMd($md) {
     93 		$this->db->query("DELETE FROM " . DB_PREFIX . "securetrading_ws_order WHERE md = '" . $this->db->escape($md) . "'");
     94 	}
     95 
     96 	public function updateReference($order_id, $transaction_reference) {
     97 		$this->db->query("UPDATE " . DB_PREFIX . "securetrading_ws_order SET transaction_reference = '" . $this->db->escape($transaction_reference) . "' WHERE order_id = " . (int)$order_id);
     98 
     99 		if ($this->db->countAffected() == 0) {
    100 			$this->db->query("INSERT INTO " . DB_PREFIX . "securetrading_ws_order SET order_id = " . (int)$order_id . ", transaction_reference = '" . $this->db->escape($transaction_reference) . "', `created` = now(), `modified` = now()");
    101 		}
    102 	}
    103 
    104 	public function getOrderId($md) {
    105 		$row = $this->db->query("SELECT order_id FROM " . DB_PREFIX . "securetrading_ws_order WHERE md = '" . $this->db->escape($md) . "' LIMIT 1")->row;
    106 
    107 		if (isset($row['order_id']) && !empty($row['order_id'])) {
    108 			return $row['order_id'];
    109 		} else {
    110 			return false;
    111 		}
    112 	}
    113 
    114 	public function confirmOrder($order_id, $order_status_id, $comment = '', $notify = false) {
    115 		$this->load->model('checkout/order');
    116 
    117 		$this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = 0 WHERE order_id = " . (int)$order_id);
    118 
    119 		$this->model_checkout_order->addOrderHistory($order_id, $order_status_id, $comment, $notify);
    120 
    121 		$order_info = $this->model_checkout_order->getOrder($order_id);
    122 
    123 		$securetrading_ws_order = $this->getOrder($order_info['order_id']);
    124 
    125 		$amount = $this->currency->format($order_info['total'], $order_info['currency_code'], false, false);
    126 
    127 		switch($this->config->get('payment_securetrading_ws_settle_status')){
    128 			case 0:
    129 				$trans_type = 'auth';
    130 				break;
    131 			case 1:
    132 				$trans_type = 'auth';
    133 				break;
    134 			case 2:
    135 				$trans_type = 'suspended';
    136 				break;
    137 			case 100:
    138 				$trans_type = 'payment';
    139 				break;
    140 			default :
    141 				$trans_type = '';
    142 		}
    143 
    144 		$this->db->query("UPDATE `" . DB_PREFIX . "securetrading_ws_order` SET `settle_type` = '" . $this->config->get('payment_securetrading_ws_settle_status') . "', `modified` = now(), `currency_code` = '" . $this->db->escape($order_info['currency_code']) . "', `total` = '" . $amount . "' WHERE order_id = " . (int)$order_info['order_id']);
    145 
    146 		$this->db->query("INSERT INTO `" . DB_PREFIX . "securetrading_ws_order_transaction` SET `securetrading_ws_order_id` = '" . (int)$securetrading_ws_order['securetrading_ws_order_id'] . "', `amount` = '" . $amount . "', type = '" . $trans_type . "',  `created` = now()");
    147 	}
    148 
    149 	public function updateOrder($order_id, $order_status_id, $comment = '', $notify = false) {
    150 		$this->load->model('checkout/order');
    151 
    152 		$this->db->query("UPDATE `" . DB_PREFIX . "order` SET order_status_id = " . (int)$order_status_id . " WHERE order_id = "  . (int)$order_id);
    153 
    154 		$this->model_checkout_order->addOrderHistory($order_id, $order_status_id, $comment, $notify);
    155 	}
    156 
    157 	public function logger($message) {
    158 		$log = new Log('secure.log');
    159 		$log->write($message);
    160 	}
    161 }