【二十九】php之简易微信二维码支付

参考二维码支付接口文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5

index.php

 1 <!DOCTYPE html>
 2 <html>
 3 <?php
 4 // 生成随机数,长度为30位
 5   function getNonceStr($length = 30) 
 6     {
 7         $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
 8         $str ="";
 9         for ( $i = 0; $i < $length; $i++ )  {  
10             $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
11         } 
12         return $str;
13     }
14 ?>
15 <head>
16     <title></title>
17 </head>
18 <body>
19 <form action="pay.php" method="post">
20     订单号:<input type="" name="out_trade_no" value="<?php echo getNonceStr(); ?>">
21     支付金额为:<input type="text" name="total_fee"><br/>
22     <input type="submit" value="付款">
23 </form>
24 </body>
25 </html>
View Code

pay.php(生成数据向接口请求)

  1 <?php
  2 //必填参数
  3     $appid="wx8fd788902902f0**";
  4     $mch_id="14894801**";
  5     $nonce_str=getNonceStr();
  6     $body="cmftest";
  7     $out_trade_no=$_POST['out_trade_no'];
  8     $total_fee=$_POST['total_fee'];
  9     // 注意:spbill_create_ip的值必须是ip。不然生成的签名是错误的
 10     // $spbill_create_ip=$_SERVER['REMOTE_ADDR'];
 11     $spbill_create_ip="127.0.0.1";
 12     $notify_url="http://127.0.0.1/wx_pay/res.php";
 13     $trade_type="NATIVE";
 14     $KEY = 'MIIEvAIBADANBgkqhkiG9w0BAQEF****';
 15     $device_info="WEB";
 16 // 将这些值设置为数组形式
 17     $value=array(
 18         'appid' => $appid, 
 19         'mch_id' =>$mch_id,
 20         'nonce_str'=>$nonce_str,
 21         'out_trade_no'=>$out_trade_no,
 22         'total_fee'=>$total_fee,
 23         'spbill_create_ip'=>$spbill_create_ip,
 24         'notify_url'=>$notify_url,
 25         'device_info'=>$device_info,
 26         'body'=>$body,        
 27         'trade_type'=>$trade_type
 28         );
 29 //生成签名
 30     $sign=MakeSign($value,$KEY);
 31 // 往数组中增加签名字段
 32     $value["sign"]=$sign;
 33 // 将数组内的数据转为xml格式
 34     $res= ToXml($value);
 35     echo $res;
 36 //接口请求地址
 37     $url="https://api.mch.weixin.qq.com/pay/unifiedorder";
 38 //向接口请求
 39     $result = wx_post($url,$res);
 40     var_dump($result);
 41 $erweima= $result->code_url;
 42 echo $erweima;
 43 // 从结果xml中取出二维码的另外一种方式
 44 // // 截取这个字符串第一次出现的位置(下标)
 45 // $indexnum= strpos($result,"<code_url><![CDATA[");
 46 // // 计算这个字符串的长度
 47 // $strsumnum= strlen("<code_url><![CDATA[");
 48 // $str=substr($result,$indexnum+$strsumnum);
 49 // $a=strpos($str,"]");
 50 // $a=substr($str,0,$a);
 51 // echo $a;
 52 // 使用curl库的post方法向接口发送请求
 53 function wx_post($url,$data){
 54         $ch = curl_init();
 55         curl_setopt($ch, CURLOPT_URL, $url);
 56         curl_setopt($ch, CURLOPT_HEADER, FALSE);
 57         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 58         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 59         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 60         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
 61         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 62         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
 63         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 64         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 65         $tmpInfo = curl_exec($ch);
 66         if (curl_errno($ch)) {
 67             return curl_error($ch);
 68         }
 69         // 转换形式良好的 XML 字符串为 SimpleXMLElement 对象,然后输出对象的键和元素
 70         $tmpInfo=simplexml_load_string($tmpInfo);
 71          return $tmpInfo;
 72     }
 73 //随机字符串    
 74   function getNonceStr($length = 30) 
 75     {
 76         $chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
 77         $str ="";
 78         for ( $i = 0; $i < $length; $i++ )  {  
 79             $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
 80         } 
 81         return $str;
 82     }
 83 //生成签名
 84  function MakeSign($sign,$KEY)
 85     {
 86         //签名步骤一:按字典序排序参数
 87         ksort($sign);
 88         // var_dump($sign);
 89         $string =ToUrlParams($sign);
 90         // echo "<br/>$string<br/>";
 91         //签名步骤二:在string后加入KEY
 92         $string = $string . "&key=".$KEY;
 93         // echo "$string";
 94         //签名步骤三:MD5加密
 95         $string = md5($string);
 96         //签名步骤四:所有字符转为大写
 97         $result = strtoupper($string);
 98         return $result;
 99     }
100 // 拼接key和value值
101 function ToUrlParams($sign)
102     {
103         $buff = "";
104         foreach ($sign as $k => $v)
105         {
106             if($k != "sign" && $v != "" && !is_array($v)){
107                 $buff .= $k . "=" . $v . "&";
108             }
109         }
110         
111         $buff = trim($buff, "&");
112         return $buff;
113     }
114 //将数组内的数据全部转为xml
115 function ToXml($sign)
116     {
117         $xml="<xml>";
118         // $xml = "&lt;xml&gt;";
119         foreach ($sign as $key=>$val)
120         {
121             if (is_numeric($val)){
122                 // $xml.="&lt;".$key."&gt;".$val."&lt;/".$key."&gt;";
123                 $xml.="<".$key.">".$val."</".$key.">";
124 
125 
126             }else{
127                 // $xml.="&lt;".$key."&gt;&lt;![CDATA[".$val."]]&gt;&lt;/".$key."&gt;";
128                 $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
129             }
130         }
131         $xml.="</xml>";
132         // $xml.="&lt;/xml&gt;";
133         return $xml; 
134     }
135 ?>
136 <!DOCTYPE html>
137 <html>
138 <head>
139     <title></title>
140 </head>
141 <body>
142 您的订单号为<?php echo $out_trade_no; ?>,您支付的金额为<?php echo $total_fee; ?>
143     <form action="pay_to.php" method="post">
144     <input type="hidden" name="two_code" value="<?php echo $erweima;?>">
145     <input type="submit" value="付款支付" >
146     </form>
147 
148 </body>
149 </html>
View Code

pay_to.php(生成二维码)

1 <?php
2     $value=$_POST["two_code"];
3     include 'D:\wamp64\www\wx_pay\phpqrcode\phpqrcode.php';   
4     QRcode::png("$value");
5 ?>
View Code

 界面显示:

posted @ 2018-01-16 08:45  花花妹子。  阅读(1099)  评论(0编辑  收藏  举报