public Map<String, String> wxPay(String openId, String body, String out_trade_no, String total_fee, String trade_type, String notify_url) {
String spbill_create_ip = IPUtil.getIp();
Map<String, String> data = new HashMap<>();
data.put("body", body); // 订单标签
data.put("out_trade_no", out_trade_no); //订单号(本地数据库中)
data.put("total_fee", total_fee); //订单金额
data.put("spbill_create_ip", spbill_create_ip); //ip
data.put("notify_url", notify_url); //回调方法的地址
data.put("trade_type", trade_type); // 此处指定为公众号支付 JSAPI
data.put("openid", openId); //保存在session的openid
try {
WXPay wxPay = new WXPay(new WXPayConfigImpl());
Map<String, String> unifiedOrderRes = wxPay.unifiedOrder(data);
return toJsapiPayParam(unifiedOrderRes);
} catch (Exception e) {
throw new ServiceException(e);
}
}
//获取客户端的ip
public static String getIp(HttpServletRequest request) {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
return "127.0.0.1";
}
String ip = request.getHeader("X-Real-IP");
String xip = request.getHeader("x-forwarded-for");
if (!checkIP(ip)) {
ip = request.getHeader("REMOTE-HOST");
}
if (StringUtils.isNotBlank(ip) && !ip.equals(xip)) {
logger.info("getipne X-Real-IP " + ip + " ,x-forwarded-for " + xip);
}
if (!checkIP(ip) || !ip.equals(xip)) {
String info = request.getHeader("x-forwarded-for");
if (StringUtils.isNotBlank(info)) {
String[] ips = info.trim().split(",");
for (String s : ips) {
if (!"unknown".equalsIgnoreCase(s)) {//取第一个非unknown的ip地址
ip = s;
break;
}
}
}
}
if (!checkIP(ip)) {
ip = request.getHeader("Proxy-Client-IP");//只在 Apache(Weblogic Plug-In Enable)+WebLogic 搭配下出现
}
if (!checkIP(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");//只在 Apache(Weblogic Plug-In Enable)+WebLogic 搭配下出现
}
if (!checkIP(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (!checkIP(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (!checkIP(ip)) {
ip = request.getRemoteAddr();
}
// 多级反向代理
if (null != ip && !"".equals(ip.trim())) {
StringTokenizer st = new StringTokenizer(ip, ",");
if (st.countTokens() > 1) {
return st.nextToken();
}
}
return ip;
}
private Map<String, String> toJsapiPayParam(Map<String, String> unifiedOrderRes) throws Exception {
if(unifiedOrderRes.get("prepay_id")!=null){
String prepay_id = unifiedOrderRes.get("prepay_id");
Map<String,String> jsapiPayParam=new HashMap<>();
jsapiPayParam.put("appId", WeChatConfigs.getAppID());
jsapiPayParam.put("timeStamp", String.valueOf(System.currentTimeMillis()/1000));
jsapiPayParam.put("nonceStr", WXPayUtil.generateNonceStr());
jsapiPayParam.put("package","prepay_id="+prepay_id);
jsapiPayParam.put("signType","MD5");
String sign = WXPayUtil.generateSignature(jsapiPayParam, WeChatConfigs.getApiSecret(), WXPayConstants.SignType.MD5);
jsapiPayParam.put("paySign",sign);
return jsapiPayParam;
}else if(unifiedOrderRes.get("err_code_des")!=null){
logger.error(JSONObject.toJSONString(unifiedOrderRes));
throw new ServiceException(unifiedOrderRes.get("err_code_des"));
}else{
logger.error(JSONObject.toJSONString(unifiedOrderRes));
throw new ServiceException("微信支付出错");
}
}
<dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>