@ -15,10 +15,12 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.divide.PayDivideUnifiedDto
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO ;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO ;
import cn.iocoder.yudao.framework.pay.core.enums.divide.PayDivideRefundStatusRespEnum ;
import cn.iocoder.yudao.module.member.api.activitypay.ActivityApi ;
import cn.iocoder.yudao.module.member.api.activitypay.dto.ActivityPayRespDTO ;
import cn.iocoder.yudao.module.pay.controller.admin.wxprofitsharing.vo.WxProfitsharingPageReqVO ;
import cn.iocoder.yudao.module.pay.controller.admin.wxprofitsharing.vo.WxProfitsharingSaveReqVO ;
import cn.iocoder.yudao.module.pay.controller.app.divide.vo.ActivityRefundMoney ;
import cn.iocoder.yudao.module.pay.controller.app.divide.vo.DrawMoneyVO ;
import cn.iocoder.yudao.module.pay.controller.app.divide.vo.RefundMoneyVO ;
import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO ;
import cn.iocoder.yudao.module.pay.dal.dataobject.dividecompany.DivideCompanyDO ;
import cn.iocoder.yudao.module.pay.dal.dataobject.refundlog.PayRefundLogDO ;
@ -92,6 +94,8 @@ public class WxProfitsharingServiceImpl implements WxProfitsharingService {
private WxRefundMapper wxRefundMapper ;
@Resource
private PayRefundLogMapper payRefundLogMapper ;
@Resource
private ActivityApi activityApi ;
@Override
public Long createWxProfitsharing ( WxProfitsharingSaveReqVO createReqVO ) {
@ -847,7 +851,7 @@ public class WxProfitsharingServiceImpl implements WxProfitsharingService {
}
@Override
public Boolean r efundMoney( RefundMoneyVO refundMoney ) {
public void activityR efundMoney( Activity RefundMoney refundMoney ) {
//判断用户是否正在退款
List < WxRefundDO > refundDOList1 = wxRefundMapper . selectList ( Wrappers . < WxRefundDO > lambdaQuery ( )
@ -857,91 +861,80 @@ public class WxProfitsharingServiceImpl implements WxProfitsharingService {
throw new ServiceException ( REFUND_NOT_COMPLETE ) ;
}
//判断是否有金额可 退款
int money = refundMoney . getTotalPrice ( ) ;
//判断退款金额是否正确
ActivityPayRespDTO activityPay = activityApi . getActivityPay ( refundMoney . getOrderId ( ) ) ;
if ( activityPay = = null ) {
throw new ServiceException ( new ErrorCode ( 1_007_902_002 , " 未查询到付款订单 " ) ) ;
}
if ( ! activityPay . getMobile ( ) . equals ( refundMoney . getMobile ( ) ) ) {
throw new ServiceException ( REFUND_MOBILE_ERROR ) ;
}
int money = activityPay . getPrice ( ) . multiply ( new BigDecimal ( 100 ) ) . intValue ( ) ;
//交易时间超过一年的订单无法提交退款(按支付成功时间+365天计算)
LocalDateTime now = LocalDateTime . now ( ) ; // 获取当前时间
LocalDateTime thre eYears Ago = now . minusDays ( 364 ) ;
//查询最近的充值订单,时间倒序退款
List < WxOrderDO > wxOrderDOList = wxOrderMapper . selectList ( Wrappers . < WxOrderDO > lambdaQuery ( )
. eq ( WxOrderDO : : getOpenId , refundMoney . getChannelUserId ( ) )
. eq ( WxOrderDO : : getTransactionId , refundMoney . getTransactionId ( ) )
. gt ( WxOrderDO : : getRefundPrice , 0 )
. ge ( WxOrderDO : : getCreateTime , threeYearsAgo )
. orderByDesc ( WxOrderDO : : getCreateTime ) ) ;
if ( wxOrderDOList . isEmpty ( ) ) {
throw new ServiceException ( new ErrorCode ( 1_007_902_002 , " 未查询到付款订单 " ) ) ;
LocalDateTime on eYearAgo = now . minusDays ( 364 ) ;
if ( activityPay . getCreateTime ( ) . isBefore ( oneYearAgo ) ) {
throw new ServiceException ( new ErrorCode ( 1_007_902_002 , " 订单已超退款时限 " ) ) ;
}
Map < Long , WxOrderDO > orderMap = wxOrderDOList . stream ( ) . collect ( Collectors . toMap ( WxOrderDO : : getId , vo - > vo ) ) ;
//筛选出未分账的订单
List < WxOrderDO > unDivideList = wxOrderDOList . stream ( ) . filter ( vo - > vo . getRefundPrice ( ) . equals ( vo . getProfitsharingPrice ( ) ) ) . collect ( Collectors . toList ( ) ) ;
//筛选出已分账的订单
List < WxOrderDO > divideList = wxOrderDOList . stream ( ) . filter ( vo - > ! vo . getRefundPrice ( ) . equals ( vo . getProfitsharingPrice ( ) ) ) . collect ( Collectors . toList ( ) ) ;
//筛选出分账完的订单
List < WxOrderDO > divideCpList = divideList . stream ( ) . filter ( vo - > vo . getProfitsharingPrice ( ) = = 0 ) . collect ( Collectors . toList ( ) ) ;
//剩下的是未分账完的订单
List < WxOrderDO > divideUnCpList = divideList . stream ( ) . filter ( vo - > vo . getProfitsharingPrice ( ) ! = 0 ) . collect ( Collectors . toList ( ) ) ;
//查询充值订单
WxOrderDO wxOrderDO = wxOrderMapper . selectOne ( Wrappers . < WxOrderDO > lambdaQuery ( )
. eq ( WxOrderDO : : getOpenId , refundMoney . getChannelUserId ( ) )
. eq ( WxOrderDO : : getTransactionId , activityPay . getTransactionId ( ) )
. last ( " limit 1 " ) ) ;
if ( wxOrderDO = = null ) {
throw new ServiceException ( new ErrorCode ( 1_007_902_002 , " 充值订单不存在 " ) ) ;
}
//生成退款订单
List < WxRefundDO> refundDOList = createRefund ( unDivideList , divideCpList , divideUnCpList , money , refundMoney . getUserId ( ) ) ;
WxRefundDO refundDO = createActivity Refund ( wxOrderDO , money , refundMoney . getUserId ( ) ) ;
int failNum = 0 ;
//调用微信退款接口
if ( CollectionUtil . isNotEmpty ( refundDOList ) ) {
for ( WxRefundDO wxRefundDO : refundDOList ) {
WxOrderDO wxOrderDO = orderMap . get ( wxRefundDO . getPayWxOrderId ( ) ) ;
PayChannelDO channel = channelService . validPayChannel ( wxOrderDO . getChannelId ( ) ) ;
PayChannelDO channel = channelService . validPayChannel ( wxOrderDO . getChannelId ( ) ) ;
PayClient client = channelService . getPayClient ( wxOrderDO . getChannelId ( ) ) ;
PayRefundUnifiedReqDTO payRefundUnifiedReqDTO = new PayRefundUnifiedReqDTO ( ) ;
payRefundUnifiedReqDTO . setTransactionId ( activityPay . getTransactionId ( ) )
. setReason ( " 预定退款 " )
. setRefundPrice ( refundDO . getTotalPrice ( ) )
. setOutRefundNo ( refundDO . getOutRefundNo ( ) )
. setPayPrice ( refundDO . getTotalPrice ( ) )
. setNotifyUrl ( genChannelRefundNotifyUrlActivity ( channel , " 1 " ) ) ;
PayRefundRespDTO payRefundRespDTO = client . unifiedRefund ( payRefundUnifiedReqDTO ) ;
PayClient client = channelService . getPayClient ( wxOrderD O . getChannelId ( ) ) ;
PayRefundUnifiedReqDTO payRefundUnifiedReqDTO = new PayRefundUnifiedReqDTO ( ) ;
payRefundUnifiedReqDTO . s etTransactionId ( wxOrderDO . getTransactionId ( ) )
. setReason ( " 预定退款 " )
. setRefundPrice ( wx RefundD O. getTotalPrice ( ) )
. setOutRefundNo ( wxRefundDO . getOutRefundNo ( ) )
. setPayPrice ( wxOrderDO . getTotalPrice ( ) )
. setNotifyUrl ( genChannelRefundNotifyUrl ( channel ) ) ;
PayRefundRespDTO payRefundRespDTO = client . unifiedRefund ( payRefundUnifiedReqDTO ) ;
String channelRefundNo = payRefundRespDTO . getChannelRefundNo ( ) ;
wxRefundDO . setRefundId ( channelRefundNo ) ;
if ( cn . iocoder . yudao . framework . pay . core . enums . refund . PayRefundStatusRespEnum . WAITING . getStatus ( ) . equals ( payRefundRespDTO . getStatus ( ) ) ) {
wxRefundDO . setStatus ( PayDivideRefundStatusRespEnum . PROCESSING . getStatus ( ) ) ;
} else if ( cn . iocoder . yudao . framework . pay . core . enums . refund . PayRefundStatusRespEnum . SUCCESS . getStatus ( ) . equals ( payRefundRespDTO . getStatus ( ) ) ) {
wxRefundDO . setStatus ( PayDivideRefundStatusRespEnum . SUCCESS . getStatus ( ) ) ;
} else {
failNum + + ;
wxRefundDO . setStatus ( PayDivideRefundStatusRespEnum . ABNORMAL . getStatus ( ) ) ;
}
wxRefundMapper . insert ( wxRefundDO ) ;
}
String channelRefundNo = payRefundRespDT O . getChannelRefundNo ( ) ;
refundDO . setRefundId ( channelRefundNo ) ;
if ( cn . iocoder . yudao . framework . pay . core . enums . refund . PayRefundStatusRespEnum . WAITING . g etStatus ( ) . equals ( payRefundRespDTO . getStatus ( ) ) ) {
refundDO . setStatus ( PayDivideRefundStatusRespEnum . PROCESSING . getStatus ( ) ) ;
} else if ( cn . iocoder . yudao . framework . pay . core . enums . refund . PayRefundStatusRespEnum . SUCCESS . getStatus ( ) . equals ( pay RefundRespDT O. getStatus ( ) ) ) {
refundDO . setStatus ( PayDivideRefundStatusRespEnum . SUCCESS . getStatus ( ) ) ;
} else {
refundDO . setStatus ( PayDivideRefundStatusRespEnum . ABNORMAL . getStatus ( ) ) ;
}
return failNum < = 0 ;
wxRefundMapper . insert ( refundDO ) ;
}
public WxRefundDO createRefundOne ( WxOrderDO wxOrderDO , int money , Long userId ) {
public WxRefundDO createActivity Refund ( WxOrderDO wxOrderDO , int money , Long userId ) {
//退款订单
List < WxRefundDO > backList = new ArrayList < > ( ) ;
WxRefundDO wxRefundDO = new WxRefundDO ( ) ;
wxRefundDO . setPayWxOrderId ( wxOrderDO . getId ( ) ) ;
wxRefundDO . setUserId ( userId ) ;
wxRefundDO . setOutRefundNo ( noRedisDAO . generate ( payProperties . getRefundNoPrefix ( ) ) ) ;
wxRefundDO . setTotalPrice ( money ) ;
String jsonString = JsonUtils . toJsonString ( wxOrderDO ) ;
divideRedisTemplate . opsForValue ( ) . set ( " RA " + wxOrderDO . getId ( ) , jsonString ) ;
return wxRefundDO ;
}
private String genChannelRefundNotifyUrlActivity ( PayChannelDO channel , String type ) {
if ( " 1 " . equals ( type ) ) {
return payProperties . getRefundNotifyUrl ( ) + " / " + channel . getId ( ) ;
}
return payProperties . getRefundNotifyUrl ( ) + " / " + channel . getId ( ) ;
}
}