微信扫码支付Native方式二以及支付回调


官方API文档https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5

 

1.使用jar包

 1      <!--微信支付  -->
 2      <dependency>
 3         <groupId>com.github.wxpay</groupId>
 4         <artifactId>wxpay-sdk</artifactId>
 5         <version>3.0.9</version>
 6      </dependency>
 7 
 8     <!--httpclient-->
 9     <dependency>
10         <groupId>org.apache.httpcomponents</groupId>
11         <artifactId>httpclient</artifactId>              
12     </dependency>

2.统一下单接口

serviceImpl类

 1 public Map createNative(String out_trade_no, String total_fee) {
 2         // TODO 自动生成的方法存根
 3         Map<String,String> param=new HashMap<>();
 4         param.put("appid", appid);    //小程序id
 5         param.put("mch_id", partner);//商户号
 6         param.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串        
 7         param.put("body", "测试数据");//商品描述
 8         param.put("out_trade_no", out_trade_no);//商户订单号
 9         param.put("total_fee",total_fee);//总金额(分)
10         param.put("spbill_create_ip", "127.0.0.1");//IP
11         param.put("notify_url", notifyurl);//回调地址
12         param.put("trade_type", "NATIVE");//交易类型
13         param.put("product_id",out_trade_no);//订单
14         try {
15             //2.生成要发送的xml 
16             String xmlParam = WXPayUtil.generateSignedXml(param, partnerkey);
17             HttpClient client=new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
18             client.setHttps(true);
19             client.setXmlParam(xmlParam);
20             client.post();        
21             //3.获得结果 
22             String result = client.getContent();
23             System.out.println(result);
24             Map<String, String> resultMap = WXPayUtil.xmlToMap(result);            
25             Map<String, String> map=new HashMap<>();
26             map.put("code_url", resultMap.get("code_url"));//二维码支付地址
27             map.put("total_fee", total_fee);//总金额
28             map.put("out_trade_no",out_trade_no);//订单号
29             return map;
30         } catch (Exception e) {
31             e.printStackTrace();
32             return new HashMap<>();
33         }            
34     }

controller

 1  @RequestMapping("/createNative")
 2         public AjaxJson createNativePay(){
 3             //雪花算法类 随机生成号码
 4             IdWorker idworker=new IdWorker(); 
 5             long oid=idworker.nextId();
 6             //订单实体类
 7             SfOrders sfOrders=new SfOrders();
 8             //set订单id
 9             sfOrders.setOid(oid);
10             //支付状态
11             sfOrders.setPay_id(2);
12             //添加到数据库
13             int x=ordersMapper.add(sfOrders);        
14             //判断是否添加成功
15             if(x>=1) {
16                 //获取插入的id
17                 Long keyid=publicMapper.getPrimarykey();
18                 //转换keyid类型
19                 int k=keyid.intValue();    
20                 //根据id查询这一条数据
21                 SfOrders orders=ordersMapper.getById(k);
22                 //根据订单id,支付金额生成二维码
23                 Map map=weixinPayService.createNative(orders.getOid()+"","1");
24                 return AjaxJson.getSuccessData(map);     
25             }
26             return AjaxJson.getSuccess();
27         }

页面写法,有想要测试是否能支付的朋友可以试试

点击下载生成二维码的包qrcode.js

 

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <!-- 所有的 css & js 资源 -->
 7         <link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
 8         <link rel="stylesheet" href="../../static/sa.css">
 9         <script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
10         <script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
11         <script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>
12         <script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
13         <script src="../../static/sa.js"></script>
14         <script src="../../static/qrcode/qrcode.js"></script>
15         <style>
16         </style>
17     </head>
18     <body>
19         <div class="vue-box">
20             <div class="c-item" style="min-width: 0px;">
21                 <el-button @click="queryCode()">点击事件二维码</el-button>
22             </div>
23         </div>
24 
25         <div id="code"></div>
26 
27         <script>
28             var app = new Vue({
29                 el: '.vue-box',
30                 data: {
31                     code_url: '',
32                     out_trade_no: '' 
33                 },
34                 
35                 methods: {
36                     queryCode: function() {
37                         //  统一下单 
38                         // ajax返回 下单的数据  
39                         sa.ajax('/pay/createNative', function(res) {
40                             console.log(res);
41                             new QRCode(document.getElementById('code'), res.data.code_url); // 创建二维码
42                             this.out_trade_no = res.data.out_trade_no; //  赋值  res里获取的赋值给data里面的 
43                             console.log(res.data.out_trade_no + "----------订单id")
44                         }.bind(this), {});
45                     },    
46                 },
47                 created: function() {
48 
49                 }
50             })
51         </script>
52     </body>
53 </html>

写到这里了是可以去页面测试扫码支付的。

3.支付回调

 1  @RequestMapping("/wxnotify")
 2  public void wxnotify(HttpServletRequest request, HttpServletResponse response) {
 3        String resXml = "";
 4        InputStream inStream;
 5        try {
 6            inStream = request.getInputStream();
 7            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
 8            byte[] buffer = new byte[1024];
 9            int len = 0;
10            while ((len = inStream.read(buffer)) != -1) {
11                outSteam.write(buffer, 0, len);
12            }
13 
14            WXPayUtil.getLogger().info("wxnotify:微信支付----start----");
15 
16            // 获取微信调用我们notify_url的返回信息
17            String result = new String(outSteam.toByteArray(), "utf-8");
18            WXPayUtil.getLogger().info("wxnotify:微信支付----result----=" + result);
19 
20            // 关闭流
21            outSteam.close();
22            inStream.close();
23 
24            // xml转换为map
25            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
26            //判断状态 验证签名是否正确
27            boolean isSuccess = false;
28            
29            if (WXPayConstants.SUCCESS.equalsIgnoreCase(resultMap.get(WXPayConstants.RESULT_CODE))) {
30 
31                WXPayUtil.getLogger().info("wxnotify:微信支付----返回成功");
32 
33                if (WXPayUtil.isSignatureValid(resultMap, WXPayConstants.API_KEY)) {
34                   
35                    WXPayUtil.getLogger().info("wxnotify:微信支付----验证签名成功");
36 
37                    resXml = resSuccessXml;
38                    isSuccess = true;
39 
40                } else {
41                    WXPayUtil.getLogger().error("wxnotify:微信支付----判断签名错误");
42                }
43 
44            } else {
45                WXPayUtil.getLogger().error("wxnotify:支付失败,错误信息:" + resultMap.get(WXPayConstants.ERR_CODE_DES));
46                resXml = resFailXml;
47            }
48            // 回调方法,处理业务 - 修改订单状态
49            WXPayUtil.getLogger().info("wxnotify:微信支付回调:修改的订单===>" + resultMap.get("out_trade_no"));
50            int x=ordersService.updateByOid(resultMap.get("out_trade_no"));
51            if (x>=1) {
52                WXPayUtil.getLogger().info("wxnotify:微信支付回调:修改订单支付状态成功");
53            } else {
54                WXPayUtil.getLogger().error("wxnotify:微信支付回调:修改订单支付状态失败");
55            }
56            
57        } catch (Exception e) {
58            WXPayUtil.getLogger().error("wxnotify:支付回调发布异常:", e);
59        } finally {
60            try {
61                // 处理业务完毕
62                BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
63                out.write(resXml.getBytes());
64                out.flush();
65                out.close();
66            } catch (IOException e) {
67                WXPayUtil.getLogger().error("wxnotify:支付回调发布异常:out:", e);
68            }
69        }
70 
71    }

上面定义的WXPayConstants这个类是自己封装的类,有朋友写到这个地方时,可以直接把这个变量的值等于它的小写字母。例如:

public static final String RESULT_CODE = "result_code";

写到这里就已经完成扫码支付了,如果有朋友是用这个方法做支付的话可以参考。有不懂的地方可以在下面留言。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM