This commit is contained in:
wyn
2026-05-07 08:00:15 +08:00
parent f47bca3a8b
commit e1eecb5cdc
2 changed files with 92 additions and 0 deletions

View File

@@ -1,7 +1,17 @@
package com.peanut.modules.pay.alipay.controller; package com.peanut.modules.pay.alipay.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peanut.common.utils.R; import com.peanut.common.utils.R;
import com.peanut.config.Constants;
import com.peanut.modules.book.service.BuyOrderService;
import com.peanut.modules.book.service.MyUserService;
import com.peanut.modules.common.entity.BuyOrder;
import com.peanut.modules.common.entity.BuyOrderRefund;
import com.peanut.modules.common.entity.BuyOrderRefundLog;
import com.peanut.modules.common.entity.MyUserEntity;
import com.peanut.modules.common.service.BuyOrderRefundLogService;
import com.peanut.modules.common.service.BuyOrderRefundService;
import com.peanut.modules.pay.alipay.dto.AlipayDTO; import com.peanut.modules.pay.alipay.dto.AlipayDTO;
import com.peanut.modules.pay.alipay.dto.ReFundDTO; import com.peanut.modules.pay.alipay.dto.ReFundDTO;
import com.peanut.modules.pay.alipay.service.AliPayService; import com.peanut.modules.pay.alipay.service.AliPayService;
@@ -11,6 +21,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map; import java.util.Map;
@@ -24,6 +36,14 @@ public class AliPayController {
@Autowired @Autowired
private AliPayService aliPayService; private AliPayService aliPayService;
@Autowired
private BuyOrderService buyOrderService;
@Autowired
private MyUserService myUserService;
@Autowired
private BuyOrderRefundService buyOrderRefundService;
@Autowired
private BuyOrderRefundLogService buyOrderRefundLogService;
/** /**
* 请求支付宝接口支付 * 请求支付宝接口支付
@@ -50,6 +70,65 @@ public class AliPayController {
*/ */
@RequestMapping("/refund") @RequestMapping("/refund")
public R refund(@RequestBody Map<String,Object> params) { public R refund(@RequestBody Map<String,Object> params) {
Object orderIdObject = params.get("orderId");
if(orderIdObject == null || orderIdObject.toString().trim().equals("")){
return R.error("orderId不能为空");
}
int orderId = Integer.parseInt(orderIdObject.toString());
QueryWrapper<BuyOrder> queryWapper = new QueryWrapper<>();
queryWapper.eq("order_id",orderId);
BuyOrder buyOrder = buyOrderService.getOne(queryWapper);
if(buyOrder==null){
return R.error("订单不存在");
}
MyUserEntity user = myUserService.getById(buyOrder.getUserId());
if (user == null) {
return R.error("订单对应用户不存在");
}
if(Constants.ORDER_STATUS_REFUND.equals(buyOrder.getOrderStatus())
|| Constants.ORDER_STATUS_REFUNDING.equals(buyOrder.getOrderStatus())){
return R.error("请勿重复提交退款");
}else if (Constants.ORDER_STATUS_TO_BE_PAID.equals(buyOrder.getOrderStatus())
|| Constants.ORDER_STATUS_FAIL.equals(buyOrder.getOrderStatus())
|| Constants.ORDER_STATUS_OUT_OF_TIME.equals(buyOrder.getOrderStatus())) {
return R.error("当前订单状态不支持退单");
}
buyOrder.setOrderStatus(Constants.ORDER_STATUS_REFUNDING);
buyOrderService.updateById(buyOrder);
BigDecimal refundFee = buyOrder.getRealMoney();
BigDecimal shippingMoney = buyOrder.getShippingMoney()==null?BigDecimal.ZERO:buyOrder.getShippingMoney();
int deductShipping = params.containsKey("deductShipping") && params.get("deductShipping")!=null?Integer.parseInt(params.get("deductShipping").toString()):0;
if(refundFee.compareTo(BigDecimal.ZERO)>0 && shippingMoney.compareTo(BigDecimal.ZERO)>0 && deductShipping==1){
refundFee = refundFee.subtract(shippingMoney);
refundFee = refundFee.compareTo(BigDecimal.ZERO)>0?refundFee:BigDecimal.ZERO;
}
String remark = params.containsKey("remark") && params.get("remark").toString()!=null?params.get("remark").toString():"";
BuyOrderRefund buyOrderRefund = new BuyOrderRefund();
String refundNo = buyOrderRefundService.genRefundNo();
buyOrderRefund.setRefundNo(refundNo);
buyOrderRefund.setOrderId(buyOrder.getOrderId());
buyOrderRefund.setOrderSn(buyOrder.getOrderSn());
buyOrderRefund.setUserId(buyOrder.getUserId());
buyOrderRefund.setType(params.containsKey("deductShipping")?"后台":"线上"); //id
buyOrderRefund.setPayType(buyOrder.getPaymentMethod());
buyOrderRefund.setFee(refundFee);
buyOrderRefund.setJfDeduction(buyOrder.getJfDeduction());
buyOrderRefund.setShippingMoney(deductShipping==1?buyOrder.getShippingMoney():BigDecimal.ZERO);
buyOrderRefund.setDeductShipping(deductShipping);
buyOrderRefund.setRemark(remark);
buyOrderRefundService.save(buyOrderRefund);
buyOrderRefundLogService.insertRefundLog(buyOrderRefund.getId(),0,1);
int refundId = buyOrderRefund.getId();
buyOrderRefundLogService.insertRefundLog(refundId,1,0);
String refund = aliPayService.refund(params); String refund = aliPayService.refund(params);
return R.ok().put("msg",refund); return R.ok().put("msg",refund);
} }

View File

@@ -159,6 +159,17 @@ public class AliPayServiceImpl implements AliPayService {
// log.error(">>>>>>>>>>验签结果 flag = {}", flag); // log.error(">>>>>>>>>>验签结果 flag = {}", flag);
if (flag) { if (flag) {
log.info(">>>>>>>>>>验签通过"); log.info(">>>>>>>>>>验签通过");
String refundStatus = params.get("refund_status");
if(refundStatus!=null){
String outTradeNo = params.get("out_trade_no");
if ("REFUND_SUCCESS".equals(refundStatus)) {
String outBizNo = params.get("out_biz_no"); // 退款单号
String refundAmount = params.get("refund_amount");// 退款金额
// 业务:更新退款流水、订单退款状态、幂等判断
log.info("退款回调:订单=" + outTradeNo + ",退款单号=" + outBizNo);
}
}
//验签通过 获取交易状态 //验签通过 获取交易状态
String tradeStatus = params.get("trade_status"); String tradeStatus = params.get("trade_status");
//只处理支付成功的订单: 修改交易表状态,支付成功 //只处理支付成功的订单: 修改交易表状态,支付成功
@@ -336,6 +347,7 @@ public class AliPayServiceImpl implements AliPayService {
LambdaQueryWrapper<PayZfbOrderEntity> wrapper = new LambdaQueryWrapper(); LambdaQueryWrapper<PayZfbOrderEntity> wrapper = new LambdaQueryWrapper();
wrapper.eq(PayZfbOrderEntity::getRelevanceoid,params.get("orderSn").toString()); wrapper.eq(PayZfbOrderEntity::getRelevanceoid,params.get("orderSn").toString());
PayZfbOrderEntity payZfbOrder = payZfbOrderService.getOne(wrapper); PayZfbOrderEntity payZfbOrder = payZfbOrderService.getOne(wrapper);
ReFundDTO reFundDTO = new ReFundDTO(); ReFundDTO reFundDTO = new ReFundDTO();
reFundDTO.setOutTrandeNo(payZfbOrder.getOutTradeNo()); reFundDTO.setOutTrandeNo(payZfbOrder.getOutTradeNo());
reFundDTO.setTradeNo(payZfbOrder.getTradeNo()); reFundDTO.setTradeNo(payZfbOrder.getTradeNo());
@@ -344,6 +356,7 @@ public class AliPayServiceImpl implements AliPayService {
reFundDTO.setRefundAmount(new BigDecimal(params.get("refundFee").toString())); reFundDTO.setRefundAmount(new BigDecimal(params.get("refundFee").toString()));
// reFundDTO.setOutRequestNo(params.get("outRequestNo").toString()); // reFundDTO.setOutRequestNo(params.get("outRequestNo").toString());
reFundDTO.setOutRequestNo(UUID.randomUUID().toString()); reFundDTO.setOutRequestNo(UUID.randomUUID().toString());
BuyOrder buyOrder = buyOrderService.getOne(new LambdaQueryWrapper<BuyOrder>() BuyOrder buyOrder = buyOrderService.getOne(new LambdaQueryWrapper<BuyOrder>()
.eq(BuyOrder::getOrderSn,params.get("orderSn"))); .eq(BuyOrder::getOrderSn,params.get("orderSn")));
String mchName = ""; String mchName = "";