mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 20:28:44 +08:00 
			
		
		
		
	mall + pay:
1. 优化订单管理的接口实现
This commit is contained in:
		@@ -56,31 +56,20 @@ public class PayOrderController {
 | 
			
		||||
        return success(PayOrderConvert.INSTANCE.convert(payOrderService.getOrder(id)));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO 芋艿:看看怎么优化下;
 | 
			
		||||
    @GetMapping("/get-detail")
 | 
			
		||||
    @Operation(summary = "获得支付订单详情")
 | 
			
		||||
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
 | 
			
		||||
    @PreAuthorize("@ss.hasPermission('pay:order:query')")
 | 
			
		||||
    public CommonResult<PayOrderDetailsRespVO> getOrderDetail(@RequestParam("id") Long id) {
 | 
			
		||||
        PayOrderDO order = payOrderService.getOrder(id);
 | 
			
		||||
        if (ObjectUtil.isNull(order)) {
 | 
			
		||||
            return success(new PayOrderDetailsRespVO());
 | 
			
		||||
        if (order == null) {
 | 
			
		||||
            return success(null);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        PayAppDO appDO = appService.getApp(order.getAppId());
 | 
			
		||||
        PayChannelEnum channelEnum = PayChannelEnum.getByCode(order.getChannelCode());
 | 
			
		||||
 | 
			
		||||
        // TODO @aquan:文案,都是前端 format;
 | 
			
		||||
        PayOrderDetailsRespVO respVO = PayOrderConvert.INSTANCE.orderDetailConvert(order);
 | 
			
		||||
        respVO.setAppName(ObjectUtil.isNotNull(appDO) ? appDO.getName() : "未知应用");
 | 
			
		||||
        respVO.setChannelCodeName(ObjectUtil.isNotNull(channelEnum) ? channelEnum.getName() : "未知渠道");
 | 
			
		||||
 | 
			
		||||
        PayOrderExtensionDO extensionDO = orderExtensionService.getOrderExtension(order.getSuccessExtensionId());
 | 
			
		||||
        if (ObjectUtil.isNotNull(extensionDO)) {
 | 
			
		||||
            respVO.setPayOrderExtension(PayOrderConvert.INSTANCE.orderDetailExtensionConvert(extensionDO));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return success(respVO);
 | 
			
		||||
        // 拼接返回
 | 
			
		||||
        PayAppDO app = appService.getApp(order.getAppId());
 | 
			
		||||
        PayOrderExtensionDO orderExtension = orderExtensionService.getOrderExtension(order.getSuccessExtensionId());
 | 
			
		||||
        return success(PayOrderConvert.INSTANCE.convert(order, orderExtension, app));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/submit")
 | 
			
		||||
@@ -90,6 +79,7 @@ public class PayOrderController {
 | 
			
		||||
        return success(respVO);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO 芋艿:优化
 | 
			
		||||
    @GetMapping("/page")
 | 
			
		||||
    @Operation(summary = "获得支付订单分页")
 | 
			
		||||
    @PreAuthorize("@ss.hasPermission('pay:order:query')")
 | 
			
		||||
@@ -116,6 +106,7 @@ public class PayOrderController {
 | 
			
		||||
        return success(new PageResult<>(pageList, pageResult.getTotal()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO 芋艿:优化
 | 
			
		||||
    @GetMapping("/export-excel")
 | 
			
		||||
    @Operation(summary = "导出支付订单Excel")
 | 
			
		||||
    @PreAuthorize("@ss.hasPermission('pay:order:export')")
 | 
			
		||||
 
 | 
			
		||||
@@ -13,32 +13,30 @@ import java.time.LocalDateTime;
 | 
			
		||||
@ToString(callSuper = true)
 | 
			
		||||
public class PayOrderDetailsRespVO extends PayOrderBaseVO {
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "支付订单编号")
 | 
			
		||||
    @Schema(description = "支付订单编号", required = true, example = "1024")
 | 
			
		||||
    private Long id;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "应用名称")
 | 
			
		||||
    @Schema(description = "应用名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道源码")
 | 
			
		||||
    private String appName;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "渠道编号名称")
 | 
			
		||||
    private String channelCodeName;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "创建时间")
 | 
			
		||||
    @Schema(description = "创建时间", required = true)
 | 
			
		||||
    private LocalDateTime createTime;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 支付订单扩展
 | 
			
		||||
     */
 | 
			
		||||
    private PayOrderExtension payOrderExtension;
 | 
			
		||||
    private PayOrderExtension extension;
 | 
			
		||||
 | 
			
		||||
    @Data
 | 
			
		||||
    @Schema(description = "支付订单扩展")
 | 
			
		||||
    public static class PayOrderExtension {
 | 
			
		||||
 | 
			
		||||
        @Schema(description = "支付订单号")
 | 
			
		||||
        @Schema(description = "支付订单号", required = true, example = "1024")
 | 
			
		||||
        private String no;
 | 
			
		||||
 | 
			
		||||
        @Schema(description = "支付异步通知的内容")
 | 
			
		||||
        private String channelNotifyData;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ 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.controller.admin.order.vo.*;
 | 
			
		||||
import cn.iocoder.yudao.module.pay.controller.app.order.vo.AppPayOrderSubmitRespVO;
 | 
			
		||||
import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO;
 | 
			
		||||
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
 | 
			
		||||
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
 | 
			
		||||
import org.mapstruct.Mapper;
 | 
			
		||||
@@ -30,9 +31,16 @@ public interface PayOrderConvert {
 | 
			
		||||
 | 
			
		||||
    PayOrderRespDTO convert2(PayOrderDO order);
 | 
			
		||||
 | 
			
		||||
    PayOrderDetailsRespVO orderDetailConvert(PayOrderDO bean);
 | 
			
		||||
 | 
			
		||||
    PayOrderDetailsRespVO.PayOrderExtension orderDetailExtensionConvert(PayOrderExtensionDO bean);
 | 
			
		||||
    default PayOrderDetailsRespVO convert(PayOrderDO order, PayOrderExtensionDO orderExtension, PayAppDO app) {
 | 
			
		||||
        PayOrderDetailsRespVO respVO = convertDetail(order);
 | 
			
		||||
        respVO.setExtension(convert(orderExtension));
 | 
			
		||||
        if (app != null) {
 | 
			
		||||
            respVO.setAppName(app.getName());
 | 
			
		||||
        }
 | 
			
		||||
        return respVO;
 | 
			
		||||
    }
 | 
			
		||||
    PayOrderDetailsRespVO convertDetail(PayOrderDO bean);
 | 
			
		||||
    PayOrderDetailsRespVO.PayOrderExtension convert(PayOrderExtensionDO bean);
 | 
			
		||||
 | 
			
		||||
    List<PayOrderRespVO> convertList(List<PayOrderDO> list);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ import cn.hutool.core.lang.Pair;
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import cn.hutool.core.util.RandomUtil;
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import cn.hutool.extra.spring.SpringUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.pay.config.PayProperties;
 | 
			
		||||
@@ -161,7 +162,7 @@ public class PayOrderServiceImpl implements PayOrderService {
 | 
			
		||||
 | 
			
		||||
        // 4. 如果调用直接支付成功,则直接更新支付单状态为成功。例如说:付款码支付,免密支付时,就直接验证支付成功
 | 
			
		||||
        if (unifiedOrderResp != null) {
 | 
			
		||||
            notifyPayOrder(channel, unifiedOrderResp);
 | 
			
		||||
            getSelf().notifyPayOrder(channel, unifiedOrderResp);
 | 
			
		||||
            // 如有渠道错误码,则抛出业务异常,提示用户
 | 
			
		||||
            if (StrUtil.isNotEmpty(unifiedOrderResp.getChannelErrorCode())) {
 | 
			
		||||
                throw exception(ORDER_SUBMIT_CHANNEL_ERROR, unifiedOrderResp.getChannelErrorCode(),
 | 
			
		||||
@@ -234,11 +235,17 @@ public class PayOrderServiceImpl implements PayOrderService {
 | 
			
		||||
        // 校验支付渠道是否有效
 | 
			
		||||
        PayChannelDO channel = channelService.validPayChannel(channelId);
 | 
			
		||||
        // 更新支付订单为已支付
 | 
			
		||||
        TenantUtils.execute(channel.getTenantId(), () -> notifyPayOrder(channel, notify));
 | 
			
		||||
        TenantUtils.execute(channel.getTenantId(), () -> getSelf().notifyPayOrder(channel, notify));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO 芋艿:事务问题
 | 
			
		||||
    private void notifyPayOrder(PayChannelDO channel, PayOrderRespDTO notify) {
 | 
			
		||||
    /**
 | 
			
		||||
     * 通知并更新订单的支付结果
 | 
			
		||||
     *
 | 
			
		||||
     * @param channel 支付渠道
 | 
			
		||||
     * @param notify 通知
 | 
			
		||||
     */
 | 
			
		||||
    @Transactional(rollbackFor = Exception.class) // 注意,如果是方法内调用该方法,需要通过 getSelf().notifyPayOrder(channel, notify) 调用,否则事务不生效
 | 
			
		||||
    public void notifyPayOrder(PayChannelDO channel, PayOrderRespDTO notify) {
 | 
			
		||||
        // 情况一:支付成功的回调
 | 
			
		||||
        if (PayOrderStatusRespEnum.isSuccess(notify.getStatus())) {
 | 
			
		||||
            notifyOrderSuccess(channel, notify);
 | 
			
		||||
@@ -396,4 +403,13 @@ public class PayOrderServiceImpl implements PayOrderService {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得自身的代理对象,解决 AOP 生效问题
 | 
			
		||||
     *
 | 
			
		||||
     * @return 自己
 | 
			
		||||
     */
 | 
			
		||||
    private PayOrderServiceImpl getSelf() {
 | 
			
		||||
        return SpringUtil.getBean(getClass());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -137,7 +137,7 @@ public class PayRefundServiceImpl implements PayRefundService {
 | 
			
		||||
                .setOutRefundNo(refund.getNo())
 | 
			
		||||
                .setNotifyUrl(genChannelRefundNotifyUrl(channel))
 | 
			
		||||
                .setReason(reqDTO.getReason());
 | 
			
		||||
        PayRefundRespDTO refundRespDTO = client.unifiedRefund(unifiedReqDTO); // TODO 增加一个 channelErrorCode、channelErrorMsg 字段
 | 
			
		||||
        PayRefundRespDTO refundRespDTO = client.unifiedRefund(unifiedReqDTO);
 | 
			
		||||
        // 2.3 处理退款返回
 | 
			
		||||
        notifyRefund(channel, refundRespDTO);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user