private static bool hasValidCountryCode(string iban, out IbanFormatViolation validationResult) { validationResult = IbanFormatViolation.NO_VIOLATION; if (iban.Length < Consts.IBAN_COUNTRY_CODE_LENGTH) { validationResult = IbanFormatViolation.COUNTRY_CODE_TWO_LETTERS; } else { string countryCode = GetCountryCode(iban); if (!countryCode.Equals(countryCode.ToUpper()) || !char.IsLetter(iban[0]) || !char.IsLetter(iban[1])) { validationResult = IbanFormatViolation.COUNTRY_CODE_UPPER_CASE_LETTERS; } else { CountryCodeEntry countryEntry = CountryCode.GetCountryCode(countryCode); if (countryEntry == null) { validationResult = IbanFormatViolation.COUNTRY_CODE_EXISTS; } else { BBanStructure structure = Bban.GetStructureForCountry(countryEntry); if (structure == null) { validationResult = IbanFormatViolation.COUNTRY_CODE_UNSUPPORTED; } } } } return(validationResult == IbanFormatViolation.NO_VIOLATION); }
private static void validateCountryCode(string iban) { if (iban.Length < Consts.IBAN_COUNTRY_CODE_LENGTH) { throw new IbanFormatException("Input must contain 2 letters for country code", IbanFormatViolation.COUNTRY_CODE_TWO_LETTERS, iban); } string countryCode = GetCountryCode(iban); if (!countryCode.Equals(countryCode.ToUpper()) || !char.IsLetter(iban[0]) || !char.IsLetter(iban[1])) { throw new IbanFormatException("IBAN's country code must contain upper case letters", IbanFormatViolation.COUNTRY_CODE_UPPER_CASE_LETTERS, iban); } CountryCodeEntry countryEntry = CountryCode.GetCountryCode(countryCode); if (countryEntry == null) { throw new IbanFormatException("IBAN contains non existing country code", IbanFormatViolation.COUNTRY_CODE_EXISTS, iban); } BBanStructure structure = Bban.GetStructureForCountry(countryEntry); if (structure == null) { throw new UnsupportedCountryException("IBAN contains not supported country code", countryCode); } }
/// <summary> /// Format BBAN string /// </summary> /// <returns>BBAN string</returns> private string formatBban() { StringBuilder sb = new StringBuilder(); BBanStructure bbanStructure = Bban.GetStructureForCountry(_countryCodeEntry); if (bbanStructure == null) { throw new UnsupportedCountryException("Country code is not supported", _countryCodeEntry.Alpha2); } foreach (BBanEntry entry in bbanStructure.Entries) { switch (entry.EntryType) { case BBanEntryType.BANK_CODE: reformatBankCode(entry.Length); sb.Append(_bankCode); break; case BBanEntryType.BRANCH_CODE: sb.Append(_branchCode); break; case BBanEntryType.BALANCE_ACCOUNT_NUMBER: sb.Append(_balanceAccountNumber); break; case BBanEntryType.ACCOUNT_NUMBER_PREFIX: reformatAccountNumberPrefix(entry.Length); sb.Append(_accountNumberPrefix); break; case BBanEntryType.ACCOUNT_NUMBER: reformatAccountNumber(entry.Length); sb.Append(_accountNumber); break; case BBanEntryType.NATIONAL_CHECK_DIGIT: sb.Append(_nationalCheckDigit); break; case BBanEntryType.ACCOUNT_TYPE: sb.Append(_accountType); break; case BBanEntryType.OWNER_ACCOUNT_NUMBER: sb.Append(_ownerAccountType); break; case BBanEntryType.IDENTIFICATION_NUMBER: sb.Append(_identificationNumber); break; } } return(sb.ToString()); }
private static BBanStructure getBbanStructure(CountryCodeEntry countryCode) => Bban.GetStructureForCountry(countryCode);
/// <summary> /// Checks whether country is supported. /// It is checked by trying to find the country code in defined BBAN structures. /// </summary> /// <param name="alpha2Code">Alpha2 code for country</param> /// <returns>True if country code is supported, othewise false</returns> public static bool IsSupportedCountry(string alpha2Code) => (CountryCode.GetCountryCode(alpha2Code) != null) && (Bban.GetStructureForCountry(alpha2Code) != null);
/// <summary> /// Checks whether country is supported. /// It is checked by trying to find the country code in defined BBAN structures. /// </summary> /// <param name="countryCode">Country code object</param> /// <returns>True if country code is supported, othewise false</returns> public static bool IsSupportedCountry(CountryCodeEntry countryCode) => (CountryCode.GetCountryCode(countryCode?.Alpha2) != null) && (Bban.GetStructureForCountry(countryCode) != null);