Does anyone know how to parse a credit card string input from a Magnetic Card Swiper?
I tried a JavaScript parser but never got it to work. This is what the input looks like.
%BNNNNNNNNNNNNNNNN^DOE/JOHN ^1210201901000101000100061000000?;NNNNNNNNNNNNNNNN=12102019010106111001?
The N's are the credit card number.
These tracks contain the credit card account number, name, expiration date, service code, and card verification code. Credit cards primarily or exclusively use the first two tracks. The third track sometimes contains additional information such as a country code or currency code.
Magnetic Stripe Cards This standard tape strip contains three magnetic tracks that are used to store the card's code data. The card is usually presented to the reader by swiping or inserting it into the reader, which obtains the card's code using a magnetic head that detects the magnetic field generated by its strip.
A magnetic stripe reader, also called a magstripe reader, is a hardware device that reads the information encoded in the magnetic stripe located on the back of a plastic badge.
See the Magnetic Stripe Card entry @ Wikipedia:
Track one, Format B:
- Start sentinel — one character (generally '%')
- Format code="B" — one character (alpha only)
- Primary account number (PAN) — up to 19 characters. Usually, but not always, matches the credit card number printed on the front of the card.
- Field Separator — one character (generally '^')
- Name — two to 26 characters
- Field Separator — one character (generally '^')
- Expiration date — four characters in the form YYMM.
- Service code — three characters
- Discretionary data — may include Pin Verification Key Indicator (PVKI, 1 character), PIN Verification Value (PVV, 4 characters), Card Verification Value or Card Verification Code (CVV or CVK, 3 characters)
- End sentinel — one character (generally '?')
- Longitudinal redundancy check (LRC) — one character (Most reader devices do not return this value when the card is swiped to the presentation layer, and use it only to verify the input internally to the reader.)
I hope the data is fake, otherwise Anyone could get the:
And I'm not sure but I think the credit card number (or # of possibilities) can be computed using the LRC.
I did you one better: I made a video showing how to do exactly this with ASP.Net/c#:
http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing
Here is the section of code that you probably care about:
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); } }
The complete code is on that website I linked above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With