Processing a Credit Card Swipe using ASP.Net C#
View Part 2
What we are building
We are building an ASP.Net website that allows you to: plug in a USB Magnetic Card Reader, scan a credit card, parse out the credit card information required to charge the card and display it to the screen.
How we will build
Follow my instructions in the following video and
you will be well on your way to processing credit cards via card swipes. It was
a challenge to find any useful information on how to process credit card swipes.
I was not able to find a single article about how to handle credit card swipes using
ASP.Net C#, but I was able to find a couple of JavaScript parsers.
While my example isn't feature-packed, it is functional. If you would like
to enhance the code during your implementation, please send me a copy so that I
can update my sample and give you credit!
Watch Processing a Credit Card Swipe (USB Reader) with ASP.Net c#
View Part 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CreditCardSwipe
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CardReader_OTC(object sender, EventArgs e)
{
bool CaretPresent = false;
bool EqualPresent = false;
CaretPresent = CardReader.Text.Contains("^");
EqualPresent = CardReader.Text.Contains("=");
if (CaretPresent)
{
string[] CardData = CardReader.Text.Split('^');
//B1234123412341234^CardUser/John^030510100000019301000000877000000?
PersonName.Text = FormatName(CardData[1]);
CardNumber.Text = FormatCardNumber(CardData[0]);
CardExpiration.Text = CardData[2].Substring(2, 2) + "/" + CardData[2].Substring(0, 2);
}
else if (EqualPresent)
{
string[] CardData = CardReader.Text.Split('=');
//1234123412341234=0305101193010877?
CardNumber.Text = FormatCardNumber(CardData[0]);
CardExpiration.Text = CardData[1].Substring(2, 2) + "/" + CardData[1].Substring(0, 2);
}
}
private string FormatCardNumber(string o)
{
string result = string.Empty;
result = Regex.Replace(o, "[^0-9]", string.Empty);
return result;
}
private string FormatName(string o)
{
string result = string.Empty;
if (o.Contains("/"))
{
string[] NameSplit = o.Split('/');
result = NameSplit[1] + " " + NameSplit[0];
}
else
{
result = o;
}
return result;
}
}
}