001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util;
016    
017    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
018    import com.liferay.portal.kernel.util.CalendarUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.TimeZoneUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.util.Calendar;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class CreditCard {
032    
033            public static String hide(String number) {
034                    return hide(number, StringPool.STAR);
035            }
036    
037            public static String hide(String number, String x) {
038                    if (number == null) {
039                            return number;
040                    }
041    
042                    int numberLen = number.length();
043    
044                    if (numberLen > 4) {
045                            StringBundler sb = new StringBundler(numberLen - 3);
046    
047                            for (int i = 0; i < numberLen - 4; i++) {
048                                    sb.append(x);
049                            }
050    
051                            sb.append(number.substring(numberLen - 4, numberLen));
052    
053                            number = sb.toString();
054                    }
055    
056                    return number;
057            }
058    
059            public static boolean isValidExpirationDate(
060                    int expirationMonth, int expirationYear) {
061    
062                    Calendar calendar = CalendarFactoryUtil.getCalendar(
063                            TimeZoneUtil.getDefault(), LocaleUtil.getDefault());
064    
065                    if (CalendarUtil.isFuture(expirationMonth, expirationYear)) {
066                            return true;
067                    }
068                    else if ((expirationMonth == calendar.get(Calendar.MONTH)) &&
069                                     (expirationYear == calendar.get(Calendar.YEAR))) {
070    
071                            return true;
072                    }
073    
074                    return false;
075            }
076    
077            public static boolean isValidNumber(String number, String type) {
078                    number = StringUtil.extractDigits(number);
079    
080                    if (type.equals("visa")) {
081                            if (!number.startsWith("4")) {
082                                    return false;
083                            }
084    
085                            if (number.length() != 13 &&
086                                    number.length() != 16) {
087    
088                                    return false;
089                            }
090                    }
091                    else if (type.equals("mastercard")) {
092                            if (!number.startsWith("51") &&
093                                    !number.startsWith("52") &&
094                                    !number.startsWith("53") &&
095                                    !number.startsWith("54") &&
096                                    !number.startsWith("55")) {
097    
098                                    return false;
099                            }
100    
101                            if (number.length() != 16) {
102                                    return false;
103                            }
104                    }
105                    else if (type.equals("discover")) {
106                            if (!number.startsWith("6011")) {
107    
108                                    return false;
109                            }
110    
111                            if (number.length() != 16) {
112                                    return false;
113                            }
114                    }
115                    else if (type.equals("amex")) {
116                            if (!number.startsWith("34") &&
117                                    !number.startsWith("35") &&
118                                    !number.startsWith("36") &&
119                                    !number.startsWith("37")) {
120    
121                                    return false;
122                            }
123    
124                            if (number.length() != 15) {
125                                    return false;
126                            }
127                    }
128    
129                    return Validator.isLUHN(number);
130            }
131    
132    }