This commit is contained in:
wyn
2026-04-30 09:54:18 +08:00
parent 6bc3f33d3d
commit a4edccc7de
15 changed files with 278 additions and 117 deletions

View File

@@ -30,6 +30,10 @@ public class Constants {
* 订单状态 - 退款 * 订单状态 - 退款
*/ */
public static final String ORDER_STATUS_REFUND = "6"; public static final String ORDER_STATUS_REFUND = "6";
/**
* 订单状态 - 退款中
*/
public static final String ORDER_STATUS_REFUNDING = "7";
/** /**
* 订单状态 - 全部 * 订单状态 - 全部

View File

@@ -35,6 +35,7 @@ import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.*; import com.peanut.modules.common.service.*;
import com.peanut.modules.master.service.CourseCatalogueService; import com.peanut.modules.master.service.CourseCatalogueService;
import com.peanut.modules.pay.alipay.service.AliPayService; import com.peanut.modules.pay.alipay.service.AliPayService;
import com.peanut.modules.pay.weChatPay.dto.WeChatRefundInfo;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo; import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.service.WxpayService; import com.peanut.modules.pay.weChatPay.service.WxpayService;
import com.peanut.modules.sys.entity.SysConfigEntity; import com.peanut.modules.sys.entity.SysConfigEntity;
@@ -603,24 +604,28 @@ public class BuyOrderController {
return R.error("退款信息不存在"); return R.error("退款信息不存在");
} }
QueryWrapper<BuyOrderRefundLog> wapper2 = new QueryWrapper<>(); QueryWrapper<BuyOrderRefundLog> wapper2 = new QueryWrapper<>();
wapper2.eq("refund_id",refundInfo.getId()).orderByDesc("create_time"); wapper2.eq("refund_id",refundInfo.getId()).orderByDesc("id");
List<BuyOrderRefundLog> list = buyOrderRefundLogService.list(wapper2); List<BuyOrderRefundLog> list = buyOrderRefundLogService.list(wapper2);
for (BuyOrderRefundLog log : list){ for (BuyOrderRefundLog log : list){
if(log.getType()==1){ if(log.getType()==4){
log.setTitle("天医币已退还"); log.setTitle("天医币已退还");
log.setContent("天医币已退还到账户,可在我的->天医币中查看明细"); log.setContent("天医币已退还到账户,可在我的->天医币中查看明细");
}else if(log.getType()==2){ }else if(log.getType()==5){
log.setTitle("积分已退还"); log.setTitle("积分已退还");
log.setContent("积分已退还到账户,可在我的->积分中查看明细"); log.setContent("积分已退还到账户,可在我的->积分中查看明细");
}else if(log.getType()==3){ }else if(log.getType()==2){
log.setTitle(log.getStatus()==1?"到账成功":"已退款,支付宝处理中"); log.setTitle(log.getStatus()==1?"到账成功":"已退款,支付宝处理中");
log.setContent(log.getStatus()==1?"已退到您的支付宝,到账时间以支付宝处理时间为准,可前往「支付宝-账单」查看":"已将退款资金提交给支付宝处理通常情况下退款会原路退回您的支付账户预计会在1-7天内到账"); log.setContent(log.getStatus()==1?"已退到您的支付宝,到账时间以支付宝处理时间为准,可前往「支付宝-账单」查看":"已将退款资金提交给支付宝处理通常情况下退款会原路退回您的支付账户预计会在1-7天内到账");
}else if(log.getType()==4){ }else if(log.getType()==1){
log.setTitle(log.getStatus()==1?"到账成功":"已退款,微信处理中"); log.setTitle(log.getStatus()==1?"到账成功":"已退款,微信处理中");
log.setContent(log.getStatus()==1?"已退到您的微信,到账时间以微信处理时间为准,可前往「微信-账单」查看":"已将退款资金提交给微信处理通常情况下退款会原路退回您的支付账户预计会在1-7天内到账"); log.setContent(log.getStatus()==1?"已退到您的微信,到账时间以微信处理时间为准,可前往「微信-账单」查看":"已将退款资金提交给微信处理通常情况下退款会原路退回您的支付账户预计会在1-7天内到账");
}else if(log.getType()==0){
log.setTitle(log.getStatus()==3?"重新申请退款":"发起退款");
log.setContent("系统会在1-2天内提交处理");
} }
} }
BuyOrderRefundLog log = new BuyOrderRefundLog(); /* BuyOrderRefundLog log = new BuyOrderRefundLog();
log.setId(0); log.setId(0);
log.setRefundId(refundInfo.getId()); log.setRefundId(refundInfo.getId());
log.setType(0); log.setType(0);
@@ -628,7 +633,7 @@ public class BuyOrderController {
log.setCreateTime(refundInfo.getCreateTime()); log.setCreateTime(refundInfo.getCreateTime());
log.setTitle("发起退款"); log.setTitle("发起退款");
log.setContent("系统会在1-2天内提交处理"); log.setContent("系统会在1-2天内提交处理");
list.add(log); list.add(log);*/
Map<String,Object> result = new HashMap<>(); Map<String,Object> result = new HashMap<>();
BigDecimal shippingMoney = refundInfo.getDeductShipping()==1?refundInfo.getShippingMoney():BigDecimal.ZERO; BigDecimal shippingMoney = refundInfo.getDeductShipping()==1?refundInfo.getShippingMoney():BigDecimal.ZERO;
@@ -658,19 +663,19 @@ public class BuyOrderController {
if (user == null) { if (user == null) {
return R.error("订单对应用户不存在"); return R.error("订单对应用户不存在");
} }
if(Constants.ORDER_STATUS_REFUND.equals(buyOrder.getOrderStatus())
BuyOrderRefund buyOrderRefund = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("order_id",buyOrder.getOrderId())); || Constants.ORDER_STATUS_REFUNDING.equals(buyOrder.getOrderStatus())){
if(buyOrderRefund!=null){ return R.error("请勿重复提交退款");
return R.error("请勿重复申请退款"); }else if (Constants.ORDER_STATUS_TO_BE_PAID.equals(buyOrder.getOrderStatus())
}else{
buyOrderRefund = new BuyOrderRefund();
}
if (Constants.ORDER_STATUS_TO_BE_PAID.equals(buyOrder.getOrderStatus())
|| Constants.ORDER_STATUS_FAIL.equals(buyOrder.getOrderStatus()) || Constants.ORDER_STATUS_FAIL.equals(buyOrder.getOrderStatus())
|| Constants.ORDER_STATUS_REFUND.equals(buyOrder.getOrderStatus())
|| Constants.ORDER_STATUS_OUT_OF_TIME.equals(buyOrder.getOrderStatus())) { || Constants.ORDER_STATUS_OUT_OF_TIME.equals(buyOrder.getOrderStatus())) {
return R.error("当前订单状态不支持退单"); return R.error("当前订单状态不支持退单");
} }
if(Constants.PAYMENT_METHOD_ALI_PAY.equals(buyOrder.getPaymentMethod())){
return R.error("当前订单支付方式暂不支持退单");
}
//设置状态为已退款
buyOrder.setOrderStatus(Constants.ORDER_STATUS_REFUNDING);
BigDecimal refundFee = buyOrder.getRealMoney(); BigDecimal refundFee = buyOrder.getRealMoney();
BigDecimal shippingMoney = buyOrder.getShippingMoney()==null?BigDecimal.ZERO:buyOrder.getShippingMoney(); BigDecimal shippingMoney = buyOrder.getShippingMoney()==null?BigDecimal.ZERO:buyOrder.getShippingMoney();
@@ -681,12 +686,13 @@ public class BuyOrderController {
} }
String remark = params.containsKey("remark") && params.get("remark").toString()!=null?params.get("remark").toString():""; String remark = params.containsKey("remark") && params.get("remark").toString()!=null?params.get("remark").toString():"";
BuyOrderRefund buyOrderRefund = new BuyOrderRefund();
String refundNo = buyOrderRefundService.genRefundNo(); String refundNo = buyOrderRefundService.genRefundNo();
buyOrderRefund.setRefundNo(refundNo); buyOrderRefund.setRefundNo(refundNo);
buyOrderRefund.setOrderId(buyOrder.getOrderId()); buyOrderRefund.setOrderId(buyOrder.getOrderId());
buyOrderRefund.setOrderSn(buyOrder.getOrderSn()); buyOrderRefund.setOrderSn(buyOrder.getOrderSn());
buyOrderRefund.setUserId(buyOrder.getUserId()); buyOrderRefund.setUserId(buyOrder.getUserId());
buyOrderRefund.setType("线上"); //id buyOrderRefund.setType(params.containsKey("deductShipping")?"后台":"线上"); //id
buyOrderRefund.setPayType(buyOrder.getPaymentMethod()); buyOrderRefund.setPayType(buyOrder.getPaymentMethod());
buyOrderRefund.setFee(refundFee); buyOrderRefund.setFee(refundFee);
buyOrderRefund.setJfDeduction(buyOrder.getJfDeduction()); buyOrderRefund.setJfDeduction(buyOrder.getJfDeduction());
@@ -695,54 +701,68 @@ public class BuyOrderController {
buyOrderRefund.setRemark(remark); buyOrderRefund.setRemark(remark);
buyOrderRefundService.save(buyOrderRefund); buyOrderRefundService.save(buyOrderRefund);
int refundId = buyOrderRefund.getId(); buyOrderRefundLogService.insertRefundLog(buyOrderRefund.getId(),0,1);
int refundId = buyOrderRefund.getId();
if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) { if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
if(refundFee.compareTo(BigDecimal.ZERO)>0){ if(refundFee.compareTo(BigDecimal.ZERO)>0){
//退还虚拟币
transactionDetailsService.refundRecord(buyOrder,user,refundFee); transactionDetailsService.refundRecord(buyOrder,user,refundFee);
user.setPeanutCoin(user.getPeanutCoin().add(refundFee)); user.setPeanutCoin(user.getPeanutCoin().add(refundFee));
myUserService.updateById(user); myUserService.updateById(user);
buyOrderRefundLogService.insertRefundLog(refundId,1,1); //记录退款进度
buyOrderRefundLogService.insertRefundLog(refundId,4,1);
//积分、其他权益退回
buyOrderService.refundOrder(buyOrder,user,refundId); buyOrderService.refundOrder(buyOrder,user,refundId);
//更改订单状态为已 //更改订单状态为已退
buyOrder.setOrderStatus(Constants.ORDER_STATUS_REFUND); buyOrder.setOrderStatus(Constants.ORDER_STATUS_REFUND);
buyOrderService.updateById(buyOrder); buyOrderService.updateById(buyOrder);
return R.ok("ok");
} }
} else if (Constants.PAYMENT_METHOD_WECHAT_PAY.equals(buyOrder.getPaymentMethod())) { } else if (Constants.PAYMENT_METHOD_WECHAT_PAY.equals(buyOrder.getPaymentMethod())) {
buyOrderRefundLogService.insertRefundLog(refundId,4,0); buyOrderRefundLogService.insertRefundLog(refundId,1,0);
if (refundFee.compareTo(BigDecimal.ZERO) > 0) { if (refundFee.compareTo(BigDecimal.ZERO) > 0) {
Map<String, Object> refundParams = new HashMap<>(); WeChatRefundInfo weChatRefundInfo = new WeChatRefundInfo();
refundParams.put("orderSn", buyOrder.getOrderSn()); weChatRefundInfo.setOrderSn(buyOrder.getOrderSn());
refundParams.put("refundFee", refundFee); weChatRefundInfo.setOrderId(buyOrder.getOrderId());
refundParams.put("reason", StringUtils.isBlank(remark) ? "后台申请退款" : remark); weChatRefundInfo.setOrderType(buyOrder.getOrderType());
String refundResult = wxpayService.refund(refundParams); weChatRefundInfo.setRefundNo(buyOrderRefund.getRefundNo());
JSONObject resultJson = JSONObject.parseObject(refundResult); weChatRefundInfo.setTotalAmount(buyOrder.getRealMoney());
if (!"SUCCESS".equals(resultJson.getString("status"))) { weChatRefundInfo.setRefundAmount(buyOrderRefund.getFee());
return R.error("微信退款申请失败"); weChatRefundInfo.setRemark("用户申请退款");
}else{ weChatRefundInfo.setRefundId(buyOrderRefund.getId());
buyOrderRefundLogService.insertRefundLog(refundId,4,2); if(buyOrder.getCome()==null||buyOrder.getCome()==0){
} weChatRefundInfo.setAppName(buyOrder.getAppName());
} else if (buyOrder.getCome()==2){
} weChatRefundInfo.setAppName("wumen");
} else if (Constants.PAYMENT_METHOD_ALI_PAY.equals(buyOrder.getPaymentMethod())) { } else if (buyOrder.getCome()==1) {
buyOrderRefundLogService.insertRefundLog(refundId,3,0); weChatRefundInfo.setAppName("zmzm");
if (refundFee.compareTo(BigDecimal.ZERO) > 0) { } else if (buyOrder.getCome()==3) {
Map<String, Object> refundParams = new HashMap<>(); weChatRefundInfo.setAppName("xlkj");
refundParams.put("orderSn", buyOrder.getOrderSn()); } else if (buyOrder.getCome()==4) {
refundParams.put("refundFee", refundFee); weChatRefundInfo.setAppName("thyy");
String refundResult = aliPayService.refund(refundParams);
JSONObject resultJson = JSONObject.parseObject(refundResult);
JSONObject responseJson = resultJson.getJSONObject("alipay_trade_refund_response");
if (responseJson == null
|| !"10000".equals(responseJson.getString("code"))
|| !"Y".equals(responseJson.getString("fund_change"))) {
return R.error("支付宝退款申请失败");
}else{
buyOrderRefundLogService.insertRefundLog(refundId,3,2);
} }
wxpayService.refund(weChatRefundInfo);
} }
} }
// else if (Constants.PAYMENT_METHOD_ALI_PAY.equals(buyOrder.getPaymentMethod())) {
// buyOrderRefundLogService.insertRefundLog(refundId,2,0);
// if (refundFee.compareTo(BigDecimal.ZERO) > 0) {
// Map<String, Object> refundParams = new HashMap<>();
// refundParams.put("orderSn", buyOrder.getOrderSn());
// refundParams.put("refundFee", refundFee);
// String refundResult = aliPayService.refund(refundParams);
// JSONObject resultJson = JSONObject.parseObject(refundResult);
// JSONObject responseJson = resultJson.getJSONObject("alipay_trade_refund_response");
// if (responseJson == null
// || !"10000".equals(responseJson.getString("code"))
// || !"Y".equals(responseJson.getString("fund_change"))) {
// return R.error("支付宝退款申请失败");
// }else{
// buyOrderRefundLogService.insertRefundLog(refundId,3,2);
// }
// }
// }
return R.ok("ok"); return R.ok("ok");
} }

View File

@@ -74,6 +74,4 @@ public interface BuyOrderService extends IService<BuyOrder> {
BigDecimal getRefundFee(Map<String, Object> params, BuyOrder buyOrder); BigDecimal getRefundFee(Map<String, Object> params, BuyOrder buyOrder);
void refundOrder(BuyOrder buyOrder, MyUserEntity user, int refundId); void refundOrder(BuyOrder buyOrder, MyUserEntity user, int refundId);
void refundOrder(BuyOrder buyOrder);
} }

View File

@@ -220,6 +220,8 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
} else if (type.equals("2")) { } else if (type.equals("2")) {
orderEntity.setPaymentDate(new Date()); orderEntity.setPaymentDate(new Date());
orderEntity.setOrderStatus("3"); orderEntity.setOrderStatus("3");
}else if (type.equals("6")) {
orderEntity.setOrderStatus("6");
} }
updateById(orderEntity); updateById(orderEntity);
} }
@@ -475,12 +477,10 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
} }
} }
if(b.getOrderStatus().equals(Constants.ORDER_STATUS_REFUND)){ if(b.getOrderStatus().equals(Constants.ORDER_STATUS_REFUND)){
QueryWrapper<BuyOrderRefund> wrapperRefund = new QueryWrapper<>(); BuyOrderRefund refundInfo = buyOrderRefundService.getRefundInfoByOrderId(b.getOrderId());
wrapperRefund.eq("order_id",b.getOrderId());
BuyOrderRefund refundInfo = buyOrderRefundService.getOne(wrapperRefund);
b.setRefundRemark(refundInfo.getRemark()); b.setRefundRemark(refundInfo.getRemark());
} }
b.setRefundableStatus(b.getOrderStatus().equals("6")?false:refundableStatus); b.setRefundableStatus(b.getOrderStatus().equals("6")|| b.getOrderStatus().equals("7")||Constants.PAYMENT_METHOD_ALI_PAY.equals(b.getPaymentMethod())?false:refundableStatus);
b.setProductList(buyOrderProducts); b.setProductList(buyOrderProducts);
//设置快递信息传递给前端 //设置快递信息传递给前端
ConsigneeVo consigneeVo = new ConsigneeVo(); ConsigneeVo consigneeVo = new ConsigneeVo();
@@ -536,7 +536,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
wrapper.eq(BuyOrder::getUserId,userOrderDto.getUserId()); wrapper.eq(BuyOrder::getUserId,userOrderDto.getUserId());
// wrapper.eq(BuyOrder::getOrderType,"order");//这里有点问题 // wrapper.eq(BuyOrder::getOrderType,"order");//这里有点问题
if(userOrderDto.getOrderStatus()==null){ if(userOrderDto.getOrderStatus()==null){
Integer[] sts = {0,1,2,3,6}; Integer[] sts = {0,1,2,3,6,7};
wrapper.in(BuyOrder::getOrderStatus,sts); wrapper.in(BuyOrder::getOrderStatus,sts);
}else{ }else{
wrapper.eq(BuyOrder::getOrderStatus,userOrderDto.getOrderStatus()); wrapper.eq(BuyOrder::getOrderStatus,userOrderDto.getOrderStatus());
@@ -585,7 +585,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
} }
} }
} }
b.setRefundableStatus(b.getOrderStatus().equals("6")?false:refundableStatus); b.setRefundableStatus(b.getOrderStatus().equals("6") || b.getOrderStatus().equals("7") || Constants.PAYMENT_METHOD_ALI_PAY.equals(b.getPaymentMethod())?false:refundableStatus);
b.setProductList(buyOrderProducts); b.setProductList(buyOrderProducts);
b.setTimestamp(b.getCreateTime().getTime()/1000); b.setTimestamp(b.getCreateTime().getTime()/1000);
//充值订单填充充值商品信息 //充值订单填充充值商品信息
@@ -1089,40 +1089,5 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
//回滚库存 //回滚库存
shopProductService.rollbackStock(buyOrder); shopProductService.rollbackStock(buyOrder);
} }
}
@Override
public void refundOrder(BuyOrder buyOrder){
if(buyOrder.getCouponId()!=null && buyOrder.getCouponId()!=0){
couponService.rollbackCoupon(buyOrder.getCouponId());
}
QueryWrapper<BuyOrderProduct> productQueryWrapper = new QueryWrapper<>();
productQueryWrapper.eq("order_id",buyOrder.getOrderId());
List<BuyOrderProduct> orderProducts = buyOrderProductService.list(productQueryWrapper);
//积分恢复
BigDecimal jf = buyOrder.getJfDeduction()==null?BigDecimal.ZERO : buyOrder.getJfDeduction();
MyUserEntity user = myUserService.getById(buyOrder.getUserId());
if(jf.compareTo(BigDecimal.ZERO)>0){
jfTransactionDetailsService.refundJfTransaction(buyOrder,user);
user.setJf(jf.add(user.getJf()));
myUserService.updateById(user);
buyOrderRefundLogService.insertRefundLog(refundId,2,1);
}
if(buyOrder.getOrderType().equals("vip")){
userVipService.refundVip(buyOrder);
}else{
//撤回本订单购买赠送的优惠券
couponService.refundZSCouponHistoryByOrder(buyOrder);
/*//恢复用户权益(点穴/时辰取穴/五运六气/肿瘤古方)
myUserService.rollbackUserPowers(buyOrder, orderProducts);*/
//撤回电子书权限
userEbookBuyService.rollbackUserEbooks(buyOrder, orderProducts);
//撤回课程权限
removeCourseToUser(buyOrder);
//回滚库存
shopProductService.rollbackStock(buyOrder);
}
} }
} }

View File

@@ -32,4 +32,5 @@ public class BuyOrderRefund implements Serializable {
private int status; private int status;
private String remark; private String remark;
private Date createTime; private Date createTime;
private String wxRefundNo;
} }

View File

@@ -27,7 +27,7 @@ public class BuyOrderRefundLog implements Serializable {
private Date createTime; private Date createTime;
private int type; private int type;
private int status; private int status;
private int reason;
@TableField(exist = false) @TableField(exist = false)
private String title; private String title;

View File

@@ -6,4 +6,5 @@ import com.peanut.modules.common.entity.BuyOrderRefundLog;
public interface BuyOrderRefundLogService extends IService<BuyOrderRefundLog> { public interface BuyOrderRefundLogService extends IService<BuyOrderRefundLog> {
void insertRefundLog(int buyOrderRefundId, int type, int status); void insertRefundLog(int buyOrderRefundId, int type, int status);
void insertRefundLog(int buyOrderRefundId, int type, int status, String reason);
} }

View File

@@ -9,4 +9,6 @@ import java.math.BigDecimal;
public interface BuyOrderRefundService extends IService<BuyOrderRefund> { public interface BuyOrderRefundService extends IService<BuyOrderRefund> {
int insertBuyOrderRefund(BuyOrder buyOrder, BigDecimal refundFee, int deductShipping,String remark); int insertBuyOrderRefund(BuyOrder buyOrder, BigDecimal refundFee, int deductShipping,String remark);
String genRefundNo(); String genRefundNo();
BuyOrderRefund getRefundInfoByOrderId(int orderId);
} }

View File

@@ -2,6 +2,7 @@ package com.peanut.modules.common.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.common.dao.BuyOrderRefundLogDao; import com.peanut.modules.common.dao.BuyOrderRefundLogDao;
import com.peanut.modules.common.entity.BuyOrder;
import com.peanut.modules.common.entity.BuyOrderRefund; import com.peanut.modules.common.entity.BuyOrderRefund;
import com.peanut.modules.common.entity.BuyOrderRefundLog; import com.peanut.modules.common.entity.BuyOrderRefundLog;
import com.peanut.modules.common.service.BuyOrderRefundLogService; import com.peanut.modules.common.service.BuyOrderRefundLogService;
@@ -19,4 +20,12 @@ public class BuyOrderRefundLogServiceImpl extends ServiceImpl<BuyOrderRefundLogD
log.setStatus(status); log.setStatus(status);
this.save(log); this.save(log);
} }
@Override
public void insertRefundLog(int refundId, int type, int status, String reason){
BuyOrderRefundLog log = new BuyOrderRefundLog();
log.setRefundId(refundId);
log.setType(type);
log.setStatus(status);
this.save(log);
}
} }

View File

@@ -1,5 +1,6 @@
package com.peanut.modules.common.service.impl; package com.peanut.modules.common.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peanut.modules.common.dao.BuyOrderRefundDao; import com.peanut.modules.common.dao.BuyOrderRefundDao;
import com.peanut.modules.common.entity.BuyOrder; import com.peanut.modules.common.entity.BuyOrder;
@@ -37,4 +38,11 @@ public class BuyOrderRefundServiceImpl extends ServiceImpl<BuyOrderRefundDao, Bu
int random = new Random().nextInt(900) + 100; // 100~999 int random = new Random().nextInt(900) + 100; // 100~999
return "RF" + time + random; return "RF" + time + random;
} }
@Override
public BuyOrderRefund getRefundInfoByOrderId(int orderId){
QueryWrapper<BuyOrderRefund> wrapperRefund = new QueryWrapper<>();
wrapperRefund.eq("order_id",orderId).last("LIMIT 1");
BuyOrderRefund refundInfo = this.getOne(wrapperRefund);
return refundInfo;
}
} }

View File

@@ -7,8 +7,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.common.utils.ExpressOrderUtil; import com.peanut.common.utils.ExpressOrderUtil;
import com.peanut.common.utils.Query; import com.peanut.common.utils.Query;
import com.peanut.config.Constants;
import com.peanut.modules.common.dao.*; import com.peanut.modules.common.dao.*;
import com.peanut.modules.common.entity.*; import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.BuyOrderRefundService;
import com.peanut.modules.common.service.BuyOrderService; import com.peanut.modules.common.service.BuyOrderService;
import com.peanut.modules.common.to.PrepareOrderDto; import com.peanut.modules.common.to.PrepareOrderDto;
import com.peanut.modules.common.vo.UserBaseVo; import com.peanut.modules.common.vo.UserBaseVo;
@@ -44,6 +46,8 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
private AiBuyConfigDao aiBuyConfigDao; private AiBuyConfigDao aiBuyConfigDao;
@Autowired @Autowired
private ShopProductCourseDao shopProductCourseDao; private ShopProductCourseDao shopProductCourseDao;
@Autowired
private BuyOrderRefundService buyOrderRefundService;
@Override @Override
public Map<String, Object> initPrepareOrder(PrepareOrderDto prepareOrderDto) { public Map<String, Object> initPrepareOrder(PrepareOrderDto prepareOrderDto) {
@@ -75,7 +79,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
wrapper.eq(BuyOrder::getUserId,params.get("userId")); wrapper.eq(BuyOrder::getUserId,params.get("userId"));
wrapper.eq(BuyOrder::getCome,params.get("come")); wrapper.eq(BuyOrder::getCome,params.get("come"));
if(StringUtils.isEmpty(params.get("orderStatus").toString())){ if(StringUtils.isEmpty(params.get("orderStatus").toString())){
Integer[] sts = {0,1,2,3,6}; Integer[] sts = {0,1,2,3,6,7};
wrapper.in(BuyOrder::getOrderStatus,sts); wrapper.in(BuyOrder::getOrderStatus,sts);
}else{ }else{
wrapper.eq(BuyOrder::getOrderStatus,params.get("orderStatus").toString()); wrapper.eq(BuyOrder::getOrderStatus,params.get("orderStatus").toString());
@@ -147,7 +151,7 @@ public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrder> impl
b.setExpressList(expressOrders); b.setExpressList(expressOrders);
} }
} }
b.setRefundableStatus(b.getOrderStatus().equals("6")?false:refundableStatus); b.setRefundableStatus(b.getOrderStatus().equals("6") || b.getOrderStatus().equals("7")|| Constants.PAYMENT_METHOD_ALI_PAY.equals(b.getPaymentMethod())?false:refundableStatus);
} }
} }
return page; return page;

View File

@@ -126,6 +126,7 @@ public class WeChatPayController {
*/ */
@RequestMapping("/refund" ) @RequestMapping("/refund" )
public R refund(@RequestBody Map<String,Object> map){ public R refund(@RequestBody Map<String,Object> map){
return R.ok(wxpayService.refund(map)); return R.ok(wxpayService.refund(map));
} }
@@ -135,11 +136,13 @@ public class WeChatPayController {
*/ */
@PostMapping("/refundNotify") @PostMapping("/refundNotify")
public void refundNotify(HttpServletRequest request){ public void refundNotify(HttpServletRequest request){
wxpayService.refundNotify(request); wxpayService.refundNotify(request);
} }
@PostMapping("/lsRefundNotify") @PostMapping("/lsRefundNotify")
public void lsRefundNotify(HttpServletRequest request){ public void lsRefundNotify(HttpServletRequest request){
wxpayService.lsRefundNotify(request); wxpayService.lsRefundNotify(request);
} }

View File

@@ -0,0 +1,21 @@
package com.peanut.modules.pay.weChatPay.dto;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class WeChatRefundInfo implements Serializable {
private String orderSn;
private Integer orderId;
private String orderType;
private String refundNo;
private BigDecimal totalAmount;
private BigDecimal refundAmount;
private String remark;
private String appName;
private String currency;
private String notify_url;
private int refundId;
}

View File

@@ -2,6 +2,7 @@ package com.peanut.modules.pay.weChatPay.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.peanut.modules.common.entity.PayWechatOrderEntity; import com.peanut.modules.common.entity.PayWechatOrderEntity;
import com.peanut.modules.pay.weChatPay.dto.WeChatRefundInfo;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo; import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -19,6 +20,8 @@ public interface WxpayService extends IService<PayWechatOrderEntity> {
String refund(Map<String,Object> map); String refund(Map<String,Object> map);
void refund(WeChatRefundInfo refundInfo);
void refundNotify(HttpServletRequest request); void refundNotify(HttpServletRequest request);
void lsRefundNotify(HttpServletRequest request); void lsRefundNotify(HttpServletRequest request);

View File

@@ -8,7 +8,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.common.utils.ShiroUtils; import com.peanut.common.utils.ShiroUtils;
import com.peanut.config.Constants;
import com.peanut.modules.book.service.BookBuyConfigService; import com.peanut.modules.book.service.BookBuyConfigService;
import com.peanut.modules.book.service.BuyOrderService; import com.peanut.modules.book.service.BuyOrderService;
import com.peanut.modules.book.service.MyUserService; import com.peanut.modules.book.service.MyUserService;
@@ -21,11 +20,13 @@ import com.peanut.modules.master.service.UserCourseBuyService;
import com.peanut.modules.pay.refund.entity.PayRefundOrder; import com.peanut.modules.pay.refund.entity.PayRefundOrder;
import com.peanut.modules.pay.refund.service.PayRefundOrderService; import com.peanut.modules.pay.refund.service.PayRefundOrderService;
import com.peanut.modules.pay.weChatPay.config.WechatPayConfig; import com.peanut.modules.pay.weChatPay.config.WechatPayConfig;
import com.peanut.modules.pay.weChatPay.dto.WeChatRefundInfo;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo; import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.service.WxpayService; import com.peanut.modules.pay.weChatPay.service.WxpayService;
import com.peanut.modules.pay.weChatPay.util.HttpUtils; import com.peanut.modules.pay.weChatPay.util.HttpUtils;
import com.peanut.modules.pay.weChatPay.util.WechatPayValidator; import com.peanut.modules.pay.weChatPay.util.WechatPayValidator;
import com.peanut.modules.pay.weChatPay.util.WxPayUtil; import com.peanut.modules.pay.weChatPay.util.WxPayUtil;
import com.qiniu.util.Json;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -79,10 +80,6 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
@Autowired @Autowired
private JfTransactionDetailsDao jfTransactionDetailsDao; private JfTransactionDetailsDao jfTransactionDetailsDao;
@Autowired @Autowired
private BuyOrderRefundService buyOrderRefundService;
@Autowired
private BuyOrderRefundLogService buyOrderRefundLogService;
@Autowired
private UserVipService userVipService; private UserVipService userVipService;
@Autowired @Autowired
private CouponService couponService; private CouponService couponService;
@@ -94,6 +91,12 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
private AiVipLogService aiVipLogService; private AiVipLogService aiVipLogService;
@Autowired @Autowired
private UserCourseBuyLogDao userCourseBuyLogDao; private UserCourseBuyLogDao userCourseBuyLogDao;
@Autowired
private BuyOrderRefundLogService buyOrderRefundLogService;
@Autowired
private MyUserService myUserService;
@Autowired
private BuyOrderRefundService buyOrderRefundService;
@Override @Override
public void prepay(WechatPaymentInfo paymentInfo){ public void prepay(WechatPaymentInfo paymentInfo){
@@ -141,6 +144,7 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
}else { }else {
responseJson = wxPayUtil.doPostWexinV3(wechatPayConfig.getPayUrl(), json.toJSONString(),""); responseJson = wxPayUtil.doPostWexinV3(wechatPayConfig.getPayUrl(), json.toJSONString(),"");
} }
System.out.println(responseJson);
String prepayId = responseJson.getString("prepay_id"); String prepayId = responseJson.getString("prepay_id");
if (paymentInfo.getBuyOrderId() == null) { if (paymentInfo.getBuyOrderId() == null) {
payWechatOrderService.add(paymentInfo.getOrderSn(), prepayId); payWechatOrderService.add(paymentInfo.getOrderSn(), prepayId);
@@ -355,6 +359,71 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
return responseJson.toJSONString(); return responseJson.toJSONString();
} }
@Override
public void refund(WeChatRefundInfo refundInfo) {
Map<String, Object> paramMap = new HashMap<>();
BigDecimal refund = new BigDecimal(refundInfo.getRefundAmount().toString());
//原订单总金额
BigDecimal total = refundInfo.getTotalAmount();
// 这里 * 100微信支付单位为 ‘分’
BigDecimal hand = new BigDecimal("100");
Map<String, Object> amountMap = new HashMap<>();
amountMap.put("refund", refund.multiply(hand));
amountMap.put("total", total.multiply(hand).intValue());
amountMap.put("currency", "CNY");
paramMap.put("amount", amountMap);
paramMap.put("out_refund_no", refundInfo.getRefundNo());
//微信支付订单号
paramMap.put("out_trade_no", refundInfo.getOrderSn());
//退款原因
paramMap.put("reason", refundInfo.getRemark());
String appid = "";
if (refundInfo.getAppName()==null||"".equals(refundInfo.getAppName())){
appid = wechatPayConfig.getAppId();
}else if ("zmzm".equals(refundInfo.getAppName())){
appid = wechatPayConfig.getZmzmappId();
} else if ("wumen".equals(refundInfo.getAppName())) {
appid = wechatPayConfig.getWumenappId();
} else if ("xlkj".equals(refundInfo.getAppName())) {
appid = wechatPayConfig.getXlkjappId();
} else if ("thyy".equals(refundInfo.getAppName())) {
appid = wechatPayConfig.getThyyappId();
}
// app id
paramMap.put("appid", appid);
String notify_url = wechatPayConfig.getRefundNotifyUrl();
String mchName = "";
if ("trainingClass".equals(refundInfo.getOrderType())||"lsorder".equals(refundInfo.getOrderType())) {
mchName = "LS";
notify_url = wechatPayConfig.getLsRefundNotifyUrl();
}
paramMap.put("notify_url", notify_url);
JSONObject json = JSONObject.parseObject(JSON.toJSONString(paramMap));
log.info("=======微信退款申请请求参数:{}", json.toJSONString());
log.info(">>>>>>>>>>App请求微信退款申请接口");
JSONObject responseJson = wxPayUtil.doPostWexinV3(wechatPayConfig.getRefundUrl(), json.toJSONString(),mchName);
// JSONObject responseJson = new JSONObject();
// responseJson.put("status","SUCCESS");
// responseJson.put("refund_id",System.currentTimeMillis());
System.out.println(responseJson);
log.info(">>>>>>>>>>>微信退款返回的信息是 resJson = {}", responseJson.toJSONString());
if ("SUCCESS".equals(responseJson.get("status"))){
buyOrderRefundLogService.insertRefundLog(refundInfo.getRefundId(),1,2);
BuyOrderRefund buyOrderRefund = buyOrderRefundService.getById(refundInfo.getRefundId());
buyOrderRefund.setWxRefundNo(responseJson.getString("refund_id"));
buyOrderRefundService.updateById(buyOrderRefund);
}else{
buyOrderRefundLogService.insertRefundLog(refundInfo.getRefundId(),1,3);
}
// return responseJson.toJSONString();
}
private void userCoinJf(BuyOrder order){ private void userCoinJf(BuyOrder order){
MyUserEntity userEntity = userService.getById(order.getUserId()); MyUserEntity userEntity = userService.getById(order.getUserId());
userEntity.setJf(userEntity.getJf().subtract(order.getJfDeduction())); userEntity.setJf(userEntity.getJf().subtract(order.getJfDeduction()));
@@ -378,6 +447,18 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
// 解密resource中的通知数据 // 解密resource中的通知数据
String resource = bodyMap.get("resource").toString(); String resource = bodyMap.get("resource").toString();
Map<String, Object> resourceMap = WechatPayValidator.decryptFromResource(resource, wechatPayConfig.getApiV3Key(), 2); Map<String, Object> resourceMap = WechatPayValidator.decryptFromResource(resource, wechatPayConfig.getApiV3Key(), 2);
/*BuyOrder orderFalse = buyOrderService.getOne(new QueryWrapper<BuyOrder>().eq("user_id",149299).orderByDesc("order_id"));
BuyOrderRefund refundFalse = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("user_id",149299).orderByDesc("id"));
Map<String, Object> resourceMap = new HashMap<>();
resourceMap.put("refund_status","SUCCESS");
resourceMap.put("out_trade_no",orderFalse.getOrderSn());
resourceMap.put("refund_id",System.currentTimeMillis());
resourceMap.put("out_refund_no",refundFalse.getRefundNo());
Map<String,BigDecimal> amount = new HashMap<>();
amount.put("refund",orderFalse.getRealMoney());
resourceMap.put("amount",amount);*/
log.info("微信退款回调结果 msg={}",resourceMap); log.info("微信退款回调结果 msg={}",resourceMap);
if ("SUCCESS".equals(resourceMap.get("refund_status").toString())){ if ("SUCCESS".equals(resourceMap.get("refund_status").toString())){
log.info(">>>>>>>>>>>微信退款成功!<<<<<<<<<<<<<"); log.info(">>>>>>>>>>>微信退款成功!<<<<<<<<<<<<<");
@@ -385,15 +466,40 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
w.selectAll(BuyOrder.class); w.selectAll(BuyOrder.class);
w.leftJoin(PayWechatOrderEntity.class,PayWechatOrderEntity::getOrderSn, BuyOrder::getOrderSn); w.leftJoin(PayWechatOrderEntity.class,PayWechatOrderEntity::getOrderSn, BuyOrder::getOrderSn);
w.eq("t1.order_id",resourceMap.get("transaction_id").toString()); w.eq("t1.order_id",resourceMap.get("transaction_id").toString());
BuyOrder order = buyOrderService.getOne(w); BuyOrder order = buyOrderService.getOne(w);
BuyOrderRefund buyOrderRefund = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("order_id",order.getOrderId()));
buyOrderRefund.setStatus(1);
buyOrderRefundService.updateById(buyOrderRefund);
buyOrderRefundLogService.insertRefundLog(buyOrderRefund.getId(),4,1); PayRefundOrder refund = new PayRefundOrder();
refund.setPayType("1");
refund.setOrderId(order.getOrderId());
refund.setTradeNo(resourceMap.get("transaction_id").toString());
refund.setOutTradeNo(resourceMap.get("out_trade_no").toString());
refund.setRefundFee(((Map)resourceMap.get("amount")).get("refund").toString());
refundOrderService.save(refund);
MyUserEntity user = myUserService.getById(order.getUserId());
BuyOrderRefund refundInfo = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("order_id",order.getOrderId()));
refundInfo.setStatus(1);
refundInfo.setWxRefundNo( resourceMap.get("refund_id").toString());
buyOrderRefundService.updateById(refundInfo);
buyOrderService.refundOrder(order,user,refundInfo.getId());
buyOrderRefundLogService.insertRefundLog(refundInfo.getId(),1,1);
buyOrderService.updateOrderStatus(order.getUserId(),order.getOrderSn(),"6");
// refundOrderService.businessOpt(order);
}else{
if(resourceMap.containsKey("out_refund_no")){
String refundNo = resourceMap.get("out_refund_no").toString();
BuyOrderRefund refundInfo = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("refund_no",refundNo));
refundInfo.setStatus(2);
refundInfo.setWxRefundNo( resourceMap.get("refund_id").toString());
buyOrderRefundService.updateById(refundInfo);
buyOrderRefundLogService.insertRefundLog(refundInfo.getId(),1,3,resourceMap.get("reason").toString());
}
buyOrderService.refundOrder();
} }
} }
@Override @Override
@@ -413,6 +519,7 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
w.leftJoin(PayWechatOrderEntity.class,PayWechatOrderEntity::getOrderSn, BuyOrder::getOrderSn); w.leftJoin(PayWechatOrderEntity.class,PayWechatOrderEntity::getOrderSn, BuyOrder::getOrderSn);
w.eq("t1.order_id",resourceMap.get("transaction_id").toString()); w.eq("t1.order_id",resourceMap.get("transaction_id").toString());
BuyOrder order = buyOrderService.getOne(w); BuyOrder order = buyOrderService.getOne(w);
PayRefundOrder refund = new PayRefundOrder(); PayRefundOrder refund = new PayRefundOrder();
refund.setPayType("1"); refund.setPayType("1");
refund.setOrderId(order.getOrderId()); refund.setOrderId(order.getOrderId());
@@ -420,17 +527,32 @@ public class WxpayServiceImpl extends ServiceImpl<PayWechatOrderDao, PayWechatOr
refund.setOutTradeNo(resourceMap.get("out_trade_no").toString()); refund.setOutTradeNo(resourceMap.get("out_trade_no").toString());
refund.setRefundFee(((Map)resourceMap.get("amount")).get("refund").toString()); refund.setRefundFee(((Map)resourceMap.get("amount")).get("refund").toString());
refundOrderService.save(refund); refundOrderService.save(refund);
if (Constants.ORDER_STATUS_REFUND.equals(order.getOrderStatus())) {
BuyOrderRefund buyOrderRefund = buyOrderRefundService.getOne(new LambdaQueryWrapper<BuyOrderRefund>() //refundOrderService.businessOpt(order);
.eq(BuyOrderRefund::getOrderId, order.getOrderId())
.orderByDesc(BuyOrderRefund::getId) MyUserEntity user = myUserService.getById(order.getUserId());
.last("limit 1"));
if (buyOrderRefund != null) { BuyOrderRefund refundInfo = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("order_id",order.getOrderId()));
buyOrderRefundLogService.insertRefundLog(buyOrderRefund.getId(),4,1); refundInfo.setStatus(1);
} refundInfo.setWxRefundNo( resourceMap.get("refund_id").toString());
} else { buyOrderRefundService.updateById(refundInfo);
refundOrderService.businessOpt(order);
buyOrderService.refundOrder(order,user,refundInfo.getId());
buyOrderRefundLogService.insertRefundLog(refundInfo.getId(),1,1);
buyOrderService.updateOrderStatus(order.getUserId(),order.getOrderSn(),"6");
}else{
if(resourceMap.containsKey("out_refund_no")){
String refundNo = resourceMap.get("out_refund_no").toString();
BuyOrderRefund refundInfo = buyOrderRefundService.getOne(new QueryWrapper<BuyOrderRefund>().eq("refund_no",refundNo));
refundInfo.setStatus(2);
refundInfo.setWxRefundNo( resourceMap.get("refund_id").toString());
buyOrderRefundService.updateById(refundInfo);
buyOrderRefundLogService.insertRefundLog(refundInfo.getId(),1,3,resourceMap.get("reason").toString());
} }
} }
} }