springboot集成微信jsapi支付,使用v3接口,实现微信退款,并接受退款回调
springboot集成微信jsapi支付,使用v3接口,实现微信退款,并接受退款回调
·
1. 集成微信支付步骤看我得另一个博客,流程都是完整的,亲测可用
springboot集成微信jsapi支付,使用v3接口,实现获取预支付交易id_微信支付jsapiservice.prepay-CSDN博客
2. 微信退款实现,只贴实现类,refundNotifyUrl就是退款信息得回调地址,记住要和你得退款接口对应
@Override
@Transactional(rollbackFor = Exception.class)
public Result weixinRefund(RefundDto refundDto,BywOrder bywOrder) {
//请求对象
CreateRequest request = new CreateRequest();
AmountReq amountReq = new AmountReq();
//退款金额
amountReq.setRefund(new BigDecimal(refundDto.getRefundAmount()).longValue());
//原订单金额
amountReq.setTotal(bywOrder.getOrderAmount().longValue());
//货币类型(默认人民币)
amountReq.setCurrency("CNY");
request.setOutRefundNo(refundDto.getOutTradeNo());
request.setAmount(amountReq);
request.setOutTradeNo(refundDto.getOriginOutTradeNo());
request.setReason("订单退款");
request.setNotifyUrl(refundNotifyUrl);
//返回信息
Refund refund = refundService.create(request);
if (SUCCESS.equals(refund.getStatus().SUCCESS)) {
refundDto.setRefundStatus(true);
insertRefundOrder(refundDto,bywOrder);
return Result.ok(refund, 200, "调用退款申请成功");
} else {
refundDto.setRefundStatus(false);
insertRefundOrder(refundDto,bywOrder);
return Result.error(500, "调用退款申请失败");
}
3. 接收微信退款信息通知
@PostMapping("/weixinRefundNotify")
public void refund_notify(@RequestBody String body,HttpServletRequest request, HttpServletResponse response) throws Exception {
weixinPayService.weixinRefundNotify(body,request,response);
}
@Override
public void weixinRefundNotify(String body, HttpServletRequest request, HttpServletResponse response) throws Exception {
//获取报文
//随机串
String nonceStr = request.getHeader("Wechatpay-Nonce");
//微信传递过来的签名
String signature = request.getHeader("Wechatpay-Signature");
//证书序列号(微信平台)
String serialNo = request.getHeader("Wechatpay-Serial");
//时间戳
String timestamp = request.getHeader("Wechatpay-Timestamp");
InputStream is = null;
try {
is = request.getInputStream();
// 构造 RequestParam
com.wechat.pay.java.core.notification.RequestParam requestParam = new com.wechat.pay.java.core.notification.RequestParam.Builder()
.serialNumber(serialNo)
.nonce(nonceStr)
.signature(signature)
.timestamp(timestamp)
.body(body)
.build();
// 如果已经初始化了 RSAAutoCertificateConfig,可以直接使用 config
// 初始化 NotificationParser
// 验签、解密并转换成 Transaction
RefundNotification refundNotification = notificationParser.parse(requestParam, RefundNotification.class);
String orderNo = refundNotification.getOutTradeNo();
//记录日志信息
System.out.println("订单号:" + orderNo);
if (SUCCESS.equals(refundNotification.getRefundStatus().SUCCESS)) {
System.out.println("退款成功");
//TODO------
//根据自己的需求处理相应的业务逻辑,异步
JSONObject jsonObject = JSONObject.parseObject(refundNotification.toString());
System.out.println("微信退款回调信息:" + jsonObject);
JSONObject resultJson = new JSONObject();
resultJson.put("out_trade_no", jsonObject.getString("out_trade_no"));
resultJson.put("refund_status", jsonObject.getString("refund_status"));
//业务处理
//通知微信回调成功
response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
} else {
System.out.println("微信回调失败,JsapiPayController.payNotify.transaction:" + refundNotification.toString());
//通知微信回调失败
response.getWriter().write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
}
}
response.getWriter().write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>");
需要回调微信成功或失败,不然会出发微信的重试机制,可能会发多次退款回调信息

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)