1. 程式人生 > >微信支付後臺總是返回資料簽名錯誤篇一

微信支付後臺總是返回資料簽名錯誤篇一

ps:微信支付後臺總是返回資料簽名錯誤,在本地呼叫支付方法返回正常,可以獲取到prepay_id,可是到程式碼上傳到伺服器,在手機裡面呼叫,總是說,簽名錯誤。我的錯誤原因是因為,商品描述,body欄位傳的中文字串。把中文改完拼音就可以了。個人推斷應該是編碼的問題。

String appid = CommonsUtil.appid; // 公眾賬號 ID

String mch_id = CommonsUtil.mchId;// 商戶號
String body = "蒙坤尚烤牛肉乾";// 商品描述
String total_fee = "1"; // 商品總價
Object str = new Date().getTime();
String out_trade_no = appid + "_" + str.toString();// 商戶訂單號 ,公眾賬號
// ID-時間(到毫秒)
// String out_trade_no = appid+"_"+DateUtil.getYMDHMSSString();//商戶訂單號
// ,公眾賬號 ID-時間(到毫秒)
String notify_url = CommonsUtil.notifyUrl;// 通知地址
String spbill_create_ip = CommonsUtil.serverIp; // 訂單生成的機器 IP


String uri = CommonsUtil.commonPay;
HashMap<String, String> parameter = new HashMap<String, String>();
// 設定統一支付介面引數
// 設定必填引數
parameter.put("appid", appid);
parameter.put("mch_id", mch_id);// 商戶號
parameter.put("nonce_str", StringUtil.sjzfc(30)); // 隨機字串
parameter.put("body", body);// 商品描述
parameter.put("out_trade_no", out_trade_no);// 商戶訂單號
parameter.put("total_fee", total_fee);// 總金額,訂單總金額,單位為分,不能帶小數點
parameter.put("notify_url", notify_url);// 通知地址
parameter.put("trade_type", "JSAPI");// 交易型別
parameter.put("openid", "oRBSJjiV7VgEohfyTu6KGxElE93s"); // 公眾賬號 ID
parameter.put("spbill_create_ip", spbill_create_ip); // 訂單生成的機器 IP
parameter.put("sign", new WxHelper().GetBizSign(parameter)); // 簽名
String xml = MessageUtil.ArrayToXml(parameter);
System.out.println("common pay xml : " + xml);
String res = RemoteClient.xmlPost(uri, xml);
System.out.println("weixin common pay result : " + res);
String prepay_id = "";
try {
Map<String, String> requestMap = MessageUtil.parseXml(res);
String return_code = requestMap.get("return_code");
String return_msg = requestMap.get("return_msg");
if (return_code.equals("SUCCESS")) {
String result_code = requestMap.get("result_code");
if (result_code.equals("SUCCESS")) {
prepay_id = requestMap.get("prepay_id");
} else {
String err_code = requestMap.get("err_code");
String erroMsgDesc = requestMap.get("err_code_des");
System.out.println("weixin pay is error!err_code: "
+ err_code + " erroMsgDesc: " + erroMsgDesc);
}
} else {// 協議錯誤,系統異常
System.out.println("weixin pay is error!協議錯誤,系統異常;return_msg: "
+ return_msg);
}
} catch (Exception e) {
System.out
.println("getCommonPay is error!,使用統一支付介面,獲取prepay_id,微信回撥/n"
+ e);
}
System.out.println(prepay_id);