<?xml version="1.0" encoding="windows-1252"?>
<node id="192205" title="Parsing credit card track data" created="2002-08-22 18:55:30" updated="2005-07-31 20:25:57">
<type id="115">
perlquestion</type>
<author id="17000">
Ovid</author>
<data>
<field name="doctext">
&lt;p&gt;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 [http://www.howstuffworks.com/question503.htm|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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;code&gt;
    # works with both a keyed number and a track data
    my $card_num = $tender-&gt;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;
&lt;/code&gt;

&lt;p&gt;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 [http://www.google.com/|oracle] let me down.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;br /&gt;
&lt;a href="/index.pl?node=Ovid&amp;lastnode_id=1072"&gt;Ovid&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;small&gt;Join the &lt;a href="http://setiathome.ssl.berkeley.edu/stats/team/team_86606.html"&gt;Perlmonks Setiathome Group&lt;/a&gt; or just click on the the link and check out our stats.&lt;/small&gt;&lt;/p&gt;</field>
</data>
</node>
