前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >订单系统同时支持微信支付和QQ钱包 - 观察者模式&类策略模式

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

作者头像
十毛
发布2019-12-16 17:58:06
4950
发布2019-12-16 17:58:06
举报

最近交易系统升级,需要增加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();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档