進行代碼接入前,需在微信後臺填寫受權回調域名,此域名必須通過ICP備案php
請求微信統一下單接口,接口地址:https://api.mch.weixin.qq.com...web
MWEB
以上即是H5支付下單所須要的參數api
代碼:數組
/** * 組建簽名 * @param array $params 請求參數 * @param string $key 祕鑰 */ public function genSign($params, $key) { foreach ($params as $k=>$v) { if (!$v) { unset($params[$k]); } } ksort($params); $paramStr = ''; foreach ($params as $k => $v) { $paramStr = $paramStr . $k . '=' . $v . '&'; } $paramStr = $paramStr . 'key='.$key; $sign = strtoupper(md5($paramStr)); return $sign; }
將參數轉爲XML數據,便可發起請求
將數組轉爲XML代碼:微信
/** * 將數組轉爲XML * @param array $params 支付請求參數 */ public function array_to_xml($params) { if(!is_array($params)|| count($params) <= 0) { return false; } $xml = "<xml>"; foreach ($params as $key=>$val) { if (is_numeric($val)) { $xml.="<".$key.">".$val."</".$key.">"; } else { $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; }
請求代碼:app
$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_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $return = curl_exec($ch); curl_close($ch); return $return;
微信返回的是XML數據:curl
<xml><return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wxdded766660f9b840]]></appid> <mch_id><![CDATA[1516216351]]></mch_id> <device_info><![CDATA[100]]></device_info> <nonce_str><![CDATA[2DUN2i2pGnlC6vDi]]></nonce_str> <sign><![CDATA[95CEA831D598299097A32D8FEEC6BDEF]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx22194530678545eb3713f2f10724143329]]></prepay_id> <trade_type><![CDATA[MWEB]]></trade_type> <mweb_url><![CDATA[https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx22194530678545eb3713f2f10724143329&package=87106983]]></mweb_url>
return_code
爲SUCCESS
表明支付請求成功;mweb_url
則爲支付跳轉頁,此時客戶端經過mweb_url已經能夠調起微信支付異步
在獲得微信返回的mweb_url
參數後,可在服務端進一步得到deepLink
代碼:微信支付
/** * 獲取微信支付中間頁deepLink參數 * @param string $url 微信返回的mweb_url * @param string $ip 用戶端IP */ public function getDeeplink(string $url, string $ip) { $headers = array("X-FORWARDED-FOR:$ip", "CLIENT-IP:$ip"); ob_start(); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HTTPHEADER , $headers ); curl_setopt ($ch, CURLOPT_REFERER, "pay.o9di.cn"); curl_setopt( $ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; Android 6.0.1; OPPO R11s Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0.2883.91 Mobile Safari/537.36'); curl_exec($ch); curl_close ($ch); $out = ob_get_contents(); ob_clean(); $a = preg_match('/weixin:\/\/wap.*/',$out, $str); if ($a) { return substr($str[0], 0, strlen($str[0])-1); } else { return ''; } }
weixin://wap/pay?prepayid%3Dwx22201221074146ac747121890095299503&package=2656135616&noncestr=1542888966&sign=e31dbc2d1231708ff8a982b15a6c7646
即爲獲得的deepLink值,客戶端也能夠經過此值直接調起支付ui