zoukankan      html  css  js  c++  java
  • java之微信支付通知

    微信支付,是现在大多数平台都需要接入的一个支付方式,没办法,谁让现在的用户都习惯了这种消费方式呢

    我今天只说说微信支付通知,我们后台怎么接收通知,并把我们的订单的状态改为已支付,

    至于为什么不说支付的那部分,微信的文档还算是比较清晰的,但是,只有支付通知那里,文档就几个字,对于我这种小白来说,看着就像天书,哈哈哈哈哈哈

    统一下单这一块,我给大家推荐一个 jar 里面对于微信支付的功能基本都封装了,这个 jar 是在一个叫做 nutz 的框架里面,发现的,有兴趣的话,可以研究一下这个框架,挺不错的一个框架

    引入下面的 jar 之后使用也是非常简单的,只需要

    WxApi2Impl wxApi = new WxApi2Impl();

    然后进去看看里面的方法,微信的,基本都有,参数里面的对象,这个 jar 都封装了,要是这样还不会用,我就没办法了

    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutzwx</artifactId>
        <version>1.r.63.r3</version>
    </dependency>

    好了,扯远了,回来看支付通知:------------------------------------------------------------------------------------------------------------------------------------

    方法也很简单

     看这里,下面代码红色字体那里,貌似是一定要这种方式把这段写回去,其他方法好像不行,不知道为毛,我还没有那个水平????????

    下面那个验证签名那里,看你怎么去生成签名的,就怎么去验证了,上面的 jar 有生成签名的方法,可以用那个方法再生成一次,再对比就行

    <xml>

      <return_code><![CDATA[SUCCESS]]></return_code>

      <return_msg><![CDATA[OK]]></return_msg>
    </xml>

    /**
         * 微信支付回调
         * @author XJB、
         * @date 2018年3月27日 上午9:58:04
         * @param req
         * @return
         * @throws Exception 
         */
        @At("/payCallBack")
        @Ok("raw")
        public void payCallBack (HttpServletRequest req, HttpServletResponse response) throws Exception {
    
            log.info("
    
    
    
    
    >>>>>>>>>>>>>>>>>>>>>>>>微信回调开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    
    
    
    ");
            
            String xmlStr = WxUtil.getXmlString(req);
            Map<String, Object> map = WxUtil.xmlToMap(xmlStr);
            
            // 验证返回是否成功
            if (map.get("return_code").equals("SUCCESS")) {
                log.info("通信成功>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                // 验证签名是否正确
                if (WxPaySign.createSign(PropertiesUtil.readProperty("wxpay_key"), map).equals(map.get("sign"))) {
                    // 修改订单支付状态
                    System.out.println(map.get("out_trade_no").toString());
                    wbCarOrderService.updateOrderPayStatus(map.get("out_trade_no").toString());
                }
            }
            String str = WxUtil.returnXML(map.get("return_code").toString());
            response.getWriter().write(str);
            response.getWriter().flush();
            
            log.info("
    
    
    
    
    >>>>>>>>>>>>>>>>>>>>>>>>微信回调结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    
    
    
    ");
        }

     这是WxUtil 的方法,都给大伙送上了,基本的流程就这样了

    public class WxUtil {
        /**
         * XML格式字符串转换为Map
         *
         * @param strXML
         *            XML字符串
         * @return XML数据转换后的Map
         * @throws Exception
         */
        public static Map<String, Object> xmlToMap(String strXML) throws Exception {
            Map<String, Object> data = new HashMap<String, Object>();
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            try {
                stream.close();
            } catch (Exception ex) {
    
            }
            return data;
        }
        
        /**
         * IO解析获取微信的数据
         * 
         * @param request
         * @return
         */
        public static String getXmlString(HttpServletRequest request) {
            BufferedReader reader = null;
            String line = "";
            String xmlString = null;
            try {
                reader = request.getReader();
                StringBuffer inputString = new StringBuffer();
    
                while ((line = reader.readLine()) != null) {
                    inputString.append(line);
                }
                xmlString = inputString.toString();
            } catch (Exception e) {
                
            }
    
            return xmlString;
        }
        
        /**
         * 返回给微信服务端的xml
         * @param return_code
         * @return
         */
        public static String returnXML(String return_code) {
    
            return "<xml><return_code><![CDATA["
    
                    + return_code
    
                    + "]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        }
    }

    大神请绕道,小白可留言,哈哈哈哈哈哈哈哈~~~~~~~~~~~~~~~~~~~~~~~

  • 相关阅读:
    小猪存钱罐
    SSL与HTTPS协议
    KVM之XFS磁盘扩容
    vue学习笔记(一)
    ant打包总结
    github上传代码总结
    java中map遍历的总结
    angularjs初学总结
    angularjs常用指令
    github上传代码总结
  • 原文地址:https://www.cnblogs.com/xjbBill/p/8780670.html
Copyright © 2011-2022 走看看