dever 6 年 前
コミット
6e6e2e58d5
5 ファイル変更20 行追加16 行削除
  1. 3 1
      lib/Wechat.php
  2. 1 12
      sdk/wechat.php
  3. 2 2
      sdk/wechat/WxPay.Api.php
  4. 6 1
      sdk/wechat/WxPay.Config.php
  5. 8 0
      src/Api.php

+ 3 - 1
lib/Wechat.php

@@ -17,7 +17,9 @@ class Wechat extends Core
 			'key' => $config['file_key'],
 		);
 
-		$this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout']);
+		$config['ip'] = (isset($config['ip']) && $config['ip']) ? $config['ip'] : Dever::ip();
+
+		$this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout'], $config['ip']);
 	}
 
 	/**

+ 1 - 12
sdk/wechat.php

@@ -1,16 +1,5 @@
 <?php
-function get_client_ip() {
-    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
-        $ip = getenv('HTTP_CLIENT_IP');
-    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
-        $ip = getenv('HTTP_X_FORWARDED_FOR');
-    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
-        $ip = getenv('REMOTE_ADDR');
-    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
-        $ip = $_SERVER['REMOTE_ADDR'];
-    }
-    return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
-}
+
 Dever::apply('sdk/wechat/WxPay.Api', 'pay');
 Dever::apply('sdk/wechat/WxPay.JsApiPay', 'pay');
 Dever::apply('sdk/wechat/WxPay.Config', 'pay');

+ 2 - 2
sdk/wechat/WxPay.Api.php

@@ -50,7 +50,7 @@ class WxPayApi
 		
 		$inputObj->SetAppid($config->GetAppId());//公众账号ID
 		$inputObj->SetMch_id($config->GetMerchantId());//商户号
-		$inputObj->SetSpbill_create_ip(get_client_ip());//终端ip	   	    
+		$inputObj->SetSpbill_create_ip($config->GetIp());//终端ip	   	    
 		$inputObj->SetNonce_str(self::getNonceStr());//随机字符串
 		
 		//签名
@@ -261,7 +261,7 @@ class WxPayApi
 			throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
 		}
 		
-		$inputObj->SetSpbill_create_ip(get_client_ip());//终端ip
+		$inputObj->SetSpbill_create_ip($config->GetIp());//终端ip
 		$inputObj->SetAppid($config->GetAppId());//公众账号ID
 		$inputObj->SetMch_id($config->GetMerchantId());//商户号
 		$inputObj->SetNonce_str(self::getNonceStr());//随机字符串

+ 6 - 1
sdk/wechat/WxPay.Config.php

@@ -2,7 +2,7 @@
 
 class WxPayConfig
 {
-	public function set($appid, $appsecret, $merchantid, $notify, $key, $ssl, $type, $timeout = 600)
+	public function set($appid, $appsecret, $merchantid, $notify, $key, $ssl, $type, $timeout = 600, $ip = '')
 	{
 		$this->appid = $appid;
 		$this->appsecret = $appsecret;
@@ -12,8 +12,13 @@ class WxPayConfig
 		$this->ssl = $ssl;
 		$this->type = $type;
 		$this->timeout = $timeout;
+		$this->ip = $ip;
 		return $this;
 	}
+	public function GetIp()
+	{
+		return $this->ip;
+	}
 	public function GetAppId()
 	{
 		return $this->appid;

+ 8 - 0
src/Api.php

@@ -128,6 +128,7 @@ class Api
 		$this->openid = $this->getParam($param, 'openid');
 		$this->other = $this->getParam($param, 'other');
 		$this->h5 = $this->getParam($param, 'h5');
+		$this->ip = $this->getParam($param, 'ip');
 
 		if (!$this->project_id) {
 			$this->project_id = false;
@@ -157,6 +158,10 @@ class Api
 			Dever::alert('没有支付金额');
 		}
 
+		if (!$this->ip) {
+			$this->ip = Dever::ip();
+		}
+
 		return $this->pay();
 	}
 
@@ -185,6 +190,9 @@ class Api
 		if (isset($this->refer) && $this->refer) {
 			$pay['refer'] = $this->refer;
 		}
+		if (isset($this->ip) && $this->ip) {
+			$pay['ip'] = $this->ip;
+		}
 		
 		$this->method = new $method($pay);
 		return $this;