From 59677f1c5ff433fd18e0dc783670c417f38a86bf Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Mon, 24 Feb 2025 10:21:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E9=80=80=E6=AC=BE=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/config/TencentPayConfiguration.java | 29 ++++--- .../controller/OrderRefundController.java | 80 ++++++++++++------- .../order/controller/WxPayController.java | 77 +++++++++++++++++- .../scheduler/OrderTimeoutScheduler.java | 4 +- .../central/order/service/IRefundService.java | 4 +- .../order/service/impl/RefundServiceImpl.java | 5 +- .../src/main/resources/application.yml | 2 + .../main/resources/application-dev.properties | 4 +- .../resources/application-prod.properties | 2 + 9 files changed, 156 insertions(+), 51 deletions(-) diff --git a/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java b/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java index d1136267..cd9b4838 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java +++ b/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java @@ -3,6 +3,7 @@ package com.central.order.config; import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.RSAAutoCertificateConfig; import com.wechat.pay.java.core.notification.NotificationConfig; +import com.wechat.pay.java.core.notification.NotificationParser; import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; import com.wechat.pay.java.service.refund.RefundService; import lombok.Getter; @@ -44,28 +45,32 @@ public class TencentPayConfiguration { */ @Bean public JsapiServiceExtension initialJsapiServiceExtension() { - Config config = - new RSAAutoCertificateConfig.Builder() - .merchantId(merchantId) - .privateKeyFromPath(privateKeyPath) - .merchantSerialNumber(merchantSerialNumber) - .apiV3Key(apiV3Key) - .build(); // 构建service - return new JsapiServiceExtension.Builder().config(config).build(); + return new JsapiServiceExtension.Builder().config(getConfig()).build(); } @Bean public RefundService initialRefundService() { - Config config = - new RSAAutoCertificateConfig.Builder() + // 构建service + return new RefundService.Builder().config(getConfig()).build(); + } + + /** + * 微信支付配置获取 + * @return + */ + private Config getConfig() { + return new RSAAutoCertificateConfig.Builder() .merchantId(merchantId) .privateKeyFromPath(privateKeyPath) .merchantSerialNumber(merchantSerialNumber) .apiV3Key(apiV3Key) .build(); - // 构建service - return new RefundService.Builder().config(config).build(); + } + + @Bean + public NotificationParser initialNotificationParser() { + return new NotificationParser(initialNotificationConfig()); } @Bean diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderRefundController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderRefundController.java index fae602ac..7f83e3e1 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderRefundController.java +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderRefundController.java @@ -1,6 +1,7 @@ package com.central.order.controller; import com.central.common.model.Result; +import com.central.order.enums.OrderStatus; import com.central.order.model.Order; import com.central.order.service.IOrderService; import com.central.order.service.IRefundService; @@ -10,14 +11,16 @@ import com.wechat.pay.java.service.refund.model.CreateRequest; import com.wechat.pay.java.service.refund.model.Refund; import io.swagger.v3.oas.annotations.Operation; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/orders") +@RequestMapping("/refund") public class OrderRefundController { @Autowired @@ -29,39 +32,56 @@ public class OrderRefundController { @Autowired private IRefundService refundService; + @Value("${tencent.refund-notify-url}") + private String refundNotifyUrl; + @Operation(summary = "微信支付退款") - @PostMapping("/{orderId}/refund") - public Result refundOrder(@PathVariable("orderId") Long orderId) { - Order order = orderService.getById(orderId); - Assert.notNull(order,"ORDER DATA不存在"); - CreateRequest request = new CreateRequest(); - request.setOutTradeNo(order.getOrderNo()); - request.setOutRefundNo("RF"+order.getOrderNo()); - request.setReason("用户申请退款"); - // 退款结果回调通知地址 - request.setNotifyUrl("your_notify_url"); + @PostMapping() + @Transactional(rollbackFor = Exception.class) + public Result refundOrder(@RequestBody String orderNo) { + try { + // 检查订单是否存在 + Order order = orderService.getOrderByNumber(orderNo); + Assert.notNull(order, "ORDER DATA不存在"); - // 设置退款金额 - AmountReq amount = new AmountReq(); - // 订单总金额,单位为分 - amount.setTotal(100l); - // 退款金额,单位为分 - amount.setRefund(100l); + // 创建微信退款请求 + CreateRequest request = new CreateRequest(); + request.setOutTradeNo(order.getOrderNo()); + request.setOutRefundNo("RF" + order.getOrderNo()); + request.setReason("用户申请退款"); + // 从配置文件中获取退款结果回调通知地址 + request.setNotifyUrl(refundNotifyUrl); - request.setAmount(amount); + // 设置退款金额 + AmountReq amount = new AmountReq(); + // 订单总金额,单位为分 + amount.setTotal(Long.valueOf(order.getOrderAmount())); + // 退款金额,单位为分 + amount.setRefund(Long.valueOf(order.getOrderAmount())); + request.setAmount(amount); - // 发起退款请求 - Refund response = service.create(request); + // 发起退款请求 + Refund response = service.create(request); + Assert.notNull(response, "退款失败"); - Assert.notNull(response,"退款失败"); + // 创建退款订单 + com.central.order.model.Refund refund = new com.central.order.model.Refund(); + refund.setRefundNo("RF" + order.getOrderNo()); + refund.setOrderNo(order.getOrderNo()); + refund.setUserId(order.getUserId()); + refund.setStatus(OrderStatus.REFUNDING.getId()); - //创建退款订单 - com.central.order.model.Refund refund = new com.central.order.model.Refund(); - refund.setRefundReason(refund.getRefundReason()); - refund.setRefundNo(refund.getRefundNo()); - refund.setOrderNo(order.getOrderNo()); - refund.setUserId(order.getUserId()); - refundService.createRefund(refund); - return Result.succeed("退款成功"); + boolean res = refundService.createRefund(refund); + if (res) { + return Result.succeed("退款成功"); + } + throw new RuntimeException("创建退款订单失败"); + } catch (Exception e) { + // 记录异常信息 + e.printStackTrace(); + return Result.failed("退款失败:" + e.getMessage()); + } } + + } diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java index 63a92bfe..723259ef 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java +++ b/zlt-business/order-center/src/main/java/com/central/order/controller/WxPayController.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; import com.central.order.enums.OrderStatus; import com.central.order.model.Order; import com.central.order.service.IOrderService; +import com.central.order.service.IRefundService; import com.central.order.util.HttpServletUtils; import com.wechat.pay.java.core.exception.MalformedMessageException; import com.wechat.pay.java.core.exception.ValidationException; @@ -11,11 +12,14 @@ import com.wechat.pay.java.core.notification.NotificationConfig; import com.wechat.pay.java.core.notification.NotificationParser; import com.wechat.pay.java.core.notification.RequestParam; import com.wechat.pay.java.service.payments.model.Transaction; +import com.wechat.pay.java.service.refund.model.RefundNotification; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.Assert; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -34,6 +38,10 @@ public class WxPayController { private NotificationConfig notificationConfig; @Autowired private IOrderService orderService; + @Autowired + private IRefundService refundService; + @Autowired + private NotificationParser notificationParser; /** * 支付回调 @@ -115,8 +123,6 @@ public class WxPayController { order.setOrderStatus(OrderStatus.PAID.getId()); boolean res = orderService.updateOrder(order); - - if (res){ returnMap.put("code", "SUCCESS"); returnMap.put("message", "成功"); @@ -129,4 +135,71 @@ public class WxPayController { return JSONObject.toJSONString(returnMap); } + @PostMapping("/refund/Notify") + public String refundCallback(@RequestHeader("Wechatpay-Signature") String signature, + @RequestHeader("Wechatpay-Timestamp") String timestamp, + @RequestHeader("Wechatpay-Nonce") String nonce, + @RequestHeader("Wechatpay-Serial") String serial, + String body) { + log.info("------收到微信退款回调通知------"); + try { + // 构建请求参数 + RequestParam requestParam = new RequestParam + .Builder() + .body(body) + .nonce(nonce) + .signature(signature) + .serialNumber(serial) + .timestamp(timestamp) + .build(); + // 解析通知 + RefundNotification notification = notificationParser.parse(requestParam, RefundNotification.class); + + // 处理退款结果 + handleRefundResult(notification); + + // 返回成功响应 + return buildSuccessResponse(); + } catch (Exception e) { + // 处理异常,记录日志等 + e.printStackTrace(); + return buildErrorResponse(); + } + } + + /** + * 退款订单结果处理逻辑 + * @param notification + */ + private void handleRefundResult(RefundNotification notification) { + // 获取退款通知中的关键信息 + String outRefundNo = notification.getOutRefundNo(); + String refundStatus = notification.getRefundStatus().name(); + + // 根据退款状态进行相应处理 + if ("SUCCESS".equals(refundStatus)) { + // 退款成功,更新订单状态等操作 + com.central.order.model.Refund refund = refundService.getRefundByNo(outRefundNo); + Assert.notNull(refund,"退款订单数据不存在"); + refund.setStatus(OrderStatus.REFUNDED.getId()); + refundService.updateById(refund); + + Order order = orderService.getOrderByNumber(refund.getOrderNo()); + Assert.notNull(order,"订单数据不存在"); + order.setOrderStatus(OrderStatus.REFUNDED.getId()); + orderService.updateOrder(order); + } else if ("CLOSED".equals(refundStatus)) { + // 退款关闭,处理退款关闭逻辑 + } else { + // 其他退款状态,如退款中、失败等 + } + } + + private String buildSuccessResponse() { + return "{\"code\":\"SUCCESS\",\"message\":\"成功\"}"; + } + + private String buildErrorResponse() { + return "{\"code\":\"FAIL\",\"message\":\"失败\"}"; + } } diff --git a/zlt-business/order-center/src/main/java/com/central/order/scheduler/OrderTimeoutScheduler.java b/zlt-business/order-center/src/main/java/com/central/order/scheduler/OrderTimeoutScheduler.java index 895fec36..d86430ce 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/scheduler/OrderTimeoutScheduler.java +++ b/zlt-business/order-center/src/main/java/com/central/order/scheduler/OrderTimeoutScheduler.java @@ -22,13 +22,13 @@ public class OrderTimeoutScheduler { } // 每隔一定时间(这里设置为每分钟检查一次,可根据实际情况调整)检查订单超时情况 - @Scheduled(fixedRate = 1 * 1000) +// @Scheduled(fixedRate = 1 * 1000) public void checkOrderPaySuccess() { orderService.updateOrderStatus(); } // 每隔一定时间(这里设置为每分钟检查一次,可根据实际情况调整)检查订单超时情况 - @Scheduled(fixedRate = 60 * 1000) +// @Scheduled(fixedRate = 60 * 1000) public void processOrdersInQueue() { orderService.processOrdersInQueue(); } diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/IRefundService.java b/zlt-business/order-center/src/main/java/com/central/order/service/IRefundService.java index 02c5f518..342af85e 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/service/IRefundService.java +++ b/zlt-business/order-center/src/main/java/com/central/order/service/IRefundService.java @@ -30,9 +30,9 @@ public interface IRefundService extends ISuperService { /** * - * @param refundId + * @param refundNo */ - void updateRefund(Refund refundId) ; + Refund getRefundByNo(String refundNo) ; } diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/impl/RefundServiceImpl.java b/zlt-business/order-center/src/main/java/com/central/order/service/impl/RefundServiceImpl.java index 880a942b..a28af71a 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/service/impl/RefundServiceImpl.java +++ b/zlt-business/order-center/src/main/java/com/central/order/service/impl/RefundServiceImpl.java @@ -1,5 +1,6 @@ package com.central.order.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.central.common.lock.DistributedLock; import com.central.common.model.PageResult; @@ -69,8 +70,8 @@ public class RefundServiceImpl extends SuperServiceImpl im } @Override - public void updateRefund(Refund refund) { - baseMapper.updateById(refund); + public Refund getRefundByNo(String refundNo) { + return baseMapper.selectOne(new QueryWrapper().eq("refund_no",refundNo)); } } diff --git a/zlt-business/order-center/src/main/resources/application.yml b/zlt-business/order-center/src/main/resources/application.yml index b2261618..a834076a 100644 --- a/zlt-business/order-center/src/main/resources/application.yml +++ b/zlt-business/order-center/src/main/resources/application.yml @@ -42,6 +42,8 @@ tencent: appSecret: ${zlt.security.tencent.app-secret} accessTokenUrl: ${zlt.security.tencent.accessTokenUrl} getwxacodeUrl: https://api.weixin.qq.com/wxa/getwxacode?access_token= +# 微信退款通知地址 + refund-notify-url: ${zlt.wxrefund.notifyUrl} file: oss-url: goflyapp.oss-cn-beijing.aliyuncs.com cdn-url: cdn.goflytec.com diff --git a/zlt-config/src/main/resources/application-dev.properties b/zlt-config/src/main/resources/application-dev.properties index 39cfbfa1..5dd35b00 100644 --- a/zlt-config/src/main/resources/application-dev.properties +++ b/zlt-config/src/main/resources/application-dev.properties @@ -72,7 +72,9 @@ zlt.wxpay.merchantSerialNumber=11F4AC03C50EB582D3EDE082591D1F9A65388D51 # 微信支付申请的应用ID zlt.wxpay.appId=wx30a3c8de94a85584 # 微信支付异步通知地址(需外网可访问且与微信支付平台配置一致) -zlt.wxpay.notifyUrl=http://img.goflytec.com/api-order/wechat/pay/notify +zlt.wxpay.notifyUrl= https://img.goflytec.com/api-order/wechat/pay/Notify +# 微信退款异步通知地址(需外网可访问且与微信支付平台配置一致) +zlt.wxrefund.notifyUrl= https://img.goflytec.com/api-order/wechat/refund/Notify # 商户API私钥路径 zlt.wxpay.privateKeyPath=D:\\wx\\WXCertUtil\\cert\\1703796518_20250114_cert\\apiclient_key.pem # 商户APIV3密钥 diff --git a/zlt-config/src/main/resources/application-prod.properties b/zlt-config/src/main/resources/application-prod.properties index 53ee55d5..72df9c66 100644 --- a/zlt-config/src/main/resources/application-prod.properties +++ b/zlt-config/src/main/resources/application-prod.properties @@ -64,6 +64,8 @@ zlt.wxpay.merchantSerialNumber=11F4AC03C50EB582D3EDE082591D1F9A65388D51 zlt.wxpay.appId=wx30a3c8de94a85584 # 微信支付异步通知地址(需外网可访问且与微信支付平台配置一致) zlt.wxpay.notifyUrl=http://your_domain/weixin/pay/notify +# 微信退款异步通知地址(需外网可访问且与微信支付平台配置一致) +zlt.wxrefund.notifyUrl= https://img.goflytec.com/api-order/wechat/refund/Notify # 商户API私钥路径 zlt.wxpay.privateKeyPath=/data/zlt/cert/apiclient_key.pem # 商户APIV3密钥