Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to find out whether a card is DEBIT CARD and CREDIT CARD. Can you please provide the code in C#.Net. I am able to validate credit card but I am not able to validate Debit card. Below is the Credit card validation code-

Validating a credit card-
C#
public static bool IsCardNumberValid(string cardNumber)
{
  int i, checkSum = 0;

  // Compute checksum of every other digit starting from right-most digit
  for (i = cardNumber.Length - 1; i >= 0; i -= 2)
    checkSum += (cardNumber[i] - '0');

  // Now take digits not included in first checksum, multiple by two,
  // and compute checksum of resulting digits
  for (i = cardNumber.Length - 2; i >= 0; i -= 2)
  {
    int val = ((cardNumber[i] - '0') * 2);
    while (val > 0)
    {
      checkSum += (val % 10);
      val /= 10;
    }
  }

  // Number is valid if sum of both checksums MOD 10 equals 0
  return ((checkSum % 10) == 0);
}
Removing all non-digit characters from a credit card number.
C#
public static string NormalizeCardNumber(string cardNumber)
{
  if (cardNumber == null)
    cardNumber = String.Empty;

  StringBuilder sb = new StringBuilder();

  foreach (char c in cardNumber)
  {
    if (Char.IsDigit(c))
      sb.Append(c);
  }

  return sb.ToString();
}
Class to hold credit card type information-
C#
public enum CardType
{
  Unknown = 0,
  MasterCard = 1,
  VISA = 2,
  Amex = 3,
  Discover = 4,
  DinersClub = 5,
  JCB = 6,
  enRoute = 7
}

private class CardTypeInfo
{
  public CardTypeInfo(string regEx, int length, CardType type)
  {
    RegEx = regEx;
    Length = length;
    Type = type;
  }

  public string RegEx { get; set; }
  public int Length { get; set; }
  public CardType Type { get; set; }
}

// Array of CardTypeInfo objects.
// Used by GetCardType() to identify credit card types.
private static CardTypeInfo[] _cardTypeInfo =
{
  new CardTypeInfo("^(51|52|53|54|55)", 16, CardType.MasterCard),
  new CardTypeInfo("^(4)", 16, CardType.VISA),
  new CardTypeInfo("^(4)", 13, CardType.VISA),
  new CardTypeInfo("^(34|37)", 15, CardType.Amex),
  new CardTypeInfo("^(6011)", 16, CardType.Discover),
  new CardTypeInfo("^(300|301|302|303|304|305|36|38)", 
                   14, CardType.DinersClub),
  new CardTypeInfo("^(3)", 16, CardType.JCB),
  new CardTypeInfo("^(2131|1800)", 15, CardType.JCB),
  new CardTypeInfo("^(2014|2149)", 15, CardType.enRoute),
};

public static CardType GetCardType(string cardNumber)
{
  foreach (CardTypeInfo info in _cardTypeInfo)
  {
    if (cardNumber.Length == info.Length && 
        Regex.IsMatch(cardNumber, info.RegEx))
      return info.Type;
  }

  return CardType.Unknown;
}

Thanks,
Niraj Kumar.

What I have tried:

I tried to use else statement in which I tried finding Debit card validation.
Posted
Updated 27-Feb-20 6:03am
v2
Comments
F-ES Sitecore 27-Feb-20 10:31am    
If this is possible I'm sure you'll easily find the results by googling. If you can't find a solution by googling then it probably isn't possible.
Richard MacCutchan 27-Feb-20 11:17am    
It's not a coding issue: Debit cards are identified by the numbers on them, just like credit cards are.

First, if you want to find weather, go outside and look up.

If you want to find whether a card is a debit or credit card, you can't. There's no way to determine that from just the data on the card.
 
Share this answer
 
Comments
OriginalGriff 27-Feb-20 12:34pm    
Actually, you can - it's all in the card number prefix, apparently.
https://en.wikipedia.org/wiki/Payment_card_number
https://www.bincodes.com/creditcard-checker/

Unfortunately you need a complete list of BIN codes, and the banks don't like to just give that out ... :laugh:
Dave Kreskowiak 27-Feb-20 13:01pm    
Probably why every gas (petrol for the UK'ers) pump I use asks me "Is this a Debit or Credit card?". :)
That information is not on the card; only the issuer (bank) knows, and you will need to contact them to find out.

In other words, you are going to have to go through the APIs for whomever is doing your credit card processing to figure out which response field aligns with what you want to know.

Search this Authorize.net WSDL response for tns:BankAccountType
https://api.authorize.net/soap/v1/Service.asmx?wsdl[^]

You may also want to look into Visa Secure Remote Commerce and similar products for the other card providers as well as payment processors
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900