都是我自己工作中开发的,亲测可用,不喜勿喷。
/**
* 微信统一下单接口
* @return
*/
@RequestMapping(value = "/doUnifiedOrder", method = RequestMethod.POST)
public ResponseBean doUnifiedOrder(@RequestBody Map<String,Object> req) {
Map resultMap=new HashMap();
String openid = (String) req.get("openId");
WXPay wxpay =null;
try {
//初始化证书
myConfig.initCert();
wxpay= new WXPay(myConfig);
} catch (Exception e) {
e.printStackTrace();
}
//生成的随机字符串
String nonce_str = WXPayUtil.generateNonceStr();
//获取客户端的ip地址
//获取本机的ip地址
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
//支付金额,需要转成字符串类型,否则后面的签名会失败
String payOutMoney = WxMoney.changeY2F(String.valueOf(req.get("money")));
String tradeName = (String) req.get("tradeName");
if(tradeName!=null&&tradeName.length()>0){
}else{
tradeName = "课程支付";
}
String body = tradeName;
//商户订单号
String out_trade_no= WXPayUtil.generateNonceStr();
//统一下单接口参数
HashMap<String, String> data = new HashMap<String, String>();
data.put("appid", 你的appid);
data.put("mch_id", 商户id);
data.put("nonce_str", nonce_str);
data.put("body", body);
data.put("out_trade_no",out_trade_no);
data.put("total_fee", payOutMoney);
data.put("spbill_create_ip", "127.0.0.1");
data.put("notify_url", 回调地址);
data.put("trade_type","JSAPI");
data.put("openid", openid);
try {
Map<String, String> rMap = wxpay.unifiedOrder(data);
String return_code = (String) rMap.get("return_code");
String result_code = (String) rMap.get("result_code");
String nonceStr = WXPayUtil.generateNonceStr();
resultMap.put("nonceStr", nonceStr);
Long timeStamp = System.currentTimeMillis() / 1000;
if ("SUCCESS".equals(return_code) && return_code.equals(result_code)) {
String prepayid = rMap.get("prepay_id");
resultMap.put("package", "prepay_id="+prepayid);
resultMap.put("signType", "MD5");
//这边要将返回的时间戳转化成字符串,不然小程序端调用wx.requestPayment方法会报签名错误
resultMap.put("timeStamp", timeStamp + "");
//再次签名,这个签名用于小程序端调用wx.requesetPayment方法
resultMap.put("appId",myConfig.getAppID());
String sign = WXPayUtil.generateSignature(resultMap, myConfig.getKey());
resultMap.put("paySign", sign);
resultMap.put("out_trade_no",out_trade_no);
ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信支付",resultMap);
return responseBean;
}else{
ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信支付失败","微信支付失败");
return responseBean;
}
} catch (Exception e) {
ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信支付失败","微信支付失败");
e.printStackTrace();
return responseBean;
}
}
微信退款代码(需要传退款的订单id)
public ResponseBean refund(Map<String, Object> req) {
Map resultMap=new HashMap();
String buyId = (String) req.get("buyId");
WXPay wxpay =null;
try {
myConfig.initCert();//初始化证书
wxpay= new WXPay(myConfig);
} catch (Exception e) {
e.printStackTrace();
}
//生成的随机字符串
String nonce_str = WXPayUtil.generateNonceStr();
//获取客户端的ip地址
//获取本机的ip地址
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
//支付金额,需要转成字符串类型,否则后面的签名会失败
// int total_fee= (int) req.get("money");
String payOutMoney = WxMoney.changeY2F(String.valueOf(req.get("money")));
// String body = (String) req.get("z");
//商户订单号
String out_trade_no= WXPayUtil.generateNonceStr();
//统一下单接口参数
HashMap<String, String> data = new HashMap<String, String>();
try {
data.put("appid", myConfig.getAppID());
data.put("mch_id", myConfig.getMchID());
data.put("nonce_str", nonce_str);
data.put("sign_type", "MD5");
data.put("out_trade_no",buyId);//微信订单号
data.put("out_refund_no", out_trade_no);//商户退款单号
data.put("total_fee",payOutMoney);//支付金额,微信支付提交的金额是不能带小数点的,且是以分为单位,这边需要转成字符串类型,否则后面的签名会失败
data.put("refund_fee",payOutMoney);//退款总金额,订单总金额,单位为分,只能为整数
//MD5运算生成签名,这里是第一次签名,用于调用统一下单接口
String sign = WXPayUtil.generateSignature(data, myConfig.getKey());
data.put("sign", sign);
Map<String, String> rMap = wxpay.refund(data);
String return_code = (String) rMap.get("return_code");
String result_code = (String) rMap.get("result_code");
Long timeStamp = System.currentTimeMillis() / 1000;
if ("SUCCESS".equals(return_code) && return_code.equals(result_code)) {
ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信退款成功",rMap);
return responseBean;
}else{
ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信退款失败","微信支付失败");
return responseBean;
}
} catch (Exception e) {
ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信退款失败","微信支付失败");
e.printStackTrace();
return responseBean;
}
}
需要用到的工具类
然后在MyConfig中配置一下你们的小程序参数就行了,如果需要添加退款功能必须配置商户证书(对应在你项目中的位置)。不需要的话就把initCert方法去掉。
public class MyConfig extends WXPayConfig {
@Value("${res.imgPath}")
public String imgPath;
private byte[] certData;
@Override
public String getAppID() {
return "你的小程序id";
}
@Override
public String getMchID() {
return "商户id";
}
@Override
public String getKey() {
return "商户秘钥";
}
@Override
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
@Override
public int getHttpConnectTimeoutMs() {
return 8000;
}
@Override
public int getHttpReadTimeoutMs() {
return 10000;
}
@Override
IWXPayDomain getWXPayDomain() {
return new IWXPayDomain() {
@Override
public void report(String domain, long elapsedTimeMillis, Exception ex) {
}
@Override
public DomainInfo getDomain(WXPayConfig config) {
return new DomainInfo("api.mch.weixin.qq.com", false);
}
};
}
@Override
public void initCert() throws Exception {
String certPath = this.imgPath+"/upload/cert/"+"apiclient_cert.p12";//从微信商户平台下载的安全证书存放的目录
System.out.println(certPath);
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
}