Java后台实现微信支付和微信退款
作者:桐桐桐汀灬 时间:2023-09-06 13:44:00
微信支付流程
都是我自己工作中开发的,亲测可用,不喜勿喷。
controller中我是这么写的,你们需要根据自己的业务需求改动。ResponseBean是我自己封装的,你们可以改成你们想要的形式。
/**
* 微信统一下单接口
* @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();
}
}
来源:https://blog.csdn.net/weixin_44045144/article/details/104884298
标签:Java,微信支付,微信退款
0
投稿
猜你喜欢
Android应用程序模型之应用程序,任务,进程,线程分析
2021-10-09 02:17:45
C#正则过滤HTML标签并保留指定标签的方法
2022-12-23 18:21:39
SpringBoot资源文件的存放位置设置方式
2023-02-26 10:37:00
区分C# 中的 Struct 和 Class
2022-09-10 19:51:25
C# 中的动态创建组件(属性及事件)的实现思路及方法
2021-07-20 04:58:31
解决idea默认带的equals和hashcode引起的bug
2023-12-22 19:02:43
Android屏幕适配工具类 Android自动生成不同分辨率的值
2022-03-07 13:45:39
Java线程间的通信方式详解
2022-05-20 10:33:47
IDEA与模拟器安装调试失败的处理方法:INSTALL_PARSE_FAILED_NO_CERTIFICATES
2022-08-25 13:57:53
Spring注解驱动之BeanPostProcessor后置处理器讲解
2023-01-04 17:13:26
深入学习Java 热部署的知识
2023-10-16 18:23:24
Java实现显示指定类型的文件
2021-10-26 11:30:37
递归出现栈溢出stackoverflow的问题及解决
2023-01-29 16:36:10
Android AutoCompleteTextView控件基本用法示例
2022-04-05 08:53:43
IDEA中使用Typora编辑md文件的方法
2022-11-28 09:50:47
C#实现将Email地址转成图片显示的方法
2022-09-26 09:35:36
C#访问C++动态分配的数组指针(实例讲解)
2021-08-20 10:34:27
JAVA对list集合进行排序Collections.sort()
2023-03-31 11:14:12
Android编程自定义线程池与用法示例
2021-11-27 14:46:53
java实现压缩字符串和java字符串过滤
2023-01-06 10:35:19