|
|
|
@ -3,18 +3,17 @@ package com.bnyer.pay.design.strategy; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alipay.api.msg.MsgConstants; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.bnyer.common.core.constant.RedisKeyConstant; |
|
|
|
import com.bnyer.common.core.domain.PayInfo; |
|
|
|
import com.bnyer.common.core.enums.EnumPayType; |
|
|
|
import com.bnyer.common.redis.service.RedissonService; |
|
|
|
import com.bnyer.pay.dto.PayNotifyCheckDto; |
|
|
|
import com.bnyer.pay.mapper.PayInfoMapper; |
|
|
|
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse; |
|
|
|
import com.bnyer.pay.service.PayInfoService; |
|
|
|
import com.bnyer.pay.vo.PayInfoDetailsVo; |
|
|
|
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyV3Response; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.redisson.api.RLock; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.Objects; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@ -26,11 +25,15 @@ import java.util.concurrent.TimeUnit; |
|
|
|
@Slf4j |
|
|
|
public abstract class AbstractPayStrategy implements IPayStrategy { |
|
|
|
|
|
|
|
@Resource |
|
|
|
private PayInfoMapper payInfoMapper; |
|
|
|
private static PayInfoService payInfoService; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private RedissonService redissonService; |
|
|
|
private static RedissonService redissonService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
public void setBean(PayInfoService payInfoService, RedissonService redissonService) { |
|
|
|
AbstractPayStrategy.payInfoService = payInfoService; |
|
|
|
AbstractPayStrategy.redissonService = redissonService; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 校验是否已支付避免重复调用 |
|
|
|
@ -38,38 +41,35 @@ public abstract class AbstractPayStrategy implements IPayStrategy { |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public String payNotifyCheck(PayNotifyCheckDto checkDto){ |
|
|
|
log.error("回调结果校验开始,支付单号:{}",checkDto.getPayId()); |
|
|
|
log.info("回调结果校验开始,支付单号:{}",checkDto.getPayId()); |
|
|
|
String resultMsg = ""; |
|
|
|
RLock rLock = redissonService.getRLock(RedisKeyConstant.PAY_NOTIFY_LOCK_KEY + checkDto.getPayId()); |
|
|
|
try{ |
|
|
|
if(rLock.tryLock(2L, 10L, TimeUnit.SECONDS)){ |
|
|
|
PayInfo payInfo = payInfoMapper.selectOne(new LambdaQueryWrapper<PayInfo>().eq(PayInfo::getPayId, checkDto.getPayId()).eq(PayInfo::getIsShow, "1")); |
|
|
|
if (Objects.isNull(payInfo)){ |
|
|
|
return buildNotifyCheckResultMsg(checkDto.getPayType(),false,"business fail"); |
|
|
|
} |
|
|
|
log.info("查询到支付订单信息为:{}", JSON.toJSONString(payInfo)); |
|
|
|
PayInfoDetailsVo payInfoDetailsVo = payInfoService.queryPayInfo(checkDto.getPayId()); |
|
|
|
log.info("查询到支付订单信息为:{}", JSON.toJSONString(payInfoDetailsVo)); |
|
|
|
//订单中的金额
|
|
|
|
String payInfoPayAmount = payInfo.getPayAmount().toString(); |
|
|
|
String payInfoPayAmount = payInfoDetailsVo.getPayAmount().toString(); |
|
|
|
//回调中的金额
|
|
|
|
String notifyPayAmount = checkDto.getPayAmount(); |
|
|
|
//对账状态
|
|
|
|
Integer singleStatus = payInfo.getSingleStatus(); |
|
|
|
Integer singleStatus = payInfoDetailsVo.getSingleStatus(); |
|
|
|
log.info("回调中的金额:{},订单中的金额:{}",notifyPayAmount,payInfoPayAmount); |
|
|
|
if (payInfoPayAmount.equals(notifyPayAmount)){ |
|
|
|
if (Objects.nonNull(singleStatus)){ |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),true,checkDto.getMsg()); |
|
|
|
} |
|
|
|
}else { |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),false,"business fail"); |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),false,"trade fail"); |
|
|
|
} |
|
|
|
}else { |
|
|
|
log.error("获取锁失败,支付单号:{}",checkDto.getPayId()); |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),false,"business fail"); |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),false,"trade fail"); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("回调结果校验失败,支付单号:{}",checkDto.getPayId()); |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),false,"business fail"); |
|
|
|
resultMsg = buildNotifyCheckResultMsg(checkDto.getPayType(),false,"trade fail"); |
|
|
|
}finally { |
|
|
|
rLock.unlock(); |
|
|
|
} |
|
|
|
@ -86,7 +86,7 @@ public abstract class AbstractPayStrategy implements IPayStrategy { |
|
|
|
public String buildNotifyCheckResultMsg(EnumPayType payType,boolean isSuccess,String msg){ |
|
|
|
String result = ""; |
|
|
|
if (EnumPayType.WX_PAY == payType){ |
|
|
|
result = isSuccess?WxPayNotifyResponse.success(msg):WxPayNotifyResponse.fail(msg); |
|
|
|
result = isSuccess?WxPayNotifyV3Response.success(msg): WxPayNotifyV3Response.fail(msg); |
|
|
|
}else if (EnumPayType.ALI_PAY == payType){ |
|
|
|
result = isSuccess?MsgConstants.SUCCESS:MsgConstants.FAIL; |
|
|
|
} else if (EnumPayType.KS_PAY == payType) { |
|
|
|
|