美文网首页
订单系统同时支持微信支付和QQ钱包 - 观察者模式&类策略模式

订单系统同时支持微信支付和QQ钱包 - 观察者模式&类策略模式

作者: 十毛tenmao | 来源:发表于2019-12-10 23:11 被阅读0次

    最近交易系统升级,需要增加QQ钱包的支付方式,为了简化多种支付方式对原有交易系统的影响。利用观察者模式和类策略模式,实现了第三支付与项目交易系统的分离

    实现

    • 第三支付服务的抽象接口
    /**
     * 第三方支付服务的统一抽象接口.
     *
     * @author timxia
     * @since 2019/12/10
     */
    public interface PayService {
        PrepayResult payOrder(Order order);
    
        void applyRefund(OutRefund outRefund);
    }
    
    • 第三支付结果的监听(观察者模式)
    /**
     * 第三方支付的结果监听.
     * @author timxia
     * @since 2019/12/10
     */
    public class AbstractPayServiceNotifier {
        /**
         * 支付结果消费者列表.
         */
        private List<Consumer<PayResultContent>> payResultConsumers;
    
        /**
         * 退款结果消费者列表.
         */
        private List<Consumer<RefundResultContent>> refundResultConsumers;
        protected void notifyPayResult(PayResultContent content) {
            for (Consumer<PayResultContent> payResultConsumer : payResultConsumers) {
                payResultConsumer.accept(content);
            }
        }
    
        protected void notifyRefundResult(RefundResultContent refundResultContent) {
            for (Consumer<RefundResultContent> refundResultConsumer : refundResultConsumers) {
                refundResultConsumer.accept(refundResultContent);
            }
        }
    }
    
    • QQ钱包的实现
    /**
     * QQ钱包.
     *
     * @author timxia
     * @since 2019/12/10
     */
    @Service
    public class QqPayServiceImpl extends AbstractPayServiceNotifier implements PayService {
        @Override
        public PrepayResult payOrder(Order order) {
            return null;
        }
    
        @Override
        public void applyRefund(OutRefund outRefund) {
        }
    
        public void handleWeixinNotify(QqPayNotifyContent notifyContent) {
            //TODO 校验内容是否合法
            //TODO 更新支付订单的状态
            notifyPayResult(convertToPayResult(notifyContent));
        }
    
        private static PayResultContent convertToPayResult(QqPayNotifyContent content) {
            //TODO 转换为统一的支付结果
            return new PayResultContent();
        }
    }
    
    • 微信支付的实现
    /**
     * 微信支付.
     *
     * @author timxia
     * @since 2019/12/10
     */
    @Service
    public class WxPayServiceImpl extends AbstractPayServiceNotifier implements PayService {
        @Override
        public PrepayResult payOrder(Order order) {
            return null;
        }
    
        @Override
        public void applyRefund(OutRefund outRefund) {
        }
    
        public void handleWeixinNotify(WeixinPayNotifyContent notifyContent) {
            //TODO 校验内容是否合法
            //TODO 更新支付订单的状态
            notifyPayResult(convertToPayResult(notifyContent));
        }
    
        private static PayResultContent convertToPayResult(WeixinPayNotifyContent content) {
            //TODO 转换为统一的支付结果
            return new PayResultContent();
        }
    }
    
    • 第三方支付的统一管理(类策略模式)
      可以根据支付方法,获取对应的第三方服务
    /**
     * 所有支付方式的统一管理工具,方便调用.
     *
     * @author timxia
     * @since 2019/12/10
     */
    @Component
    public class UnifiedPayServiceManager {
        @Resource
        private Map<String, PayService> payServiceMap;
    
        /**
         * 获取支付方式对应的 第三方支付服务.
         */
        public @NotNull
        PayService getByMethod(String payMethod) {
            PayService payService = payServiceMap.get(payMethod);
            if (payService == null) {
                throw new RuntimeException("there is not pay service for: " + payMethod);
            }
            return payService;
        }
    }
    
    • 使用方法
      可以完成支付,监听微信和QQ钱包的回调
    @RestController
    @RequestMapping("home")
    @SpringBootApplication
    public class TenPayApplication {
        @Resource
        private UnifiedPayServiceManager unifiedPayService;
    
        @Resource
        private WxPayServiceImpl wxPayService;
    
        @Resource
        private QqPayServiceImpl qqPayService;
    
        public static void main(String[] args) {
            SpringApplication.run(TenPayApplication.class, args);
        }
    
    
        @PostMapping("payOrder")
        public String payOrder(String orderNo, String payMethod) {
            final PayService qqPayServiceImpl = unifiedPayService.getByMethod(payMethod);
            //TODO 根据orderNo获取Order
            Order order = new Order();
            qqPayServiceImpl.payOrder(order);
            return "success";
        }
    
        @PostMapping("/wx/notify/order")
        public Object parseOrderNotifyResult(@RequestBody WeixinPayNotifyContent notifyContent) {
            wxPayService.handleWeixinNotify(notifyContent);
            //TODO 这里需要返回微信需要的JSON
            return new Object();
        }
    
        @PostMapping("/qq/notify/order")
        public Object parseQqOrderNotifyResult(@RequestBody QqPayNotifyContent notifyContent) {
            qqPayService.handleWeixinNotify(notifyContent);
            //TODO 这里需要返回QQ钱包需要的XML
            return new Object();
        }
    }
    

    相关文章

      网友评论

          本文标题:订单系统同时支持微信支付和QQ钱包 - 观察者模式&类策略模式

          本文链接:https://www.haomeiwen.com/subject/ulengctx.html