You've already forked FrameTour-BE
fix(wxpay): 修复 Kafka 生产者空指针异常
- 添加对 profitShareKafkaProducer 的空值检查 - 在发送消息前确保 Kafka 生产者已注入 - 使用 CompletableFuture 处理异步退款消息发送 - 设置 Kafka 生产者的注入模式为 required=false - 避免因 Kafka 生产者缺失导致的服务启动失败
This commit is contained in:
@@ -31,10 +31,12 @@ import com.ycwl.basic.repository.OrderRepository;
|
||||
import com.ycwl.basic.service.mobile.WxPayService;
|
||||
import com.ycwl.basic.service.pc.ScenicService;
|
||||
import com.ycwl.basic.utils.SnowFlakeUtil;
|
||||
import io.netty.util.concurrent.CompleteFuture;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.kafka.support.SendResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@@ -42,6 +44,7 @@ import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* @Author: songmingsong
|
||||
@@ -74,7 +77,7 @@ public class WxPayServiceImpl implements WxPayService {
|
||||
@Autowired
|
||||
@Lazy
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Autowired
|
||||
@Autowired(required = false)
|
||||
private ProfitShareKafkaProducer profitShareKafkaProducer;
|
||||
|
||||
@Override
|
||||
@@ -126,6 +129,7 @@ public class WxPayServiceImpl implements WxPayService {
|
||||
invalidateStatisticsCache(scenicId);
|
||||
}
|
||||
});
|
||||
if (profitShareKafkaProducer != null) {
|
||||
profitShareKafkaProducer.sendProfitShareMessage(OrderMessage.builder()
|
||||
.orderId(callbackResponse.getOrderNo())
|
||||
.scenicId(scenicId)
|
||||
@@ -135,6 +139,7 @@ public class WxPayServiceImpl implements WxPayService {
|
||||
.timestamp(System.currentTimeMillis() / 1000)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new AppException(BizCodeEnum.ADVANCE_PAYMENT_CALLBACK_FAILED, e.toString());
|
||||
}
|
||||
@@ -157,7 +162,9 @@ public class WxPayServiceImpl implements WxPayService {
|
||||
IPayAdapter scenicPayAdapter = scenicService.getScenicPayAdapter(order.getScenicId());
|
||||
BigDecimal payPrice = order.getPayPrice();
|
||||
int priceInCents = payPrice.multiply(new BigDecimal(NumberConstant.HUNDRED)).intValue(); // 转换为分(int)
|
||||
profitShareKafkaProducer.sendRefundMessage(RefundMessage.builder()
|
||||
CompletableFuture<SendResult<String, String>> future;
|
||||
if (profitShareKafkaProducer != null) {
|
||||
future = profitShareKafkaProducer.sendRefundMessage(RefundMessage.builder()
|
||||
.refundOrderId(String.valueOf(order.getId()))
|
||||
.originalOrderId(String.valueOf(order.getId()))
|
||||
.refundAmount(payPrice.doubleValue())
|
||||
@@ -165,7 +172,11 @@ public class WxPayServiceImpl implements WxPayService {
|
||||
.paymentSystem("wechat")
|
||||
.timestamp(System.currentTimeMillis() / 1000)
|
||||
.build()
|
||||
).whenComplete((result, throwable) -> {
|
||||
);
|
||||
} else {
|
||||
future = CompletableFuture.completedFuture(null);
|
||||
}
|
||||
future.whenComplete((result, throwable) -> {
|
||||
RefundOrderRequest request = new RefundOrderRequest()
|
||||
.setOrderNo(orderId)
|
||||
.setPrice(priceInCents)
|
||||
|
||||
Reference in New Issue
Block a user