温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Thinkphp5如何实现微信扫码支付

发布时间:2021-11-25 17:49:15 来源:亿速云 阅读:344 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关Thinkphp5如何实现微信扫码支付的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Thinkphp5内核整合微信扫码支付实例完整源码,ThinkPHP框架集成微信扫码支付,压缩包内置安装说明和订单表

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

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"; 
        }

感谢各位的阅读!关于“Thinkphp5如何实现微信扫码支付”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI