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);
相关推荐
Wang's Blog31 分钟前
Java框架快速入门: Spring Security+OAuth2之密码验证规则与自定义注解
java·数据库·spring
一嘴一个橘子39 分钟前
java - redis 缓存击穿 - 互斥锁
java
shehuiyuelaiyuehao44 分钟前
算法39,位运算,消失的两个数字
java·数据结构·算法
小刘在重生~1 小时前
Java 异常体系完整笔记
java·笔记·python
抠脚小弟1 小时前
Spring Task 定时任务详解:从入门到实战
java·后端·spring
2601_962284501 小时前
Python 和Java 哪个更适合做自动化测试?
java·自动化测试·python·接口测试·性能测试
随遇而安zx1 小时前
【多线程】---AQS 原理 知识点(设计思想与源码深度解析)
java·多线程·aqs
LXMXHJ1 小时前
springboot中的线程操作
java·spring boot·后端·线程
敲代码的嘎仔2 小时前
自己设计了一个兑换码算法:自增ID + Base32转码 + 按位加权签名 + 异或混淆,面试被追问细节时终于不用慌了
java·数据库·mysql·算法·微服务·面试·职场和发展
m0_587383002 小时前
外卖CPS系统开发实战:从架构设计到运营落地全指南
java·spring·小程序·架构·需求分析