# A Credit Card Validator Service

One of the most common uses for Web Services is going to be to validate credit card numbers. I have created a simple Web Service that validates credit card numbers . This credit card validator service, though, doesn't do real-time credit card approvals . It just looks at the credit card number and determines whether it is a valid credit card number. It also determines what type of credit card it is, such as Visa, Master Card, American Express, or Discover. I have actually used this very same credit card validator service, and just added a call to my merchant account provider to do my credit card processing in my Web applications. So this Web Service is almost good enough for you to drop directly into your own Web applications. All you need to do is add the calls to your merchant account's validation server.

The first thing that must be done, though, before the credit card number is validated , is removing any unnecessary characters . I have a method called TrimToDigits() , and this method takes whatever credit card number has been typed in and reduces it to only numeric digits. For instance, if there are any blank spaces, dashes, commas, or hyphens, they are removed and what is returned is the credit card number with only numeric digits. You can see the TrimToDigits() method in Listing 14.2.

##### Listing 14.2 The TrimToDigits() Method
` public String TrimToDigits( String strCardNumber )  {      String strRet = "";      for( int i=0; i<strCardNumber.Length; i++ )      {          if( strCardNumber[i] >= '0' && strCardNumber[i] <= '9' )          {              strRet += strCardNumber[i];          }      }      return( strRet );  } `

I created a method called CheckCC() , which looks at the credit card number and determines whether it is valid.

The first thing it does is determine what type of credit card number it is. Master Card numbers are always 16 digits in length, and they always start with either a 51, 52, 53, 54, or 55. If the credit card number passed in does not meet this criteria, it is not a Master Card credit card number.

Visa credit card numbers are either 13 or 16 characters in length, and their numbers always start with 4.

Discover credit cards are always 16 characters in length, and always start with the characters 6011.

American Express credit cards are always 15 characters in length. American Express credit card numbers start with a 34 or a 37.

After establishing what type of credit card it is (either Visa, Master Card, Discover, or American Express), it's time to go through and actually validate the credit card number to see whether the digits are valid. We do this by taking the digits and creating a check sum. The steps are as follows :

1. If the digit is an even number multiply the number by 2.

2. Take the remainder of that particular number when it is divided by 10. (This is the same as using the modulus operator with the value of 10.)

3. Take this value and add it to the check sum total.

4. If this particular digit (which you just added to the check sum in step 3), when it is multiplied by 2, is greater than 9, increment the check sum by 1. If it is an odd number, simply add the number to the check sum running total.

These steps create the check sum for the entire credit card number string. You can see this code enclosed in a for loop in the CheckCC() method, which uses the variable x as the counter.

The last thing to do before it's possible to determine whether the check sum is valid is determine whether there is a remainder when the number is divided by 10. In other words, use the modulus 10 operator to see whether the result is 0 or not. If there is any remainder after dividing by 10, or if the check sum mod 10 is not 0, then there is an error and an error code is returned. Otherwise, the return of a 0 indicates that there has been no error and that this is a valid credit card number. You can see the CheckCC() method in Listing 14.3.

##### Listing 14.3 The CheckCC() Method Checks the Validity of a Credit Card Number
` public int CheckCC( String strCardNumber, String strCardType )  {      strCardType.ToUpper();      bool bLengthValid = true;      bool bPrefixValid = true;      switch( strCardType )      {          case "M":              if( strCardNumber.Length != 16 )              {                  bLengthValid = false;                  break;              }              if( !( strCardNumber.IndexOf( "51" ) == 0                   strCardNumber.IndexOf( "52" ) == 0                   strCardNumber.IndexOf( "53" ) == 0                   strCardNumber.IndexOf( "54" ) == 0                   strCardNumber.IndexOf( "55" ) == 0 ) )              {                  bPrefixValid = false;                  break;              }          break;          case "V":              if( strCardNumber.Length != 13 &&                  strCardNumber.Length != 16 )              {                  bLengthValid = false;                  break;              }               if( !( strCardNumber.IndexOf( "4" ) == 0 ) )              {                  bPrefixValid = false;                  break;              }              break;                  case "D":              if( strCardNumber.Length != 16 )              {                  bLengthValid = false;                  break;              }              if( !( strCardNumber.IndexOf( "6011" ) == 0 ) )              {                  bPrefixValid = false;                  break;               }              break;          case "A":              if( strCardNumber.Length != 15 )              {                  bLengthValid = false;                  break;              }              if( !( strCardNumber.IndexOf( "34" ) == 0                   strCardNumber.IndexOf( "37" ) == 0  ) )              {                  bPrefixValid = false;                  break;              }              break;      }      if( !bLengthValid )      {          return( -2 );      }      int nQSum = 0;      for( int x=1; x<=strCardNumber.Length; x++ )      {          int nCh = (int) strCardNumber[strCardNumber.Length-x] - '0';          if( ( x % 2 ) == 0 )          {              int nSum = 2 * nCh;              nQSum = nQSum + ( nSum % 10 );              if( nSum > 9 )              {                  nQSum++;              }           }          else          {              nQSum = nQSum + nCh;          }      }      if( ( nQSum % 10 ) != 0 )      {          return( -4 );      }      return( 0 );  } `

The actual Web method that is called from the remote machine is called CardType() . The CardType() method takes one parameter, which is a string containing the credit card number. It returns a single integer. If it returns a 1, then the credit card is a valid Master Card number. If it returns 2 then it is a valid Visa number; if it returns 3 then it is a valid American Express; and if it returns 4 it is a valid Discover number. It returns a -1 if no valid credit card was found. The code for the CardType() method can be seen in Listing 14.4.

##### Listing 14.4 The CardType() Method Provides the Overall Logic for the Validator
` [WebMethod]  public int CardType( String strCardNumber )  {      String strCardNum = TrimToDigits( strCardNumber );      if( CheckCC( strCardNum, "M" ) == 0 )      {          return( 1 );      }      if( CheckCC( strCardNum, "V" ) == 0 )      {          return( 2 );      }      if( CheckCC( strCardNum, "A" ) == 0 )      {          return( 3 );      }      if( CheckCC( strCardNum, "D" ) == 0 )      {          return( 4 );      }      return( -1 );  } `

This is an extremely simple way to provide functionality to your Web applications. You can easily create, based on this code, your own credit card validation Web Service and use it in all your Web applications. The code that consumes this credit card validation Web Service is found later in this chapter in Listing 14.8.

Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233