Credit Card Verification
From CDYNE Wiki for Web Services
Verify a Credit Card Free by cross referencing it against the Luhn formula. This formula does not check with the major Credit Card companies for validity, it merely checks the Credit Card number algorithm to make sure it is a valid number for the four major Credit Cards accepted worldwide. Please use a license key of '0' if consuming via Web Service. The luhn formula can also be used to identify the type of card by the # entered.
Contents |
[edit] Developer/Tester Information
[edit] Pricing
FREE forever. Use the CDYNE Credit Card Verification Web Service or the raw code used to program this Web Service.
[edit] Download Credit Card Verification Web Service code examples
[edit] Open Source
Below you can find the C# or VB version of the Credit Card Verification Web Service code using the Luhn Formula.
[edit] Luhn Definition
The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, was developed in the 1960s as a method of validating identification numbers. It is a simple checksum formula used to validate a variety of account numbers, such as credit card numbers and Canadian Social Insurance Numbers. Much of its notoriety comes from credit card companies' adoption of it shortly after its creation in the late 1960s by IBM scientist Hans Peter Luhn (1896–1964).
Explanation of the Luhn's algorithm can be found here.
[edit] C# Source Code
using System; /// <summary> /// Summary description for CCVerifier /// </summary> public class CCVerifier { public ReturnIndicator CheckCC(string CardNumber) { ReturnIndicator ri = new ReturnIndicator(); ri.CardValid = false; byte[] number = new byte[16]; // number to validate // Remove non-digits int len = 0; for (int i = 0; i < CardNumber.Length; i++) { if (char.IsDigit(CardNumber, i)) { if (len == 16) return ri; // number has too many digits number[len++] = byte.Parse(CardNumber[i].ToString()); } } // Use Luhn Algorithm to validate int sum = 0; for (int i = len - 1; i >= 0; i--) { if (i % 2 == len % 2) { int n = number[i] * 2; sum += (n / 10) + (n % 10); } else sum += number[i]; } ri.CardValid = (bool) (sum % 10 == 0); if ((ri.CardValid == true)) { switch (CardNumber.Substring(0, 1)) { case "3": ri.CardType = "AMEX/Diners Club/JCB"; break; case "4": ri.CardType = "VISA"; break; case "5": ri.CardType = "MasterCard"; break; case "6": ri.CardType = "Discover"; break; default: ri.CardType = "Unknown"; break; } } else { ri.CardType = "NONE"; } return ri; } public class ReturnIndicator { public string CardType; public bool CardValid; } }
The original code below (at the bottom) is INCORRECT... this one is used and tested and reflects the C# above more
Dim theNumber As String = Misc.Strings.MakeAllNumeric(accountNumber) 'i.e. 1234-5678-9012-3452 = 1234567890123452
If String.IsNullOrEmpty(theNumber) Then Return False
'Process digits from right to left Dim sum As Int32 = 0 Dim theDigit As Int32 = 0 For i As Int32 = theNumber.Length - 1 To 0 Step -1
theDigit = Convert.ToInt32(theNumber.Substring(i, 1)) 'GET THE DIGIT (starting from the right)
'USE THE EVEN DIGIT on EVEN LEN CARDS; ODD DIGIT on ODD LENGTH CARDS
'EXAMPLE: "even" len card number ending in xxx51908
' 8 + (0*2) + 9 + (1*2)
'EXAMPLE: "odd" len card number 49927398716
' 6 + (1*2) + 7 + (8*2) + 9 + (3*2)... you get the point... but see next line
' 6 + 2 + 7 + (1+6) + 9 + 6.... see the "break apart" the 16 = 1 + 6
If ((i Mod 2) = (theNumber.Length Mod 2)) Then
'the product = (theDigit * 2)
'example: 6*2 = 12 or 9*2 = 18 or 7*2 = 14
' OVER 9 THEN Break the digits up and add them together (eg. 18 becomes 1 + 8, 14 becomes 1 + 4)
' UNDER 10 THEN JUST ADD IT
If (theDigit * 2) >= 10 Then
sum += ((theDigit * 2) \ 10) + ((theDigit * 2) - 10)
Else
sum += (theDigit * 2)
End If
Else
sum += theDigit
End If
Next
'RETURN 10's COMPLIMENT Return ((sum Mod 10) = 0)
- ORGINAL CODE ******
[edit] VB.Net Source Code
Public Class CreditCardLib Public Enum eCreditCardType Invalid = 0 AMEX = 3 Visa = 4 MasterCard = 5 Discover = 6 End Enum Public Shared Function CheckCardNumber(ByVal CardNumber As String) As eCreditCardType Dim SingleChar As String Dim CharValue As Integer Dim RunningValue As Integer = 0 Dim Result As eCreditCardType CardNumber = Replace(Replace(Replace(CStr(CardNumber), "-", String.Empty), " ", String.Empty), ".", String.Empty) 'Help ensure proper format of the input Dim NumberLen As Integer = Len(CardNumber) ' Default value Result = eCreditCardType.Invalid Select Case Left(CardNumber, 1) Case "3" 'AMEX: 15 digit numbers starting with 34 or 37 If NumberLen = 15 And (Left(CardNumber, 2) = "34" Or Left(CardNumber, 2) = "37") Then Result = eCreditCardType.AMEX End If Case "4" 'Visa: 13 or 16 digit numbers starting with 4 If NumberLen = 13 Or NumberLen = 16 Then Result = eCreditCardType.Visa End If Case "5" 'MasterCard: 16 digit numbers starting with 5 If NumberLen = 16 Then Result = eCreditCardType.MasterCard End If Case "6" 'Discover: 16 digit numbers starting with 6011 or 65 If NumberLen = 16 And (Left(CardNumber, 4) = "6011" Or Left(CardNumber, 2) = "65") Then Result = eCreditCardType.Discover End If End Select If Result <> eCreditCardType.Invalid Then 'Process digits from right to left, drop last digit if total length is even Dim w As Integer = 2 * (NumberLen Mod 2) For i As Integer = NumberLen - 1 To 1 Step -1 SingleChar = Mid(CardNumber, i, 1) If IsNumeric(SingleChar) Then Select Case (i Mod 2) + w Case 0, 3 'Even Digit - Odd where total length is odd (eg. Visa vs. Amx) RunningValue += CInt(SingleChar) Case 1, 2 'Odd Digit - Even where total length is odd (eg. Visa vs. Amx) CharValue = CInt(SingleChar) * 2 If CharValue > 9 Then 'Break the digits (eg. 19 becomes 1 + 9) RunningValue += (CharValue \ 10) + (CharValue - 10) Else RunningValue += CharValue End If End Select End If Next 'Return the 10's complement of the total RunningValue = 10 - (RunningValue Mod 10) If RunningValue > 9 Then RunningValue = 0 End If ' Finally, check the checksum digit. If it's invalid then we have an invalid card. If (CStr(RunningValue) <> Right(CardNumber, 1)) Then Result = eCreditCardType.Invalid End If End If Return Result End Function End Class
