1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  
26  /**
27   * <a href="CreditCard.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   *
31   */
32  public class CreditCard {
33  
34      public static String hide(String number) {
35          return hide(number, StringPool.STAR);
36      }
37  
38      public static String hide(String number, String x) {
39          if (number == null) {
40              return number;
41          }
42  
43          int numberLen = number.length();
44  
45          if (numberLen > 4) {
46              StringBuilder sb = new StringBuilder();
47  
48              for (int i = 0; i < numberLen - 4; i++) {
49                  sb.append(x);
50              }
51  
52              sb.append(number.substring(numberLen - 4, numberLen));
53  
54              number = sb.toString();
55          }
56  
57          return number;
58      }
59  
60      public static boolean isValid(String number, String type) {
61          number = StringUtil.extractDigits(number);
62  
63          if (type.equals("visa")) {
64              if (!number.startsWith("4")) {
65                  return false;
66              }
67  
68              if (number.length() != 13 &&
69                  number.length() != 16) {
70  
71                  return false;
72              }
73          }
74          else if (type.equals("mastercard")) {
75              if (!number.startsWith("51") &&
76                  !number.startsWith("52") &&
77                  !number.startsWith("53") &&
78                  !number.startsWith("54") &&
79                  !number.startsWith("55")) {
80  
81                  return false;
82              }
83  
84              if (number.length() != 16) {
85                  return false;
86              }
87          }
88          else if (type.equals("discover")) {
89              if (!number.startsWith("6011")) {
90  
91                  return false;
92              }
93  
94              if (number.length() != 16) {
95                  return false;
96              }
97          }
98          else if (type.equals("amex")) {
99              if (!number.startsWith("34") &&
100                 !number.startsWith("35") &&
101                 !number.startsWith("36") &&
102                 !number.startsWith("37")) {
103 
104                 return false;
105             }
106 
107             if (number.length() != 15) {
108                 return false;
109             }
110         }
111 
112         return Validator.isLUHN(number);
113     }
114 
115 }