直接上代码
String nonce_str=WXPayUtil.generateNonceStr(); //WXPayUtil微信自己有的,自己下载,这里是生成随机字符串,下载地址(下载java的里面就有):https://pay.weixin.qq.com/wiki/doc/api/native_sl.php?chapter=11_1
SortedMap<String, String> signParams = new TreeMap<String, String>();
signParams.put("appid", weChatConfig.getAppWxAppId());//app_id---需要申请
signParams.put("body",weChatConfig.getBody());//商品参数信息
signParams.put("mch_id", weChatConfig.getWxMchId());//微信商户账号--需要申请
signParams.put("nonce_str", nonce_str);//32位不重复的编号
signParams.put("notify_url", weChatConfig.getNotifyUrl());//回调页面--这个是回调接口
signParams.put("out_trade_no", orderUserId);//订单编号
BigDecimal transValue = new BigDecimal("100");
// 计算微信的总金额乘以100
BigDecimal bTotalPrice = totalPrice.multiply(transValue);
int totalFee = bTotalPrice.intValue();
signParams.put("total_fee",String.valueOf(totalFee));//支付金额 单位为分
signParams.put("trade_type", "APP"); //app支付
String sign = createSign("UTF-8", signParams);//生成签名
signParams.put("sign", sign);
signParams.remove("key");//调用统一下单无需key(商户应用密钥)
String requestXml = getRequestXml(signParams);//生成Xml格式的字符串
String result = CommonUtil.httpsRequest(weChatConfig.getWxPayUnifiedorderUrl(), "POST", requestXml);
//返回的result成功结果取出prepay_id:
Map map = null;
try {
map = doXMLParse(result);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String return_code=(String) map.get("return_code");
String prepay_id =null;
if (return_code.contains("SUCCESS")){
prepay_id=(String) map.get("prepay_id");//获取到prepay_id
}
long currentTimeMillis = System.currentTimeMillis();//生成时间戳
long second = currentTimeMillis / 1000L;//(转换成秒)
String seconds = String.valueOf(second).substring(0, 10);//(截取前10位)
SortedMap<String, String> signParam = new TreeMap<String, String>();
signParam.put("appid", weChatConfig.getAppWxAppId());//app_id
signParam.put("partnerid", weChatConfig.getWxMchId());//微信商户账号
signParam.put("prepayid", prepay_id);//预付订单id
signParam.put("package", "Sign=WXPay");//默认sign=WXPay
signParam.put("noncestr", nonce_str);//自定义不重复的长度不长于32位
signParam.put("timestamp",seconds);//北京时间时间戳
String signAgain = createSign("UTF-8", signParam);//再次生成签名
signParam.put("sign", signAgain);
return ApiResult.result(signParam);
其他
//定义签名,微信根据参数字段的ASCII码值进行排序 加密签名,故使用SortMap进行参数排序
public String createSign(String characterEncoding, SortedMap<String,String> parameters){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
Object v = entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + weChatConfig.getWxApiKey());//最后加密时添加商户密钥,由于key值放在最后,所以不用添加到SortMap里面去,单独处理,编码方式采用UTF-8
String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
return sign;
}
//将封装好的参数转换成Xml格式类型的字符串
public String getRequestXml(SortedMap<String,String> parameters){
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
Set es = parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
String v = (String)entry.getValue();
if("sign".equalsIgnoreCase(k)){
}
else if ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)) {
sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");
}
else {
sb.append("<"+k+">"+v+"</"+k+">");
}
}
sb.append("<"+"sign"+">"+"<![CDATA["+parameters.get("sign")+"]]></"+"sign"+">");
sb.append("</xml>");
return sb.toString();
}
/**
* 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
* @param strxml
* @return
* @throws JDOMException
* @throws IOException
*/
public Map doXMLParse(String strxml) throws JDOMException, IOException {
strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
if(null == strxml || "".equals(strxml)) {
return null;
}
Map m = new HashMap();
InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(in);
Element root = doc.getRootElement();
List list = root.getChildren();
Iterator it = list.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
List children = e.getChildren();
if(children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = getChildrenText(children);
}
m.put(k, v);
}
//关闭流
in.close();
return m;
}
/**
* 获取子结点的xml
* @param children
* @return String
*/
public String getChildrenText(List children) {
StringBuffer sb = new StringBuffer();
if(!children.isEmpty()) {
Iterator it = children.iterator();
while(it.hasNext()) {
Element e = (Element) it.next();
String name = e.getName();
String value = e.getTextNormalize();
List list = e.getChildren();
sb.append("<" + name + ">");
if(!list.isEmpty()) {
sb.append(getChildrenText(list));
}
sb.append(value);
sb.append("</" + name + ">");
}
}
return sb.toString();
}
public class MD5Util {
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
}
public class CommonUtil {
public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
try {
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String httpRequest(String urlStr, String xml) throws UnsupportedEncodingException {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Pragma", "no-cache");// 【Pragma:】centos7报错,不合法
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "text/xml");
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(new String(xml.getBytes("UTF-8")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
sb.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new String(sb.toString().getBytes(), "UTF-8");
}
}