Last active
December 14, 2015 14:29
-
-
Save justinpfister/5100623 to your computer and use it in GitHub Desktop.
PHP Generate CreditCard numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // 1-number GenerateCreditCardUtility::credit_card_number('visa'); | |
| // many-numbers GenerateCreditCardUtility::credit_card_numbers('visa',15,10); | |
| class GenerateCreditCardUtility{ | |
| private $PrefixList = []; | |
| function __construct(){ | |
| $this->PrefixList['visa'][] = "4539"; | |
| $this->PrefixList['visa'][] = "4556"; | |
| $this->PrefixList['visa'][] = "4916"; | |
| $this->PrefixList['visa'][] = "4532"; | |
| $this->PrefixList['visa'][] = "4929"; | |
| $this->PrefixList['visa'][] = "40240071"; | |
| $this->PrefixList['visa'][] = "4485"; | |
| $this->PrefixList['visa'][] = "4716"; | |
| $this->PrefixList['visa'][] = "4"; | |
| $this->PrefixList['mastercard'][] = "51"; | |
| $this->PrefixList['mastercard'][] = "52"; | |
| $this->PrefixList['mastercard'][] = "53"; | |
| $this->PrefixList['mastercard'][] = "54"; | |
| $this->PrefixList['mastercard'][] = "55"; | |
| $this->PrefixList['amex'][] = "34"; | |
| $this->PrefixList['amex'][] = "37"; | |
| $this->PrefixList['discover'][] = "6011"; | |
| $this->PrefixList['diners'][] = "300"; | |
| $this->PrefixList['diners'][] = "301"; | |
| $this->PrefixList['diners'][] = "302"; | |
| $this->PrefixList['diners'][] = "303"; | |
| $this->PrefixList['diners'][] = "36"; | |
| $this->PrefixList['diners'][] = "38"; | |
| $this->RoutePrefixList['enroute'][] = "2014"; | |
| $this->RoutePrefixList['enroute'][] = "2149"; | |
| $this->PrefixList['jsb'][] = "35"; | |
| $this->PrefixList['voyager'][] = "8699"; | |
| } | |
| private function getPrefixArray(){ | |
| return $this->PrefixList; | |
| } | |
| /* | |
| 'prefix' is the start of the CC number as a string, any number of digits. | |
| 'length' is the length of the CC number to generate. Typically 13 or 16 | |
| */ | |
| private function completed_number($prefix, $length) { | |
| $ccnumber = $prefix; | |
| # generate digits | |
| while ( strlen($ccnumber) < ($length - 1) ) { | |
| $ccnumber .= rand(0,9); | |
| } | |
| # Calculate sum | |
| $sum = 0; | |
| $pos = 0; | |
| $reversedCCnumber = strrev( $ccnumber ); | |
| while ( $pos < $length - 1 ) { | |
| $odd = $reversedCCnumber[ $pos ] * 2; | |
| if ( $odd > 9 ) { | |
| $odd -= 9; | |
| } | |
| $sum += $odd; | |
| if ( $pos != ($length - 2) ) { | |
| $sum += $reversedCCnumber[ $pos +1 ]; | |
| } | |
| $pos += 2; | |
| } | |
| # Calculate check digit | |
| $checkdigit = (( floor($sum/10) + 1) * 10 - $sum) % 10; | |
| $ccnumber .= $checkdigit; | |
| return $ccnumber; | |
| } | |
| static function credit_card_numbers($prefix, $length, $howMany) { | |
| $generator = new self; | |
| if(!isset($generator->getPrefixArray()[$prefix])) throw new \Exception('Credit Card Prefix Unavailable'); | |
| $prefixList = $generator->getPrefixArray()[$prefix]; | |
| for ($i = 0; $i < $howMany; $i++) { | |
| $ccnumber = $prefixList[ array_rand($prefixList) ]; | |
| $result[] = $generator->completed_number($ccnumber, $length); | |
| } | |
| return $result; | |
| } | |
| static function credit_card_number($prefix) { | |
| $length = ($prefix == 'amex')? 15 : 16; | |
| return self::credit_card_numbers($prefix,$length,1)[0]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment