shop.balmet.com

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

pilibaba.php (5472B)


      1 <?php
      2 class ModelExtensionPaymentPilibaba extends Model {
      3 	public function install() {
      4 		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "pilibaba_order` (
      5 			`pilibaba_order_id` int(11) NOT NULL AUTO_INCREMENT,
      6 			`order_id` int(11) NOT NULL DEFAULT '0',
      7 			`amount` double NOT NULL,
      8 			`fee` double NOT NULL,
      9 			`tracking` VARCHAR(50) NOT NULL DEFAULT '',
     10 			`date_added` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
     11 			PRIMARY KEY (`pilibaba_order_id`)
     12 		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");
     13 	}
     14 
     15 	public function uninstall() {
     16 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "pilibaba_order`");
     17 
     18 		$this->disablePiliExpress();
     19 
     20 		$this->log('Module uninstalled');
     21 	}
     22 
     23 	public function getCurrencies() {
     24 		$ch = curl_init();
     25 		curl_setopt($ch, CURLOPT_URL, 'http://www.pilibaba.com/pilipay/getCurrency');
     26 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
     27 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     28 		curl_setopt($ch, CURLOPT_HEADER, false);
     29 		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     30 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     31 		$response = curl_exec($ch);
     32 		curl_close($ch);
     33 
     34 		return json_decode($response, true);
     35 	}
     36 
     37 	public function getWarehouses() {
     38 		$ch = curl_init();
     39 		curl_setopt($ch, CURLOPT_URL, 'http://www.pilibaba.com/pilipay/getAddressList');
     40 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
     41 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     42 		curl_setopt($ch, CURLOPT_HEADER, false);
     43 		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     44 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     45 		$response = curl_exec($ch);
     46 		curl_close($ch);
     47 
     48 		return json_decode($response, true);
     49 	}
     50 
     51 	public function getOrder($order_id) {
     52 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "pilibaba_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
     53 
     54 		if ($query->num_rows) {
     55 			return $query->row;
     56 		} else {
     57 			return false;
     58 		}
     59 	}
     60 
     61 	public function register($email, $password, $currency, $warehouse, $country, $environment) {
     62 		$this->log('Posting register');
     63 
     64 		if ($warehouse == 'other') {
     65 			$warehouse = '';
     66 		}
     67 
     68 		if ($warehouse) {
     69 			$country = '';
     70 		}
     71 
     72 		if ($environment == 'live') {
     73 			$url = 'http://en.pilibaba.com/autoRegist';
     74 		} else {
     75 			$url = 'http://preen.pilibaba.com/autoRegist';
     76 		}
     77 
     78 		$this->log('URL: ' . $url);
     79 
     80 		$app_secret = strtoupper(md5((($warehouse) ? $warehouse : $country) . '0210000574' . '0b8l3ww5' . $currency . $email . md5($password)));
     81 
     82 		$data = array(
     83 			'platformNo'  => '0210000574',
     84 			'appSecret'   => $app_secret,
     85 			'email'       => $email,
     86 			'password'    => md5($password),
     87 			'currency'    => $currency,
     88 			'logistics'   => $warehouse,
     89 			'countryCode' => $country
     90 		);
     91 
     92 		$this->log('Data: ' . print_r($data, true));
     93 
     94 		$headers = array('Accept: application/json','Content-Type: application/json');
     95 
     96 		$ch = curl_init();
     97 		curl_setopt($ch, CURLOPT_URL, $url);
     98 		curl_setopt($ch, CURLOPT_POST, true);
     99 		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    100 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    101 		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    102 		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    103 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    104 		$response = curl_exec($ch);
    105 		if (curl_errno($ch)) {
    106 			$this->log('cURL error: ' . curl_errno($ch));
    107 		}
    108 		curl_close($ch);
    109 
    110 		$this->log('Response: ' . print_r($response, true));
    111 
    112 		return json_decode($response, true);
    113 	}
    114 
    115 	public function updateTrackingNumber($order_id, $tracking_number, $merchant_number) {
    116 		$this->log('Posting tracking');
    117 
    118 		$sign_msg = strtoupper(md5($order_id . $tracking_number . $merchant_number . $this->config->get('payment_pilibaba_secret_key')));
    119 
    120 		if ($this->config->get('payment_pilibaba_environment') == 'live') {
    121 			$url = 'https://www.pilibaba.com/pilipay/updateTrackNo';
    122 		} else {
    123 			$url = 'http://pre.pilibaba.com/pilipay/updateTrackNo';
    124 		}
    125 
    126 		$url .= '?orderNo=' . $order_id . '&logisticsNo=' . $tracking_number . '&merchantNo=' . $merchant_number . '&signMsg=' . $sign_msg;
    127 
    128 		$this->log('URL: ' . $url);
    129 
    130 		$ch = curl_init();
    131 		curl_setopt($ch, CURLOPT_URL, $url);
    132 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    133 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    134 		curl_setopt($ch, CURLOPT_HEADER, false);
    135 		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    136 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    137 		$response = curl_exec($ch);
    138 		if (curl_errno($ch)) {
    139 			$this->log('cURL error: ' . curl_errno($ch));
    140 		}
    141 		curl_close($ch);
    142 
    143 		$this->db->query("UPDATE `" . DB_PREFIX . "pilibaba_order` SET `tracking` = '" . $this->db->escape($tracking_number) . "' WHERE `order_id` = '" . (int)$order_id . "'");
    144 	}
    145 
    146 	public function enablePiliExpress() {
    147 		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "extension` WHERE `type` = 'shipping' AND `code` = 'pilibaba'");
    148 
    149 		if (!$query->num_rows) {
    150 			$this->db->query("INSERT INTO `" . DB_PREFIX . "extension` SET `type` = 'shipping', `code` = 'pilibaba'");
    151 		}
    152 	}
    153 
    154 	public function disablePiliExpress() {
    155 		$this->db->query("DELETE FROM `" . DB_PREFIX . "extension` WHERE `type` = 'shipping' AND `code` = 'pilibaba'");
    156 	}
    157 
    158 	public function log($data) {
    159 		if ($this->config->has('payment_pilibaba_logging') && $this->config->get('payment_pilibaba_logging')) {
    160 			$log = new Log('pilibaba.log');
    161 
    162 			$log->write($data);
    163 		}
    164 	}
    165 }