订单工具
package com.ghgcn.antifakeApp.wxpay.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.httpclient.NameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import com.ghgcn.antifakeApp.alipay.config.AlipayConfig;
import com.ghgcn.antifakeApp.alipay.util.SignUtils;
import com.ghgcn.antifakeApp.wxpay.config.WxpayConfig;
import com.legendshop.model.entity.SubSettlement;
import com.legendshop.util.Arith;
/**
* 微信订单工具类
* @ClassName: WxOrderInfoUtil
* @Description:
* @author 刘楠
* @date 2016-12-23 上午10:57:47
*
*/
public class WxOrderInfoUtil {
static Logger logger = LoggerFactory.getLogger(WxOrderInfoUtil.class);
/**
* @throws UnsupportedEncodingException
* 生成诵支付订单
* @Title: buildOrderParamMap
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param settlement
* @param @param ip
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String buildOrderParamMap(SubSettlement settlement, String ip,String subNums) {
SortedMap<String, String> keyValues = new TreeMap<String, String>();
keyValues.put("appid", WxpayConfig.APP_ID);
keyValues.put("mch_id", WxpayConfig.MCH_ID);
keyValues.put("nonce_str", genNonceStr());
keyValues.put("body", "正品管家-订单编号 "+subNums);
keyValues.put("out_trade_no", settlement.getSubSettlementSn());
keyValues.put("total_fee", ((int) (settlement.getCashAmount() * 100))+"");
keyValues.put("spbill_create_ip", ip);
keyValues.put("notify_url", WxpayConfig.notify_url);
keyValues.put("trade_type", WxpayConfig.trade_type);
String sign = genAppSign(keyValues);
keyValues.put("sign", sign);
String unifiedorder = toXml(keyValues);
return unifiedorder;
}
/**
* 获取随机号
* @Title: genNonceStr
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
private static String genNonceStr() {
Random random = new Random();
return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
}
/**
* 获取微信Sign
* @Title: genAppSign
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param map
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
private static String genAppSign(Map<String, String> map) {
List<String> keys = new ArrayList<String>(map.keySet());
Collections.sort(keys);
StringBuilder authInfo = new StringBuilder();
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = map.get(key);
authInfo.append(key);
authInfo.append("=");
authInfo.append(value);
authInfo.append("&");
}
authInfo.append("key");
authInfo.append("=");
authInfo.append(WxpayConfig.API_KEY);
String appSign=null;
try {
appSign = MD5.getMessageDigest(authInfo.toString().getBytes("utf-8")).toUpperCase();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return appSign;
}
/**
* 转化为XML
* @Title: toXml
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param params
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String toXml(Map<String, String> params) {
StringBuilder sb = new StringBuilder();
sb.append("<xml>");
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append("<" + entry.getKey() + ">");
sb.append("<![CDATA["+entry.getValue()+"]]>");
sb.append("</" + entry.getKey() + ">");
}
sb.append("</xml>");
logger.debug("orion" + sb.toString());
/**
*转化为u8
*/
String xmlU8String = null;
try {
xmlU8String = new String(sb.toString().getBytes("utf-8"),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return xmlU8String;
}
/**
* 将预支付回调解析
* 将XML解析为MAP
* @Title: decodeXml
* @Description:
* @param @param content
* @param @return 设定文件
* @return Map<String,String> 返回类型
* @throws
*/
public static Map<String, String> decodeXml(String content) {
Map<String, String> map =null;
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
pullParser.setInput(new StringReader(content));
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
map = new HashMap<String, String>();
break;
case XmlPullParser.START_TAG:
String key = pullParser.getName();
if (key.equals("xml"))
break;
String value = pullParser.nextText();
map.put(key, value);
break;
case XmlPullParser.END_TAG:
break;
}
eventType = pullParser.next();
}
} catch (Exception e) {
logger.debug("orion", e.toString());
}
return map;
}
/**
* 生成APP接口需要的参数
* @Title: buildAppParamMap
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param prepay_id
* @param @return 设定文件
* @return Map<String,String> 返回类型
* @throws
*/
public static Map<String, String> buildAppParamMap(String prepay_id){
SortedMap<String, String> params = new TreeMap<>();
params.put("appid", WxpayConfig.APP_ID);
params.put("partnerid", WxpayConfig.MCH_ID);
params.put("prepayid", prepay_id);
params.put("package", WxpayConfig.Wx_PACKAGE);
params.put("noncestr", genNonceStr());
SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
Date date = new Date();
params.put("timestamp", format.format(date)+"");
String sign =genAppSign(params);
params.put("sign", sign);
return params;
}
/**
* 解析微信回调
* @Title: parseXml
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param inputStream
* @param @return
* @param @throws XmlPullParserException
* @param @throws IOException 设定文件
* @return Map<String,String> 返回类型
* @throws
*/
public static Map<String, String> parseXml(InputStream inputStream )throws XmlPullParserException, IOException {
Map<String, String> map = null;
XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
pullParser.setInput(inputStream, "UTF-8");
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
map = new HashMap<String, String>();
break;
case XmlPullParser.START_TAG:
String key = pullParser.getName();
if (key.equals("xml"))
break;
String value = pullParser.nextText();
map.put(key, value);
break;
case XmlPullParser.END_TAG:
break;
}
eventType = pullParser.next();
}
return map;
}
public static void main(String[] args) {
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
System.out.println(factory);
XmlPullParser pullParser = factory.newPullParser();
System.err.println(pullParser);
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
/**
* 用于验证签名
* @Title: createSign
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param map
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String createSign(Map<String, String> map) {
List<String> keys = new ArrayList<String>(map.keySet());
Collections.sort(keys);
StringBuilder authInfo = new StringBuilder();
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = map.get(key);
if (null != value && !"".equals(value) && !"sign".equals(key)&& !"key".equals(key)) {
authInfo.append(key);
authInfo.append("=");
authInfo.append(value);
authInfo.append("&");
}
}
/** 支付密钥必须参与加密,放在字符串最后面 */
authInfo.append("key");
authInfo.append("=");
authInfo.append(WxpayConfig.API_KEY);
String checkSign=null;
try {
checkSign = MD5.getMessageDigest(authInfo.toString().getBytes("utf-8")).toUpperCase();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return checkSign;
}
}