Thursday, October 18, 2012

Credit Card Number Validator

This is a small class that will check that a string value contains a valid credit card number and return true/false.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CCUtils
{
    public class CCHelper
    {
        public enum CARDTYPE
        {
            INVALID,
            VISA,
            MASTERCARD,
            AMERICAN_EXPRESS,
            EN_ROUTE,
            DINERS_CLUB
        }

        public static bool validCC(string number)
        {
            try
            {
                if (getCardId(number) != CARDTYPE.INVALID)
                    return validCCNumber(number);
                return false;
            }    catch         {
                return false;
            }
        }

        public static string getCardName(CARDTYPE id)
        {
            return id != CARDTYPE.INVALID ? id.ToString() : string.Empty;
        }

        public static bool isNumber(string n)
        {
            try
            {
                Double.Parse(n);
                return true;
            }
            catch
            {
                return false;
                //TODO;Catch number format exception and print stack trace?
            }
        }

        public static CARDTYPE getCardId(string number)
        {
            CARDTYPE valid = CARDTYPE.INVALID;
            var digit1 = number.Substring(0, 1);
            var digit2 = number.Substring(0, 2);
            var digit3 = number.Substring(0, 3);
            var digit4 = number.Substring(0, 4);

            if (isNumber(number))
            {
                //VISA prefix=4
                //length=13 or 16 (can be 15 too???)
                if (digit1 == "4")
                {
                    if (number.Length == 13 || number.Length == 16)
                    {
                        valid = CARDTYPE.VISA;
                    }
                }
                else if (digit2.CompareTo("51") >= 0 && digit2.CompareTo("55") <= 0)
                {
                    if (number.Length == 16)
                    {
                        valid = CARDTYPE.MASTERCARD;
                    }
                }
                else if (digit2 == "34" || digit2 == "37")
                {
                    if (number.Length == 15)
                    {
                        valid = CARDTYPE.AMERICAN_EXPRESS;
                    }
                }
                else if (digit4 == "2014" || digit4 == "2149")
                {
                    if (number.Length == 15)
                    {
                        valid = CARDTYPE.EN_ROUTE;
                    }
                }
                else if (digit2 == "36" || digit2 == "38" || (digit3.CompareTo("300") >=0&& digit3.CompareTo("305") <= 0))
                {
                    if (number.Length == 14)
                    {
                        valid = CARDTYPE.DINERS_CLUB;
                    }
                }
            }
            return valid;
        }

        public static bool validCCNumber(string n)
        {
            try
            {
                int j = n.Length;
                string[] s1 = new string[j];
                for (int i = 0; i < n.Length; i++)
                    s1[i] = string.Empty + n.ToCharArray()[i].ToString();
                int checksum = 0;
                for (int i = s1.Length; i >= 0; i -= 2)
                {
                    int k = 0;
                    if (i > 0)
                    {
                        k = int.Parse(s1[i - 1]) * 2;
                        if (k > 9)
                        {
                            string s = string.Empty + k;
                            k = int.Parse(s.Substring(0, 1)) + int.Parse(s.Substring(1));
                        }
                        checksum += int.Parse(s1[i]) + k;
                    }
                    else
                    {
                        checksum += int.Parse(s1[0]);
                    }
                }
                return ((checksum % 10) == 0);
            }
            catch
            {
                //TODO print stacktrace
                return false;
            }
        }

    }
}

No comments:

Post a Comment