Home > php教程 > php手册 > body text

ThinkPHP整合微信支付之Native 扫码支付 模式一

WBOY
Release: 2016-06-07 11:38:08
Original
1180 people have browsed it

大家好,这篇文章是继微信支付jsapi篇之后的微信支付系列教程第二篇:扫码支付之模式一
介绍下扫码支付目前有两种模式,模式一比模式二稍微复杂点,至于模式一与模式二的具体内容,流程,微信开发文档都有详细介绍,这里就不多说废话,接下来赶紧上教程!
首先我们还是一样,导入微信支付的类库:
ThinkPHP整合微信支付之Native 扫码支付 模式一

接下来是Public下的文件:
ThinkPHP整合微信支付之Native 扫码支付 模式一

这里的配置跟JSAPI支付一样,不需要改动
具体关于文件介绍请参考JSAPI支付,这里就不再说明了 链接地址:http://www.thinkphp.cn/code/1321.html

接下来直接看控制器部分的代码:
step1:同样,先初始化引入WxPayPubHelper类库/**<br>      * 初始化<br>      */<br>     public function _initialize()<br>     {<br>         //引入WxPayPubHelper<br>         vendor('WxPayPubHelper.WxPayPubHelper');<br>     }step2:展示扫码页面需要先生存二维码链接public function native_pay()<br>     {<br>         //设置静态链接<br>         $nativeLink = new \NativeLink_pub();<br>         <br>         //设置静态链接参数<br>         //设置必填参数<br>         //appid已填,商户无需重复填写<br>         //mch_id已填,商户无需重复填写<br>         //noncestr已填,商户无需重复填写<br>         //time_stamp已填,商户无需重复填写<br>         //sign已填,商户无需重复填写<br>         $product_id = C('WxPayConf_pub.APPID')."static";//自定义商品id<br>         $nativeLink->setParameter("product_id",$product_id);//商品id<br>         //获取链接<br>         $product_url = $nativeLink->getUrl();<br>         //使用短链接转换接口<br>         $shortUrl = new \ShortUrl_pub();<br>         //设置必填参数<br>         //appid已填,商户无需重复填写<br>         //mch_id已填,商户无需重复填写<br>         //noncestr已填,商户无需重复填写<br>         //sign已填,商户无需重复填写<br>         $shortUrl->setParameter("long_url",$product_url);//URL链接<br>         //获取短链接<br>         $codeUrl = $shortUrl->getShortUrl();<br>         $this->assign('product_url',$product_url);<br>         $this->assign('codeUrl',$codeUrl);<br>         $this->display();<br>         <br>     }以上代码对应native_pay.html页面nbsp;HTML><br> <br> <br>     <meta> <br>     <title>微信安全支付</title> <br> <br> <br>     <div> <br>         <p>扫我,扫我</p> <br>     </div> <br>     <div> <br>         <a>返回首页</a><br>     </div> <br> <br>     <script></script><br>     <script><br /> var url = "<?php echo $product_url;?>";<br /> //参数1表示图像大小,取值范围1-10;参数2表示质量,取值范围&#039;L&#039;,&#039;M&#039;,&#039;Q&#039;,&#039;H&#039;<br /> var qr = qrcode(10, &#039;M&#039;);<br /> qr.addData(url);<br /> qr.make();<br /> var dom=document.createElement(&#039;DIV&#039;);<br /> dom.innerHTML = qr.createImgTag();<br /> var element=document.getElementById("qrcode");<br /> element.appendChild(dom);<br /> </script><br> 这里注意生存二维码的js地址,我放在了Public下的JS目录下

step3:扫码之后,就会提交给我们公众平台native配置的地址对应的去方法处理
公众平台navtive配置:配置地址 http://您的域名/Pay/index.php/Home/WxNative/todoPost
ThinkPHP整合微信支付之Native 扫码支付 模式一
对应的todoPost方法在控制器中:public function todoPost()<br>     {<br>         //以log文件形式记录回调信息,用于调试<br>         $log_name = __ROOT__."/Public/native_call.log";<br>         //使用native通知接口<br>         $nativeCall = new \NativeCall_pub();<br>         <br>         //接收微信请求<br>         $xml = $GLOBALS['HTTP_RAW_POST_DATA'];<br>         log_result($log_name,"【接收到的native通知】:\n".$xml."\n");<br>         $nativeCall->saveData($xml);<br>         <br>         if($nativeCall->checkSign() == FALSE){<br>             $nativeCall->setReturnParameter("return_code","FAIL");//返回状态码<br>             $nativeCall->setReturnParameter("return_msg","签名失败");//返回信息<br>         }<br>         else<br>         {<br>             //提取product_id<br>             $product_id = $nativeCall->getProductId();<br>         <br>             //使用统一支付接口<br>             $unifiedOrder = new \UnifiedOrder_pub();<br>         <br>             //根据不同的$product_id设定对应的下单参数,此处只举例一种<br>             switch ($product_id)<br>             {<br>                 <br>                 case C('WxPayConf_pub.APPID')."static"://与native_call_qrcode.php中的静态链接二维码对应<br>                     //设置统一支付接口参数<br>                     //设置必填参数<br>                     //appid已填,商户无需重复填写<br>                     //mch_id已填,商户无需重复填写<br>                     //noncestr已填,商户无需重复填写<br>                     //spbill_create_ip已填,商户无需重复填写<br>                     //sign已填,商户无需重复填写<br>                     $unifiedOrder->setParameter("body","贡献一分钱");//商品描述<br>                     //自定义订单号,此处仅作举例<br>                     $timeStamp = time();<br>                     $out_trade_no = C('WxPayConf_pub.APPID').$timeStamp;<br>                     $unifiedOrder->setParameter("out_trade_no",$out_trade_no);//商户订单号             $unifiedOrder->setParameter("product_id","$product_id");//商品ID<br>                     $unifiedOrder->setParameter("total_fee","1");//总金额<br>                     $unifiedOrder->setParameter("notify_url",C('WxPayConf_pub.NOTIFY_URL'));//通知地址<br>                     $unifiedOrder->setParameter("trade_type","NATIVE");//交易类型<br>                     $unifiedOrder->setParameter("product_id",$product_id);//用户标识<br>                     //非必填参数,商户可根据实际情况选填<br>                     //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号<br>                     //$unifiedOrder->setParameter("device_info","XXXX");//设备号<br>                     //$unifiedOrder->setParameter("attach","XXXX");//附加数据<br>                     //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间<br>                     //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间<br>                     //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记<br>                     //$unifiedOrder->setParameter("openid","XXXX");//用户标识<br>         <br>                     //获取prepay_id<br>                     $prepay_id = $unifiedOrder->getPrepayId();<br>                     //设置返回码<br>                     //设置必填参数<br>                     //appid已填,商户无需重复填写<br>                     //mch_id已填,商户无需重复填写<br>                     //noncestr已填,商户无需重复填写<br>                     //sign已填,商户无需重复填写<br>                     $nativeCall->setReturnParameter("return_code","SUCCESS");//返回状态码<br>                     $nativeCall->setReturnParameter("result_code","SUCCESS");//业务结果<br>                     $nativeCall->setReturnParameter("prepay_id",$prepay_id);//预支付ID<br>         <br>                     break;<br>                 default:<br>                     //设置返回码<br>                     //设置必填参数<br>                     //appid已填,商户无需重复填写<br>                     //mch_id已填,商户无需重复填写<br>                     //noncestr已填,商户无需重复填写<br>                     //sign已填,商户无需重复填写<br>                     $nativeCall->setReturnParameter("return_code","SUCCESS");//返回状态码<br>                     $nativeCall->setReturnParameter("result_code","FAIL");//业务结果<br>                     $nativeCall->setReturnParameter("err_code_des","此商品无效");//业务结果<br>                     break;<br>             }<br>         <br>         }<br>         <br>         //将结果返回微信<br>         $returnXml = $nativeCall->returnXml();<br>         log_result($log_name,"【返回微信的native响应】:\n".$returnXml."\n");<br> <br>         echo $returnXml;<br>     }其实到这里你已经完成了扫码支付模式一的功能

step4:接下来写一下异步通知处理,与jsapi支付一样:public function notify()<br>     {<br>         //使用通用通知接口<br>         $notify = new \Notify_pub();<br>          <br>         //存储微信的回调<br>         $xml = $GLOBALS['HTTP_RAW_POST_DATA'];<br>         $notify->saveData($xml);<br>          <br>         //验证签名,并回应微信。<br>         //对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,<br>         //微信会通过一定的策略(如30分钟共8次)定期重新发起通知,<br>         //尽可能提高通知的成功率,但微信不保证通知最终能成功。<br>         if($notify->checkSign() == FALSE){<br>             $notify->setReturnParameter("return_code","FAIL");//返回状态码<br>             $notify->setReturnParameter("return_msg","签名失败");//返回信息<br>         }else{<br>             $notify->setReturnParameter("return_code","SUCCESS");//设置返回码<br>         }<br>         $returnXml = $notify->returnXml();<br>         echo $returnXml;<br>          <br>         //==商户根据实际情况设置相应的处理流程,此处仅作举例=======<br>          <br>         //以log文件形式记录回调信息<br>         //         $log_ = new Log_();<br>         $log_name= __ROOT__."/Public/notify_url.log";//log文件路径<br>          <br>         $this->log_result($log_name,"【接收到的notify通知】:\n".$xml."\n");<br>          <br>         if($notify->checkSign() == TRUE)<br>         {<br>             if ($notify->data["return_code"] == "FAIL") {<br>                 //此处应该更新一下订单状态,商户自行增删操作<br>                 log_result($log_name,"【通信出错】:\n".$xml."\n");<br>             }<br>             elseif($notify->data["result_code"] == "FAIL"){<br>                 //此处应该更新一下订单状态,商户自行增删操作<br>                 log_result($log_name,"【业务出错】:\n".$xml."\n");<br>             }<br>             else{<br>                 //此处应该更新一下订单状态,商户自行增删操作<br>                 log_result($log_name,"【支付成功】:\n".$xml."\n");<br>             }<br>              <br>             //商户自行增加处理流程,<br>             //例如:更新订单状态<br>             //例如:数据库操作<br>             //例如:推送支付完成信息<br>         }<br>     }native扫码支付模式一demo到此就可以啦

下面展示下测试的截图:
扫码界面:
ThinkPHP整合微信支付之Native 扫码支付 模式一

扫码结果:
ThinkPHP整合微信支付之Native 扫码支付 模式一


有问题请留言,下面还会介绍微信扫码支付模式二的详细教程
微信支付教程jsapi篇:
http://www.thinkphp.cn/code/1321.html
微信支付教程扫码模式二:
http://www.thinkphp.cn/code/1323.html
微信支付教程刷卡支付:
http://www.thinkphp.cn/code/1324.html

欢迎大家吐槽,转载请说明出处,请支持原创,谢谢!
我们的微信开发群:422579975(已满) 105195188(未满),代码已放在群文件中
欢迎大家加入讨论问题


AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template