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

2019-12-10  本文已影响0人  十毛tenmao

最近交易系统升级,需要增加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钱包.
 *
 * @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;
    }
}
@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();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读