Skip to content

Instantly share code, notes, and snippets.

@noorus
Last active August 29, 2015 13:55
Show Gist options
  • Save noorus/8772551 to your computer and use it in GitHub Desktop.
Save noorus/8772551 to your computer and use it in GitHub Desktop.
Some quickie dumb credit card decoding stuff
<?php
class CreditCard
{
const Industry_ISO = 0;
const Industry_Airlines = 1;
const Industry_Airlines2 = 2;
const Industry_Travel = 3;
const Industry_Financial = 4;
const Industry_Financial2 = 5;
const Industry_Merchandising = 6;
const Industry_Petroleum = 7;
const Industry_Telecommunications = 8;
const Industry_National = 9;
protected $mNumber = null;
protected $mChecksum = null;
protected $mIndustry = null;
protected $mCountryCode = null; // ISO 3166-1
protected $mIssuer = null;
protected $mAccount = null;
public function __construct( $code )
{
$code = filter_var( $code, FILTER_SANITIZE_NUMBER_INT );
if ( mb_strlen( $code ) < 8
|| mb_strlen( $code ) > 19
|| mb_strpos( $code, '+' ) !== false
|| mb_strpos( $code, '-' ) !== false )
throw new \RuntimeException( 'Invalid credit card number' );
$this->mNumber = $code;
$this->mChecksum = self::checksum( $code );
if ( $this->mChecksum % 10 != 0 )
throw new \RuntimeException( 'Invalid credit card checksum' );
$this->mIndustry = (int)mb_substr( $code, 0, 1 );
if ( $this->isNational() )
$this->mCountryCode = mb_substr( $code, 1, 3 );
$this->mIssuer = mb_substr( $code, 0, 6 );
$this->mAccount = mb_substr( $code, 6, mb_strlen( $code ) - 7 );
}
public function describe()
{
printf( "Credit Card\nNumber: %s\nChecksum: %d\nIndustry: %s\nCountry: %s\nIssuer: %s\nAccount Number: %s\n",
self::grouping( $this->mNumber ),
$this->mChecksum,
self::industryName( $this->mIndustry ),
$this->mCountryCode !== null ? $this->mCountryCode : "None",
$this->mIssuer,
$this->mAccount
);
}
public function getChecksum()
{
return $this->mChecksum;
}
public function getIndustry()
{
return $this->mIndustry;
}
public function isNational()
{
return $this->mIndustry == self::Industry_National;
}
public function getCountryCode()
{
return $this->mCountryCode;
}
public function getIssuer()
{
return $this->mIssuer;
}
public function getAccount()
{
return $this->mAccount;
}
public static function industryName( $industry )
{
switch ( $industry )
{
case self::Industry_ISO: return "ISO/TC68"; break;
case self::Industry_Airlines: return "Airlines"; break;
case self::Industry_Airlines2: return "Airlines 2"; break;
case self::Industry_Travel: return "Travel and Entertainment"; break;
case self::Industry_Financial: return "Banking and Financial"; break;
case self::Industry_Financial2: return "Banking and Financial 2"; break;
case self::Industry_Merchandising: return "Merchandising"; break;
case self::Industry_Petroleum: return "Petroleum"; break;
case self::Industry_Telecommunications: return "Telecommunications"; break;
case self::Industry_National: return "National"; break;
}
}
public static function grouping( $value )
{
$out = '';
$value = str_split( $value );
for ( $i = 1; $i <= count( $value ); $i++ )
{
$out .= $value[$i-1];
if ( $i % 4 == 0 )
$out .= ' ';
}
return $out;
}
public static function checksum( $code )
{
$code = str_split( $code );
$checksum = 0;
for ( $i = count( $code ) - 1; $i >= 0; $i-- )
{
$c = (int)$code[$i];
if ( ( ( $i - count( $code ) ) % 2 ) == 0 )
$c *= 2;
$c = str_split( $c );
foreach ( $c as $y )
$checksum += (int)$y;
}
return $checksum;
}
}
$cc = new CreditCard( '4485753580439' );
$cc->describe();
unset( $cc );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment