/// <summary>
        /// Calculates the check digit.
        /// The account number is given without a check digit.
        /// </summary>
        /// <param name="accountNumber">The account number without a check digit.</param>
        /// <returns>
        /// The calculated check digit for the given account number
        /// </returns>
        public string CalculateCheckDigit(NationalAccountNumber accountNumber)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            var belgiumAccountNumber = new BelgiumAccountNumber(accountNumber);

            if (String.IsNullOrEmpty(belgiumAccountNumber.AccountNumber))
            {
                throw new ArgumentException("The account number is missing.", "accountNumber");
            }
            if (String.IsNullOrEmpty(belgiumAccountNumber.BankCode))
            {
                throw new ArgumentException("The bank code is missing.", "accountNumber");
            }

            var accountNumberWithBankCode =
                String.Format("{0,3}{1,7}", belgiumAccountNumber.BankCode, belgiumAccountNumber.AccountNumber).Replace(' ', '0');

            var digits = validationMethod.CalculateCheckDigit(accountNumberWithBankCode);

            return(digits.Length < 2 ? "0" + digits : digits);
        }
Beispiel #2
0
        /// <summary>
        /// Calculates the check digit.
        /// The account number is given without a check digit.
        /// </summary>
        /// <param name="accountNumber">The account number without a check digit.</param>
        /// <returns>
        /// The calculated check digit for the given account number
        /// </returns>
        public string CalculateCheckDigit(NationalAccountNumber accountNumber)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            var portugalAccountNumber = new PortugalAccountNumber(accountNumber);

            if (String.IsNullOrEmpty(portugalAccountNumber.BankCode))
            {
                throw new ArgumentException("The bank code is missing.", "accountNumber");
            }
            if (String.IsNullOrEmpty(portugalAccountNumber.Branch))
            {
                throw new ArgumentException("The branch code is missing.", "accountNumber");
            }
            if (String.IsNullOrEmpty(portugalAccountNumber.AccountNumber))
            {
                throw new ArgumentException("The account number is missing.", "accountNumber");
            }

            var fullAccountNumber =
                String.Format("{0,4}{1,4}{2,11}", portugalAccountNumber.BankCode, portugalAccountNumber.Branch, portugalAccountNumber.AccountNumber).Replace(' ', '0');

            return(validationMethod.CalculateCheckDigit(fullAccountNumber));
        }
        /// <summary>
        /// Determines whether the specified account number is valid. The account number
        /// is given as a full number including the hypothetical check digit.
        /// validation steps:
        /// * bank code can have 3 digits max
        /// * account number can have 9 digits max (including the 2 check digits)
        /// * check digits are valid
        /// </summary>
        /// <param name="accountNumber">The account number including the hypothetical check digit.</param>
        /// <param name="validationErrors">Collection is filled up with the validation error messages</param>
        /// <returns>
        ///   <c>true</c> if the specified account number is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool Validate(NationalAccountNumber accountNumber, ICollection <ValidationError> validationErrors)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            validationErrors = validationErrors ?? new List <ValidationError>();

            var belgiumAccountNumber = new BelgiumAccountNumber(accountNumber);

            ValidationMethodsTools.ValidateMember(belgiumAccountNumber.AccountNumber, 9, validationErrors, ValidationErrorCodes.AccountNumberMissing, ValidationErrorCodes.AccountNumberTooLong);
            ValidationMethodsTools.ValidateMember(belgiumAccountNumber.BankCode, 3, validationErrors, ValidationErrorCodes.BankCodeMissing, ValidationErrorCodes.BankCodeTooLong);

            if (validationErrors.Count > 0)
            {
                return(false);
            }

            var accountNumberWithBankCode =
                String.Format("{0,3}{1,9}", belgiumAccountNumber.BankCode, belgiumAccountNumber.AccountNumber).Replace(' ', '0');

            if (!validationMethod.IsValid(accountNumberWithBankCode))
            {
                validationErrors.AddValidationErrorMessage("The validation of the check digits failed.");
            }

            return(validationErrors.Count == 0);
        }
        /// <summary>
        /// Determines whether the specified account number is valid. The given bank code will be
        /// mapped to an registered method which calculates the check digit. The account number
        /// is given as a full number including the hypothetical check digit.
        /// </summary>
        /// <param name="accountNumber">The account number including the hypothetical check digit.</param>
        /// <param name="validationErrors">Collection is filled up with the validation error messages</param>
        /// <returns>
        ///   <c>true</c> if the specified account number is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool Validate(NationalAccountNumber accountNumber, ICollection <ValidationError> validationErrors)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            validationErrors = validationErrors ?? new List <ValidationError>();

            var germanAccountNumber = new GermanyAccountNumber(accountNumber);

            ValidationMethodsTools.ValidateMember(germanAccountNumber.AccountNumber, 10, validationErrors, ValidationErrorCodes.AccountNumberMissing, ValidationErrorCodes.AccountNumberTooLong);
            ValidationMethodsTools.ValidateMember(germanAccountNumber.BankCode, 8, validationErrors, ValidationErrorCodes.BankCodeMissing, ValidationErrorCodes.BankCodeTooLong);

            if (validationErrors.Count > 0)
            {
                return(false);
            }

            var checkMethodCode = BankCodeMappingMethod.Resolve(germanAccountNumber.BankCode);

            if (string.IsNullOrEmpty(checkMethodCode))
            {
                throw new ArgumentException(String.Format("Can't resolve the check method for the given bank code {0}.", germanAccountNumber.BankCode), "accountNumber");
            }

            return(accountNumberValidationByMethodCode.IsValid(germanAccountNumber.AccountNumber, checkMethodCode));
        }
Beispiel #5
0
        /// <summary>
        /// Determines whether the specified account number is valid. The account number
        /// is given as a full number including the hypothetical check digit.
        /// validation steps:
        /// * bank code can have 4 digits max
        /// * branch code can have 4 digits max
        /// * account number can have 13 digits max (including 2 check digits)
        /// * check digit is valid
        /// </summary>
        /// <param name="accountNumber">The account number including the hypothetical check digit.</param>
        /// <param name="validationErrors">Collection is filled up with the validation error messages</param>
        /// <returns>
        ///   <c>true</c> if the specified account number is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool Validate(NationalAccountNumber accountNumber, ICollection <ValidationError> validationErrors)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            validationErrors = validationErrors ?? new List <ValidationError>();

            var portugalAccountNumber = new PortugalAccountNumber(accountNumber);

            ValidationMethodsTools.ValidateMember(portugalAccountNumber.AccountNumber, 13, validationErrors, ValidationErrorCodes.AccountNumberMissing, ValidationErrorCodes.AccountNumberTooLong);
            ValidationMethodsTools.ValidateMember(portugalAccountNumber.BankCode, 4, validationErrors, ValidationErrorCodes.BankCodeMissing, ValidationErrorCodes.BankCodeTooLong);
            ValidationMethodsTools.ValidateMember(portugalAccountNumber.Branch, 4, validationErrors, ValidationErrorCodes.BranchCodeMissing, ValidationErrorCodes.BranchCodeTooLong);

            if (validationErrors.Count > 0)
            {
                return(false);
            }

            var fullAccountNumber =
                String.Format("{0,4}{1,4}{2,13}", portugalAccountNumber.BankCode, portugalAccountNumber.Branch, portugalAccountNumber.AccountNumber).Replace(' ', '0');

            if (!validationMethod.IsValid(fullAccountNumber))
            {
                validationErrors.AddValidationErrorMessage("The validation of the check digits failed.");
            }

            return(validationErrors.Count == 0);
        }
Beispiel #6
0
        /// <summary>
        /// converts the parts of a national account number to an IBAN.
        /// There are different parts needed in dependency of the selected country
        /// </summary>
        /// <param name="nationalAccountNumber">The national account number.</param>
        /// <returns></returns>
        public override string ToIBAN(NationalAccountNumber nationalAccountNumber)
        {
            if (nationalAccountNumber == null)
            {
                throw new ArgumentNullException("nationalAccountNumber");
            }

            var abAccountNumber = CreateInstance(nationalAccountNumber);

            var bankCode          = OnlyAllowedCharacters(abAccountNumber.BankCode);
            var accountNumber     = OnlyAllowedCharacters(abAccountNumber.AccountNumber);
            var branch            = OnlyAllowedCharacters(abAccountNumber.Branch);
            var holdersNationalId = OnlyAllowedCharacters(abAccountNumber.HoldersNationalId);

            if (String.IsNullOrEmpty(bankCode))
            {
                throw new ArgumentException("The bank code is missing.");
            }
            if (String.IsNullOrEmpty(branch))
            {
                throw new ArgumentException("The branch code is missing.");
            }
            if (String.IsNullOrEmpty(accountNumber))
            {
                throw new ArgumentException("The account number is missing.");
            }
            if (String.IsNullOrEmpty(holdersNationalId))
            {
                throw new ArgumentException("The holders national id is missing.");
            }

            var bban = String.Format(BBANFormatString, bankCode, branch, accountNumber, holdersNationalId);

            bban = bban.Replace(' ', '0');
            bban = ConvertCharactersToNumbers(bban);

            Log.DebugFormat("calculating checksum for bban {0}", bban);

            var modulo = 98 - ValidationMethodsTools.CalculateModulo(bban, 97);
            var iban   = String.Format(IBANFormatString, IBANPrefix, modulo, bankCode, branch, accountNumber, holdersNationalId);

            iban = iban.Replace(' ', '0');

            Log.DebugFormat("generated IBAN: {0}", iban);

            if (iban.Length != IBANLength)
            {
                throw new InvalidOperationException(String.Format("Couldn't generate a valid IBAN from the bankcode {0}, branch {1}, the account number {2} and the national holders id {3}.", bankCode, branch, accountNumber, holdersNationalId));
            }

            return(iban);
        }
Beispiel #7
0
        /// <summary>
        /// converts the parts of a national account number to an IBAN.
        /// There are different parts needed in dependency of the selected country
        /// </summary>
        /// <param name="nationalAccountNumber">The national account number.</param>
        /// <returns></returns>
        public string ToIBAN(NationalAccountNumber nationalAccountNumber)
        {
            if (nationalAccountNumber == null)
            {
                throw new ArgumentNullException("nationalAccountNumber");
            }

            if (!specificConverters.ContainsKey(nationalAccountNumber.Country))
            {
                throw new ArgumentException(String.Format("The country {0} isn't supported.", nationalAccountNumber.Country), "nationalAccountNumber");
            }

            return(specificConverters[nationalAccountNumber.Country].ToIBAN(nationalAccountNumber));
        }
        /// <summary>
        /// Determines whether the specified account number is valid. The given bank code will be
        /// mapped to an registered method which calculates the check digit. The account number
        /// is given as a full number including the hypothetical check digit.
        /// </summary>
        /// <param name="accountNumber">The account number including the hypothetical check digit.</param>
        /// <param name="validationErrors">Collection is filled up with the validation error messages</param>
        /// <returns>
        ///   <c>true</c> if the specified account number is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool Validate(NationalAccountNumber accountNumber, ICollection <ValidationError> validationErrors)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            if (!SpecificValidations.ContainsKey(accountNumber.Country))
            {
                throw new ArgumentException(string.Format("The country {0} isn't supported.", accountNumber.Country), "accountNumber");
            }

            var specificValidation = SpecificValidations[accountNumber.Country];

            return(specificValidation.Validate(accountNumber, validationErrors));
        }
        /// <summary>
        /// Calculates the check digit. The given bank code will be
        /// mapped to an registered method which calculates the check digit.
        /// The account number is given without a check digit.
        /// </summary>
        /// <param name="accountNumber">The account number without a check digit.</param>
        /// <returns>
        /// The calculated check digit for the given account number
        /// </returns>
        public string CalculateCheckDigit(NationalAccountNumber accountNumber)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            if (!SpecificValidations.ContainsKey(accountNumber.Country))
            {
                throw new ArgumentException(string.Format("The country {0} isn't supported.", accountNumber.Country), "accountNumber");
            }

            var specificValidation = SpecificValidations[accountNumber.Country];

            return(specificValidation.CalculateCheckDigit(accountNumber));
        }
        /// <summary>
        /// Calculates the check digit. The given bank code will be
        /// mapped to an registered method which calculates the check digit.
        /// The account number is given without a check digit.
        /// </summary>
        /// <param name="accountNumber">The account number without a check digit.</param>
        /// <returns>
        /// The calculated check digit for the given account number
        /// </returns>
        public string CalculateCheckDigit(NationalAccountNumber accountNumber)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            var germanAccountNumber = new GermanyAccountNumber(accountNumber);

            var checkMethodCode = BankCodeMappingMethod.Resolve(germanAccountNumber.BankCode);

            if (string.IsNullOrEmpty(checkMethodCode))
            {
                throw new ArgumentException(String.Format("Can't resolve the check method for the given bank code {0}.", germanAccountNumber.BankCode), "accountNumber");
            }

            return(accountNumberValidationByMethodCode.CalculateCheckDigit(germanAccountNumber.AccountNumber, checkMethodCode));
        }
Beispiel #11
0
        /// <summary>
        /// converts the parts of a national account number to an IBAN.
        /// There are different parts needed in dependency of the selected country
        /// </summary>
        /// <param name="nationalAccountNumber">The national account number.</param>
        /// <returns></returns>
        public override string ToIBAN(NationalAccountNumber nationalAccountNumber)
        {
            if (nationalAccountNumber == null)
            {
                throw new ArgumentNullException("nationalAccountNumber");
            }

            var abAccountNumber = CreateInstance(nationalAccountNumber);

            var bic           = OnlyAllowedCharacters(abAccountNumber.BIC);
            var accountNumber = OnlyAllowedCharacters(abAccountNumber.AccountNumber);

            if (String.IsNullOrEmpty(bic))
            {
                throw new ArgumentException("The bic is missing.");
            }
            if (String.IsNullOrEmpty(accountNumber))
            {
                throw new ArgumentException("The account number is missing.");
            }

            var bban = String.Format(BBANFormatString, bic, accountNumber);

            bban = bban.Replace(' ', '0');
            bban = ConvertCharactersToNumbers(bban);

            Log.DebugFormat("calculating checksum for bban {0}", bban);

            var modulo = 98 - ValidationMethodsTools.CalculateModulo(bban, 97);
            var iban   = String.Format(IBANFormatString, IBANPrefix, modulo, bic, accountNumber);

            iban = iban.Replace(' ', '0');

            Log.DebugFormat("generated IBAN: {0}", iban);

            if (iban.Length != IBANLength)
            {
                throw new InvalidOperationException(String.Format("Couldn't generate a valid IBAN from the bic {0} and the account number {1}.", bic, accountNumber));
            }

            return(iban);
        }
Beispiel #12
0
        /// <summary>
        /// Calculates the check digit.
        /// The account number is given without a check digit.
        /// </summary>
        /// <param name="accountNumber">The account number without a check digit.</param>
        /// <returns>
        /// The calculated check digit for the given account number
        /// </returns>
        public string CalculateCheckDigit(NationalAccountNumber accountNumber)
        {
            if (accountNumber == null)
            {
                throw new ArgumentNullException("accountNumber", "Please provide an account number.");
            }

            var norwayAccountNumber = new NorwayAccountNumber(accountNumber);

            if (String.IsNullOrEmpty(norwayAccountNumber.BankCode))
            {
                throw new ArgumentException("The bank code is missing.", "accountNumber");
            }
            if (String.IsNullOrEmpty(norwayAccountNumber.AccountNumber))
            {
                throw new ArgumentException("The account number is missing.", "accountNumber");
            }

            var bankCodeWithBranch =
                String.Format("{0,4}{1,6}", norwayAccountNumber.BankCode, norwayAccountNumber.AccountNumber).Replace(' ', '0');

            return(validationMethod.CalculateCheckDigit(bankCodeWithBranch));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SpainAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public SpainAccountNumber(NationalAccountNumber other)
     : base(other, Country.Spain)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="IsraelAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public IsraelAccountNumber(NationalAccountNumber other)
     : base(other, Country.Israel)
 {
 }
 /// <summary>
 /// Should create a new instance of the country specific account number
 /// </summary>
 /// <param name="other">another instance which should be wrapped. can be null</param>
 /// <returns></returns>
 protected override AccountAndBankCodeNumber CreateInstance(NationalAccountNumber other)
 {
     return(other == null ? new BelgiumAccountNumber() : new BelgiumAccountNumber(other));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="KuwaitAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public KuwaitAccountNumber(NationalAccountNumber other)
     : base(other, Country.Kuwait)
 {
 }
Beispiel #17
0
 /// <summary>
 /// Should create a new instance of the country specific account number
 /// </summary>
 /// <param name="other">another instance which should be wrapped. can be null</param>
 /// <returns></returns>
 protected override AccountBankCodeAndBranchNumber CreateInstance(NationalAccountNumber other)
 {
     return(other == null ? new ItalyAccountNumber() : new ItalyAccountNumber(other));
 }
Beispiel #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnitedArabEmiratesAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public UnitedArabEmiratesAccountNumber(NationalAccountNumber other)
     : base(other, Country.UnitedArabEmirates)
 {
 }
Beispiel #19
0
 /// <summary>
 /// Should create a new instance of the country specific account number
 /// </summary>
 /// <param name="other">another instance which should be wrapped. can be null</param>
 /// <returns></returns>
 protected override AccountBankCodeAndBranchNumber CreateInstance(NationalAccountNumber other)
 {
     return(other == null ? new BosniaAndHerzegovinaAccountNumber() : new BosniaAndHerzegovinaAccountNumber(other));
 }
Beispiel #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="KazakhstanAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public KazakhstanAccountNumber(NationalAccountNumber other)
     : base(other, Country.Kazakhstan)
 {
 }
Beispiel #21
0
 /// <summary>
 /// Converts a national account number to an IBAN
 /// </summary>
 /// <param name="nationalAccountNumber">The national account number.</param>
 /// <returns></returns>
 public static string ToIBAN(this NationalAccountNumber nationalAccountNumber)
 {
     return(conversion.ToIBAN(nationalAccountNumber));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="PolandAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public PolandAccountNumber(NationalAccountNumber other)
     : base(other, Country.Poland)
 {
 }
 /// <summary>
 /// Should create a new instance of the country specific account number
 /// </summary>
 /// <param name="other">another instance which should be wrapped. can be null</param>
 /// <returns></returns>
 protected override AccountAndBICNumber CreateInstance(NationalAccountNumber other)
 {
     return(other == null ? new RomaniaAccountNumber() : new RomaniaAccountNumber(other));
 }
Beispiel #24
0
 /// <summary>
 /// Should create a new instance of the country specific account number
 /// </summary>
 /// <param name="other">another instance which should be wrapped. can be null</param>
 /// <returns></returns>
 protected override AccountAndBankCodeNumber CreateInstance(NationalAccountNumber other)
 {
     return(other == null ? new DominicanRepublicAccountNumber() : new DominicanRepublicAccountNumber(other));
 }
Beispiel #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BahrainAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public BahrainAccountNumber(NationalAccountNumber other)
     : base(other, Country.Bahrain)
 {
 }
Beispiel #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LatviaAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public LatviaAccountNumber(NationalAccountNumber other)
     : base(other, Country.Latvia)
 {
 }
 /// <summary>
 /// Should create a new instance of the country specific account number
 /// </summary>
 /// <param name="other">another instance which should be wrapped. can be null</param>
 /// <returns></returns>
 protected abstract AccountBICAndBranchNumber CreateInstance(NationalAccountNumber other);
Beispiel #28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TunisiaAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public TunisiaAccountNumber(NationalAccountNumber other)
     : base(other, Country.Tunisia)
 {
 }
Beispiel #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GreenlandAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public GreenlandAccountNumber(NationalAccountNumber other)
     : base(other, Country.Greenland)
 {
 }
Beispiel #30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SwedenAccountNumber"/> class.
 /// </summary>
 /// <param name="other">The other.</param>
 public SwedenAccountNumber(NationalAccountNumber other)
     : base(other, Country.Sweden)
 {
 }