001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class CreditCard {
026    
027            public static String hide(String number) {
028                    return hide(number, StringPool.STAR);
029            }
030    
031            public static String hide(String number, String x) {
032                    if (number == null) {
033                            return number;
034                    }
035    
036                    int numberLen = number.length();
037    
038                    if (numberLen > 4) {
039                            StringBundler sb = new StringBundler(numberLen - 3);
040    
041                            for (int i = 0; i < numberLen - 4; i++) {
042                                    sb.append(x);
043                            }
044    
045                            sb.append(number.substring(numberLen - 4, numberLen));
046    
047                            number = sb.toString();
048                    }
049    
050                    return number;
051            }
052    
053            public static boolean isValid(String number, String type) {
054                    number = StringUtil.extractDigits(number);
055    
056                    if (type.equals("visa")) {
057                            if (!number.startsWith("4")) {
058                                    return false;
059                            }
060    
061                            if (number.length() != 13 &&
062                                    number.length() != 16) {
063    
064                                    return false;
065                            }
066                    }
067                    else if (type.equals("mastercard")) {
068                            if (!number.startsWith("51") &&
069                                    !number.startsWith("52") &&
070                                    !number.startsWith("53") &&
071                                    !number.startsWith("54") &&
072                                    !number.startsWith("55")) {
073    
074                                    return false;
075                            }
076    
077                            if (number.length() != 16) {
078                                    return false;
079                            }
080                    }
081                    else if (type.equals("discover")) {
082                            if (!number.startsWith("6011")) {
083    
084                                    return false;
085                            }
086    
087                            if (number.length() != 16) {
088                                    return false;
089                            }
090                    }
091                    else if (type.equals("amex")) {
092                            if (!number.startsWith("34") &&
093                                    !number.startsWith("35") &&
094                                    !number.startsWith("36") &&
095                                    !number.startsWith("37")) {
096    
097                                    return false;
098                            }
099    
100                            if (number.length() != 15) {
101                                    return false;
102                            }
103                    }
104    
105                    return Validator.isLUHN(number);
106            }
107    
108    }