WeChat payment Chinese garbled java, payment Chinese garbled java

Source: Internet
Author: User
Tags openid

Payment Chinese garbled java, payment Chinese garbled java

There are too many php files on the Internet, and java cannot be found at all. After testing for a few days, this works. Pay attention to the red text below.


/**

* Order a product
* @ Param mm
* @ Return
*/
@ RequestMapping ("/pay ")
Public void pay (String openid, Integer Region ID, Integer mid, Integer pid, String body, String attach, Integer money, HttpServletResponse res) throws IOException {
SysHotel hotel = hdao. selectByPrimaryKey (Region ID );
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 & policy_url = % s & openid = % s & out_trade_no = % s & spbill_create_ip = % s & total_region = % s & trade_type = % s & key = % s ";
// Pre-order Signature
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, // Order Number
"127.0.0.1 ",
Money * 100 + "",
"JSAPI ",
Hotel. getWxmckey (). toUpperCase (). substring (4 );
// Generate the order number
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>" +
"<Yy_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, // Order Number
"127.0.0.1 ",
Money * 100 + "",
"JSAPI ",
Md5 ));
If ("YES". equals (cts. get ("WXdebug") {log.info ("Payment preorder:" + prepayres );};
// Jsapi payment parameters
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 payment:" + jo. toString ());};
Res. setCharacterEncoding ("UTF-8 ");
Res. getWriter (). write (jo. toString ());

}



/**
* Interface base post request
* @ Param args [interface path, xmlbody parameter]
* @ Return json string
* @ 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(.exe cute (post). getEntity (), "UTF-8 ");
} Catch (Exception e ){
Return String. format ("{\" exception \ ": \" % s \ "}", e. getMessage ());
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.