微信支付02

接上次微信支付测试支付,我们现在来做获取支付通知接口、依然是测试:

在上一篇测试支付中我们传了一个

notify_url 参数,这个参数所对应的就是下面这个方法(可以在公网上找的到);
在该接口中、我们只需要接收参数、即完成支付后、微信后端后调这个方法、将下面的参数传进来、我们只需要获取方法的参数来判断是否支付成功。
 1 public void service(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
 2         logger.info("★★★★★★★★★★ AlipayNotifyServlet 接收后台【异步】通知开始 START ★★★★★★★★★★");
 3         logger.info("收到微信异步通知");
 4         
 5         ////  下main这些是返回的参数
 6         
 7         //PrintWriter out = null;
 8         String version = null; //版本号    --
 9         String charset = null; //字符集    --
10         String sign_type = null; //签名方式    --
11         String status = null; //返回状态码    --
12         String message = null; //信息
13         String result_code = null; //业务结果    --
14         String mch_id     = null; //商户号    --
15         String device_info = null; //设备号    
16         String nonce_str = null; //随机字符串    --
17         String err_code = null; //错误代码    
18         String err_msg = null; //错误代码描述    
19         String sign = null; //签名--
20         
21         String openid = null; //用户标识    --
22         String trade_type     = null; //交易类型    --
23         String is_subscribe = null; //是否关注公众账号    --
24         String pay_result     = null; //支付结果    --
25         String pay_info     = null; //支付结果信息    
26         String transaction_id = null; //平台订单号    --
27         String out_transaction_id     = null; //第三方订单号    --
28         String sub_is_subscribe = null; //子商户是否关注    
29         String sub_appid     = null; //子商户appid    
30         String sub_openid = null; //用户openid    
31         String out_trade_no     = null; //商户订单号    --
32         String total_fee = null; //总金额    --
33         String cash_fee     = null; //现金支付金额    
34         String coupon_fee     = null; //现金券金额    
35         String fee_type     = null; //货币种类    
36         String attach = null; //附加信息    
37         String bank_type     = null; //付款银行    --
38         String bank_billno = null; //银行订单号    
39         String time_end     = null; //支付完成时间    --
40         
41         
42          try {
43                 req.setCharacterEncoding("utf-8");
44                 resp.setCharacterEncoding("utf-8");
45                 resp.setHeader("Content-type", "text/html;charset=UTF-8");
46                 String resString = XmlUtils.parseRequst(req);
47                 System.out.println("通知内容:" + resString);
48                 
49                 String respString = "fail";
50                 if(resString != null && !"".equals(resString)){
51                     Map<String,String> map = XmlUtils.toMap(resString.getBytes(), "utf-8");
52                     String res = XmlUtils.toXml(map);
53                     System.out.println("通知内容:" + res);
54                     if(map.containsKey("sign")){
55                         if(!SignUtils.checkParam(map, "61307e5f2aebcacecbcca6fe5296df9c")){
56                             res = "验证签名不通过";
57                             respString = "fail";
58                         }else{
59                              status = map.get("status");
60                             if(status != null && "0".equals(status)){   //当status和 result_code同时为0的时候支付成功、获取一下参数。
61                                  result_code = map.get("result_code");
62                                  if(result_code != null && "0".equals(result_code)){
63                                      System.out.println("version: " +  map.get("version"));
64                                      System.out.println("charset: " +  map.get("charset"));
65                                      System.out.println("sign_type: " +  map.get("sign_type"));
66                                      System.out.println("status: " +  map.get("status"));
67                                      System.out.println("result_code: " +  map.get("result_code"));
68                                      System.out.println("mch_id: " +  map.get("mch_id"));
69                                      System.out.println("openid: " +  map.get("openid"));
70                                      System.out.println("trade_type: " +  map.get("trade_type"));
71                                      System.out.println("is_subscribe: " +  map.get("is_subscribe"));
72                                      System.out.println("transaction_id: " +  map.get("transaction_id"));
73                                      System.out.println("out_transaction_id: " +  map.get("out_transaction_id"));
74                                      System.out.println("out_trade_no: " +  map.get("out_trade_no"));
75                                      System.out.println("bank_type: " +  map.get("bank_type"));
76                                      System.out.println("time_end: " +  map.get("time_end"));
77                                      
78                                      //在此我们可以针对我们的数据库进行操作。
79                                  }
80 //
88                             } 
89                             
90                             //此处可以在添加相关处理业务,校验通知参数中的商户订单号out_trade_no和金额total_fee是否和商户业务系统的单号和金额是否一致,一致后方可更新数据库表中的记录。 
91                             respString = "success";
92                         }
93                     }
94                 }
95                 resp.getWriter().write(respString);
96             } catch (Exception e) {
97                 e.printStackTrace();
98             }
}

我们现在调获取微信的支付通知接口是因为我们在  测试支付的时候配置了一个notify_url 支付通知的方法、这个方法需要放在公网上面。

然后配置自己的系统、然后通过该通知接口、就可以调运到该方法、将接口返回的参数就可以获取到了

posted @ 2018-08-27 19:49  岷州李斌  阅读(210)  评论(0编辑  收藏  举报