1 21 22 package org.apache.commons.validator; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import org.apache.commons.validator.util.Flags; 29 30 48 public class CreditCardValidator { 49 50 62 public static final int NONE = 0; 63 64 67 public static final int AMEX = 1 << 0; 68 69 72 public static final int VISA = 1 << 1; 73 74 77 public static final int MASTERCARD = 1 << 2; 78 79 82 public static final int DISCOVER = 1 << 3; 83 84 87 private Collection cardTypes = new ArrayList (); 88 89 92 public CreditCardValidator() { 93 this(AMEX + VISA + MASTERCARD + DISCOVER); 94 } 95 96 102 public CreditCardValidator(int options) { 103 super(); 104 105 Flags f = new Flags(options); 106 if (f.isOn(VISA)) { 107 this.cardTypes.add(new Visa()); 108 } 109 110 if (f.isOn(AMEX)) { 111 this.cardTypes.add(new Amex()); 112 } 113 114 if (f.isOn(MASTERCARD)) { 115 this.cardTypes.add(new Mastercard()); 116 } 117 118 if (f.isOn(DISCOVER)) { 119 this.cardTypes.add(new Discover()); 120 } 121 } 122 123 127 public boolean isValid(String card) { 128 if ((card == null) || (card.length() < 13) || (card.length() > 19)) { 129 return false; 130 } 131 132 if (!this.luhnCheck(card)) { 133 return false; 134 } 135 136 Iterator types = this.cardTypes.iterator(); 137 while (types.hasNext()) { 138 CreditCardType type = (CreditCardType) types.next(); 139 if (type.matches(card)) { 140 return true; 141 } 142 } 143 144 return false; 145 } 146 147 153 public void addAllowedCardType(CreditCardType type){ 154 this.cardTypes.add(type); 155 } 156 157 161 protected boolean luhnCheck(String cardNumber) { 162 int digits = cardNumber.length(); 164 int oddOrEven = digits & 1; 165 long sum = 0; 166 for (int count = 0; count < digits; count++) { 167 int digit = 0; 168 try { 169 digit = Integer.parseInt(cardNumber.charAt(count) + ""); 170 } catch(NumberFormatException e) { 171 return false; 172 } 173 174 if (((count & 1) ^ oddOrEven) == 0) { digit *= 2; 176 if (digit > 9) { 177 digit -= 9; 178 } 179 } 180 sum += digit; 181 } 182 183 return (sum == 0) ? false : (sum % 10 == 0); 184 } 185 186 191 public interface CreditCardType { 192 193 204 boolean matches(String card); 205 206 } 207 208 private class Visa implements CreditCardType { 209 private static final String PREFIX = "4"; 210 public boolean matches(String card) { 211 return ( 212 card.substring(0, 1).equals(PREFIX) 213 && (card.length() == 13 || card.length() == 16)); 214 } 215 } 216 217 private class Amex implements CreditCardType { 218 private static final String PREFIX = "34,37,"; 219 public boolean matches(String card) { 220 String prefix2 = card.substring(0, 2) + ","; 221 return ((PREFIX.indexOf(prefix2) != -1) && (card.length() == 15)); 222 } 223 } 224 225 private class Discover implements CreditCardType { 226 private static final String PREFIX = "6011"; 227 public boolean matches(String card) { 228 return (card.substring(0, 4).equals(PREFIX) && (card.length() == 16)); 229 } 230 } 231 232 private class Mastercard implements CreditCardType { 233 private static final String PREFIX = "51,52,53,54,55,"; 234 public boolean matches(String card) { 235 String prefix2 = card.substring(0, 2) + ","; 236 return ((PREFIX.indexOf(prefix2) != -1) && (card.length() == 16)); 237 } 238 } 239 240 } 241 | Popular Tags |