/// <summary> /// Calculates the modulo of 97 for a iban. /// </summary> /// <param name="iban">The given iban for which the modulo 97 should be calculated.</param> /// <returns>The calculated result.</returns> /// <exception cref="IbanException"> /// <list type="table"> /// <item> /// <term><see cref="RuleType"/></term> /// <description>IbanGeneratingCheckDigit</description> /// <description>When check digit could not be calculated.</description> /// </item> /// </list> /// </exception> protected int Modulo97(Iban iban) { try { if (string.IsNullOrWhiteSpace(iban.CheckDigit)) iban.CheckDigit = "00"; string input = this.BbanToNumber(iban.BBAN) + CountryCodeToNumber(iban.Country.CountryCode) + iban.CheckDigit; string output = string.Empty; for (int i = 0; i < input.Length; i++) { if (output.Length < 9) output += input[i]; else { output = (Int32.Parse(output) % 97).ToString() + input[i]; } } return Int32.Parse(output) % 97; } catch (Exception ex) { throw new IbanException(ex, IbanExceptionType.IbanGeneratingCheckDigit); } }
/// <summary> /// Compares two iban objects. /// </summary> /// <param name="obj">The iban object the current object should be compared to.</param> /// <returns>'True' if the two object are equal, otherwise 'false'.</returns> public override bool Equals(object obj) { Iban iban = (Iban)obj; if (this.IBAN.Equals(iban.IBAN) && this.BBAN.Equals(iban.BBAN) && this.Country.Equals(iban.Country) && this.CheckDigit.Equals(iban.CheckDigit) && this.Bank.BankIdentification.Equals(iban.Bank.BankIdentification) && this.AccountNumber.Equals(iban.AccountNumber)) { return(true); } return(false); }
/// <summary> /// Considers rules. When there are rules that match to bank ident or account number, those rules take effect. /// </summary> /// <param name="bank">The bank, for which the rules depend to.</param> /// <param name="rule">The rules.</param> /// <param name="country">The country the rules depend to.</param> /// <param name="accountNumber">The account number for which the rules should be checked for.</param> /// <returns>The generated result.</returns> /// <exception cref="IbanException"> /// <list type="table"> /// <item> /// <term><see cref="RuleType"/></term> /// <description>NoCalculation</description> /// <description>When iban calculation is not allowed.</description> /// </item> /// </list> /// </exception> private IbanBic ConsiderRule(Bank bank, Rule rule, Country country, string accountNumber) { string temp_bank_ident = bank.BankIdentification; string temp_account_number = accountNumber; string temp_bic = bank.BIC.Bic; Iban iban = new Iban(); IbanBic result = new IbanBic(); // Check if rule is 000000 if (rule.RuleID.Equals("000000")) { iban.Bank = new Bank(); iban.Bank.BankIdentification = bank.BankIdentification; iban.AccountNumber = accountNumber; result.IBAN = iban; result.BIC = new BankIdentifierCode() { Bic = temp_bic }; return result; } // Check if rule is 000100 if (rule.RuleID.Equals("000100")) throw new IbanException(IbanExceptionType.NoCalculation); // delete leading 0s in account number temp_account_number = temp_account_number.TrimStart(new char[] { '0' }); // consider each possible rule // 1. No Calculation if ((from count in rule.RuleElements where count.RuleType == RuleType.No_Calculation && (from bi in count.Childs where bi.RuleType == RuleType.Kto_Number_Range && _regexpHelper.RegexpMatch(bi.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident) && _regexpHelper.RegexpMatch(bi.Data, temp_account_number) select bi).Count() == 1 select count).Count() > 0) { // No calculation throw new IbanException(IbanExceptionType.NoCalculation); } // 2. Kto Mapping if ((from count in rule.RuleElements where count.RuleType == RuleType.Mappings_Kto && _regexpHelper.RegexpMatch(count.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident) && (from mapp in count.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_account_number) select mapp).Count() == 1 select count).Count() == 1) { var kto_mapping = (from kto in rule.RuleElements where kto.RuleType == RuleType.Mappings_Kto && _regexpHelper.RegexpMatch(kto.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident) && (from mapp in kto.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_account_number) select mapp).Count() == 1 select kto).First(); // account number temp_account_number = (from mapp in kto_mapping.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_account_number) select mapp.Data).First(); // should also bank ident be mapped? if (kto_mapping.Attributes.Where(t => t.Key.Equals("blz_new")).Select(t => t.Value).Count() == 1) { // there should also be a bank ident mapping temp_bank_ident = kto_mapping.Attributes.Where(t => t.Key.Equals("blz_new")).Select(t => t.Value).First(); // if bank ident has changed, get new BIC using (Bank bank_temp = _dataService.LoadBank(temp_bank_ident)) { temp_bic = bank_temp.BIC.Bic; } } } // 3. KtoKr Mapping if ((from count in rule.RuleElements where count.RuleType == RuleType.Mappings_KtoKr && _regexpHelper.RegexpMatch(count.Attributes.Where(t => t.Key.Equals("kto")).Select(t => t.Value).First(), temp_account_number) && (from mapp in count.Childs where mapp.RuleType == RuleType.Mapping && mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First().Equals(temp_account_number.Substring(0, 3)) select mapp).Count() == 1 select count).Count() == 1) { var ktokr_mapping = (from ktokr in rule.RuleElements where ktokr.RuleType == RuleType.Mappings_KtoKr && _regexpHelper.RegexpMatch(ktokr.Attributes.Where(t => t.Key.Equals("kto")).Select(t => t.Value).First(), temp_account_number) && (from mapp in ktokr.Childs where mapp.RuleType == RuleType.Mapping && mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First().Equals(temp_account_number.Substring(0, 3)) select mapp).Count() == 1 select ktokr).First(); temp_bank_ident = (from mapp in ktokr_mapping.Childs where mapp.RuleType == RuleType.Mapping && mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First().Equals(temp_account_number.Substring(0, 3)) select mapp.Data).First(); // if bank ident has changed, get new BIC using (Bank bank_temp = _dataService.LoadBank(temp_bank_ident)) { temp_bic = bank_temp.BIC.Bic; } } // 4. Bank-Ident Mapping if ((from count in rule.RuleElements where count.RuleType == RuleType.Mappings_Blz && (from mapp in count.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_bank_ident) select mapp).Count() == 1 select count).Count() == 1) { var bankident_mapping = (from bi in rule.RuleElements where bi.RuleType == RuleType.Mappings_Blz && (from mapp in bi.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_bank_ident) select mapp).Count() == 1 select bi).First(); temp_bank_ident = (from mapp in bankident_mapping.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("from")).Select(t => t.Value).First(), temp_bank_ident) select mapp.Data).First(); // if bank ident has changed, get new BIC using (Bank bank_temp = _dataService.LoadBank(temp_bank_ident)) { temp_bic = bank_temp.BIC.Bic; } } // 5. Kto Mod if ((from count in rule.RuleElements where count.RuleType == RuleType.Modification_Kto && (from mapp in count.Childs where mapp.RuleType == RuleType.Modification && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident) && _regexpHelper.RegexpMatch(mapp.Data, temp_account_number) select mapp).Count() == 1 select count).Count() == 1) { var kto_mod = (from kto in rule.RuleElements where kto.RuleType == RuleType.Modification_Kto && (from mapp in kto.Childs where mapp.RuleType == RuleType.Modification && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident) && _regexpHelper.RegexpMatch(mapp.Data, temp_account_number) select mapp).Count() == 1 select kto).First(); var mod = (from mapp in kto_mod.Childs where mapp.RuleType == RuleType.Modification && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), temp_bank_ident) && _regexpHelper.RegexpMatch(mapp.Data, temp_account_number) select mapp).First(); _regexpHelper.RegexpMatch(mod.Data, temp_account_number, out temp_account_number); } // 6. BIC mapping if ((from count in rule.RuleElements where count.RuleType == RuleType.Mappings_Bic && (from mapp in count.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), bank.BankIdentification) select mapp).Count() == 1 select count).Count() == 1) { var mapp_bic = (from mbic in rule.RuleElements where mbic.RuleType == RuleType.Mappings_Bic && (from mapp in mbic.Childs where mapp.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(mapp.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), bank.BankIdentification) select mapp).Count() == 1 select mbic).First(); temp_bic = (from bic in mapp_bic.Childs where bic.RuleType == RuleType.Mapping && _regexpHelper.RegexpMatch(bic.Attributes.Where(t => t.Key.Equals("blz")).Select(t => t.Value).First(), bank.BankIdentification) select bic.Data).First(); } iban.AccountNumber = temp_account_number; // if bank ident changed, reload bank if (!temp_bank_ident.Equals(bank.BankIdentification)) { bank = _dataService.LoadBank(temp_bank_ident); } iban.Bank = bank; result.IBAN = iban; result.BIC = new BankIdentifierCode() { Bic = temp_bic }; return result; }
/// <summary> /// Converts a given iban as string into a Iban object. /// </summary> /// <param name="iban">The given iban as string.</param> /// <returns>The converted iban object.</returns> /// <exception cref="IbanException"> /// <list type="table"> /// <item> /// <term><see cref="RuleType"/></term> /// <description>IbanValidatingFormatting</description> /// <description>If iban format is not valid.</description> /// </item> /// </list> /// </exception> protected Iban ConvertStringToIban(string iban) { try { Iban converted = new Iban { Country = _dataService.LoadCountry((ECountry)Enum.Parse(typeof(ECountry), iban.Substring(0, 2), true)), CheckDigit = iban.Substring(2, 2), IBAN = iban }; converted.Bank = new Bank(); // get bank ident converted.Bank.BankIdentification = iban.Substring(4, Int32.Parse(converted.Country.BankIdentLength)); // get account number converted.AccountNumber = iban.Substring(4 + Int32.Parse(converted.Country.BankIdentLength), Int32.Parse(converted.Country.AccountNumberLength)); // check, if iban is well formatted if (!this.CheckIbanFormatting(converted)) throw new IbanException(IbanExceptionType.IbanValidatingFormatting); converted.BBAN = this.GetBBAN(converted); return converted; } catch (IbanException ex) { throw ex; } catch (Exception ex) { throw new IbanException(ex, IbanExceptionType.IbanValidatingFormatting); } }
/// <summary> /// Checks if a iban code fits to the country format rules. /// </summary> /// <param name="iban">The given iban.</param> /// <returns>'True' if the format is ok, otherwise 'false'.</returns> private bool CheckIbanFormatting(Iban iban) { RegexHelper regexpheler = new RegexHelper(); if (regexpheler.RegexpMatch(iban.Country.RegExp, iban.IBAN)) return true; return false; }
/// <summary> /// Gets the iban code out of a Iban object (which does not contain the iban code yet). /// </summary> /// <param name="iban">The given iban object.</param> /// <returns>The iban code as string.</returns> /// /// <exception cref="IbanException"> /// <list type="table"> /// <item> /// <term><see cref="RuleType"/></term> /// <description>IbanGeneratingNotAllParameters</description> /// <description>If not all parameters are available for iban converting.</description> /// </item> /// <item> /// <term><see cref="RuleType"/></term> /// <description>IbanGeneratingFormatting</description> /// <description>If iban format is not valid.</description> /// </item> /// </list> /// </exception> protected string GetIban(Iban iban) { // Check if all necessary parameters are set if (string.IsNullOrWhiteSpace(iban.AccountNumber) || string.IsNullOrWhiteSpace(iban.Bank.BankIdentification) || string.IsNullOrWhiteSpace(iban.BBAN) || string.IsNullOrWhiteSpace(iban.CheckDigit) || iban.Country == null || string.IsNullOrWhiteSpace(iban.Country.CountryCode)) { throw new IbanException(IbanExceptionType.IbanGeneratingNotAllParameters); } // iban formatting: CountryCode || check digit || bank ident || account number string IBAN = iban.Country.CountryCode + iban.CheckDigit + iban.BBAN; iban.IBAN = IBAN; // check if iban is correct formatted if (!this.CheckIbanFormatting(iban)) { // iban not well formatted -> error throw new IbanException(IbanExceptionType.IbanGeneratingFormatting); } return IBAN; }
/// <summary> /// Gets the bban out of an iban. /// </summary> /// <param name="iban">The given iban.</param> /// <returns>The bban.</returns> /// <exception cref="IbanException"> /// <list type="table"> /// <item> /// <term><see cref="RuleType"/></term> /// <description>BBANError</description> /// <description>When bban could not be calculated.</description> /// </item> /// </list> /// </exception> protected string GetBBAN(Iban iban) { if (string.IsNullOrWhiteSpace(iban.AccountNumber) || string.IsNullOrWhiteSpace(iban.Bank.BankIdentification)) { throw new IbanException(IbanExceptionType.BBANError); } string bban = iban.Bank.BankIdentification + iban.AccountNumber; return bban; }
/// <summary> /// Synchronous method to generates an iban code and get the bic for that code. /// </summary> /// <param name="countryCode">The country for which the iban should be generated.</param> /// <param name="bankIdent">The bank ident for which the iban should be generated.</param> /// <param name="accountNumber">The account number for which the iban should be generated.</param> /// <returns>The generated result which contains iban and bic.</returns> /// <exception cref="IbanException"> /// <list type="table"> /// <item> /// <term><see cref="RuleType"/></term> /// <description>BankIdentNotValid</description> /// <description>When a given bank id is not valid (no results in datastore).</description> /// </item> /// </list> /// </exception> public IbanBic GenerateIban(ECountry countryCode, string bankIdent, string accountNumber) { Country country = null; Bank bank = null; IbanBic result = null; // 1. load country country = _dataService.LoadCountry(countryCode); // 1.2 Check bank ident if (!this.CheckBankIdent(country, bankIdent)) throw new IbanException(IbanExceptionType.BankIdentNotValid); // 2. load bank bank = _dataService.LoadBank(bankIdent); // 3. create default Iban Iban iban = new Iban() { AccountNumber = accountNumber, Country = country }; // 3.1 create default bank iban.Bank = new Bank() { BankIdentification = bankIdent }; // 4 check if account number fits maximum length result = new IbanBic(); result.IBAN = iban; result.IBAN.AccountNumber = this.CheckAccountNumber(country, result.IBAN.AccountNumber); // 5. Generate iban result.IBAN.BBAN = this.GetBBAN(result.IBAN); result.IBAN.Country = country; result.IBAN.CheckDigit = (98 - this.Modulo97(result.IBAN)).ToString("00"); result.IBAN.IBAN = this.GetIban(result.IBAN); // 6. bic is null result.BIC = null; return result; }