Introduction

The ultimate credit card utility provides all of the functions you need to validate and test credit cards in your .NET application, including:

  • IsValidNumber
  • GetCardTypeFromNumber
  • GetCardTestNumber
  • PassesLuhnTest

All code is provided in both C# and VB. In addition to the utility class, this project includes complete NUnit tests and a sample website for testing the code.

Background

If you have ever tried to process credit cards in your .NET applications you know that it can be a challenge. Even if you have a good payment processor that provides a .NET SDK, there are a number of functions you're left to handle on your own, like credit card number validation and card type detection. Stop wasting time trying to piece together a utility class from all of the code snippets scattered across the Internet. I have assembled the "ultimate" credit card utility class that provides robust credit number testing, easy credit card type detection, and functions for testing credit card interfaces in your application. To top it off, the ultimate credit card utility class also comes with NUnit tests to help guarantee the code always works as expected in your important credit card applications.

In this article, we'll look at each of the functions included in the ultimate credit card utility class and explain how simple .NET concepts are used to provide powerful functionality.

Using the code

The ultimate credit card utility class can be easily integrated into any .NET project by directly adding the .cs/.vb file or by compiling the credit card class into a separate assembly that is referenced by the application. If you have multiple applications that deal with credit cards, the best approach is to build the credit card utitlity class into a separate dll so that any updates you make in the future can easily be distributed to all of your .NET applications.

There are five basic functions in this utility class and we'll look at each in reverse order, building up from the more complex base functions to the simple validation functions.

PassesLuhnTest

At the core of any credit card utility class is Luhn's test. This test, also known as "mod 10", was created by IBM engineer Hans Luhn in the mid-1950's and stands as one of the easiest ways to validate that a series of numbers is valid and not a random collection of integers. All credit card numbers should pass Luhn's test; if they don't, it's an easy way to determine that an incorrect number has been entered.

In the ultimate credit card utility, we implement a simple function that accepts a credit card number as a string and returns True if the number passes Luhn's test.

No comments: