微信支付body中文乱码的情况

最后解决的办法就是把发送的xml文件设置一下编码即可:data = new String(data.getBytes(), "utf-8");

当时的情况是:

项目是微信支付模式一扫码后显示package info not match special pay url

在网上查了  说是prepay_id参数的问题,经排查是上一步统一下单没用正确返回prepay_id导致下一步参数不全的情况;

所以排查统一下单方法,经过测试发现body中不带中文支付正常,带中文则会造成签名不一致的情况;

最后成功的方法就是把发送的xml文件设置编码即可;

统一下单方法:

    public static String unifiedOrder(String body, String openid
            ,String out_trade_no, String spbill_create_ip, String total_fee, String trade_type, String product_id) {
        String nonce_str = WeixinPaymentUtil.randomStr();
        total_fee = Double.valueOf(total_fee) * 100 + "";
        total_fee = total_fee.substring(0, total_fee.lastIndexOf("."));
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", APPID);
        map.put("body", body);
        map.put("mch_id", MCH_ID);
        map.put("nonce_str", nonce_str);
        map.put("notify_url", NOTIFY_URL);
        map.put("openid", openid);
        map.put("out_trade_no", out_trade_no);
        map.put("spbill_create_ip", spbill_create_ip);
        map.put("total_fee", total_fee);
        map.put("trade_type", trade_type);
        map.put("product_id", product_id);
//        System.out.println("+++++++++++++++++++++++"+map);
        String sign = WeixinPaymentUtil.sign(map, true);
        String data = ""
                        +"<xml>"
                        +"<appid>"+APPID+"</appid>"
                        +"<body>"+body+"</body>"
                        +"<mch_id>"+MCH_ID+"</mch_id>"
                        +"<nonce_str>"+nonce_str+"</nonce_str>"
                        +"<notify_url>"+NOTIFY_URL+"</notify_url>"
                        +"<openid>"+openid+"</openid>"
                        +"<out_trade_no>"+out_trade_no+"</out_trade_no>"
                        +"<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>"
                        +"<total_fee>"+total_fee+"</total_fee>"
                        +"<trade_type>"+trade_type+"</trade_type>"
                        +"<product_id>"+product_id+"</product_id>"
                        +"<sign>"+sign+"</sign>"
                        +"</xml>";
        
        try {
            data = new String(data.getBytes(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String result = WeixinPaymentUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder", data);
//        System.out.println("----------------++"+result);
        String return_code = WeixinUtil.getStrFromXml(result, "/xml/return_code");
        
        if ("SUCCESS".equals(return_code)) {
            return WeixinUtil.getStrFromXml(result, "/xml/prepay_id");
        } else {
            return "";
        }
    }

之前被误导较多时间的方法:

单独编码body的情况会造成支付页面的产品描述是编码过的字节码,而支付成功后微信返回的产品信息是中文的的情况;

当时代码是这样编写的:

body = MD5Util.MD5Encoding(body);
import java.security.MessageDigest;

public class MD5Util {private static final char hexDigits1[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

   //重要的就是这里,要调这个方法签名才可以
    public static String MD5Encoding(String s) {
        byte[] btInput = null;
        try {
            btInput = s.getBytes("UTF-8");
        }catch (Exception e){
        }
        return MD5(btInput, 32);
    }


    public static String MD5(String s) {
        byte[] btInput = s.getBytes();
        return MD5(btInput, 32);
    }

    public static String MD5_16(String str) {
        byte[] btInput = str.getBytes();
        return MD5(btInput, 16);
    }

    private static String MD5(byte[] btInput, int length) {
        try {
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // MessageDigest mdInst = MessageDigest.getInstance("SHA-1");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits1[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits1[byte0 & 0xf];
            }
            String result = new String(str);
            return length == 16 ? result.substring(8, 24) : result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/MuZi0627/p/10674807.html