超时关闭的订单,检查一下汇付是否存在支付单,一起退款

This commit is contained in:
2024-03-26 09:18:09 +08:00
parent 1045d73690
commit e0d80912fd
6 changed files with 132 additions and 75 deletions

View File

@@ -40,6 +40,7 @@ import com.jsowell.pile.dto.PayOrderDTO;
import com.jsowell.pile.service.*;
import com.jsowell.pile.vo.base.MerchantInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.beans.BeanMap;
@@ -88,10 +89,10 @@ public class AdapayService {
// log.info("===============使用汇付支付-获取支付参数");
// 相同参数重复请求,返回同一个支付对象
String redisKey = CacheConstants.ADAPAY_ORDER_PARAM + dto.getOrderCode();
Map<String, Object> cacheObject = redisCache.getCacheObject(redisKey);
if (cacheObject != null) {
Map<String, Object> resultMap = redisCache.getCacheObject(redisKey);
if (resultMap != null) {
// 表示已经获取到支付参数了,后续再有支付请求就拒绝
return cacheObject;
return resultMap;
}
// 获取支付配置
@@ -148,21 +149,21 @@ public class AdapayService {
}
JSONObject expend = JSONObject.parseObject(response.get("expend").toString());
JSONObject pay_info = expend.getJSONObject("pay_info");
Map<String, Object> resultMap = JSONObject.parseObject(pay_info.toJSONString(), new TypeReference<Map<String, Object>>() {
});
if (resultMap != null) {
// 请求参数放入缓存15分钟以内返回同一个支付参数
redisCache.setCacheObject(redisKey, resultMap, 15, TimeUnit.MINUTES);
// 请求订单号放redis
redisCache.setCacheObject(CacheConstants.ORDER_WECHAT_PAY_PARAMETERS + dto.getOrderCode(), orderNo, 30, TimeUnit.MINUTES);
}
return resultMap;
resultMap = JSONObject.parseObject(pay_info.toJSONString(), new TypeReference<Map<String, Object>>() {});
}
} catch (BaseAdaPayException e) {
log.error("汇付-获取支付对象发生异常", e);
log.error("汇付-获取支付对象发生异常, orderCode:{}", dto.getOrderCode(), e);
}
return null;
// 放缓存
if (resultMap != null) {
// 请求参数放入缓存15分钟以内返回同一个支付参数
redisCache.setCacheObject(redisKey, resultMap, 15, TimeUnit.MINUTES);
// 请求订单号放redis
redisCache.setCacheObject(CacheConstants.ORDER_WECHAT_PAY_PARAMETERS + dto.getOrderCode(), orderNo, 30, TimeUnit.MINUTES);
}
return resultMap;
}
/**
@@ -1192,10 +1193,29 @@ public class AdapayService {
return resultList;
}
/**
* 根据请求号,查询支付信息列表
*/
public List<PaymentInfo> queryPaymentInfosByOrderNo(String orderNo, String wechatAppId) throws BaseAdaPayException {
List<PaymentInfo> resultList = Lists.newArrayList();
List<AdaPayment> adaPayments = queryPaymentsByOrderNo(orderNo, wechatAppId);
if (CollectionUtils.isEmpty(adaPayments)) {
return resultList;
}
for (AdaPayment adaPayment : adaPayments) {
PaymentInfo paymentInfo = PaymentInfo.builder()
.paymentId(adaPayment.getId())
.amount(adaPayment.getPay_amt())
.build();
resultList.add(paymentInfo);
}
return resultList;
}
/**
* 查询支付列表
*/
public List<AdaPayment> queryPaymentByOrderNo(String orderNo, String wechatAppId) throws BaseAdaPayException {
public List<AdaPayment> queryPaymentsByOrderNo(String orderNo, String wechatAppId) throws BaseAdaPayException {
List<AdaPayment> resultList = Lists.newArrayList();
AbstractAdapayConfig config = AdapayConfigFactory.getConfig(wechatAppId);
if (config == null) {
@@ -1215,13 +1235,13 @@ public class AdapayService {
queryListParam.put("page_size", pageSize);
queryListParam.put("order_no", orderNo);
System.out.println("查询支付对象列表请求参数:" + JSON.toJSONString(queryListParam));
// System.out.println("查询支付对象列表请求参数:" + JSON.toJSONString(queryListParam));
Map<String, Object> paymentListResult = Payment.queryList(queryListParam, wechatAppId);
if (paymentListResult == null) {
break;
}
String jsonString = JSON.toJSONString(paymentListResult);
System.out.println("查询支付对象列表result" + jsonString);
// System.out.println("查询支付对象列表result" + jsonString);
JSONObject jsonObject = JSON.parseObject(jsonString);
List<AdaPayment> list = jsonObject.getList("payments", AdaPayment.class, JSONReader.Feature.FieldBased);

View File

@@ -1,14 +1,34 @@
package com.jsowell.adapay.vo;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
import java.util.Objects;
/**
* 付款信息对象
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PaymentInfo {
// 支付id
private String paymentId;
// 金额
private String amount;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PaymentInfo that = (PaymentInfo) o;
return paymentId.equals(that.paymentId);
}
@Override
public int hashCode() {
return Objects.hash(paymentId);
}
}

View File

@@ -31,6 +31,8 @@ public interface OrderPayRecordService{
List<OrderPayRecord> getOrderPayRecordList(String orderCode);
List<PaymentInfo> queryPaymentInfosByOrderCode(String orderCode);
List<OrderDetailInfoVO.PayRecord> selectOrderPayInfoList(String orderCode);
List<PaymentInfo> parseDeductionRecord(String deductionRecord);

View File

@@ -5,6 +5,7 @@ import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.jsowell.adapay.dto.QueryConfirmReverseDTO;
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
@@ -2029,7 +2030,8 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
public List<OrderBasicInfo> getUnpaidOrderListOver15Min() {
Date now = DateUtils.addMinute(new Date(), -15);
String nowString = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, now);
return orderBasicInfoMapper.getUnpaidOrderListOver15Min(nowString);
List<OrderBasicInfo> list = orderBasicInfoMapper.getUnpaidOrderListOver15Min(nowString);
return CollectionUtils.isNotEmpty(list) ? list : Lists.newArrayList();
}
/**
@@ -2047,18 +2049,19 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
public int close15MinutesOfUnpaidOrders() {
List<OrderBasicInfo> orderList = getUnpaidOrderListOver15Min();
if (CollectionUtils.isNotEmpty(orderList)) {
List<String> orderIdList = orderList.stream()
.map(x -> String.valueOf(x.getId()))
.collect(Collectors.toList());
// 修改订单状态
updateOrderStatusById(orderIdList, OrderStatusEnum.ORDER_CLOSE_TIMEOUT.getValue());
for (OrderBasicInfo orderBasicInfo : orderList) {
this.cleanCacheByOrderCode(orderBasicInfo.getOrderCode(), orderBasicInfo.getTransactionCode());
// 通过订单号查询汇付有没有支付单
checkUnpaidOrder(orderBasicInfo);
}
// 订单id集合
List<String> orderIdList = orderList.stream()
.map(x -> String.valueOf(x.getId()))
.collect(Collectors.toList());
// 根据orderIdList修改订单状态
updateOrderStatusById(orderIdList, OrderStatusEnum.ORDER_CLOSE_TIMEOUT.getValue());
}
return orderList.size();
}
@@ -2075,59 +2078,40 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
String merchantId = orderBasicInfo.getMerchantId();
String wechatAppId = pileMerchantInfoService.queryAppIdByMerchantId(merchantId);
String delayMode = pileMerchantInfoService.getDelayModeByWechatAppId(wechatAppId);
// List<AdaPayment> adaPayments = null;
// try {
// adaPayments = adapayService.queryPaymentByOrderNo(orderCode, wechatAppId);
// } catch (BaseAdaPayException e) {
// throw new RuntimeException(e);
// }
// if (CollectionUtils.isEmpty(adaPayments)) {
// return;
// }
// 订单总金额
BigDecimal orderAmount = orderBasicInfo.getOrderAmount() == null ? BigDecimal.ZERO : orderBasicInfo.getOrderAmount();
if (orderAmount.compareTo(BigDecimal.ZERO) > 0) {
return;
}
// 如果超时关闭的订单,存在支付信息,都退款处理
List<OrderPayRecord> orderPayRecordList = orderPayRecordService.getOrderPayRecordList(orderCode);
logger.info("校验未支付订单orderCode:{}, 支付信息:{}", orderCode, JSON.toJSONString(orderPayRecordList));
if (CollectionUtils.isNotEmpty(orderPayRecordList)) {
for (OrderPayRecord orderPayRecord : orderPayRecordList) {
List<PaymentInfo> paymentInfos = orderPayRecordService.parseDeductionRecord(orderPayRecord.getDeductionRecord());
for (PaymentInfo paymentInfo : paymentInfos) {
String paymentId = paymentInfo.getPaymentId();
BigDecimal payAmt = new BigDecimal(paymentInfo.getAmount());
if (MerchantDelayModeEnum.DELAY.getMode().equals(delayMode)) {
// 延时分账商户,创建交易撤销请求
adapayService.createPaymentReverseRequest(paymentId, payAmt, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), orderCode);
} else {
// 实时分账商户,创建交易退款请求
adapayService.createRefundRequest(paymentId, payAmt, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), orderCode);
}
Set<PaymentInfo> paymentInfoSet = Sets.newHashSet();
List<PaymentInfo> paymentInfos1 = orderPayRecordService.queryPaymentInfosByOrderCode(orderCode);
paymentInfoSet.addAll(paymentInfos1);
try {
String redisKey = CacheConstants.ORDER_WECHAT_PAY_PARAMETERS + orderCode;
String orderNo = redisCache.getCacheObject(redisKey);
if (StringUtils.isNotBlank(orderNo)) {
List<PaymentInfo> paymentInfos2 = adapayService.queryPaymentInfosByOrderNo(orderNo, wechatAppId);
paymentInfoSet.addAll(paymentInfos2);
}
} catch (BaseAdaPayException e) {
throw new RuntimeException(e);
}
if (CollectionUtils.isNotEmpty(paymentInfoSet)) {
for (PaymentInfo paymentInfo : paymentInfoSet) {
String paymentId = paymentInfo.getPaymentId();
BigDecimal payAmt = new BigDecimal(paymentInfo.getAmount());
if (MerchantDelayModeEnum.DELAY.getMode().equals(delayMode)) {
// 延时分账商户,创建交易撤销请求
adapayService.createPaymentReverseRequest(paymentId, payAmt, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), orderCode);
} else {
// 实时分账商户,创建交易退款请求
adapayService.createRefundRequest(paymentId, payAmt, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), orderCode);
}
}
}
// 退款
// for (AdaPayment adaPayment : adaPayments) {
// String status = adaPayment.getStatus();
// if (!AdapayStatusEnum.SUCCEEDED.getValue().equals(status)) {
// // 不是交易完成状态,就跳过
// continue;
// }
// String paymentId = adaPayment.getId();
// BigDecimal payAmt = new BigDecimal(adaPayment.getPay_amt());
// if (MerchantDelayModeEnum.DELAY.getMode().equals(delayMode)) {
// // 延时分账商户,创建交易撤销请求
// adapayService.createPaymentReverseRequest(paymentId, payAmt, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), orderCode);
// } else {
// // 实时分账商户,创建交易退款请求
// adapayService.createRefundRequest(paymentId, payAmt, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(), orderCode);
// }
// }
}
/**

View File

@@ -78,6 +78,19 @@ public class OrderPayRecordServiceImpl implements OrderPayRecordService {
return orderPayRecordMapper.getOrderPayRecordList(orderCode);
}
@Override
public List<PaymentInfo> queryPaymentInfosByOrderCode(String orderCode) {
List<PaymentInfo> resultList = Lists.newArrayList();
List<OrderPayRecord> orderPayRecordList = getOrderPayRecordList(orderCode);
if (CollectionUtils.isNotEmpty(orderPayRecordList)) {
for (OrderPayRecord orderPayRecord : orderPayRecordList) {
List<PaymentInfo> paymentInfos = this.parseDeductionRecord(orderPayRecord.getDeductionRecord());
resultList.addAll(paymentInfos);
}
}
return resultList;
}
/**
* 查询订单支付信息
* 加缓存