PHP 喚起微信支付

<?php
/**
**/
class H5wxAction extends CommonAction {
	public function __construct() {
		parent::__construct();
		
	
	}
	/*
	字段名	變量名	必填	類型	示例值	描述
公衆賬號ID	appid	是	String(32)	wxd678efh567hg6787	微信分配的公衆賬號ID(企業號corpid即爲此appId)
商戶號	mch_id	是	String(32)	1230000109	微信支付分配的商戶號
設備號	device_info	否	String(32)	013467007045764	終端設備號(門店號或收銀設備ID),注意:PC網頁或公衆號內支付請傳"WEB"
隨機字符串	nonce_str	是	String(32)	5K8264ILTKCH16CQ2502SI8ZNMTM67VS	隨機字符串,不長於32位。推薦隨機數生成算法
簽名	sign	是	String(32)	C380BEC2BFD727A4B6845133519F3AD6	簽名,詳見簽名生成算法
簽名類型	sign_type	否	String(32)	HMAC-SHA256	簽名類型,目前支持HMAC-SHA256和MD5,默認爲MD5
商品描述	body	是	String(128)	騰訊充值中心-QQ會員充值	
商品簡單描述,該字段須嚴格按照規範傳遞,具體請見參數規定
商品詳情	detail	否	String(6000)		單品優惠字段(暫未上線)
附加數據	attach	否	String(127)	深圳分店	附加數據,在查詢API和支付通知中原樣返回,該字段主要用於商戶攜帶訂單的自定義數據
商戶訂單號	out_trade_no	是	String(32)	20150806125346	商戶系統內部的訂單號,32個字符內、可包含字母, 其他說明見商戶訂單號
貨幣類型	fee_type	否	String(16)	CNY	符合ISO 4217標準的三位字母代碼,默認人民幣:CNY,其他值列表詳見貨幣類型
總金額	total_fee	是	Int	888	訂單總金額,單位爲分,詳見支付金額
終端IP	spbill_create_ip	是	String(16)	123.12.12.123	必須傳正確的用戶端IP,詳見獲取用戶ip指引
交易起始時間	time_start	否	String(14)	20091225091010	訂單生成時間,格式爲yyyyMMddHHmmss,如2009年12月25日9點10分10秒錶示爲20091225091010。其他詳見時間規則
交易結束時間	time_expire	否	String(14)	20091227091010	
訂單失效時間,格式爲yyyyMMddHHmmss,如2009年12月27日9點10分10秒錶示爲20091227091010。其他詳見時間規則
注意:最短失效時間間隔必須大於5分鐘
商品標記	goods_tag	否	String(32)	WXG	商品標記,代金券或立減優惠功能的參數,說明詳見代金券或立減優惠
通知地址	notify_url	是	String(256)	http://www.weixin.qq.com/wxpay/pay.php	接收微信支付異步通知回調地址,通知url必須爲直接可訪問的url,不能攜帶參數。
交易類型	trade_type	是	String(16)	MWEB	H5支付的交易類型爲MWEB
商品ID	product_id	否	String(32)	12235413214070356458058	trade_type=NATIVE,此參數必傳。此id爲二維碼中包含的商品ID,商戶自行定義。
指定支付方式	limit_pay	否	String(32)	no_credit	no_credit--指定不能使用信用卡支付
用戶標識	openid	否	String(128)	oUpF8uMuAJO_M2pxb1Q9zNjWeS6o	trade_type=JSAPI,此參數必傳,用戶在商戶appid下的唯一標識。openid如何獲取,可參考【獲取openid】。企業號請使用【企業號OAuth2.0接口】獲取企業號內成員userid,再調用【企業號userid轉openid接口】進行轉換
場景信息	scene_info	是	String(256)	//IOS移動應用
{"h5_info": {"type":"IOS","app_name": "王者榮耀","bundle_id": "com.tencent.wzryIOS"}}

//安卓移動應用
{"h5_info": {"type":"Android","app_name": "王者榮耀","package_name": "com.tencent.tmgp.sgame"}}

//WAP網站應用
{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "騰訊充值"}}	該字段用於上報支付的場景信息,針對H5支付有以下三種場景,請根據對應場景上報,H5支付不建議在APP端使用,針對場景1,2請接入APP支付,不然可能會出現兼容性問題

1,IOS移動應用
{"h5_info": //h5支付固定傳"h5_info" 
    {"type": "",  //場景類型
     "app_name": "",  //應用名
     "bundle_id": ""  //bundle_id
     }
}

2,安卓移動應用
{"h5_info": //h5支付固定傳"h5_info" 
    {"type": "",  //場景類型
     "app_name": "",  //應用名
     "package_name": ""  //包名
     }
}

3,WAP網站應用
{"h5_info": //h5支付固定傳"h5_info" 
   {"type": "",  //場景類型
    "wap_url": "",//WAP網站URL地址
    "wap_name": ""  //WAP 網站名
    }
}

	
	*/
	public function config(){
		$array=array(
			'appid'=>'****', //公衆賬號ID	appid	是	String(32)	wxd678efh567hg6787	微信分配的公衆賬號ID(企業號corpid即爲此appId)
			'mch_id'=>'****',// 商戶號	mch_id	是	String(32)	1230000109	微信支付分配的商戶號
			'nonce_str'=>rand(100000,999999),// 隨機字符串	nonce_str	是	String(32)	5K8264ILTKCH16CQ2502SI8ZNMTM67VS	隨機字符串,不長於32位。推薦隨機數生成算法
			'key'=>'****',
			'APPSECRET'=>'*****',
		
		);
		return $array;
	
	}
	
	public function H5WeiXin(){
	
		$config = $this->config();
		
		
		$count=1;
		//$money= 1;//充值金額   
         
       
       $userip = $this->getIP(); //獲得用戶設備IP
       $appid = $config['appid']; 
       $mch_id = $config['mch_id']; 
       $key = $config['key']; 
       
       $rand = rand(00000,99999);  
       $out_trade_no = $_POST['number'];//訂單號  
       $nonce_str=MD5($rand);//隨機字符串  
       $body = $data['title'];//內容  
       $total_fee = $count; //金額  
       $spbill_create_ip = $userip; //IP  
       $notify_url = 'http://'.$_SERVER['HTTP_HOST'].'/Wap/H5wx/WxHD'; //回調地址  
       $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].'/Wap/H5wx/is_wxpay?number='.$_POST['number']; //回調地址  
       $trade_type = 'MWEB';//交易類型
       $scene_info ='{"h5_info":{"type":"Wap","wap_url":"'.$_SERVER['HTTP_HOST'].'","wap_name":"'.$data['title'].'"}}';//場景信息 
       $signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";  
       $strSignTmp = $signA."&key=$key"; //拼接字符串   
	   

	   
       $sign = strtoupper(MD5($strSignTmp)); // MD5 後轉換成大寫  
       $post_data = "<xml>  
                       <appid>$appid</appid>  
                       <body>$body</body>  
                       <mch_id>$mch_id</mch_id>  
                       <nonce_str>$nonce_str</nonce_str>  
                       <notify_url>$notify_url</notify_url>  
                       <out_trade_no>$out_trade_no</out_trade_no>  
                       <scene_info>$scene_info</scene_info>  
                       <spbill_create_ip>$spbill_create_ip</spbill_create_ip>  
                       <total_fee>$total_fee</total_fee>  
                       <trade_type>$trade_type</trade_type>  
                       <sign>$sign</sign>  
                   </xml>";//拼接成XML 格式  
       $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
       $dataxml = $this->http_post($url,$post_data);
       $objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); 
		
		
		if($objectxml['return_code']=='SUCCESS'){
			$json['url'] = $objectxml['mweb_url'].'&redirect_url='.$redirect_url; //喚起微信鏈接
			$json['msg'] = '1';
		
		}else{
			$json['info'] ='參數錯誤';
			$json['msg'] ='0';
		}
	
		echo json_encode($json);
	
	
	
	}
	//查詢是否支付成功  示例 根據需要調整
	public function is_wxpay(){
		$out_trade_no = $_GET['number'];
		
		$type = substr($out_trade_no,0,1);	
		if($type=='B'){
			$is_state = M('Bond')->where(array('number'=>$out_trade_no))->find();
			$url = U('Court/details',array('id'=>$is_state['aid']));
		}elseif($type=='W'){
			$is_state = M('Wallet')->where(array('number'=>$out_trade_no))->find();
			$url = U('Member/wallet');
		}else{
			$is_state =M('viplog')->where(array('number'=>$out_trade_no))->find();
			$url = U('Member/vip');
		}
		
		if($is_state['state']=='1'){
			cookie('id',$is_state['mid']);
			header("location: ".$url."");
		}else{
			echo '<h1>未查詢到此訂單的付款信息</h1>';
		}
		
	
	
	}
	
	//微信回調地址
	public function WxHD(){
			$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
			$array = $this->xmlToArray($GLOBALS['HTTP_RAW_POST_DATA']);
			M('api_log')->add(array('content'=>json_encode($this->xmlToArray($GLOBALS['HTTP_RAW_POST_DATA'])).$array['out_trade_no']));
				
			
			
			if($array['return_code']=='SUCCESS'){
				
				$out_trade_no=$array['out_trade_no'];
				//$number = explode('-',$out_trade_no);
				$type = substr($out_trade_no,0,1);	
				
				header("location: http://".$_SERVER['HTTP_HOST'].$url."");
						
						
						
				
			}
		
		
		
			
		
	}
public function xmlToArray($xml){ 
 
 //禁止引用外部xml實體 
 
libxml_disable_entity_loader(true); 
 
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
 
$val = json_decode(json_encode($xmlstring),true); 
 
return $val; 
 
} 
public function convertUrlQuery($query)
{
  $queryParts = explode('&', $query);
  $params = array();
  foreach ($queryParts as $param) {
    $item = explode('=', $param);
    $params[$item[0]] = $item[1];
  }
  return implode('&',$params);
}
	
public function http_post($url, $data) {  
      $ch = curl_init();  
      curl_setopt($ch, CURLOPT_URL,$url);  
      curl_setopt($ch, CURLOPT_HEADER,0);  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
      curl_setopt($ch, CURLOPT_POST, 1);  
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
      $res = curl_exec($ch);  
      curl_close($ch);  
      return $res;  
  } 
	
	public function getIP() { 
		if(getenv('HTTP_CLIENT_IP')) { 
			$ip = getenv('HTTP_CLIENT_IP'); 
		}elseif (getenv('HTTP_X_FORWARDED_FOR')) { 
			$ip = getenv('HTTP_X_FORWARDED_FOR'); 
		}elseif (getenv('HTTP_X_FORWARDED')) { 
			$ip = getenv('HTTP_X_FORWARDED'); 
		}elseif (getenv('HTTP_FORWARDED_FOR')) { 
		$ip = getenv('HTTP_FORWARDED_FOR'); 

		}elseif (getenv('HTTP_FORWARDED')) { 
			$ip = getenv('HTTP_FORWARDED'); 
		}else { 
			$ip = $_SERVER['REMOTE_ADDR']; 
		} 
		return $ip; 
	} 
	
	
	

}

 
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章