//微信支付必备的ASCII字典序排序并拼接商户平台设置的密钥key
public static String signature(Map<String, String> map, String key) {
Set<String> keySet = map.keySet();
String[] str = new String[map.size()];
StringBuilder tmp = new StringBuilder();
// 进行字典排序
str = keySet.toArray(str);
Arrays.sort(str);
for (int i = 0; i < str.length; i++) {
String t = str[i] + "=" + map.get(str[i]) + "&";
tmp.append(t);
}
if (null != key) {
tmp.append("key=" + key);
}
return tmp.toString();
}
//md5加密
public static String transformMD5(String inputStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
char[] charArray = inputStr.toCharArray(); // 将字符串转换为字符数组
byte[] byteArray = new byte[charArray.length]; // 创建字节数组
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
// 将得到的字节数组进行MD5运算
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer md5Str = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
if (Integer.toHexString(0xFF & md5Bytes[i]).length() == 1)
md5Str.append("0").append(Integer.toHexString(0xFF & md5Bytes[i]));
else
md5Str.append(Integer.toHexString(0xFF & md5Bytes[i]));
}
return md5Str.toString();
}
//解析微信返回来的xml数据
public static Map<String,String> getDomxml(String result) throws DocumentException{
Map map = new HashMap();
InputStream in=new ByteArrayInputStream(result.getBytes());
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(in);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
@SuppressWarnings("unchecked")
List <Element>elementList = root.elements();
for (Element element : elementList) {
map.put(element.getName(), element.getText());
}
// 返回信息
return map;
}
//发送https请求,调用微信支付接口
public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
// 创建SSLContext
StringBuffer buffer=null;
try{
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(requestMethod);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
//往服务器端写内容
if(null !=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
// 读取服务器端返回的内容
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
//wxappidutil中的属性
//小程序appid
public static String appid = "appid";
//小程序密钥
public static String secret = "密钥";
//小程序中支付接口的商户id
public static String shanghuid = "商户id";
//小程序中支付接口APIkey
public static String key = "支付api key";(商户平台里面的)
//统一下单接口
public static String zhifuUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//微信支付详细请看
<a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1">微信支付</a>
//微信支付
public static Object getData(WxPay wxpay) throws DocumentException {
Map<String ,String>map = new HashMap<String,String>();
map.put("appid", WxAppidUtil.appid);
map.put("mch_id", WxAppidUtil.shanghuid);
map.put("body", "ligeflag");
map.put("nonce_str", "1add1a30ac87aa2db72f57a2375d8fec");
map.put("notify_url", "https://www.ligeflag.com/ligeflag/wxpay/getresult.do");
map.put("openid", wxpay.getOpenid());
map.put("out_trade_no", wxpay.getOrderid());
map.put("spbill_create_ip", "服务器ip地址");
map.put("total_fee", wxpay.getJine()+"");
map.put("trade_type", "JSAPI");
String StringA = signature(map,WxAppidUtil.key);
System.out.println("加上密钥后的数据"+StringA);
String sign = Md5Util.transformMD5(StringA).toUpperCase();
System.out.println(sign);
String xml = "<xml>" +
"<appid>"+ WxAppidUtil.appid +"</appid>"+
"<body>ligeflag</body>"+
"<mch_id>"+ WxAppidUtil.shanghuid +"</mch_id>"+
"<nonce_str>随机字符串</nonce_str>"+
"<notify_url>https://www.ligeflag.com/ligeflag/wxpay/getresult.do</notify_url>"+
"<openid>"+ wxpay.getOpenid() +"</openid>"+
"<out_trade_no>"+wxpay.getOrderid()+"</out_trade_no>"+
"<spbill_create_ip>119.3.233.118</spbill_create_ip>"+
"<total_fee>"+wxpay.getJine()+"</total_fee>"+
"<trade_type>JSAPI</trade_type>"+
"<sign>"+ sign +"</sign>"+
"</xml>";
String result = HttpClientUtil.httpRequest(WxAppidUtil.zhifuUrl, "POST", xml);
System.out.println(result);
Map<String,String> resmap = getDomxml(result);
String return_code = resmap.get("return_code");//返回状态码
Map<String,String> twoSignMap = new HashMap<String,String>();
if(return_code=="SUCCESS"||return_code.equals(return_code)){
// 业务结果
Long timeStamp= System.currentTimeMillis()/1000;
String prepay_id = resmap.get("prepay_id");//返回的预付单信息
twoSignMap.put("nonceStr", "随机字符串");
twoSignMap.put("package", "prepay_id="+prepay_id);
twoSignMap.put("timeStamp", timeStamp+"");
twoSignMap.put("appId", WxAppidUtil.appid);
twoSignMap.put("signType", "MD5");
StringA = signature(twoSignMap,WxAppidUtil.key);
sign = Md5Util.transformMD5(StringA).toUpperCase();
wxpay.setTime(timeStamp+"");
wxpay.setNoncestr("随机字符串");
wxpay.setPackageStr("prepay_id="+prepay_id);
wxpay.setSigntype("MD5");
wxpay.setPaySign(sign);
}
return wxpay;
}
//微信获取openid (按照链接中的一步步操作即可)
<a href="https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html">网页授权获取openid</a>