001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.Validator;
027:
028: /**
029: * <a href="CreditCard.java.html"><b><i>View Source</i></b></a>
030: *
031: * @author Brian Wing Shun Chan
032: *
033: */
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: StringMaker sm = new StringMaker();
049:
050: for (int i = 0; i < numberLen - 4; i++) {
051: sm.append(x);
052: }
053:
054: sm.append(number.substring(numberLen - 4, numberLen));
055:
056: number = sm.toString();
057: }
058:
059: return number;
060: }
061:
062: public static boolean isValid(String number, String type) {
063: number = StringUtil.extractDigits(number);
064:
065: if (type.equals("visa")) {
066: if (!number.startsWith("4")) {
067: return false;
068: }
069:
070: if (number.length() != 13 && number.length() != 16) {
071:
072: return false;
073: }
074: } else if (type.equals("mastercard")) {
075: if (!number.startsWith("51") && !number.startsWith("52")
076: && !number.startsWith("53")
077: && !number.startsWith("54")
078: && !number.startsWith("55")) {
079:
080: return false;
081: }
082:
083: if (number.length() != 16) {
084: return false;
085: }
086: } else if (type.equals("discover")) {
087: if (!number.startsWith("6011")) {
088:
089: return false;
090: }
091:
092: if (number.length() != 16) {
093: return false;
094: }
095: } else if (type.equals("amex")) {
096: if (!number.startsWith("34") && !number.startsWith("35")
097: && !number.startsWith("36")
098: && !number.startsWith("37")) {
099:
100: return false;
101: }
102:
103: if (number.length() != 15) {
104: return false;
105: }
106: }
107:
108: return Validator.isLUHN(number);
109: }
110:
111: }
|