All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.business.pay.selector.WxSelector Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.business.pay.selector;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import matrix.boot.common.utils.AssertUtil;
import matrix.business.pay.properties.PayProperties;
import me.chanjar.weixin.mp.api.WxMpService;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信选择器
 *
 * @author 36509
 * 2023/3/3
 **/
public class WxSelector {

    /**
     * 默认的类型编码
     */
    private String defaultTypeCode = null;

    /**
     * 微信支付配置字典项
     */
    private final Map wePayPropertiesMap = new HashMap<>();

    /**
     * 微信支付配置字典项
     */
    private final Map wxPayConfigMap = new HashMap<>();

    /**
     * 微信支付服务字典项
     */
    private final Map wxPayServiceMap = new HashMap<>();

    /**
     * 微信公众号对接字典项
     */
    private final Map wxMpServiceMap = new HashMap<>();

    /**
     * 放入微信支付配置
     *
     * @param typeCode        类型编码
     * @param wePayProperties 微信支付配置
     */
    public void put(String typeCode, PayProperties.WePayProperties wePayProperties) {
        AssertUtil.state(!wePayPropertiesMap.containsKey(typeCode), String.format("%s has exists!", typeCode));
        if (defaultTypeCode == null) {
            defaultTypeCode = typeCode;
        }
        wePayPropertiesMap.put(typeCode, wePayProperties);
    }

    /**
     * 放入微信支付配置
     *
     * @param typeCode    类型编码
     * @param wxPayConfig 微信支付配置
     */
    public void put(String typeCode, WxPayConfig wxPayConfig) {
        AssertUtil.state(!wxPayConfigMap.containsKey(typeCode), String.format("%s has exists!", typeCode));
        wxPayConfigMap.put(typeCode, wxPayConfig);
    }

    /**
     * 放入微信支付服务
     *
     * @param typeCode     类型编码
     * @param wxPayService 微信支付服务
     */
    public void put(String typeCode, WxPayService wxPayService) {
        AssertUtil.state(!wxPayServiceMap.containsKey(typeCode), String.format("%s has exists!", typeCode));
        wxPayServiceMap.put(typeCode, wxPayService);
    }

    /**
     * 放入微信公众号对接
     *
     * @param typeCode    类型编码
     * @param wxMpService 微信公众号对接
     */
    public void put(String typeCode, WxMpService wxMpService) {
        AssertUtil.state(!wxMpServiceMap.containsKey(typeCode), String.format("%s has exists!", typeCode));
        wxMpServiceMap.put(typeCode, wxMpService);
    }

    /**
     * 默认微信支付配置
     *
     * @return 默认微信支付配置
     */
    public PayProperties.WePayProperties getDefaultWePayProperties() {
        return wePayPropertiesMap.get(defaultTypeCode);
    }

    /**
     * 默认微信支付配置
     *
     * @return 默认微信支付配置
     */
    public WxPayConfig getDefaultWxPayConfig() {
        return wxPayConfigMap.get(defaultTypeCode);
    }

    /**
     * 默认微信支付服务
     *
     * @return 默认微信支付服务
     */
    public WxPayService getDefaultWxPayService() {
        return wxPayServiceMap.get(defaultTypeCode);
    }

    /**
     * 默认微信公众号对接
     *
     * @return 默认微信公众号对接
     */
    public WxMpService getDefaultWxMpService() {
        return wxMpServiceMap.get(defaultTypeCode);
    }

    /**
     * 微信支付配置
     *
     * @param typeCode 类型编码
     * @return 微信支付配置
     */
    public PayProperties.WePayProperties getWePayProperties(String typeCode) {
        if (typeCode == null) {
            return getDefaultWePayProperties();
        }
        AssertUtil.state(wePayPropertiesMap.containsKey(typeCode), String.format("%s not exists!", typeCode));
        return wePayPropertiesMap.get(typeCode);
    }

    /**
     * 微信支付配置
     *
     * @param typeCode 类型编码
     * @return 微信支付配置
     */
    public WxPayConfig getWxPayConfig(String typeCode) {
        if (typeCode == null) {
            return getDefaultWxPayConfig();
        }
        AssertUtil.state(wxPayConfigMap.containsKey(typeCode), String.format("%s not exists!", typeCode));
        return wxPayConfigMap.get(typeCode);
    }

    /**
     * 微信支付服务
     *
     * @param typeCode 类型编码
     * @return 微信支付服务
     */
    public WxPayService getWxPayService(String typeCode) {
        if (typeCode == null) {
            return getDefaultWxPayService();
        }
        AssertUtil.state(wxPayServiceMap.containsKey(typeCode), String.format("%s not exists!", typeCode));
        return wxPayServiceMap.get(typeCode);
    }

    /**
     * 微信公众号对接
     *
     * @param typeCode 类型编码
     * @return 微信公众号对接
     */
    public WxMpService getWxMpService(String typeCode) {
        if (typeCode == null) {
            return getDefaultWxMpService();
        }
        AssertUtil.state(wxMpServiceMap.containsKey(typeCode), String.format("%s not exists!", typeCode));
        return wxMpServiceMap.get(typeCode);
    }

    /**
     * 获取默认的类型编码
     *
     * @return 默认的类型编码
     */
    public String getDefaultTypeCode() {
        return defaultTypeCode;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy