WeChat APP payment Java server

WeChat APP pays the Java server, climbs out of half of the pit, and records the experience.

Similarly, the WeChat APP payment application and configuration will not be explained here. Get the APPID and partnerid, and then set up your own key on the merchant platform.

Compared with Alipay, WeChat is indeed more pitted. The official documents are not clear. If you are not careful, you will fall into the pit. Moreover, there is no customer service. The experience of the WeChat payment server can be used as a reference for the small partners who need to use it later and learn together. .

First of all, because the official demo of the server is not given, I also looked for other people's demos on the Internet, and then debugged it myself, and I won't go into details about interacting with WeChat. I refer to the demo of this great god, and put the appid in it, etc. Wait for the parameters to be replaced with the ID you applied for, and then call and execute to get the result http://blog.csdn.net/seven_cm/article/details/41559301
After I downloaded the Great God demo, I replaced the appid and other parameters with the ID I applied for, and then called and executed to get the result.
The process here is: after the order is generated in the background, the parameter information is packaged and signed, and passed Post is sent to WeChat, WeChat returns data in xml format, and there is a very important prepayid in the returned data—the prepayment transaction session ID . This data needs to be passed to the front end, so we need to parse the xml data sent by this WeChat

Generate signature rules:

    /** 
         * 微信支付签名算法sign 
         * @param characterEncoding 
         * @param parameters 
         * @return 
         */  
        @SuppressWarnings("rawtypes")  
        public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){  
            StringBuffer sb = new StringBuffer();  
            Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序)  
            Iterator it = es.iterator();  
            while(it.hasNext()) {  
                Map.Entry entry = (Map.Entry)it.next();  
                String k = (String)entry.getKey();  
                Object v = entry.getValue();  
                if(null != v && !"".equals(v)   
                        && !"sign".equals(k) && !"key".equals(k)) {  
                    sb.append(k + "=" + v + "&");  
                }  
            }  
            sb.append("key=" + Key);  
            System.out.println("字符串拼接后是:"+sb.toString());  
            String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();  
            return sign;  
        }  

First signature, i.e. order content

//参数:开始生成签名
        SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
        parameters.put("appid", appid);
        parameters.put("mch_id", mch_id);
        parameters.put("nonce_str", nonce_str);
        parameters.put("body", body);
        parameters.put("nonce_str", nonce_str);
        parameters.put("detail", detail);
        parameters.put("attach", attach);
        parameters.put("out_trade_no", out_trade_no);
        parameters.put("total_fee", total_fee);
        parameters.put("time_start", time_start);
        parameters.put("time_expire", time_expire);
        parameters.put("notify_url", notify_url);
        parameters.put("trade_type", trade_type);
        parameters.put("spbill_create_ip", spbill_create_ip);

        String sign = WXSignUtils.createSign("UTF-8", parameters);
        System.out.println("签名是:"+sign);

Generated xml parameters, parsing prepayid

//构造xml参数
        String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);

        String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        String method = "POST";

        String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();

        System.out.println(weixinPost);

        ParseXMLUtils.jdomParseXml(weixinPost);//解析key值

The preparation is complete here, and then we need to pass the parameters to the client

The parameters required by the client, the official documentation says:
It just explains the required parameters and doesn't say where the parameters come from and how to pass them.

The
appid here is the ID of our WeChat payment application
partnerid is the account prepayid of the merchant platform applied for. The
prepayment ID is the value obtained after parsing the xml data as mentioned above. The
official said, temporarily fill in the fixed value Sign=WXPay
noncestr randomly generated 16 Note for the string
timestamp : This is a huge pit, the timestamp here refers to the number of seconds from 1970 to the initiation of the payment, and it is ten digits , Calendar.getInstance().getTimeInMillis() This gets 13 digits, correct The one is: Calendar.getInstance().getTimeInMillis()/1000 to get ten seconds

sign The secondary signature returned to the client, Jujukeng, detailed below:

The server needs a total of two signatures. The first time the signature is generated, it is returned to the WeChat server. The content of the signature is the order information. Please refer to the demo above.

The second signature is the signature returned by the server to the client. The method is the same as the first signature. The content of the signature is the above six parameters plus the key applied by the merchant platform.

After getting the signature, it is OK to return the above parameters and signature to the client through json, and the rest is for the client to parse the json data and call WeChat payment.

After the server transmits the data correctly, the front-end can quickly initiate the payment. The main reason is that the official WeChat document is too brief. I also tried to adjust the data again and again with my front-end partners. As long as the WeChat is incorrect, it will only report -1, which is very helpless. , compared to the Alipay development documentation is simply too good.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325569393&siteId=291194637