I know after reading the title some of your minds start wandering towards the dark side but I assure you this is strictly an educational post. When writing applications that accept credit cards you usually need the ability to run two types of tests. First we need to provide the user with a credit card entry form. In this for they will select the type of credit card, enter their name, number, expiration and security code. We usually provide some type of validation to make sure that the users credit card is a valid one. In ColdFusion we can use the isValid('creditcard',number) function to test the validity of the number. We also need the ability to pass real numbers to our processor. These cards never get processed in test mode but they are looking for a real number.
With these two problems at hand I set out to create a component that would allow us to generate credit card numbers. To be able to create the component you really need to understand how credit card numbers are generated. I could go into a really long post here on how the algorithm works but there are already a couple of really good posts out there that I would like to refer you to. Go ahead and look at these articles and then come back to this post.
- http://www.thetaoofmakingmoney.com/2007/04/12/324.html
- http://www.moneybluebook.com/how-to-create-and-generate-valid-credit-card-numbers/
First off wasn't that cool stuff. It's awesome to understand how things work. It really is a bunch of random numbers but numbers that follow a specific algorithm. With that we can now look how we can do this in ColdFusion. This is entirely written in cfscript in ColdFusion 9 but we accomplish the same in previous versions. The first thing we need to do is setup some initial values. The ccnumber is an array that will hold our number and the default length of the credit card number will be 16. Next for this function to work we need to tell it what type of credit card number we are trying to generate. Based on the credit card type we know that each of them follow some specific rules. For example visa starts with 4 and is 16 digits long. Once we know that we can generate the rest of the number. Next comes the tricky part of the algorithm. The very last number is whats know as the check digit number. Starting from the number to the left of it we are going to double every other digit. If the digit is 2 digits (greater than 9) we are going to add the two numbers. For example if the number was 9 we would double it to 18 but instead of the value being 18 we would take 1+8 and use 9 as our value. Using that logic we can get the value for each of our numbers and then add up the sum. Finally to make our credit card number valid our sum needs to be divisible by 10. If it's not we can offset the sum by using some math and setting the check digit to a number that will make our sum divisible by 10.
As you can see this is some pretty cool stuff, now that I have my component I can runs some tests by using the following code.
The complete code is up on the project site at http://ccgenerator.riaforge.org. Please let me know if you have any feedback / suggestions about the project
