Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Parsing credit card track data

by Ovid (Cardinal)
on Aug 22, 2002 at 22:55 UTC ( [id://192205] : perlquestion . print w/replies, xml ) Need Help??

Ovid has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have any suggestions for a robust method of parsing credit card track data? Background: I need to be able to print credit card numbers on receipts, with all but the last four number turned to asterisks. If the credit card is swiped through the reader, as opposed to the number being keyed in by hand, then I get the magnetic "track data" (which you can read about here). Unfortunately, I can't seem to find clear specs on this. Further, American Express seems to have a slightly different format than other credit cards.

Based upon a combination of the above article and an actual examination of various credit cards in the office, I have the following snippet of code:

# works with both a keyed number and a track data my $card_num = $tender->ccnumber; $card_num =~ s/^.? # Start sentinel [bB]? # Format code="B" ( # Capture to $1 [^^]+ # Primary account number ) # End of card .* # Slurp up the rest /$1/x; $card_num =~ s/^(.+)(.{4})$/('*'x(length $1)).$2/e;

Unfortunately, this is just an ad hoc solution and I would love to find something more robust. I couldn't find any CPAN modules and the oracle let me down.


Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Parsing credit card track data
by Arien (Pilgrim) on Aug 22, 2002 at 23:37 UTC
Re: Parsing credit card track data
by mattr (Curate) on Aug 26, 2002 at 10:49 UTC
    On the off chance that the oracle might have missed a useful Japanese page I took a swing. Found a very interesting page in Japanese which covers different standards and some history of terminals and codes. Anyway you can probably see the code names in double-width numerals even if you can't read the rest of the kanji. Certainly the MC/Cirrus logos will show up..

    Basically there seem to be two major standards in Japan for the magnetic strip, JIS-I and JIS-II. I think this page is from 4 years ago. It says that II was meant for domestic use and I is the international standard, but that II was pretty much scrapped for compatibility.

    JIS-I is the same as the ISO standard. Here is a brief translation of this first paragraph for JIS-I.

    A. Tracks used and related standards
    Credit cards use the second track part (ABA Standard = American Bankers Association Standard). The format follows the ISO standard (ISO 2894/3166/3554/4217).

    Note: I looked at and 2894 is withdrawn. 3166 is the country code spec. 3554 is also withdrawn, but is the magnetic stripe encoding spec(?). 4217 is the currency code. Unfortunately it looks like they want you to pay to see the standards (jeez!). I started surfing (the sitemap) and found but it is all not very helpful. Hmpf. Maybe they should hire a programmer to provide a credit card parsing service. It is tantalizing but I must be too dense, can't find where the info is.

    B. JIS-I type format
    Encoded data content uses the following format.
    Starting code/Bank number(maximum 19 digits)/separator/expiry date/free region/ending code/LRC, and the member number is the only decisive key used to determine the issuing bank. (Usually that is the top 3 to 6 digits, but it is allowed to be a region of up to 12 digits)

    C. ISO Standard Industry Assignments(International Credit Card Assignments)
    (You can probably read this table if you have an recent browser. It says what industry is what code, 0 to 9.)

    Update: I found mention on the infowar ml about someone who was asking a similar question, and whether the magstripe could hold credit history info. There are machines that say they can parse, according to the oracle, but I'm not so sure the credit companies want people to know what is in there..

    Here's a lousy standards-for-sale store at X9: Track 3 Data (for $40) is: - Found it by giving the oracle " magnetic".

    I found a brief description of the 3 or 4 stripes and who made them at Introduction to Magnetic Stripe Cards from North Florida U. and this pdf from might actually be of some help.

    Actually that last PDF is extremely detailed, but only a rough breakdown of the central field.