web123456

WeChat payment callback processing

After WeChat payment is completed, you need to jump to the page you specified, and you need to prevent WeChat callbacks from being called multiple times.

Solution: (The first callback successfully jumps to the page you set, the second callback successfully determines whether the business has been processed, and the WeChat server is informed that it has been processed)

 

import .OutputStream;
import ;

import javax.servlet.;
import ;

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

import ;

/**
* @Description: WeChat h5 payment callback
     * @return
     * @throws Exception
     * @throws WeixinException
     * @date 
     */
    @RequestMapping("/weixinH5CallBack")
    public ModelAndView wxNotify(HttpServletRequest request,HttpServletResponse response) throws Exception{
        ModelAndView  model = new ModelAndView();
        try {
//Xml returned by WeChat
            String notityXml = ((), "UTF-8");
            String resXml = "";
("Received message:" + notityXml);
            if ((notityXml)) {
//Storing the parsed results in HashMap
               Map map = (notityXml);
                String returnCode = (String) ("return_code");
                if("SUCCESS".equals(returnCode)){  
if(judgment whether the service is processed, completed){
//Notify the WeChat server that has paid successfully
                                resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
                                        + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
                                OutputStream outputStream = ();
                                (resXml, outputStream, "UTF-8");
                                ();
                            }  else {
Business Processing
                            }
                       
                } 
            }
        } catch (Exception e) {
            ();
        } 
        ("pay/paySuccess");
        return model;
    }

 

PayUtil:

/**
* Parses xml and returns the first-level element key-value pair. If the first-level element has children, the value of this node is the xml data of the child node.
     * @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);
        }  
//Close the stream
        (); 
        return m;
    }
    /**
* Get the xml of the child node
     * @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 ();
    }