微信支付中文亂碼 java,支付中文亂碼java

來源:互聯網
上載者:User

支付中文亂碼 java,支付中文亂碼java

網上太多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&notify_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());
}
}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.