微信支付回调处理

时间:2025-04-10 16:21:13

微信支付完成后需要跳转自己指定的页面,同时需要防止微信回调多次调用

解决思路:(第一次回调成功跳转自己设定的页面,第二次回调成功判断业务是否处理完成,处理完成则告知微信服务器已处理)

 

import .OutputStream;
import ;

import javax.servlet.;
import ;

import org.apache.;
import ;
import ;
import ;
import ;
import ;

import ;

/**
     * @Description:微信h5支付回调
     * @return
     * @throws Exception
     * @throws WeixinException
     * @date 
     */
    @RequestMapping("/weixinH5CallBack")
    public ModelAndView wxNotify(HttpServletRequest request,HttpServletResponse response) throws Exception{
        ModelAndView  model = new ModelAndView();
        try {
            //微信返回的xml
            String notityXml = ((), "UTF-8");
            String resXml = "";
            ("接收到的报文:" + notityXml);
            if ((notityXml)) {
                // 将解析结果存储在HashMap中
               Map map = (notityXml);
                String returnCode = (String) ("return_code");
                if("SUCCESS".equals(returnCode)){  
                        if(判断业务是否处理完成,已完成){
                                //通知微信服务器已经支付成功
                                resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
                                        + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
                                OutputStream outputStream = ();
                                (resXml, outputStream, "UTF-8");
                                ();
                            }  else {
                                业务处理
                            }
                       
                } 
            }
        } catch (Exception e) {
            ();
        } 
        ("pay/paySuccess");
        return model;
    }

 

PayUtil:

/**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws Exception {
        if(null == strxml || "".equals(strxml)) {
            return null;
        }        
        Map m = new HashMap();
        InputStream in = String2Inputstream(strxml);
        SAXBuilder builder = new SAXBuilder();
        Document doc = (in);
        Element root = ();
        List list = ();
        Iterator it = ();
        while(()) {
            Element e = (Element) ();
            String k = ();
            String v = "";
            List children = ();
            if(()) {
                v = ();
            } else {
                v = getChildrenText(children);
            }
            (k, v);
        }  
        //关闭流
        (); 
        return m;
    }
    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if(!()) {
            Iterator it = ();
            while(()) {
                Element e = (Element) ();
                String name = ();
                String value = ();
                List list = ();
                ("<" + name + ">");
                if(!()) {
                    (getChildrenText(list));
                }
                (value);
                ("</" + name + ">");
            }
        }
        return ();
    }