Click here to Skip to main content
11,641,717 members (63,019 online)
Click here to Skip to main content
Add your own
alternative version

Ultimate .NET Credit Card Utility Class

, 29 Aug 2007 CPOL 109.7K 3.6K 151
A powerfully simple .NET utility class for validating and testing credit card numbers in C# and VB.
UltimateCreditCardUtilityCS_src.zip
CredCardUtilityTestsCS
CreditCardUtilityTests
bin
Debug
CreditCardUtility.dll
CreditCardUtilityTest.dll
nunit.framework.dll
CreditCardUtilityTests.csproj.user
Properties
CreditCardTestSiteCS
Bin
CreditCardUtility.dll
RadAjax.Net2.dll
RadInput.Net2.dll
images
innerBkgnd.png
loading4.gif
pageBkgn.jpg
UltimateCreditCardUtility.gif
CreditCardUtilityCS
bin
Debug
CreditCardUtility.dll
CreditCardUtility.csproj.user
Properties
UltimateCreditCardUtilityVB_src.zip
CreditCardTestSiteVB
Bin
CreditCardUtility.dll
RadAjax.Net2.dll
RadInput.Net2.dll
images
innerBkgnd.png
loading4.gif
pageBkgn.jpg
UltimateCreditCardUtility.gif
CreditCardUtilityTestsVB
CreditCardUtilityTests
bin
Debug
CreditCardUtility.dll
CreditCardUtilityTests.dll
nunit.framework.dll
CreditCardUtilityTests.vbproj.user
My Project
Application.myapp
Settings.settings
CreditCardUtilityVB
CreditCardUtility
bin
Debug
CreditCardUtility.dll
CreditCardUtility.pdb
My Project
Application.myapp
Settings.settings
Imports NUnit.Framework
Imports Bluelaser.Utilities

<TestFixture()> _
Public Class CreditCardUtilityTests
    '=========================================================================
    ' CreditCardUtilityTests Change Log
    '
    ' Date             Developer       Description
    ' 30 Aug 2007      T. Anglin       Changed test credit card numbers in
    '                                  GetCardTypeFromNumber and PassesLuhnTest
    '                                  to match PayPal issued test numbers.
    '=========================================================================

    <Test()> _
    Public Sub IsValidNumberTypeTest()
        Dim numDash As String = "4111-1111-1111-1111"
        Dim numSpace As String = "4111 1111 1111 1111"
        Dim numNoSpace As String = "4111111111111111"
        Dim numBadSep As String = "4111.1111.1111.1111"
        Dim numBadLen As String = "4111-1111-1111-111"

        Assert.IsTrue(CreditCardUtility.IsValidNumber(numDash), "IsValidNumber should allow numbers with dashes")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(numSpace), "IsValidNumber should allow numbers with spaces")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(numNoSpace), "IsValidNumber should allow numbers with no spaces")
        Assert.IsFalse(CreditCardUtility.IsValidNumber(numBadLen), "IsValidNumber should not allow numbers with too few numbers")
        Assert.IsFalse(CreditCardUtility.IsValidNumber(numBadSep), "IsValidNumber should not allow numbers with dot separators")
    End Sub

    <Test()> _
    Public Sub IsValidNumberCardTypeTest()
        Dim visaDash As String = "4111-1111-1111-1111"
        Dim visaSpace As String = "4111 1111 1111 1111"
        Dim visaNoSpace As String = "4111111111111111"
        Dim visaBad As String = "4111.1111.1111.1111"

        Assert.IsTrue(CreditCardUtility.IsValidNumber(visaDash, CreditCardTypeType.Visa), "IsValidNumber should allow Visa numbers with dashes")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(visaSpace, CreditCardTypeType.Visa), "IsValidNumber should allow Visa numbers with spaces")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(visaNoSpace, CreditCardTypeType.Visa), "IsValidNumber should allow Visa numbers with no spaces")
        Assert.IsFalse(CreditCardUtility.IsValidNumber(visaBad, CreditCardTypeType.Visa), "IsValidNumber should not allow Visa numbers with decimals")

        Dim amexDash As String = "3400-000000-00009"
        Dim amexSpace As String = "3400 000000 00009"
        Dim amexNoSpace As String = "340000000000009"
        Dim amexBad As String = "3400.000000.00009"

        Assert.IsTrue(CreditCardUtility.IsValidNumber(amexDash, CreditCardTypeType.Amex), "IsValidNumber should allow Amex numbers with dashes")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(amexSpace, CreditCardTypeType.Amex), "IsValidNumber should allow Amex numbers with spaces")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(amexNoSpace, CreditCardTypeType.Amex), "IsValidNumber should allow Amex numbers with no spaces")
        Assert.IsFalse(CreditCardUtility.IsValidNumber(amexBad, CreditCardTypeType.Amex), "IsValidNumber should not allow Amex numbers with decimals")

        Dim mcDash As String = "5500-0000-0000-0004"
        Dim mcSpace As String = "5500 0000 0000 0004"
        Dim mcNoSpace As String = "5500000000000004"
        Dim mcBad As String = "5500.0000.0000.0004"

        Assert.IsTrue(CreditCardUtility.IsValidNumber(mcDash, CreditCardTypeType.MasterCard), "IsValidNumber should allow MC numbers with dashes")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(mcSpace, CreditCardTypeType.MasterCard), "IsValidNumber should allow MC numbers with spaces")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(mcNoSpace, CreditCardTypeType.MasterCard), "IsValidNumber should allow MC numbers with no spaces")
        Assert.IsFalse(CreditCardUtility.IsValidNumber(mcBad, CreditCardTypeType.MasterCard), "IsValidNumber should not allow MC numbers with decimals")

        Dim discoverDash As String = "6011-0000-0000-0004"
        Dim discoverSpace As String = "6011 0000 0000 0004"
        Dim discoverNoSpace As String = "6011000000000004"
        Dim discoverBad As String = "6011.0000.0000.0004"

        Assert.IsTrue(CreditCardUtility.IsValidNumber(discoverDash, CreditCardTypeType.Discover), "IsValidNumber should allow Discover numbers with dashes")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(discoverSpace, CreditCardTypeType.Discover), "IsValidNumber should allow Discover numbers with spaces")
        Assert.IsTrue(CreditCardUtility.IsValidNumber(discoverNoSpace, CreditCardTypeType.Discover), "IsValidNumber should allow Discover numbers with no spaces")
        Assert.IsFalse(CreditCardUtility.IsValidNumber(discoverBad, CreditCardTypeType.Discover), "IsValidNumber should not allow Discover numbers with decimals")
    End Sub

    <Test()> _
    Public Sub GetCardTypeFromNumberTest()
        Dim visaNum As String = "4111-1111-1111-1111"
        Dim amexNum As String = "3782-822463-10005"
        Dim mcNum As String = "5105-1051-0510-5100"
        Dim discoverNum As String = "6011-1111-1111-1117"

        Assert.AreEqual(CreditCardTypeType.Visa, CreditCardUtility.GetCardTypeFromNumber(visaNum), "GetCardTypeFromNumber should return Visa")
        Assert.AreEqual(CreditCardTypeType.Amex, CreditCardUtility.GetCardTypeFromNumber(amexNum), "GetCardTypeFromNumber should return Amex")
        Assert.AreEqual(CreditCardTypeType.MasterCard, CreditCardUtility.GetCardTypeFromNumber(mcNum), "GetCardTypeFromNumber should return MasterCard")
        Assert.AreEqual(CreditCardTypeType.Discover, CreditCardUtility.GetCardTypeFromNumber(discoverNum), "GetCardTypeFromNumber should return Discover")
    End Sub

    <Test()> _
    Public Sub PassesLuhnTestTest()
        'According to PayPal, the valid test numbers that should be used
        'for testing card transactions are:
        'Credit Card Type              Credit Card Number
        'American Express              378282246310005
        'American Express              371449635398431
        'American Express Corporate    378734493671000
        'Diners Club                   30569309025904
        'Diners Club                   38520000023237
        'Discover                      6011111111111117
        'Discover                      6011000990139424
        'MasterCard                    5555555555554444
        'MasterCard                    5105105105105100
        'Visa                          4111111111111111
        'Visa                          4012888888881881
        'Src: https://www.paypal.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
        'Credit: Scott Dorman, http://www.geekswithblogs.net/sdorman

        Dim visaNum As String = "4111-1111-1111-1111"
        Dim amexNum As String = "3782-822463-10005"
        Dim mcNum As String = "5105-1051-0510-5100"
        Dim discoverNum As String = "6011-1111-1111-1117"

        Assert.IsTrue(CreditCardUtility.PassesLuhnTest(visaNum), "PassesLuhnTest should return true for this Visa number")
        Assert.IsTrue(CreditCardUtility.PassesLuhnTest(amexNum), "PassesLuhnTest should return true for this Amex number")
        Assert.IsTrue(CreditCardUtility.PassesLuhnTest(mcNum), "PassesLuhnTest should return true for this MC number")
        Assert.IsTrue(CreditCardUtility.PassesLuhnTest(discoverNum), "PassesLuhnTest should return true for this Discover number")

        Dim visaBad As String = "4111-0000-0000-0000"

        Assert.IsFalse(CreditCardUtility.PassesLuhnTest(visaBad), "PassesLuhnTest should return false for this number")
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

BlueLaser05
Web Developer
United States United States
Todd Anglin is an Experts Exchange ASP.NET Master and a Telerik Technical Evangelist responsible for building a strong Telerik community across the globe. Before joining Telerik, Todd worked as a developer in a Fortune 200 financial services company in San Antonio as a Systems Analyst supporting applications on a wide range of platforms and technologies, including Unix, Windows Server, Informix, Oracle, and SQL Server. Todd graduated Magna Cum Laude with Business Honors from Mays Business School at Texas A&M University with a BBA in MIS and now resides with his wife in The Woodlands, Texas.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 29 Aug 2007
Article Copyright 2007 by BlueLaser05
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid