服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解

ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解

2021-07-31 15:53gaoyl101 PHP教程

这篇文章主要介绍了ThinkPHP框架整合微信支付之Native 扫码支付模式,结合图文形式详细分析了thinkPHP整合微信扫码支付功能的具体步骤与相关操作技巧,以及与模式一的区别,需要的朋友可以参考下

本文实例讲述了thinkphp框架整合微信支付之native 扫码支付模式二。分享给大家供大家参考,具体如下:

大家好,这篇文章是继微信支付之native 扫码支付 模式一之后的微信支付系列教程第三篇:扫码支付之模式二

介绍下扫码支付目前有两种模式,模式一比模式二稍微复杂点,至于模式一与模式二的具体内容,流程,微信开发文档都有详细介绍,这里就不多说废话,接下来赶紧上教程!

首先我们还是一样,导入微信支付的类库:

ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解

接下来是public下的文件:

ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解

以上跟Native扫码模式一一样,不明白请看 http://www.zzvips.com/article/180166.html

jsAPI支付请看:http://www.zzvips.com/article/180165.html

step1:同样,先初始化引入wxpaypubhelper类库

?
1
2
3
4
5
6
7
8
/**
     * 初始化
     */
    public function _initialize()
    {
        //引入wxpaypubhelper
        vendor('wxpaypubhelper.wxpaypubhelper');
    }

step2:这里就跟扫码支付模式一有区别了:根据订单生产二维码,使用统一支付接口,请看代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public function createqrcode()
    {
        //使用统一支付接口
        $unifiedorder new \unifiedorder_pub();
        
        //设置统一支付接口参数
        //设置必填参数
        //appid已填,商户无需重复填写
        //mch_id已填,商户无需重复填写
        //noncestr已填,商户无需重复填写
        //spbill_create_ip已填,商户无需重复填写
        //sign已填,商户无需重复填写
        $unifiedorder->setparameter("body","贡献一分钱");//商品描述
        //自定义订单号,此处仅作举例
        $timestamp = time();
        $out_trade_no = c('wxpayconf_pub.appid')."$timestamp";
        $unifiedorder->setparameter("out_trade_no","$out_trade_no");//商户订单号 
        $unifiedorder->setparameter("total_fee","1");//总金额
        $unifiedorder->setparameter("notify_url", c('wxpayconf_pub.notify_url'));//通知地址 
        $unifiedorder->setparameter("trade_type","native");//交易类型
        //非必填参数,商户可根据实际情况选填
        //$unifiedorder->setparameter("sub_mch_id","xxxx");//子商户号  
        //$unifiedorder->setparameter("device_info","xxxx");//设备号 
        //$unifiedorder->setparameter("attach","xxxx");//附加数据 
        //$unifiedorder->setparameter("time_start","xxxx");//交易起始时间
        //$unifiedorder->setparameter("time_expire","xxxx");//交易结束时间 
        //$unifiedorder->setparameter("goods_tag","xxxx");//商品标记 
        //$unifiedorder->setparameter("openid","xxxx");//用户标识
        //$unifiedorder->setparameter("product_id","xxxx");//商品id
        
        //获取统一支付接口结果
        $unifiedorderresult $unifiedorder->getresult();
        
        //商户根据实际情况设置相应的处理流程
        if ($unifiedorderresult["return_code"] == "fail"
        {
            //商户自行增加处理流程
            echo "通信出错:".$unifiedorderresult['return_msg']."<br>";
        }
        elseif($unifiedorderresult["result_code"] == "fail")
        {
            //商户自行增加处理流程
            echo "错误代码:".$unifiedorderresult['err_code']."<br>";
            echo "错误代码描述:".$unifiedorderresult['err_code_des']."<br>";
        }
        elseif($unifiedorderresult["code_url"] != null)
        {
            //从统一支付接口获取到code_url
            $code_url $unifiedorderresult["code_url"];
            //商户自行增加处理流程
            //......
        }
        $this->assign('out_trade_no',$out_trade_no);
        $this->assign('code_url',$code_url);
        $this->assign('unifiedorderresult',$unifiedorderresult);
        
        $this->display('qrcode');
    }

对应qrcode.html页面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>微信安全支付</title>
</head>
<body>
    <div align="center" id="qrcode">
    </div>
    <div align="center">
        <p>订单号:<?php echo $out_trade_no; ?></p>
    </div>
    <div align="center">
        <form  action="./order_query.php" method="post">
            <input name="out_trade_no" type='hidden' value="<?php echo $out_trade_no; ?>">
            <button type="submit" >查询订单状态</button>
        </form>
    </div>
    <br>
    <div align="center">
        <form  action="./refund.php" method="post">
            <input name="out_trade_no" type='hidden' value="<?php echo $out_trade_no; ?>">
            <input name="refund_fee" type='hidden' value="1">
            <button type="submit" >申请退款</button>
        </form>
    </div>
    <br>
    <div align="center">
        <a href="../index.php" rel="external nofollow" >返回首页</a>
    </div>
</body>
    <script src="__public__/js/qrcode.js"></script>
    <script>
        if(<?php echo $unifiedorderresult["code_url"] != null; ?>)
        {
            var url = "<?php echo $code_url;?>";
            //参数1表示图像大小,取值范围1-10;参数2表示质量,取值范围'l','m','q','h'
            var qr = qrcode(10, 'm');
            qr.adddata(url);
            qr.make();
            var wording=document.createelement('p');
            wording.innerhtml = "扫我,扫我";
            var code=document.createelement('div');
            code.innerhtml = qr.createimgtag();
            var element=document.getelementbyid("qrcode");
            element.appendchild(wording);
            element.appendchild(code);
        }
    </script>
</html>

模式二不需要配置公众平台了,所以简单就简单在这里

step3:异步通知,这里都一样:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public function notify()
    {
        //使用通用通知接口
        $notify new \notify_pub();
         
        //存储微信的回调
        $xml $globals['http_raw_post_data'];
        $notify->savedata($xml);
         
        //验证签名,并回应微信。
        //对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
        //微信会通过一定的策略(如30分钟共8次)定期重新发起通知,
        //尽可能提高通知的成功率,但微信不保证通知最终能成功。
        if($notify->checksign() == false){
            $notify->setreturnparameter("return_code","fail");//返回状态码
            $notify->setreturnparameter("return_msg","签名失败");//返回信息
        }else{
            $notify->setreturnparameter("return_code","success");//设置返回码
        }
        $returnxml $notify->returnxml();
        echo $returnxml;
         
        //==商户根据实际情况设置相应的处理流程,此处仅作举例=======
         
        //以log文件形式记录回调信息
        //         $log_ = new log_();
        $log_name= __root__."/public/notify_url.log";//log文件路径
         
        $this->log_result($log_name,"【接收到的notify通知】:\n".$xml."\n");
         
        if($notify->checksign() == true)
        {
            if ($notify->data["return_code"] == "fail") {
                //此处应该更新一下订单状态,商户自行增删操作
                log_result($log_name,"【通信出错】:\n".$xml."\n");
            }
            elseif($notify->data["result_code"] == "fail"){
                //此处应该更新一下订单状态,商户自行增删操作
                log_result($log_name,"【业务出错】:\n".$xml."\n");
            }
            else{
                //此处应该更新一下订单状态,商户自行增删操作
                log_result($log_name,"【支付成功】:\n".$xml."\n");
            }
             
            //商户自行增加处理流程,
            //例如:更新订单状态
            //例如:数据库操作
            //例如:推送支付完成信息
        }
    }

这样模式二的扫码支付就ok了 是不是相对来说简单很多?

下面是测试截图:

扫码截图:

ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解

 扫码结果:

ThinkPHP框架整合微信支付之Native 扫码支付模式二图文详解

有问题请留言,下面还会介绍微信支付刷卡支付的详细教程

微信支付教程jsapi篇:
http://www.zzvips.com/article/180165.html

微信支付教程扫码模式二:
http://www.zzvips.com/article/180166.html

微信支付教程刷卡支付:
www.zzvips.com/article/180171.html

希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。

原文链接:http://www.thinkphp.cn/code/1323.html

延伸 · 阅读

精彩推荐