微信支付中文乱码 java
时间:2015-05-26 18:48:29
收藏:0
阅读:990
网上太多php的,java的完全找不到,测试了几天,这个行得通,注意下方红色文字
/**
* 购买产品下单* @param mm
* @return
*/
@RequestMapping("/pay")
public void pay(String openid,Integer hotelid,Integer mid,Integer pid,String body,String attach,Integer money,HttpServletResponse res) throws IOException{
SysHotel hotel=hdao.selectByPrimaryKey(hotelid);
String nonceStr=Constants.randomstr(32);
long timestamp=new Date().getTime()/100;
String sign="appid=%s&attach=%s&body=%s&device_info=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&openid=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s&key=%s";
//预下单签名
body=new String(body.getBytes("utf-8"),"iso8859-1");
attach=new String(attach.getBytes("utf-8"),"iso8859-1");
String md5=MD5.digest(String.format(sign,
hotel.getAppid(),
attach,
body,
"WEB",
hotel.getWxmcid(),
nonceStr,
cts.get("wxserver.domain")+"/paycallback",
openid,
"h"+hotel.getId()+"p"+pid+"m"+mid+"t"+timestamp, //订单号
"127.0.0.1",
money*100+"",
"JSAPI",
hotel.getWxmckey())).toUpperCase().substring(4);
//生成订单号
String resxml="<xml>"+
"<appid>%s</appid>"+
"<attach>%s</attach>"+
"<body>%s</body>"+
"<device_info>%s</device_info>"+
"<mch_id>%s</mch_id>"+
"<nonce_str>%s</nonce_str>"+
"<notify_url>%s</notify_url>"+
"<openid>%s</openid>"+
"<out_trade_no>%s</out_trade_no>"+
"<spbill_create_ip>%s</spbill_create_ip>"+
"<total_fee>%s</total_fee>"+
"<trade_type>%s</trade_type>"+
"<sign>%s</sign>"+
"</xml>";
String prepayres=WeixinAPIRequest.unifiedorder(String.format(resxml,
hotel.getAppid(),
attach,
body,
"WEB",
hotel.getWxmcid(),
nonceStr,
cts.get("wxserver.domain")+"/paycallback",
openid,
"h"+hotel.getId()+"p"+pid+"m"+mid+"t"+timestamp, //订单号
"127.0.0.1",
money*100+"",
"JSAPI",
md5));
if("YES".equals(cts.get("WXdebug"))){log.info("微信支付预下单:"+prepayres);};
//jsapi支付参数
JSONObject jo=new JSONObject();
if("SUCCESS".equals(getSection(prepayres, "return_code"))){
String prepay_id=getSection(prepayres, "prepay_id");
jo.put("appId", hotel.getAppid());
jo.put("timeStamp", timestamp);
jo.put("package", "prepay_id="+prepay_id);
jo.put("nonceStr", nonceStr);
jo.put("signType", "MD5");
jo.put("paySign", MD5.digest(String.format("appId=%s&nonceStr=%s&package=%s&signType=%s&timeStamp=%s&key=%s",
hotel.getAppid(),
nonceStr,
"prepay_id="+prepay_id,
"MD5",
timestamp+"",
hotel.getWxmckey())).toUpperCase().substring(4));
}
if("YES".equals(cts.get("WXdebug"))){log.info("微信JSAPI支付:"+jo.toString());};
res.setCharacterEncoding("utf-8");
res.getWriter().write( jo.toString());
}
/**
* 微信接口基础post请求
* @param args[接口path,xmlbody参数]
* @return json字符串
* @throws ClientProtocolException
* @throws IOException
*/
private static final String postpay(String path,String xml){
try{
HttpPost post = new HttpPost(String.format(PAYBASE, path));
StringEntity entity = new StringEntity(xml,"iso8859-1");
entity.setContentEncoding("utf-8");
entity.setContentType("text/xml");
post.setEntity(entity);
return EntityUtils.toString(new DefaultHttpClient().execute(post).getEntity(),"utf-8");
}catch(Exception e){
return String.format("{\"exception\":\"%s\"}", e.getMessage());
}
}
评论(0)