Esempio n. 1
0
        private void ValidateProperties()
        {
            // Validating and setting up Authentication type
            Enumerations.ValidateAuthenticationType(AuthenticationType);

            if (string.Equals(AuthenticationType, Enumerations.AuthenticationType.HTTP_SIGNATURE.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                IsHttpSignAuthType = true;
            }
            else if (string.Equals(AuthenticationType, Enumerations.AuthenticationType.JWT.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                IsJwtTokenAuthType = true;
            }

            if (string.IsNullOrEmpty(TimeOut))
            {
                TimeOut = string.Empty;   // In Milllisec
            }

            // setting up hostname based on the run environment value
            if (string.IsNullOrEmpty(RunEnvironment))
            {
                throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - RunEnvironment is Mandatory");
            }
            else if (Constants.OldRunEnvironmentConstants.Contains(RunEnvironment.ToUpper()))
            {
                throw new Exception($"{Constants.DeprecationPrefix} The value \"{RunEnvironment}\" for this field `RunEnvironment` has been deprecated and will not be used anymore.\n\nPlease refer to the README file [ https://github.com/CyberSource/cybersource-rest-samples-csharp/blob/master/README.md ] for information about the new values that are accepted.");
            }

            HostName = RunEnvironment.ToLower();

            // AUTHENTICATION MECHANISM SPECIFIC CHECKS
            // 1. FOR HTTP SIGNATURE
            if (IsHttpSignAuthType)
            {
                if (string.IsNullOrEmpty(MerchantId))
                {
                    throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantID is Mandatory");
                }

                if (string.IsNullOrEmpty(MerchantKeyId))
                {
                    throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantKeyId is Mandatory");
                }

                if (string.IsNullOrEmpty(MerchantSecretKey))
                {
                    throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantSecretKey is Mandatory");
                }
            }

            // 2. FOR JWT TOKEN
            else if (IsJwtTokenAuthType)
            {
                if (string.IsNullOrEmpty(MerchantId))
                {
                    throw new Exception($"{Constants.ErrorPrefix} Merchant Config field - MerchantID is Mandatory");
                }

                if (string.IsNullOrEmpty(KeyAlias))
                {
                    KeyAlias = MerchantId;
                    throw new Exception($"{Constants.WarningPrefix} KeyAlias not provided. Assigning the value of: [MerchantID]");
                }

                if (!string.Equals(KeyAlias, MerchantId))
                {
                    KeyAlias = MerchantId;
                    throw new Exception($"{Constants.WarningPrefix} Incorrect value of KeyAlias provided. Assigning the value of: [MerchantID]");
                }

                if (string.IsNullOrEmpty(KeyPass))
                {
                    KeyPass = MerchantId;
                    throw new Exception($"{Constants.WarningPrefix} KeyPassword not provided. Assigning the value of: [MerchantID]");
                }

                if (string.IsNullOrEmpty(KeyDirectory))
                {
                    KeyDirectory = Constants.P12FileDirectory;
                    throw new Exception($"{Constants.WarningPrefix} KeysDirectory not provided. Using Default Path: {KeyDirectory}");
                }

                if (string.IsNullOrEmpty(KeyfileName))
                {
                    KeyfileName = MerchantId;
                    throw new Exception($"{Constants.WarningPrefix} KeyfileName not provided. Assigning the value of: [MerchantId]");
                }

                P12Keyfilepath = KeyDirectory + "\\" + KeyfileName + ".p12";
            }
        }
        /// <summary>
        /// Validate properties in the object.
        /// </summary>
        private void ValidateProperties()
        {
            if (string.IsNullOrEmpty(MerchantId))
            {
                throw new MerchantConfigException($"{(object) Constants.ErrorPrefix} Merchant Config field - MerchantID is Mandatory");
            }

            if (AuthenticationType == AuthenticationType.HTTP_SIGNATURE)
            {
                IsHttpSignAuthType = true;
            }
            else if (AuthenticationType == AuthenticationType.JWT)
            {
                IsJwtTokenAuthType = true;
            }

            if (string.IsNullOrEmpty(TimeOut))
            {
                TimeOut = string.Empty;
            }

            if (string.IsNullOrEmpty(RunEnvironment))
            {
                throw new MerchantConfigException($"{Constants.ErrorPrefix} Merchant Config field - RunEnvironment is Mandatory");
            }

            HostName = !RunEnvironment.ToUpper().Equals(Constants.CybsSandboxRunEnv.ToUpper())
                ? (!RunEnvironment.ToUpper().Equals(Constants.CybsProdRunEnv.ToUpper())
                    ? RunEnvironment.ToLower()
                    : Constants.CybsProdHostName)
                : Constants.CybsSandboxHostName;

            if (IsHttpSignAuthType)
            {
                if (string.IsNullOrEmpty(MerchantKeyId))
                {
                    throw new MerchantConfigException($"{Constants.ErrorPrefix} Merchant Config field - MerchantKeyId is Mandatory");
                }
                if (string.IsNullOrEmpty(MerchantSecretKey))
                {
                    throw new MerchantConfigException($"{Constants.ErrorPrefix} Merchant Config field - MerchantSecretKey is Mandatory");
                }
            }
            else
            {
                if (!IsJwtTokenAuthType)
                {
                    return;
                }
                if (string.IsNullOrEmpty(KeyAlias))
                {
                    KeyAlias = MerchantId;
                    // TODO: replace with ILogger common for .NET Core
                    //Logger.Warn($"{Constants.WarningPrefix} KeyAlias not provided. Assigning the value of: [MerchantID]");
                }

                if (!string.Equals(KeyAlias, MerchantId))
                {
                    KeyAlias = MerchantId;
                    // TODO: replace with ILogger common for .NET Core
                    //Logger.Warn($"{Constants.WarningPrefix} Incorrect value of KeyAlias provided. Assigning the value of: [MerchantID]");
                }

                if (string.IsNullOrEmpty(KeyPass))
                {
                    KeyPass = MerchantId;
                    // TODO: replace with ILogger common for .NET Core
                    //Logger.Warn($"{Constants.WarningPrefix} KeyPassword not provided. Assigning the value of: [MerchantID]");
                }

                if (string.IsNullOrEmpty(KeyDirectory))
                {
                    KeyDirectory = Constants.P12FileDirectory;
                    // TODO: replace with ILogger common for .NET Core
                    //Logger.Warn($"{Constants.WarningPrefix} KeysDirectory not provided. Using Default Path: {KeyDirectory}");
                }

                if (string.IsNullOrEmpty(KeyfileName))
                {
                    KeyfileName = MerchantId;
                    // TODO: replace with ILogger common for .NET Core
                    //Logger.Warn($"{Constants.WarningPrefix} KeyfileName not provided. Assigning the value of: [MerchantId]");
                }

                P12Keyfilepath = Path.Combine(KeyDirectory, KeyfileName + ".p12");
            }
        }