shop.balmet.com

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

alipay_cross.php (4832B)


      1 <?php
      2 class ModelExtensionPaymentAlipayCross extends Model {
      3 	var $https_verify_url = 'https://mapi.alipay.com/gateway.do?service=notify_verify&';
      4 	var $https_verify_url_test = 'https://openapi.alipaydev.com/gateway.do?service=notify_verify&';
      5 	var $alipay_config;
      6 
      7 	public function getMethod($address, $total) {
      8 		$this->load->language('extension/payment/alipay_cross');
      9 
     10 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_alipay_cross_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
     11 
     12 		if ($this->config->get('payment_alipay_cross_total') > 0 && $this->config->get('payment_alipay_cross_total') > $total) {
     13 			$status = false;
     14 		} elseif (!$this->config->get('payment_alipay_cross_geo_zone_id')) {
     15 			$status = true;
     16 		} elseif ($query->num_rows) {
     17 			$status = true;
     18 		} else {
     19 			$status = false;
     20 		}
     21 
     22 		$method_data = array();
     23 
     24 		if ($status) {
     25 			$method_data = array(
     26 				'code'       => 'alipay_cross',
     27 				'title'      => $this->language->get('text_title'),
     28 				'terms'      => '',
     29 				'sort_order' => $this->config->get('payment_alipay_cross_sort_order')
     30 			);
     31 		}
     32 
     33 		return $method_data;
     34 	}
     35 
     36 	function buildRequestMysign($para_sort) {
     37 		$prestr = $this->createLinkstring($para_sort);
     38 
     39 		$mysign = "";
     40 		switch (strtoupper(trim($this->alipay_config['sign_type']))) {
     41 			case "MD5" :
     42 				$mysign = $this->md5Sign($prestr, $this->alipay_config['key']);
     43 				break;
     44 			default :
     45 				$mysign = "";
     46 		}
     47 
     48 		return $mysign;
     49 	}
     50 
     51 
     52 	function buildRequestPara($alipay_config, $para_temp) {
     53 		$this->alipay_config = $alipay_config;
     54 
     55 		$para_filter = $this->paraFilter($para_temp);
     56 
     57 		$para_sort = $this->argSort($para_filter);
     58 
     59 		$mysign = $this->buildRequestMysign($para_sort);
     60 
     61 		$para_sort['sign'] = $mysign;
     62 		$para_sort['sign_type'] = strtoupper(trim($this->alipay_config['sign_type']));
     63 
     64 		return $para_sort;
     65 	}
     66 
     67 	function verifyNotify($alipay_config){
     68 		$this->alipay_config = $alipay_config;
     69 
     70 		if(empty($_POST)) {
     71 			return false;
     72 		}
     73 		else {
     74 			$isSign = $this->getSignVeryfy($_POST, $_POST["sign"]);
     75 
     76 			$responseTxt = 'false';
     77 			if (! empty($_POST["notify_id"])) {
     78 				$responseTxt = $this->getResponse($_POST["notify_id"]);
     79 			}
     80 
     81 			//Veryfy
     82 			if (preg_match("/true$/i",$responseTxt) && $isSign) {
     83 				return true;
     84 			} else {
     85 				$this->log->write($responseTxt);
     86 				return false;
     87 			}
     88 		}
     89 	}
     90 
     91 	function getSignVeryfy($para_temp, $sign) {
     92 		$para_filter = $this->paraFilter($para_temp);
     93 
     94 		$para_sort = $this->argSort($para_filter);
     95 
     96 		$prestr = $this->createLinkstring($para_sort);
     97 
     98 		switch (strtoupper(trim($this->alipay_config['sign_type']))) {
     99 			case "MD5" :
    100 				$isSgin = $this->md5Verify($prestr, $sign, $this->alipay_config['key']);
    101 				break;
    102 			default :
    103 				$isSgin = false;
    104 		}
    105 
    106 		return $isSgin;
    107 	}
    108 
    109 	function getResponse($notify_id) {
    110 		$partner = trim($this->alipay_config['partner']);
    111 		$veryfy_url = $this->config->get('payment_alipay_cross_test') == "sandbox" ? $this->https_verify_url_test : $this->https_verify_url;
    112 		$veryfy_url .= "partner=" . $partner . "&notify_id=" . $notify_id;
    113 		$responseTxt = $this->getHttpResponseGET($veryfy_url, $this->alipay_config['cacert']);
    114 
    115 		return $responseTxt;
    116 	}
    117 
    118 	function createLinkstring($para) {
    119 		$arg  = "";
    120 		while (list ($key, $val) = each ($para)) {
    121 			$arg .= $key . "=" . $val . "&";
    122 		}
    123 		//remove the last char '&'
    124 		$arg = substr($arg, 0, count($arg)-2);
    125 
    126 		return $arg;
    127 	}
    128 
    129 	function paraFilter($para) {
    130 		$para_filter = array();
    131 		while (list ($key, $val) = each ($para)) {
    132 			if($key == "sign" || $key == "sign_type" || $val == "")continue;
    133 			else	$para_filter[$key] = $para[$key];
    134 		}
    135 		return $para_filter;
    136 	}
    137 
    138 	function argSort($para) {
    139 		ksort($para);
    140 		reset($para);
    141 		return $para;
    142 	}
    143 
    144 	function getHttpResponseGET($url,$cacert_url) {
    145 		$curl = curl_init($url);
    146 		curl_setopt($curl, CURLOPT_HEADER, 0 );
    147 		curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
    148 		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    149 		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    150 		curl_setopt($curl, CURLOPT_CAINFO,$cacert_url);
    151 		$responseText = curl_exec($curl);
    152 		if (!$responseText) {
    153 			$this->log->write('ALIPAY NOTIFY CURL_ERROR: ' . var_export(curl_error($curl), true));
    154 		}
    155 		curl_close($curl);
    156 
    157 		return $responseText;
    158 	}
    159 
    160 	function md5Sign($prestr, $key) {
    161 		$prestr = $prestr . $key;
    162 		return md5($prestr);
    163 	}
    164 
    165 	function md5Verify($prestr, $sign, $key)
    166 	{
    167 		$prestr = $prestr . $key;
    168 		$mysgin = md5($prestr);
    169 
    170 		if ($mysgin == $sign) {
    171 			return true;
    172 		} else {
    173 			return false;
    174 		}
    175 	}
    176 }
    177