001    /**
002     * Copyright (c) 2000-present 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     * @deprecated As of 7.0.0, replaced by {@link
031     *             com.liferay.shopping.util.CreditCard)}
032     */
033    @Deprecated
034    public class CreditCard {
035    
036            public static String hide(String number) {
037                    return hide(number, StringPool.STAR);
038            }
039    
040            public static String hide(String number, String x) {
041                    if (number == null) {
042                            return number;
043                    }
044    
045                    int numberLen = number.length();
046    
047                    if (numberLen > 4) {
048                            StringBundler sb = new StringBundler(numberLen - 3);
049    
050                            for (int i = 0; i < numberLen - 4; i++) {
051                                    sb.append(x);
052                            }
053    
054                            sb.append(number.substring(numberLen - 4, numberLen));
055    
056                            number = sb.toString();
057                    }
058    
059                    return number;
060            }
061    
062            public static boolean isValidExpirationDate(
063                    int expirationMonth, int expirationYear) {
064    
065                    Calendar calendar = CalendarFactoryUtil.getCalendar(
066                            TimeZoneUtil.getDefault(), LocaleUtil.getDefault());
067    
068                    if (CalendarUtil.isFuture(expirationMonth, expirationYear)) {
069                            return true;
070                    }
071                    else if ((expirationMonth == calendar.get(Calendar.MONTH)) &&
072                                     (expirationYear == calendar.get(Calendar.YEAR))) {
073    
074                            return true;
075                    }
076    
077                    return false;
078            }
079    
080            public static boolean isValidNumber(String number, String type) {
081                    number = StringUtil.extractDigits(number);
082    
083                    if (type.equals("visa")) {
084                            if (!number.startsWith("4")) {
085                                    return false;
086                            }
087    
088                            if ((number.length() != 13) && (number.length() != 16)) {
089                                    return false;
090                            }
091                    }
092                    else if (type.equals("mastercard")) {
093                            if (!number.startsWith("51") && !number.startsWith("52") &&
094                                    !number.startsWith("53") && !number.startsWith("54") &&
095                                    !number.startsWith("55")) {
096    
097                                    return false;
098                            }
099    
100                            if (number.length() != 16) {
101                                    return false;
102                            }
103                    }
104                    else if (type.equals("discover")) {
105                            if (!number.startsWith("6011")) {
106                                    return false;
107                            }
108    
109                            if (number.length() != 16) {
110                                    return false;
111                            }
112                    }
113                    else if (type.equals("amex")) {
114                            if (!number.startsWith("34") && !number.startsWith("35") &&
115                                    !number.startsWith("36") && !number.startsWith("37")) {
116    
117                                    return false;
118                            }
119    
120                            if (number.length() != 15) {
121                                    return false;
122                            }
123                    }
124    
125                    return Validator.isLUHN(number);
126            }
127    
128    }