微信H5支付 在其他瀏覽器調用微信支付


微信H5支付的相關資料不是很多、不過步驟上來說不是很復雜 比公眾號支付簡單很多。

先上官方文檔吧 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1

微信H5支付是需要單獨申請的 這里就不多說了 自己去申請就OK了 

這里要注意 設置的 授權回調頁面域名 是指的支付成功后跳轉的頁面

申請通過了之后 我們會得到幾個參數 下來就展示下代碼吧

 1 public function wxPay(){
 2         $id=intval($_GET['id']);
 3         $msg=M("Order")->where("id=".$id)->find();  //獲取支付的訂單信息
 4         $info=M("Product")->where("id=".$msg['pid'])->find();     //獲取后台配置的微信參數
 5         
 6         $userip = $_SERVER["REMOTE_ADDR"]; //獲得用戶設備IP
 7         $appid = $info['appid'];//微信給的appid
 8         $mch_id = $info['mchid'];//微信官方的
 9         $key = $info['mykey'];//自己設置的微信商家key
10         $out_trade_no = $msg['dh']; //訂單號
11         
12 
13         $nonce_str=MD5($out_trade_no);//隨機字符串
14         $total_fee = $msg['price']*100; //金額*100
15         $spbill_create_ip = $userip; //IP
16         $notify_url = "http://".$_SERVER['SERVER_NAME']."/index.php/Home/Back/wx_back"; //回調地址 jishu.whwlhd.com/index.php/Home/Pay/wx/id/
17         $trade_type = 'MWEB';//交易類型 具體看API 里面有詳細介紹
18         $body="H5支付";
19     
20          $scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://jishu.whwlhd.com","wap_name":"支付"}}';//場景信息 必要參數
21          $signA ="appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_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";
22 
23          $strSignTmp = $signA."&key=$key"; //拼接字符串  注意順序微信有個測試網址 順序按照他的來 直接點下面的校正測試 包括下面XML  是否正確
24          $sign = strtoupper(MD5($strSignTmp)); // MD5 后轉換成大寫
25 
26          $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>
27         </xml>";//拼接成XML 格式
28 
29 
30         $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信傳參地址
31 
32         $dataxml = $this->http_post($url,$post_data,$headers); 
33         $objectxml = (array)simplexml_load_string($dataxml,'SimpleXMLElement',LIBXML_NOCDATA); //將微信返回的XML 轉換成數組
34         
35         if($objectxml['return_code'] == 'SUCCESS'){
36             $redirect_url = urlencode("http://jishu.whwlhd.com/index.php?a=index&m=Pay&uid=26");
37             $url = $objectxml['mweb_url'].'&redirect_url='.$redirect_url;
38             echo "<script> window.location.href='$url'</script>";
39             exit;
40         }     
41     }
 1 public function http_post($url='',$post_data=array(),$header=array(),$timeout=30) {
 2             
 3             $ch = curl_init();  
 4             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查  
 5             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 從證書中檢查SSL加密算法是否存在  
 6             curl_setopt($ch, CURLOPT_URL, $url);  
 7             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
 8             curl_setopt($ch, CURLOPT_POST, true);  
 9             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
10             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
11             curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
12             
13             $response = curl_exec($ch);  
14 
15             curl_close($ch);
16 
17             return $response;
18         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM