1. 程式人生 > >微信H5支付,從第三方手機瀏覽器中直接打開支付頁面

微信H5支付,從第三方手機瀏覽器中直接打開支付頁面

pan 兩個 add field out 字典 註意 cti 返回

首先在商戶平臺通開H5支付功能,然後幫後綁定,支付完成之後需要跳轉的地址,開通之後就可以開發H5支付;

首先是簽名,臥槽,說到這個就想罵人,

技術分享

官方文檔的解說;文科生哪能看得懂什麽是集合;

下面就來簽名:

<?php

//生成隨機字符串
function getNonceNum($numLen=16){
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $numLen; $i++) {
      $str
.= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } //獲取用戶ip地址 function get_client_ip(){ $cip = "unknown"; if($_SERVER[‘REMOTE_ADDR‘]){ $cip = $_SERVER[‘REMOTE_ADDR‘]; }elseif(getenv("REMOTE_ADDR")){ $cip = getenv("REMOTE_ADDR"); } return
$cip; } $getNonceNumstring = getNonceNum(); $out_trade_no = date("Ymd").getNonceNum(); $ip = get_client_ip(); $param[‘appid‘] = "############"; $param[‘mch_id‘] = ""############";"; $param[‘nonce_str‘] = $getNonceNumstring; $param[‘body‘] = "###";
$param[‘out_trade_no‘] = $out_trade_no; $param[‘total_fee‘] = 1; $param[‘spbill_create_ip‘] = $ip; $param[‘notify_url‘] = "http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php"; $param[‘trade_type‘] = "MWEB"; $param[‘scene_info‘] = ‘{"h5_info": "h5_info" {"type": "IOS","app_name": "h5支付測試","bundle_id": ""}}‘; //字典排序 ksort($param); $sign_raw = ""; foreach($param as $k => $v){ $sign_raw .= $k."=".$v."&"; } $sign_raw .= "key=############"; //生成簽名 $sign = strtoupper(md5($sign_raw));

將上面的###替換成你們的參數;太敏感就不貼出來了

然後就是發送xml數據請求接口;

$xml = <<<EOF
<xml>
<appid>###</appid>
<body>###</body>
<mch_id>###</mch_id>
<nonce_str>$getNonceNumstring</nonce_str>
<notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
<out_trade_no>$out_trade_no</out_trade_no>
<spbill_create_ip>$ip</spbill_create_ip>
<total_fee>1</total_fee>
<trade_type>MWEB</trade_type>
<scene_info>{"h5_info": "h5_info" {"type": "IOS","app_name": "h5支付測試","bundle_id": ""}}</scene_info>
<sign>$sign</sign>
</xml>
EOF;

$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

//調用接口,返回xml數據包含跳轉url;
$result = postXmlCurl($xml, $url);


//解析xml
$xml = simplexml_load_string($result);
$bigarr = array();

//循環生成數組
foreach($xml->children() as $child){
    $key =  $child->getName();
    $bigarr["$key"] = "$child";//必須加引號
}
$tiaozhuanurl = $bigarr[‘mweb_url‘]."&redirect_url=####";

?>



<a href="<?php echo $tiaozhuanurl;?>">
<h1>h5支付</h1>
</a>

<?php

function postXmlCurl($xml, $url, $useCert = false, $second = 30){        
    $ch = curl_init();
    //設置超時
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    
    
    curl_setopt($ch,CURLOPT_URL, $url);
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }else{
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//嚴格校驗
    } 
    //設置header
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    //要求結果為字符串且輸出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if($useCert == true){
        //設置證書
        //使用證書:cert 與 key 分別屬於兩個.pem文件
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘PEM‘);
        curl_setopt($ch,CURLOPT_SSLCERT, "###.pem");
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,‘PEM‘);
        curl_setopt($ch,CURLOPT_SSLKEY, "###.pem");
        
        curl_setopt($ch, CURLOPT_CAINFO, "###.pem‘); 
    }
    //post提交方式
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    //運行curl
    $data = curl_exec($ch);
    //返回結果
    if($data){
        curl_close($ch);
        return $data;
    } else { 
        $error = curl_errno($ch);
        curl_close($ch);
        echo "error";
    }
}

其中要註意的就是xml解析;這個不需要證書;

微信H5支付,從第三方手機瀏覽器中直接打開支付頁面