From 85901209dd91d38eb403751186db4e4fe24c52ed Mon Sep 17 00:00:00 2001 From: Valkov Dmitry Date: Wed, 1 Feb 2023 14:49:44 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20iban-?= =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- .../Service/Generators/IbanGenerator.ts | 74 +++++++++++-------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/manifest.json b/manifest.json index 199cf69..dd831d8 100644 --- a/manifest.json +++ b/manifest.json @@ -27,7 +27,7 @@ "manifest_version": 2, "permissions": [ "tabs", "contextMenus", "\u003Call_urls>", "storage" ], "short_name": "SVEAK helper", - "version": "1.0.0", + "version": "1.0.1", "commands": { "_execute_browser_action": { "suggested_key": { diff --git a/popup/src/classes/Service/Generators/IbanGenerator.ts b/popup/src/classes/Service/Generators/IbanGenerator.ts index 4532b79..80dc280 100644 --- a/popup/src/classes/Service/Generators/IbanGenerator.ts +++ b/popup/src/classes/Service/Generators/IbanGenerator.ts @@ -2,16 +2,16 @@ import { Country } from '@/classes/Enum/Country' export class IbanGenerator { generate (country: Country) { - const t = this.getBankAccount(country) - const n = this.getNationalCheckDigit(t) - const r = this.getBankCode(country) + const shortBankAccount = this.getBankAccount(country) + const bankCode = this.getBankCode(country) + const estonianCheckDigit = this.getEstonianCheckDigit(bankCode + shortBankAccount) switch (country) { case Country.ESTONIA: - return country.toUpperCase() + this.getIbanCheckDigits(r + t + n + '141400') + r + t + n + return country.toUpperCase() + this.getIbanCheckDigits(bankCode + shortBankAccount + estonianCheckDigit + '141400') + bankCode + shortBankAccount + estonianCheckDigit case Country.LITHUANIA: - return country.toUpperCase() + this.getIbanCheckDigits(r + t + '212900') + r + t + return country.toUpperCase() + this.getIbanCheckDigits(bankCode + shortBankAccount + '212900') + bankCode + shortBankAccount default: - return country.toUpperCase() + this.getIbanCheckDigits(r + t + '252100') + r + t + return country.toUpperCase() + this.getIbanCheckDigits(bankCode + shortBankAccount + '252100') + bankCode + shortBankAccount } } @@ -26,36 +26,50 @@ export class IbanGenerator { return t + String(i - (o % i)).padStart(2, '0') } - getNationalCheckDigit (e: string) { - const t = [7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7] - const n = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - let r = 0 - const i = e.length - let o = 18 - for (let a = i - 1; a > -1; a--) { - n[o] = t[o] * +e[a] - o-- - } - o = 18 - for (let a = i - 1; a > -1; a--) { - r -= -n[o] - o-- + /** + * + * @param bban c 5 знака 16 символов + * @return одну цифру + */ + getEstonianCheckDigit (bban: string) { + const toCheck = bban.substring(2, 15) + const weights = [7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7] + + let sum = 0 + for (let index = 0; index < toCheck.length; index++) { + sum += parseInt(toCheck.charAt(index), 10) * weights[index] } + const remainder = sum % 10 - return String(r % 10 !== 0 ? 10 - r % 10 : 0) - } + return remainder === 0 ? 0 : 10 - remainder + }; - getBankCode (e: Country) { - const t = ['16000003', '11602231', '10301612', '18900002', '15002240', '19401018', '19101152'] - const n = ['2200', '1010', '3300', '7700', '1700', '4204'] - const r = ['20900', '72900', '74000', '40100', '21200', '72300', '21700', '21400', '70440', '71800', '73000'] - switch (e) { + getBankCode (country: Country) { + const plBankCodes = [ + '16000003', // (BNPPL Centrala) BNP PARIBAS BANK POLSKA SA Centrala + '11602231', // Bank Millennium SA Bankowy Punkt Rozlicze Nr 4 + '10301612', // BH Regionalne Centrum Rozliczeń + '18900002', // HYPO Centrala + '15002240', // KBSA O. w Ełku + '19401018', // CABP F. nr 3 we Wrocławiu + '19101152' + ] + const eeBankCodes = [ + '2200', // Swedbank + '1010', // SEB + '3300', // Danske Bank A/S Eesti filiaal (Sampo Pank) + '7700', // LHV Pank + '1700', // Luminor + '4204' // COOP Bank + ] + const ltBankCodes = ['20900', '72900', '74000', '40100', '21200', '72300', '21700', '21400', '70440', '71800', '73000'] + switch (country) { case Country.POLAND: - return t[Math.floor(Math.random() * t.length)] + return plBankCodes[Math.floor(Math.random() * plBankCodes.length)] case Country.ESTONIA: - return n[Math.floor(Math.random() * n.length)] + return eeBankCodes[Math.floor(Math.random() * eeBankCodes.length)] default: - return r[Math.floor(Math.random() * r.length)] + return ltBankCodes[Math.floor(Math.random() * ltBankCodes.length)] } }