2025-11-14 20:48:08 +08:00

101 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.EbikeWxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 用户订单支付任务。检测支付状态,更新订单状态。
*
* @author yanglei
* @since 2025-10-22 16:09
*/
@Slf4j
@Service
public class WxPayTask {
@Resource
private WxPayConfig wxPayConfig;
@Resource
private EbikeWxPayService wxPayService;
@Resource
private EbikePaymentService ebikePaymentService;
@Resource
private EbikeRefundService ebikeRefundService;
/**
* 查询创建未超过24小时并且未支付的订单
*/
public void checkOrderStatus() {
log.info("checkOrderStatus 执行......");
// 1. 查询未支付的订单
List<EbikePayment> ebikePaymentList = ebikePaymentService.getNoPayOrderByDuration(wxPayConfig.getExpireMinute());
// 2. 遍历订单,查询支付状态
for (EbikePayment ebikePayment : ebikePaymentList) {
log.warn("未支付的订单号 ===> {}", ebikePayment.getOrderId());
// 调用微信支付查询接口,查询支付状态
Transaction transaction = wxPayService.queryOrderByOutTradeNo(String.valueOf(ebikePayment.getTradeId()));
// 3. 更新订单状态
if (transaction != null) {
ebikePaymentService.updatePaymentStatus(transaction);
}
}
}
/**
* 查询创建超过24小时并且未支付的订单
*/
public void closeOrder() {
log.info("closeOrder 执行......");
// 1. 查询未支付的超时订单
List<EbikePayment> ebikePaymentList = ebikePaymentService.getExpireOrderByDuration(wxPayConfig.getExpireMinute());
// 2. 遍历订单,关闭订单
for (EbikePayment ebikePayment : ebikePaymentList) {
log.warn("超时未支付的订单号 ===> {}", ebikePayment.getOrderId());
// 调用微信支付关闭接口,关闭订单
boolean close = wxPayService.closeOrder(String.valueOf(ebikePayment.getTradeId()));
if (close) {
// 3. 更新订单状态
Transaction transaction = new Transaction();
transaction.setTradeState(Transaction.TradeStateEnum.CLOSED);
transaction.setTradeStateDesc("订单关闭");
ebikePaymentService.updatePaymentStatus(transaction);
}
}
}
/**
* 查询创建未超过24小时并且未成功的退款单
*/
public void checkRefundStatus() {
log.info("checkRefundStatus 执行......");
// 1. 查询未成功的退款单
List<EbikeRefund> ebikeRefundList = ebikeRefundService.getNoSuccessRefundOrderByDuration(wxPayConfig.getExpireMinute());
// 2. 遍历退款单,查询退款状态
for (EbikeRefund ebikeRefund : ebikeRefundList) {
log.warn("超时未退款的退款单号 ===> {}", ebikeRefund.getRefundId());
// 调用微信退款查询接口,查询退款状态
Refund refund = wxPayService.queryRefundByOutTradeNo(ebikeRefund.getRefundOrderId());
if (refund != null) {
// 3. 更新退款单状态
ebikeRefundService.updateRefundStatus(refund);
}
}
}
}