微信支付参数用的是一个Map,类似于request.getParameterMap,得到的Map,封装了页面传入的值
public WeChatModel weChatPay(PageData pd) {
//定义了当前时间,Dome中的,项目中没有用到
Date now = new Date();
//拿到Map中当前用户的ID
String user_id = pd.getString("doctorId");
//定义了随机字符串,当前项目中没有用到
String orderid = UuidUtil.get32UUID();
//该笔交易的唯一订单号,该方法是定义了当前时间戳
String out_trade_no = OrderNoUtil.getOrderNo();//订单号
//拿到发起请求客户端的IP地址
/*String attach=user_id+","+orderid;*/
String spbill_create_ip=pd.getString("spbill_create_ip");
/拿到该笔交易的价格,为支付宝不同的是,微信的金额是以分为单位计算的
String total_fee = pd.getString("price");
//定义微信支付对象,该对象的属性就是微信的配置,商户号,APPid,密钥等
WXMyConfigUtil config = new WXMyConfigUtil();
System.err.println(spbill_create_ip);
try {
//提交订单方法
//这里的最后一个参数type设置的为1,具体不明
Map<String, String> result = this.dounifiedOrder(user_id, out_trade_no, total_fee, spbill_create_ip, 1);
String nonce_str = (String) result.get("nonce_str");
String prepay_id = (String) result.get("prepay_id");
Long time = System.currentTimeMillis() / 1000;
String timestamp = time.toString();
// 签名生成算法
MD5Util md5Util = new MD5Util();
Map<String, String> map = new HashMap<>();
map.put("appid", config.getAppID());
map.put("partnerid", config.getMchID());
map.put("package", "Sign=WXPay");
map.put("noncestr", nonce_str);
map.put("timestamp", timestamp);
map.put("prepayid", prepay_id);
String sign = md5Util.getSign(map);
//因为是接口业务,所以要给前段返回数据,
//创建一个自定义对象,
WeChatModel weChatModel = new WeChatModel();
//微信的APPID
weChatModel.setAppid(config.getAppID());
//微信的商户id
weChatModel.setPartnerid(config.getMchID());
//设置常量值,具体不明
weChatModel.setPackages("Sign=WXPay");
//提交订单返回的信息
weChatModel.setNoncestr(nonce_str);
//当前时间戳相关
weChatModel.setTimestamp(timestamp);
//提交订单返回的信息
weChatModel.setPrepayid(prepay_id);
//设置签名信息
weChatModel.setSign(sign);
return weChatModel;
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
e.printStackTrace();
return null;
}
}
微信支付提交订单的方法
public Map<String,String> dounifiedOrder(String attach, String out_trade_no, String total_fee,
String spbill_create_ip, int type) throws Exception{
Map<String, String> fail = new HashMap<>();
//创建微信支付对象,对象属性就是微信的具体配置
WXMyConfigUtil config = new WXMyConfigUtil();
//微信支付对象官方,需引入
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
// 异步通知地址(请注意必须是外网)
//配置支付回调函数
data.put("notify_url", textConfig.getPath() + "/api/purse/pay/notifyWeChatPay");
// data.put("notify_url", textConfig.getPath() + "/api/master/notify");
//设置支付信息
data.put("body", "钱包充值");
//设置该笔交易唯一订单号
data.put("out_trade_no", out_trade_no);
//设置该笔交易价格
data.put("total_fee", total_fee);
//设置客户端ip
data.put("spbill_create_ip", spbill_create_ip);
//设置常量值
data.put("trade_type", "APP");
//申请人的ID
data.put("attach", attach);
StringBuffer url = new StringBuffer();
try {
//微信发起提交
Map<String, String> resp = wxpay.unifiedOrder(sortMapByKey(data));
System.out.println(resp);
System.out.println("--------------"+total_fee);
String returnCode = resp.get("return_code"); // 获取返回码
String returnMsg = resp.get("return_msg");
if ("SUCCESS".equals(returnCode)) { // 若返回码为SUCCESS,则会返回一个result_code,再对该result_code进行判断
String resultCode = (String) resp.get("result_code");
String errCodeDes = (String) resp.get("err_code_des");
System.out.print(errCodeDes);
if ("SUCCESS".equals(resultCode)) {
// 获取预支付交易回话标志
Map<String, String> map = new HashMap<>();
String prepay_id = resp.get("prepay_id");
String signType = "MD5";
map.put("signType", signType);
map.put("prepay_id", prepay_id);
String sign = MD5Util.getSign(map);
resp.put("realsign", sign);
url.append("prepay_id=" + prepay_id + "&signType=" + signType + "&sign=" + sign);
return resp;
} else {
url.append(errCodeDes);
}
} else {
url.append(returnMsg);
}
} catch (Exception e) {
logger.error(e.getMessage());
System.out.println("aaaaaaaaaaaaa");
System.out.println(e);
/* logger.info(e.getMessage()); */
}
return fail;
}
// map 的key值根据字母排序
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> sortMap = new TreeMap<String, String>(new Comparator<String>() {
public int compare(String obj1, String obj2) {
return obj1.compareTo(obj2);
}
});
sortMap.putAll(map);
return sortMap;
}
public WeChatModel weChatPay(PageData pd) {
//定义了当前时间,Dome中的,项目中没有用到
Date now = new Date();
//拿到Map中当前用户的ID
String user_id = pd.getString("doctorId");
//定义了随机字符串,当前项目中没有用到
String orderid = UuidUtil.get32UUID();
//该笔交易的唯一订单号,该方法是定义了当前时间戳
String out_trade_no = OrderNoUtil.getOrderNo();//订单号
//拿到发起请求客户端的IP地址
/*String attach=user_id+","+orderid;*/
String spbill_create_ip=pd.getString("spbill_create_ip");
/拿到该笔交易的价格,为支付宝不同的是,微信的金额是以分为单位计算的
String total_fee = pd.getString("price");
//定义微信支付对象,该对象的属性就是微信的配置,商户号,APPid,密钥等
WXMyConfigUtil config = new WXMyConfigUtil();
System.err.println(spbill_create_ip);
try {
//提交订单方法
//这里的最后一个参数type设置的为1,具体不明
Map<String, String> result = this.dounifiedOrder(user_id, out_trade_no, total_fee, spbill_create_ip, 1);
String nonce_str = (String) result.get("nonce_str");
String prepay_id = (String) result.get("prepay_id");
Long time = System.currentTimeMillis() / 1000;
String timestamp = time.toString();
// 签名生成算法
MD5Util md5Util = new MD5Util();
Map<String, String> map = new HashMap<>();
map.put("appid", config.getAppID());
map.put("partnerid", config.getMchID());
map.put("package", "Sign=WXPay");
map.put("noncestr", nonce_str);
map.put("timestamp", timestamp);
map.put("prepayid", prepay_id);
String sign = md5Util.getSign(map);
//因为是接口业务,所以要给前段返回数据,
//创建一个自定义对象,
WeChatModel weChatModel = new WeChatModel();
//微信的APPID
weChatModel.setAppid(config.getAppID());
//微信的商户id
weChatModel.setPartnerid(config.getMchID());
//设置常量值,具体不明
weChatModel.setPackages("Sign=WXPay");
//提交订单返回的信息
weChatModel.setNoncestr(nonce_str);
//当前时间戳相关
weChatModel.setTimestamp(timestamp);
//提交订单返回的信息
weChatModel.setPrepayid(prepay_id);
//设置签名信息
weChatModel.setSign(sign);
return weChatModel;
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
e.printStackTrace();
return null;
}
}
微信支付提交订单的方法
public Map<String,String> dounifiedOrder(String attach, String out_trade_no, String total_fee,
String spbill_create_ip, int type) throws Exception{
Map<String, String> fail = new HashMap<>();
//创建微信支付对象,对象属性就是微信的具体配置
WXMyConfigUtil config = new WXMyConfigUtil();
//微信支付对象官方,需引入
WXPay wxpay = new WXPay(config);
Map<String, String> data = new HashMap<String, String>();
// 异步通知地址(请注意必须是外网)
//配置支付回调函数
data.put("notify_url", textConfig.getPath() + "/api/purse/pay/notifyWeChatPay");
// data.put("notify_url", textConfig.getPath() + "/api/master/notify");
//设置支付信息
data.put("body", "钱包充值");
//设置该笔交易唯一订单号
data.put("out_trade_no", out_trade_no);
//设置该笔交易价格
data.put("total_fee", total_fee);
//设置客户端ip
data.put("spbill_create_ip", spbill_create_ip);
//设置常量值
data.put("trade_type", "APP");
//申请人的ID
data.put("attach", attach);
StringBuffer url = new StringBuffer();
try {
//微信发起提交
Map<String, String> resp = wxpay.unifiedOrder(sortMapByKey(data));
System.out.println(resp);
System.out.println("--------------"+total_fee);
String returnCode = resp.get("return_code"); // 获取返回码
String returnMsg = resp.get("return_msg");
if ("SUCCESS".equals(returnCode)) { // 若返回码为SUCCESS,则会返回一个result_code,再对该result_code进行判断
String resultCode = (String) resp.get("result_code");
String errCodeDes = (String) resp.get("err_code_des");
System.out.print(errCodeDes);
if ("SUCCESS".equals(resultCode)) {
// 获取预支付交易回话标志
Map<String, String> map = new HashMap<>();
String prepay_id = resp.get("prepay_id");
String signType = "MD5";
map.put("signType", signType);
map.put("prepay_id", prepay_id);
String sign = MD5Util.getSign(map);
resp.put("realsign", sign);
url.append("prepay_id=" + prepay_id + "&signType=" + signType + "&sign=" + sign);
return resp;
} else {
url.append(errCodeDes);
}
} else {
url.append(returnMsg);
}
} catch (Exception e) {
logger.error(e.getMessage());
System.out.println("aaaaaaaaaaaaa");
System.out.println(e);
/* logger.info(e.getMessage()); */
}
return fail;
}
// map 的key值根据字母排序
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> sortMap = new TreeMap<String, String>(new Comparator<String>() {
public int compare(String obj1, String obj2) {
return obj1.compareTo(obj2);
}
});
sortMap.putAll(map);
return sortMap;
}