Processing a Credit Card Swipe using ASP.Net C#

Your, or your client, own a small business that wants to start taking credit cards. You would like to avoid the processing fees, credit checks, heafty equipment charges and everything else that comes along with having the big banks handle your credit card processing. Forget that noise: here is a cheaper solution for you!

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.

Screenshot of ASP.Net Credit Card swipe reader in action!

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;
        }
    }
}