package com.telrob.BaseJava;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class TestWei2 {
public static String weixinUnifiedOrderUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";
public static String WXappkey="bcY7VxM935RRrw3dTTGTMbFEoPlB1234";
public static String WXAPPappid="wx66412340150db74c";
public static String WXmch_id="1521234281";
public static String WXnotify_url="http://aaaawoodsports.com:8988/weixin/weiXinPayCallback.do";
public static void main(String[] args) {
try {
Map<String,String>map=new HashMap<String,String>();
map.put("appid", WXAPPappid);
map.put("mch_id", WXmch_id);
map.put("nonce_str","123456");
map.put("body", "abcd");
map.put("out_trade_no", "20150806125346");
map.put("total_fee", "1");
map.put("spbill_create_ip", "175.6.244.18");
map.put("notify_url", WXnotify_url);
map.put("trade_type", "APP");
String sing=getSign(map,WXappkey);
map.put("sign", sing);
String xml=convertParam(map);
//System.out.println(xml);
String result=ssl(weixinUnifiedOrderUrl,xml);
System.out.println(result);
}catch(Exception e) {
}
}
public static String getSign(Map<String, String> param,String key) throws Exception {
StringBuffer sign = new StringBuffer();
sign.append("appid=").append(param.get("appid"));
sign.append("&body=").append(param.get("body"));
sign.append("&mch_id=").append(param.get("mch_id"));
sign.append("&nonce_str=").append(param.get("nonce_str"));
sign.append("¬ify_url=").append(param.get("notify_url"));
sign.append("&out_trade_no=").append(param.get("out_trade_no"));
sign.append("&spbill_create_ip=").append(param.get("spbill_create_ip"));
sign.append("&total_fee=").append(param.get("total_fee"));
sign.append("&trade_type=").append(param.get("trade_type"));
sign.append("&key=").append(key);
return DigestUtils.md5Hex(sign.toString()).toUpperCase();
}
private static String convertParam(Map<String, String> param) throws Exception {
String result = "<xml>\r\n";
if (param != null) {
for (String key : param.keySet()) {
String value = param.get(key);
result += "<" + key + "><![CDATA[" + value + "]]></" + key + ">\r\n";
}
}
result += "</xml>\r\n";
return result;
}
/**
* 发送请求
*/
private static String ssl(String url, String data) {
StringBuffer message = new StringBuffer();
try {
CloseableHttpClient httpclient = HttpClients.custom().build();
HttpPost httpost = new HttpPost(url);
httpost.addHeader("Connection", "keep-alive");
httpost.addHeader("Accept", "*/*");
httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpost.addHeader("Host", "api.mch.weixin.qq.com");
httpost.addHeader("X-Requested-With", "XMLHttpRequest");
httpost.addHeader("Cache-Control", "max-age=0");
httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
httpost.setEntity(new StringEntity(data, "UTF-8"));
CloseableHttpResponse response = httpclient.execute(httpost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(entity.getContent(), "UTF-8"));
String text;
while ((text = bufferedReader.readLine()) != null) {
message.append(text);
}
}
EntityUtils.consume(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
return message.toString();
}
}