Thinkphp5整合微信扫码支付【原创

来源:https://www.sucaihuo.com/php/3261.html 2017-11-30 21:34浏览(9774) 收藏

Thinkphp5整合微信扫码支付,TP5微信扫码完整源码,ThinkPHP框架集成微信扫码支付,压缩包内置安装说明和订单表
Thinkphp5整合微信扫码支付
分类:PHP > Thinkphp 难易:中级
下载资源 下载积分: 998 积分

打开首页生成订单,并显示支付二维码

public function index() {
        $wechat = new Wechat();
        $data['order'] = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); //订单号
        $data['set'] = "测试";
        $data['money'] = 0.01;
        $data['userid'] = 1;
        if (!Db::execute('INSERT INTO order_sucaihuo(uid,order_no,order_money,addtime) VALUES(?,?,?,?)', [$data['userid'], $data['order'], $data['money'], time()])) {
            return '失败,请重试!';
        }
        $url = $wechat->send($data);
        $data['url'] = 'http://paysdk.weixin.qq.com/example/qrcode.php?data=' . $url;
        // return '<img alt="模式二扫码支付" src="http://paysdk.weixin.qq.com/example/qrcode.php?data='.$url.'" style="width:150px;height:150px;"/>';
        return view('index', $data);
    }

回调验证并更改订单状态

if ($WeChatNotify->notify($xml) == true) {
            file_put_contents('./time.txt', date("Y-m-d H:i:s"));
            //$WeChatNotify->getValues()  获取到xml转换为数组的键值对
            //out_trade_no对应的商户订单号
            //total_fee为订单金额的一百的倍数  也就是total_fee/100为支付的金额
            //还有几个键值对需要用的话可以打印出来看  都是微信官方定义的
            $data = $WeChatNotify->getValues();
            file_put_contents('./data.txt', json_encode($data));
            if (empty($data) || empty($data['out_trade_no']) || empty($data['total_fee'])) {
                return;
            }
            $orderData = Db::query("SELECT * FROM order_sucaihuo WHERE order_no='" . $data['out_trade_no'] . "' AND state=0");
            if (empty($orderData)) {
                return;
            }
            $orderData = $orderData[0];
            if ($orderData['order_money'] != $data['total_fee'] / 100) {
                return;
            }
            $orderResult = Db::execute("UPDATE order_sucaihuo SET state=1,update_time=" . time() . "");
            if (!$orderResult) {
                return;
            }
            return "SUCCESS";
        }
声明:本文为原创文章,如需转载,请注明来源sucaihuo.com并保留原文链接:https://www.sucaihuo.com/php/3261.html
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2