|
|
@@ -27,6 +27,7 @@ import com.izouma.nineth.dto.PageQuery;
|
|
|
import com.izouma.nineth.enums.*;
|
|
|
import com.izouma.nineth.event.CreateAssetEvent;
|
|
|
import com.izouma.nineth.event.CreateOrderEvent;
|
|
|
+import com.izouma.nineth.event.OrderNotifyEvent;
|
|
|
import com.izouma.nineth.event.TransferAssetEvent;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
import com.izouma.nineth.repo.*;
|
|
|
@@ -46,6 +47,7 @@ import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.context.event.EventListener;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.redis.core.BoundSetOperations;
|
|
|
import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
@@ -471,16 +473,16 @@ public class OrderService {
|
|
|
|
|
|
public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
|
|
|
log.info("订单回调 orderId: {}, payMethod: {}, transactionId: {}", orderId, payMethod, transactionId);
|
|
|
+ BoundSetOperations<String, Object> listOps = redisTemplate.boundSetOps("orderNotify::" + orderId);
|
|
|
+ listOps.add(transactionId);
|
|
|
+ listOps.expire(7, TimeUnit.DAYS);
|
|
|
+
|
|
|
BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("orderLock::" + orderId);
|
|
|
Boolean flag = ops.setIfAbsent(1, 1, TimeUnit.DAYS);
|
|
|
if (!Boolean.TRUE.equals(flag)) {
|
|
|
- log.info("订单回调失败 orderId: {} redis锁定", orderId);
|
|
|
- errorOrderRepo.save(ErrorOrder.builder()
|
|
|
- .orderId(orderId)
|
|
|
- .transactionId(transactionId)
|
|
|
- .payMethod(payMethod)
|
|
|
- .errorMessage("redis锁定")
|
|
|
- .build());
|
|
|
+ log.info("订单回调失败 orderId: {} redis锁定, 重新发送到队列", orderId);
|
|
|
+ rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
|
|
|
+ new OrderNotifyEvent(orderId, payMethod, transactionId, System.currentTimeMillis()));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -565,6 +567,7 @@ public class OrderService {
|
|
|
redisTemplate.delete("orderLock::" + orderId);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@EventListener
|
|
|
public void onCreateAsset(CreateAssetEvent event) {
|
|
|
Asset asset = event.getAsset();
|
|
|
@@ -607,6 +610,23 @@ public class OrderService {
|
|
|
log.error("订单取消失败 {}, redis锁了", order.getId());
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ Set<Object> transactionIds = redisTemplate.opsForSet().members("orderNotify::" + order.getId());
|
|
|
+ if (transactionIds != null && transactionIds.size() > 0) {
|
|
|
+ if (transactionIds.parallelStream().anyMatch(transactionId -> {
|
|
|
+ try {
|
|
|
+ Map<String, Object> map = Payment.query(transactionId.toString());
|
|
|
+ return "succeeded".equalsIgnoreCase(MapUtils.getString(map, "status"));
|
|
|
+ } catch (BaseAdaPayException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ })) {
|
|
|
+ log.info("订单已经支付成功,不能取消 {}", order.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
if (order.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
throw new BusinessException("已支付订单无法取消");
|