Provides common functions performed on credit cards.

bool CheckCardNumberWithLuhn(string cardNumber)

Determines if a credit card number validates against the Luhn algorithm. This method allows you check the validity of a credit card before hitting your credit card provider.

string GetCardType(string cardNumber)

Determines the card type based on the card number. Return values are MASTER, VISA, DISCOVER, AMEX, or null

Console.WriteLine(CreditCard.GetCardType("4000 0000 0000 0000")); // VISA
Console.WriteLine(CreditCard.GetCardType("5200123456789012")); // MASTER

string GetCardTypeName(string cardNumber)

Determines the name of the card based on its card number.

Console.WriteLine(CreditCard.GetCardTypeName("5200 1234 5678 9012")); // MasterCard

string GetCardTypeNameFromType(string typeString)

Determines the name of the name of the card based on the type string as returned by GetCardType.

Console.WriteLine(CreditCard.GetCardTypeNameFromType("AMEX")); // American Express

Type Return value
MASTER MasterCard
AMEX American Express

As an instance

For convenient passing of credit card parameters in your program, this class can also be instantiated and filled with the following credit card properties: Cvv, Expiry, Name, and Number.

var card = new CreditCard
	Cvv = "111",
	Expiry = new DateTime(2010, 1, 31, 23, 59, 59), // for 01/10
	Name = "SharpNick"
	Number = "4000 1234 5678 9012";

Console.WriteLine(card.Type); // VISA
HypotheticalPaymentGateway.ChargeCard(card, 10);

Last edited Aug 26, 2010 at 1:15 PM by weiwong, version 7


JoelMMCC Sep 20, 2010 at 6:02 PM 
Do be aware that if any application (most especially including Web apps) so much as touches credit card data for any purpose, it MUST be FULLY compliant with PCI-DSS in every respect, as of July 1st, 2010! This includes not only the application itself, but any supporting software, the Web hosting environment (it’s pretty darn near impossible to be PCI-DSS compliant on nearly any form of traditional shared hosting, for instance), business operations (e.g. all admins must have their own logins — no sharing — and everything they do must be logged), etc. etc. etc. You must verify continued compliance quarterly, which may involve hiring compliance-testing services to rigorously test your site for unpatched vulnerabilities, exploits, etc.

Failure to do this can have very dire consequences indeed, and be very, very costly. It would not be a good idea to “roll your own” e-Commerce application using this library until (A) the library itself has been vetted as being fully PCI-DSS compliant, and (B) you know EXACTLY what you’re doing, and have vetted your own software and every aspect of the complete system and its hosting as being PCI-DSS compliant, and are willing to invest the expense to keep it that way over the long haul.