Swipe Card or Credit card reading

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Below code get the card number,holder name,expire month,expire year, account number

 

string m_LastName = “”, m_FirstName = “”;
string m_CardNum = “”, m_ExpDate = “”;
string m_CardType = “”;
model.SwipeCard = “%B1111091890791111^SWEETMAN/WILLIAM^0806101000000610989 00849000000?;1111091890791111=08061010610989849?”;

/*
* Track 1 of a Credit Card
* Format B.
* %B1111091890791111^SWEETMAN/WILLIAM^0806101000000610989 00849000000?;1111091890791111=08061010610989849?
*
Total            79 characters max.

*SS (Header) – optional
*FC (Format code)
*Credit Card Number (maximum of 19 digits)
*FS (field separator) – usually ^
*Cardholder’s Name
*FS (field separator) – usually ^
*Expiration Date (YYMM)
*Service Code (maximum of 3 digits)
*Discretionary Data – may include some or all of the following:
*PVKI – PIN Verification Key Indicator
*PVV – PIN Verification Value
*CVV – Card Verification Value
*CVC – Card Verification Code
*ES (terminator) – optional
*LRC (Longitudinal Redunancy Check Character)

Sample of Track 1 Credit Card Data – without header or terminator

B4826762900098714^DOE/JANE S^0009836000000000760000008970000

Track 2 of a Credit Card

*SS (header) – optional
*· Credit Card Number
*FS (field separator) – usually =
*· Expiration Date (YYMM)
*· Discretionary Data
*ES (terminator) – optional
*LRC (Longitudinal Redundancy Check)

Sample of Track 2 Credit Card Data – without header or terminator

4826762900098714=09836000076000897000

*
* %E? Format.
* header
* Credit Card Number
* field separator = usually =
* Expiration Date (YYMM)
* Discretionary Data
* SAMPLE:
* %E?;111197007001111=0604101190633331111?
*/
// sub-string markers
int iBeg = 0;
int iEnd = 0;
// date manipulation
string cstrMonth = “”;
string cstrYear = “”;
// determine CC format
int CardFormat = -1;

if (model.SwipeCard.Length > 10)
{
if (model.SwipeCard.Substring(1, 1) == “B”)
CardFormat = 0;
else if (model.SwipeCard.IndexOf(“%E?;”, 0, model.SwipeCard.Length) >= 0)
CardFormat = 1;
}
try
{
switch (CardFormat)
{
case 0:
// just work with Track 1
// Get CC Number, Surname, First name, Exp Date
iBeg = 2;
iEnd = model.SwipeCard.IndexOf(“^”, 0, model.SwipeCard.Length);
if (iEnd == -1)
throw new Exception(“CC Number not found.”);
m_CardNum = model.SwipeCard.Substring(iBeg, iEnd – iBeg);
iBeg = model.SwipeCard.IndexOf(“/”, 0, model.SwipeCard.Length) + 1;
if (iBeg <= 0)
throw new Exception(“Surname not found.”);
m_LastName = model.SwipeCard.Substring(iEnd + 1, iBeg – iEnd – 2);
iEnd = model.SwipeCard.IndexOf(“^”, iBeg, model.SwipeCard.Length – iBeg);
if (iEnd <= 0)
throw new Exception(“First name not found.”);
m_FirstName = model.SwipeCard.Substring(iBeg, iEnd – iBeg);
//
m_ExpDate = model.SwipeCard.Substring(iEnd + 1, 4);
cstrMonth = m_ExpDate.Substring(m_ExpDate.Length – 2, 2);
cstrYear = m_ExpDate.Substring(0, 2);
m_ExpDate = cstrMonth + cstrYear;
//

model.CardholderName = m_FirstName + ” ” + m_LastName;
model.CardNumber = m_CardNum;
model.ExpireMonth = cstrMonth;
model.ExpireYear = “20” + cstrYear;

m_CardType = model.SwipeCard.Substring(2, 1);

if (m_CardType == “3”)
model.CreditCardType = “Amex”;

else if (m_CardType == “4”)
model.CreditCardType = “Visa”;

else if (m_CardType == “5”)
model.CreditCardType = “MasterCard”;

else if (m_CardType == “6”)
model.CreditCardType = “Discover”;

break;
case 1:
// Get CC Number and Exp Date
iBeg = 4;
iEnd = model.SwipeCard.IndexOf(“=”, 0, model.SwipeCard.Length);
if (iEnd == -1)
throw new Exception(“CC Number not found.”);
m_CardNum = model.SwipeCard.Substring(iBeg, iEnd – iBeg);
//
m_ExpDate = model.SwipeCard.Substring(iEnd + 1, 4);
cstrMonth = m_ExpDate.Substring(m_ExpDate.Length – 2, 2);
cstrYear = m_ExpDate.Substring(0, 2);
m_ExpDate = cstrMonth + cstrYear;
break;
default:
throw new Exception(“Unknown Format.”);
} //switch
} //try
catch (Exception ex)
{
//MessageBox.Show(“Error: unknown/bad CC format.rn” + ex.Message + “rnPlease try a different Credit Card or type the number in manually.”, “Parse Error”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally { }


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.