001    /**
002     * Copyright (c) 2000-2013 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.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) && (number.length() != 16)) {
086                                    return false;
087                            }
088                    }
089                    else if (type.equals("mastercard")) {
090                            if (!number.startsWith("51") &&
091                                    !number.startsWith("52") &&
092                                    !number.startsWith("53") &&
093                                    !number.startsWith("54") &&
094                                    !number.startsWith("55")) {
095    
096                                    return false;
097                            }
098    
099                            if (number.length() != 16) {
100                                    return false;
101                            }
102                    }
103                    else if (type.equals("discover")) {
104                            if (!number.startsWith("6011")) {
105                                    return false;
106                            }
107    
108                            if (number.length() != 16) {
109                                    return false;
110                            }
111                    }
112                    else if (type.equals("amex")) {
113                            if (!number.startsWith("34") &&
114                                    !number.startsWith("35") &&
115                                    !number.startsWith("36") &&
116                                    !number.startsWith("37")) {
117    
118                                    return false;
119                            }
120    
121                            if (number.length() != 15) {
122                                    return false;
123                            }
124                    }
125    
126                    return Validator.isLUHN(number);
127            }
128    
129    }