diff --git a/org.adempiere.base/.classpath b/org.adempiere.base/.classpath index 8fea4bc2c7..15964d86e7 100644 --- a/org.adempiere.base/.classpath +++ b/org.adempiere.base/.classpath @@ -15,6 +15,7 @@ + diff --git a/org.adempiere.base/META-INF/MANIFEST.MF b/org.adempiere.base/META-INF/MANIFEST.MF index 36ab982535..a64afe48db 100644 --- a/org.adempiere.base/META-INF/MANIFEST.MF +++ b/org.adempiere.base/META-INF/MANIFEST.MF @@ -12,7 +12,8 @@ Bundle-ClassPath: base.jar, jfreechart-1.0.19.jar, jnlp.jar, barcode4j-2.1.jar, - bsh-2.0b6.jar + bsh-2.0b6.jar, + commons-validator-1.6.jar Export-Package: bsh, bsh.classpath, bsh.collection, diff --git a/org.adempiere.base/build.properties b/org.adempiere.base/build.properties index cfe1fab328..85f93f6cad 100644 --- a/org.adempiere.base/build.properties +++ b/org.adempiere.base/build.properties @@ -27,7 +27,8 @@ bin.includes = META-INF/,\ OSGI-INF/addressvalidationeventhandler.xml,\ schema/,\ barcode4j-2.1.jar,\ - bsh-2.0b6.jar + bsh-2.0b6.jar,\ + commons-validator-1.6.jar output.base.jar = bin/ src.includes = schema/ source.base.jar = src/ diff --git a/org.adempiere.base/copyjars.xml b/org.adempiere.base/copyjars.xml index cb9a07c0c4..385fdb2d80 100644 --- a/org.adempiere.base/copyjars.xml +++ b/org.adempiere.base/copyjars.xml @@ -11,6 +11,7 @@ + diff --git a/org.adempiere.base/src/org/compiere/model/MBPBankAccount.java b/org.adempiere.base/src/org/compiere/model/MBPBankAccount.java index 28fb46e405..5d7cbcb9e8 100644 --- a/org.adempiere.base/src/org/compiere/model/MBPBankAccount.java +++ b/org.adempiere.base/src/org/compiere/model/MBPBankAccount.java @@ -213,7 +213,7 @@ public class MBPBankAccount extends X_C_BP_BankAccount if (MSysConfig.getBooleanValue(MSysConfig.IBAN_VALIDATION, true, Env.getAD_Client_ID(Env.getCtx()))) { if (!Util.isEmpty(getIBAN())) { setIBAN(IBAN.normalizeIBAN(getIBAN())); - if (!IBAN.isCheckDigitValid(getIBAN())) { + if (!IBAN.isValid(getIBAN())) { log.saveError("Error", "IBAN is invalid"); return false; } diff --git a/org.adempiere.base/src/org/compiere/model/MBankAccount.java b/org.adempiere.base/src/org/compiere/model/MBankAccount.java index 8f4c076123..eac26ff408 100644 --- a/org.adempiere.base/src/org/compiere/model/MBankAccount.java +++ b/org.adempiere.base/src/org/compiere/model/MBankAccount.java @@ -135,7 +135,7 @@ public class MBankAccount extends X_C_BankAccount if (MSysConfig.getBooleanValue(MSysConfig.IBAN_VALIDATION, true, Env.getAD_Client_ID(Env.getCtx()))) { if (!Util.isEmpty(getIBAN())) { setIBAN(IBAN.normalizeIBAN(getIBAN())); - if (!IBAN.isCheckDigitValid(getIBAN())) { + if (!IBAN.isValid(getIBAN())) { log.saveError("Error", "IBAN is invalid"); return false; } diff --git a/org.adempiere.base/src/org/compiere/model/MPayment.java b/org.adempiere.base/src/org/compiere/model/MPayment.java index 1399cb8bd2..1aff56459b 100644 --- a/org.adempiere.base/src/org/compiere/model/MPayment.java +++ b/org.adempiere.base/src/org/compiere/model/MPayment.java @@ -801,7 +801,7 @@ public class MPayment extends X_C_Payment if (MSysConfig.getBooleanValue(MSysConfig.IBAN_VALIDATION, true, Env.getAD_Client_ID(Env.getCtx()))) { if (!Util.isEmpty(getIBAN())) { setIBAN(IBAN.normalizeIBAN(getIBAN())); - if (!IBAN.isCheckDigitValid(getIBAN())) { + if (!IBAN.isValid(getIBAN())) { log.saveError("Error", "IBAN is invalid"); return false; } diff --git a/org.adempiere.base/src/org/compiere/model/MPaymentTransaction.java b/org.adempiere.base/src/org/compiere/model/MPaymentTransaction.java index 2bbbb56fa5..c367709afa 100644 --- a/org.adempiere.base/src/org/compiere/model/MPaymentTransaction.java +++ b/org.adempiere.base/src/org/compiere/model/MPaymentTransaction.java @@ -86,7 +86,7 @@ public class MPaymentTransaction extends X_C_PaymentTransaction implements Proce if (MSysConfig.getBooleanValue(MSysConfig.IBAN_VALIDATION, true, Env.getAD_Client_ID(Env.getCtx()))) { if (!Util.isEmpty(getIBAN())) { setIBAN(IBAN.normalizeIBAN(getIBAN())); - if (!IBAN.isCheckDigitValid(getIBAN())) { + if (!IBAN.isValid(getIBAN())) { log.saveError("Error", "IBAN is invalid"); return false; } diff --git a/org.adempiere.base/src/org/compiere/util/IBAN.java b/org.adempiere.base/src/org/compiere/util/IBAN.java index 30b2d62fee..3cbb374fc4 100644 --- a/org.adempiere.base/src/org/compiere/util/IBAN.java +++ b/org.adempiere.base/src/org/compiere/util/IBAN.java @@ -1,11 +1,19 @@ -// adapted from http://how-blogz.blogspot.de/2007/08/java-iban-check-digit-validation.html -// soastation.banking.IBAN - +/****************************************************************************** + * Product: iDempiere Business Suite ERP/CRM/SCM * + * Copyright (C) 2017 Markus Bozem * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * + *****************************************************************************/ package org.compiere.util; -import java.math.BigInteger; - -import java.util.ResourceBundle; +import org.apache.commons.validator.routines.IBANValidator; public class IBAN { @@ -22,73 +30,16 @@ public class IBAN { } return null ; } + /** - * Determines if the given IBAN is valid based on the check digit. To - * validate the checksum: 1. Check that the total IBAN length is correct as - * per the country. If not, the IBAN is invalid. 2. Move the four initial - * characters to the end of the string. 3. Replace the letters in the string - * with digits, expanding the string as necessary, such that A=10, B=11 and - * Z=35. 4. Convert the string to an integer and mod-97 the entire number. - * If the remainder is 1 you have a valid IBAN number. - * * @param iban - * @return boolean indicating if specific IBAN has a valid check digit + * @return boolean indicating if specific IBAN is valid */ - public static boolean isCheckDigitValid(String iban) { - try { - if (null == iban) - return false; - int validIBANLength = getValidIBANLength(iban); - if (validIBANLength < 4) - return false; - if (iban.length() != validIBANLength) - return false; - - BigInteger numericIBAN = getNumericIBAN(iban, false); - - int checkDigit = numericIBAN.mod(new BigInteger("97")).intValue(); - return checkDigit == 1; - } catch (Exception e) { - return false; - } + + public static boolean isValid(String iban) { + IBANValidator iBANValidator = IBANValidator.getInstance() ; + + return iBANValidator.isValid(iban) ; } - - /** - * Using the IBAN.properties file gets the valid fixed length value for a - * country code. Only uses the first 2 characters of the given string. - * - * @param countryCode - * @return - */ - public static int getValidIBANLength(String countryCode) { - String code = countryCode.substring(0, 2).toUpperCase(); - String length = ResourceBundle.getBundle(IBAN.class.getCanonicalName()).getString("length." + code); - if (length == null) - return -1; - return Integer.valueOf(length).intValue(); - } - - private static BigInteger getNumericIBAN(String iban, boolean isCheckDigitAtEnd) { - String endCheckDigitIBAN = iban; - if (!isCheckDigitAtEnd) { - // Move first four characters to end of string to put check digit at - // end - endCheckDigitIBAN = iban.substring(4) + iban.substring(0, 4); - } - StringBuffer numericIBAN = new StringBuffer(); - for (int i = 0; i < endCheckDigitIBAN.length(); i++) { - if (Character.isDigit(endCheckDigitIBAN.charAt(i))) { - numericIBAN.append(endCheckDigitIBAN.charAt(i)); - } else { - numericIBAN.append(10 + getAlphabetPosition(endCheckDigitIBAN.charAt(i))); - } - } - - return new BigInteger(numericIBAN.toString()); - } - - private static int getAlphabetPosition(char letter) { - return Character.valueOf(Character.toUpperCase(letter)).compareTo(Character.valueOf('A')); - } - + } diff --git a/org.adempiere.base/src/org/compiere/util/IBAN.properties b/org.adempiere.base/src/org/compiere/util/IBAN.properties deleted file mode 100644 index b7f8ee6107..0000000000 --- a/org.adempiere.base/src/org/compiere/util/IBAN.properties +++ /dev/null @@ -1,45 +0,0 @@ -# IBAN related properties. Includes IBAN lengths for different countries -length.AD=24 -length.AT=20 -length.BE=16 -length.BA=20 -length.BG=22 -length.CH=21 -length.CY=28 -length.CZ=24 -length.DE=22 -length.DK=18 -length.EE=20 -length.ES=24 -length.FO=18 -length.FI=18 -length.FR=27 -length.GB=22 -length.GI=23 -length.GL=18 -length.GR=27 -length.HU=28 -length.HR=21 -length.IE=22 -length.IS=26 -length.IT=27 -length.LI=21 -length.LT=20 -length.LU=20 -length.LV=21 -length.MA=24 -length.MC=27 -length.MK=19 -length.MT=31 -length.NL=18 -length.NO=15 -length.PL=28 -length.PT=25 -length.RO=24 -length.RS=22 -length.SE=24 -length.SI=19 -length.SK=24 -length.SM=27 -length.TN=24 -length.TR=26