博客列表 >laravel框架之微信扫码支付对接

laravel框架之微信扫码支付对接

我是郭富城
我是郭富城原创
2020年07月05日 03:35:281155浏览

1. 下载php的sdk

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1

2. 把下载的sdk源码放到vendor类库

讲文件解压并根据个人喜好放到vendor目录

3. 在商城控制器中添加支付方法

  1. // 支付
  2. public function pay(Request $request) {
  3. // 生成二维码
  4. // echo __DIR__;
  5. // D:\laravel\app\Http\Controllers
  6. // exit;
  7. $wx_pay_path = __DIR__ . '/../../../vendor/wx_pay/';
  8. require_once $wx_pay_path . "lib/WxPay.Api.php";
  9. require_once $wx_pay_path . "example/WxPay.NativePay.php";
  10. // require_once 'log.php';
  11. $notify = new \NativePay();
  12. $input = new \WxPayUnifiedOrder();
  13. $input->SetBody("test");
  14. $input->SetAttach("test");
  15. $input->SetOut_trade_no("sdkphp123456789".date("YmdHis"));
  16. $input->SetTotal_fee("1");
  17. $input->SetTime_start(date("YmdHis"));
  18. $input->SetTime_expire(date("YmdHis", time() + 600));
  19. $input->SetGoods_tag("test");
  20. $input->SetNotify_url("http://paysdk.weixin.qq.com/notify.php");
  21. $input->SetTrade_type("NATIVE");
  22. $input->SetProduct_id("123456789");
  23. $result = $notify->GetPayUrl($input);
  24. $data['url2'] = $result["code_url"];
  25. // echo '<pre>';
  26. // var_dump($result);
  27. return view('shop/pay',$data);
  28. }

4. 在商城控制器中添加二维码生成的方法

  1. public function creatqrcode() {
  2. $wx_pay_path = __DIR__ . '/../../../vendor/wx_pay/';
  3. require_once $wx_pay_path . 'example/phpqrcode/phpqrcode.php';
  4. $url = urldecode($_GET["data"]);
  5. if(substr($url, 0, 6) == "weixin"){
  6. \QRcode::png($url);
  7. }else{
  8. header('HTTP/1.1 404 Not Found');
  9. }
  10. }

5. 填写相关的微信商户信息

目录example下的WxPay.Config.php
修改基本信息

  1. public function GetAppId()
  2. {
  3. // AppID账号
  4. return '微信公众号的appid';
  5. }
  6. public function GetMerchantId()
  7. {
  8. // 微信商户号
  9. return '你的微信商户号';
  10. }
  11. public function GetKey()
  12. {
  13. // API密钥
  14. return '32位的api密钥';
  15. }
  16. public function GetAppSecret()
  17. {
  18. // 公众帐号secert
  19. return '请登录微信公众号,重置一个appsecret';
  20. }

6. 前端付款二维码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>微信支付二维码</title>
  6. <link rel="stylesheet" href="/static/plugins/layui/css/layui.css">
  7. <link rel="stylesheet" href="/static/css/index.css">
  8. <script src="/static/plugins/layui/layui.js"></script>
  9. </head>
  10. <body>
  11. <div class="wx_pay">
  12. <img alt="扫码支付" src="/shop/creatqrcode?data=<?php echo urlencode($url2);?>" />
  13. </div>
  14. </body>
  15. </html>

7. 效果图

8. 总结

到目前为止,已经可以正常生成预设信息的付款二维码了,取得了阶段性的胜利,下一个博客讲进一步讲解如何获取数据库中的订单信息并产生支付二维码等进阶内容。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议