package com.cdzy.payment.task; import com.cdzy.payment.config.WxPayConfig; import com.cdzy.payment.model.entity.EbikePayment; import com.cdzy.payment.model.entity.EbikeRefund; import com.cdzy.payment.service.EbikePaymentService; import com.cdzy.payment.service.EbikeRefundService; import com.cdzy.payment.service.WxPayService; import com.wechat.pay.java.service.payments.model.Transaction; import com.wechat.pay.java.service.refund.model.Refund; import com.wechat.pay.java.service.refund.model.Status; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.List; /** * 用户订单支付任务。检测支付状态,更新订单状态。 * * @author dingchao * @date 2025/4/25 * @modified by: */ @Slf4j @Component public class WsPayTask { @Resource private WxPayConfig wxPayConfig; @Resource private WxPayService wxPayService; @Resource private EbikePaymentService ebikePaymentService; @Resource private EbikeRefundService ebikeRefundService; /** * 从第0秒开始每隔30秒执行1次,查询创建超过5分钟,并且未支付的订单 */ @Scheduled(cron = "0/30 * * * * ?") public void orderConfirm() throws Exception { log.info("orderConfirm 执行......"); // 1. 查询未支付的订单 List ebikePaymentList = ebikePaymentService.getNoPayOrderByDuration(wxPayConfig.getExpireMinute()); // 2. 遍历订单,查询支付状态 for (EbikePayment ebikePayment : ebikePaymentList) { log.warn("超时未支付的订单号 ===> {}", ebikePayment.getOrderId()); // 调用微信支付查询接口,查询支付状态 Transaction transaction = wxPayService.queryOrderByOutTradeNo(ebikePayment.getOrderId()); // 3. 更新订单状态 if (transaction != null) ebikePaymentService.updatePaymentStatus(ebikePayment.getRecordId(), transaction); } } /** * 从第0秒开始每隔30秒执行1次,查询创建超过5分钟,并且未成功的退款单 */ @Scheduled(cron = "0/30 * * * * ?") public void refundConfirm() throws Exception { log.info("refundConfirm 执行......"); // 1. 查询未成功的退款单 List ebikeRefundList = ebikeRefundService.getNoSuccessRefundOrderByDuration(wxPayConfig.getExpireMinute()); // 2. 遍历退款单,查询退款状态 for (EbikeRefund ebikeRefund : ebikeRefundList) { log.warn("超时未退款的退款单号 ===> {}", ebikeRefund.getRefundId()); // 调用微信退款查询接口,查询退款状态 Refund refund = wxPayService.queryRefundByOutNo(ebikeRefund.getRefundId()); if (refund!= null&& Status.SUCCESS.equals(refund.getStatus())){ // 3. 更新退款单状态 //ebikeRefundService.updateRefundStatus(ebikeRefund.getRefundId(), 2); } } } }