Tag: PHP

Fake Credit Cards

Recently I was needed to create sample credit card numbers that will pass the Luhn Algorithm so that they will pass basic credit card testing mechanisms.

My first idea was to search google and find someone that already did it. Unfortunately I did not find any snippets of code (I didn’t really look hard I have to be honest) in PHP. Luckily for me I have found a javascript one that I could easily convert into PHP - This is How Credit Card Numbers Are Generated via Cody Taylor.

I just noticed something happend to the code while i copied it here.

I will update it tonight!

function generateFakeCreditCard() {
    	$random_number = '';
    	$card_number = '4';
    	for($i = 2; $i     		$random_number = rand(1,9);
    		$card_number = $card_number.$random_number;
    	}
    	$sum = 0;
    	$digit = 0;
    	for($i = 1; $i  9) {
    				$digit = intval($digit[0])+intval($digit[1]);
    			}
    			$sum += $digit;
    		} else {
    			$sum += $card_number[$i-1];
    		}
    	}
    	$remains = $sum%10;
    	if ($remains != 0) {
    		$last_digit = intval($card_number[15]);
    		if ($remains     			$card_number = substr($card_number, 0, 15) . $last_digit-$remains;
    		} else {
    			$card_number = substr($card_number, 0, 15) . $last_digit+(10-$remains);
    		}
    	}
    	$sum = 0;
    	$digit = 0;
    	for($i = 1; $i  9) {
    				$digit = intval($digit[0])+intval($digit[1]);
    			}
    			$sum += $digit;
    		} else {
    			$sum += $card_number[$i-1];
    		}
    	}
    	return strval($card_number);
}

Just copy this code as is and call it and it will return a credit card number that will pass Luhn Algorithm.

None of the generated cards will work! – The purpose for generating the credit card numbers are to supply developers with a credit card for testing of a payment system. Do not use this for anything illegal!