mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
pay:示例订单,增加发起退款的操作
This commit is contained in:
@ -11,6 +11,8 @@ import cn.iocoder.yudao.module.pay.convert.demo.PayDemoOrderConvert;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoOrderDO;
|
||||
import cn.iocoder.yudao.module.pay.service.demo.PayDemoOrderService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -20,6 +22,7 @@ import javax.annotation.security.PermitAll;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
|
||||
@Tag(name = "管理后台 - 示例订单")
|
||||
@ -54,4 +57,12 @@ public class PayDemoOrderController {
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@PutMapping("/refund")
|
||||
@Operation(description = "退款示例订单")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
public CommonResult<Boolean> refundDemoOrder(@RequestParam("id") Long id) {
|
||||
payDemoOrderService.refundDemoOrder(id, getClientIP());
|
||||
return success(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -36,6 +36,12 @@ public class PayDemoOrderRespVO {
|
||||
@Schema(description = "订单支付时间")
|
||||
private LocalDateTime payTime;
|
||||
|
||||
@Schema(description = "支付渠道", example = "alipay_qr")
|
||||
private String payChannelCode;
|
||||
|
||||
@Schema(description = "支付退款编号", example = "23366")
|
||||
private Long payRefundId;
|
||||
|
||||
@Schema(description = "退款金额,单位:分", required = true, example = "14039")
|
||||
private Integer refundPrice;
|
||||
|
||||
|
@ -73,13 +73,15 @@ public class PayDemoOrderDO extends BaseDO {
|
||||
|
||||
// ========== 退款相关字段 ==========
|
||||
/**
|
||||
* 退款金额
|
||||
* 支付退款单号
|
||||
*/
|
||||
private Long payRefundId;
|
||||
/**
|
||||
* 退款金额,单位:分
|
||||
*/
|
||||
private Integer refundPrice;
|
||||
/**
|
||||
* 退款时间
|
||||
*
|
||||
* 由于可以多次退款,记录最后一次退款的时间
|
||||
* 退款完成时间
|
||||
*/
|
||||
private Date refundTime;
|
||||
|
||||
|
@ -47,4 +47,12 @@ public interface PayDemoOrderService {
|
||||
*/
|
||||
void updateDemoOrderPaid(Long id, Long payOrderId);
|
||||
|
||||
/**
|
||||
* 退款示例订单
|
||||
*
|
||||
* @param id 编号
|
||||
* @param userIp 用户编号
|
||||
*/
|
||||
void refundDemoOrder(Long id, String userIp);
|
||||
|
||||
}
|
||||
|
@ -2,16 +2,16 @@ package cn.iocoder.yudao.module.pay.service.demo;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
|
||||
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderRespDTO;
|
||||
import cn.iocoder.yudao.module.pay.api.refund.PayRefundApi;
|
||||
import cn.iocoder.yudao.module.pay.api.refund.dto.PayRefundCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.PayDemoOrderCreateReqVO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoOrderDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoOrderMapper;
|
||||
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -56,6 +56,8 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService {
|
||||
|
||||
@Resource
|
||||
private PayOrderApi payOrderApi;
|
||||
@Resource
|
||||
private PayRefundApi payRefundApi;
|
||||
|
||||
@Resource
|
||||
private PayDemoOrderMapper payDemoOrderMapper;
|
||||
@ -180,4 +182,36 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService {
|
||||
return payOrder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refundDemoOrder(Long id, String userIp) {
|
||||
// 1. 校验订单是否可以退款
|
||||
PayDemoOrderDO order = validateDemoOrderCanRefund(id);
|
||||
|
||||
// 2.1 创建退款单
|
||||
Long payRefundId = payRefundApi.createPayRefund(new PayRefundCreateReqDTO()
|
||||
.setAppId(PAY_APP_ID).setUserIp(getClientIP()) // 支付应用
|
||||
.setMerchantOrderId(order.getId().toString()) // 业务的订单编号
|
||||
.setReason("想退钱").setAmount(order.getPrice())); // 价格信息
|
||||
// 2.2 更新支付单到 demo 订单
|
||||
payDemoOrderMapper.updateById(new PayDemoOrderDO().setId(id)
|
||||
.setPayRefundId(payRefundId).setRefundPrice(order.getPrice()));
|
||||
}
|
||||
|
||||
private PayDemoOrderDO validateDemoOrderCanRefund(Long id) {
|
||||
// 校验订单是否存在
|
||||
PayDemoOrderDO order = payDemoOrderMapper.selectById(id);
|
||||
if (order == null) {
|
||||
throw exception(PAY_DEMO_ORDER_NOT_FOUND);
|
||||
}
|
||||
// 校验订单是否支付
|
||||
if (!order.getPayed()) {
|
||||
throw exception(PAY_DEMO_ORDER_REFUND_FAIL_NOT_PAID);
|
||||
}
|
||||
// 校验是否已经发起退款
|
||||
if (order.getPayRefundId() != null) {
|
||||
throw exception(PAY_DEMO_ORDER_REFUND_FAIL_REFUNDED);
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user