SpringBoot支付回调枚举+策略+工厂模式

定义业务枚举

java 复制代码
@Getter
public enum BizTypePrefixEnum {

    PAYMENT("PMT", "消费"),
    RECHARGE("RCH", "充值"),
    WITHDRAWAL("WDW", "提现"),
    REFUND("RFD", "退款"),
    RED_PACKET_PAY("RP", "红包支付"),

    ;
    private final String code;

    private final String desc;


    BizTypePrefixEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static BizTypePrefixEnum getStartsWith(String outTradeNo) {
        for (BizTypePrefixEnum bizTypePrefixEnum : BizTypePrefixEnum.values()) {
            if (outTradeNo.startsWith(bizTypePrefixEnum.getCode())) {
                return bizTypePrefixEnum;
            }
        }
        return null;
    }
}

定义策略接口

java 复制代码
public interface NotifyProcessHandler {

    /**
     * 支付回调参数
     *
     * @param req
     */
    void process(NotifyPaymentReq req);

    /**
     * 业务类型
     *
     * @return
     */
    BizTypePrefixEnum getBizType();

}

不同枚举业务,分别定义实现类

java 复制代码
@Slf4j
@Service
public class PaymentNotifyHandler implements NotifyProcessHandler {

    @Override
    public void process(NotifyPaymentReq req) {
        // todo 消费支付结果处理

    }

    @Override
    public BizTypePrefixEnum getBizType() {
        return BizTypePrefixEnum.PAYMENT;
    }
}
java 复制代码
@Slf4j
@Service
public class RechargeNotifyHandler implements NotifyProcessHandler {
    @Override
    public void process(NotifyPaymentReq req) {
        // todo 充值支付结果处理

    }

    @Override
    public BizTypePrefixEnum getBizType() {
        return BizTypePrefixEnum.RECHARGE;
    }
}

...等等其他枚举处理类

创建工厂,初始化加载策略类

java 复制代码
@Component
public class NotifyProcessFactory implements InitializingBean {

    @Autowired
    private List<NotifyProcessHandler> notifyProcessHandlerList;

    private final Map<BizTypePrefixEnum, NotifyProcessHandler> handlerMap = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        for (NotifyProcessHandler notifyProcessHandler : notifyProcessHandlerList) {
            handlerMap.put(notifyProcessHandler.getBizType(), notifyProcessHandler);
        }
    }

    public NotifyProcessHandler getHandler(BizTypePrefixEnum bizType) {
        return handlerMap.get(bizType);
    }

}

注入工厂,实现处理策略

java 复制代码
   @Autowired
    private NotifyProcessFactory notifyProcessFactory;

   NotifyProcessHandler handler = notifyProcessFactory.getHandler(BizTypePrefixEnum.getStartsWith(req.getOutTradeNo()));
            handler.process(req);
相关推荐
b***666142 分钟前
Spring Boot 整合 Apollo 配置中心实战
java·spring boot·后端
CoderYanger1 小时前
递归、搜索与回溯-综合练习:27.黄金矿工
java·算法·leetcode·深度优先·1024程序员节
b***66611 小时前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
i***11861 小时前
springboot使用redis
spring boot·redis·后端
vx_vxbs661 小时前
【SSM高校普法系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
android·java·python·mysql·小程序·php·idea
张较瘦_1 小时前
Springboot3 | ResponseEntity 完全使用教程
java·springboot·开发
毕设源码-郭学长1 小时前
【开题答辩全过程】以 高校兼职系统为例,包含答辩的问题和答案
java·spring boot
黄嚯嚯1 小时前
Jackson 多态反序列化详解:基于字段自动选择子类的优雅方案
java
h***38181 小时前
SpringBoot - Cookie & Session 用户登录及登录状态保持功能实现
java·spring boot·后端
一只乔哇噻1 小时前
java后端工程师+AI大模型进修ing(研一版‖day57)
java·开发语言·人工智能·算法·语言模型