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

com.payermax.sdk.config.GlobalMerchantConfig Maven / Gradle / Ivy

There is a newer version: 1.0.12
Show newest version
package com.payermax.sdk.config;

import com.payermax.sdk.enums.ErrorCodeEnum;
import com.payermax.sdk.exceptions.PayermaxException;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhu.q
 */
public class GlobalMerchantConfig {

    private static MerchantConfig DEFAULT_MERCHANT_CONFIG = null;

    private static final Map MERCHANT_CONFIG_MAP = new ConcurrentHashMap<>();


    public static void setDefaultConfig(MerchantConfig defaultMerchantConfig) {
        DEFAULT_MERCHANT_CONFIG = defaultMerchantConfig;
        MERCHANT_CONFIG_MAP.put(defaultMerchantConfig.getMerchantNo(), defaultMerchantConfig);
    }

    public static void addConfig(MerchantConfig config) {
        if (StringUtils.isEmpty(config.getMerchantNo())) {
            throw new PayermaxException(ErrorCodeEnum.CONFIG_INVALID, "merchantNo is empty");
        }
        if (StringUtils.isEmpty(config.getAppId())) {
            throw new PayermaxException(ErrorCodeEnum.CONFIG_INVALID, "appId is empty");
        }
        if (StringUtils.isEmpty(config.getMerchantPrivateKey())) {
            throw new PayermaxException(ErrorCodeEnum.CONFIG_INVALID, "merchantPrivateKey is empty");
        }
        if (StringUtils.isEmpty(config.getPayermaxPublicKey())) {
            throw new PayermaxException(ErrorCodeEnum.CONFIG_INVALID, "payermaxPublicKey is empty");
        }
        MERCHANT_CONFIG_MAP.put(config.getMerchantNo(), config);
    }

    public static MerchantConfig getDefaultConfig() {
        if (DEFAULT_MERCHANT_CONFIG == null) {
            throw new PayermaxException(ErrorCodeEnum.CONFIG_INVALID, "default merchant config is null");
        }
        return DEFAULT_MERCHANT_CONFIG;
    }

    public static MerchantConfig getConfig(String merchantNo) {

        if (StringUtils.isEmpty(merchantNo)) {
            throw new PayermaxException(ErrorCodeEnum.PARAMS_INVALID, "merchantNo is empty");
        }

        MerchantConfig merchantConfig = MERCHANT_CONFIG_MAP.get(merchantNo);
        if (merchantConfig == null) {
            throw new PayermaxException(ErrorCodeEnum.PARAMS_INVALID,
                    "can not find merchant config by merchantNo:" + merchantNo);
        }
        return merchantConfig;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy