利用工厂模式简化if else,改造回调处理流程

1.接受回调处理的接口

@RestController
@Slf4j
public class BaseNotifyController extends BaseController {
    @Autowired
    NotifyFactory notifyFactory;

    /**
     * 接受支付宝统一回调处理
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/alipay/notify/baseCallBack")
    ResponseResult<String> baseCallBack(@RequestBody AliPayNotifyReq req) {
        log.info("【接受支付宝统一回调处理】请求参数:{}", JSONObject.toJSONString(req));
        ResponseResult<String> re = new ResponseResult<>();
        try {
            String data = req.getNotifyData();
            JSONObject content = JSONObject.parseObject(data);
            String msg_method = content.getString("msg_method");
            NotifyHandleService valueHandle = notifyFactory.getHandle(msg_method);
            re = valueHandle.notifyHandle(req);
        } catch (Exception e) {
            log.error("【接受支付宝统一回调处理】error:{}", e);
            return ResponseResult.build(-1,"支付宝统一回调处理异常",null);
        }
        log.info("【接受支付宝统一回调处理】返回结果:{}", JSONObject.toJSONString(re));
        return re;
    }
}

2.定义工厂方法

@Component
public class NotifyFactory {

    static Map<String, String> operationMap = new HashMap<>();

    static {
        operationMap.put(NotifyMsgMethodEnum.ZMGO_SIGN_NOTIFY.getCode(), ZmGoSignNotifyServiceImpl.class.getName());
        operationMap.put(NotifyMsgMethodEnum.ZMGO_AGREEMENT_CHANGED.getCode(), ZmGoAgreementChangedNotifyServiceImpl.class.getName());
        operationMap.put(NotifyMsgMethodEnum.ZMGO_SETTLE_NOTIFY.getCode(), ZmGoSettleSignNotifyServiceImpl.class.getName());
    }

    public  NotifyHandleService getHandle(String msg_method) throws Exception {
        String className = operationMap.get(msg_method);
        if (className == null) {
            throw new Exception("msg_method:" + msg_method + " not fund");
        }
        Class cs = Class.forName(className);
        NotifyHandleService handle = (NotifyHandleService) ApplicationContextHelper.getBean(cs);
        return handle;
    }

3.每个业务提供实现类

​
@Slf4j
@Service
public class ZmGoSignNotifyServiceImpl extends NotifyBaseImpl implements NotifyHandleService {

    @Override
    @Transactional
    public ResponseResult<String> notifyHandle(AliPayNotifyReq req) {
     //处理具体的业务
     return ResponseResult.buildSuccessResponse();
    }
}

​

4.NotifyBaseImpl 提供通用的验签和其他处理

@Service
@Slf4j
public class NotifyBaseImpl {
    @Resource
    private AliPayConfiguration aliPayConfig;
    /**
     * 校验签名
     *
     * @para notifyData
     * @return
     * @throws Exception
     */
    public boolean alipaySignCheck(Map<String, String> notifyData) throws Exception {
        log.info("【notifyData】:{}", JSONObject.toJSONString(notifyData));
        boolean check = AlipaySignature.rsaCheckV1(notifyData, aliPayConfig.getPublicKey(), AliPayConfiguration.UTF8, "RSA2");
        log.info("【验签结果】:{}",check);
        return check;
    }

    /**
     * 获取bizContent对象
     */
    JSONObject getBizContent(AliPayNotifyReq req) throws Exception {
        if (req == null) {
            throw new Exception("AliPayNotifyReq not empty");
        }
        String notify = req.getNotifyData();
        JSONObject notifyJson = JSONObject.parseObject(notify);
        if (notifyJson == null) {
            throw new Exception("notifyData not empty");
        }
        String bizContent = notifyJson.getString("biz_content");
        if (bizContent == null) {
            throw new Exception("bizContent not empty");
        }
        JSONObject bizContentJson = JSONObject.parseObject(bizContent);
        return bizContentJson;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值