/** * Validates a credit card number, with a Luhn check if possible. * * @param integer $number credit card number * @param string|array $type card type, or an array of card types * @return boolean * @uses Valid::luhn */ public static function credit_card($number, $type = NULL) { // Remove all non-digit characters from the number if (($number = preg_replace('/\\D+/', '', $number)) === '') { return FALSE; } if ($type == NULL) { // Use the default type $type = 'default'; } elseif (is_array($type)) { foreach ($type as $t) { // Test each type for validity if (Valid::credit_card($number, $t)) { return TRUE; } } return FALSE; } $cards = Kohana::config('credit_cards'); // Check card type $type = strtolower($type); if (!isset($cards[$type])) { return FALSE; } // Check card number length $length = strlen($number); // Validate the card length by the card type if (!in_array($length, preg_split('/\\D+/', $cards[$type]['length']))) { return FALSE; } // Check card number prefix if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) { return FALSE; } // No Luhn check required if ($cards[$type]['luhn'] == FALSE) { return TRUE; } return Valid::luhn($number); }
/** * Tests Valid::luhn() * * @test * @covers Valid::luhn * @dataProvider provider_luhn() * @param string $number Credit card number * @param boolean $expected */ public function test_luhn($number, $expected) { $this->assertSame($expected, Valid::luhn($number)); }
/** * Validates a credit card number, with a Luhn check if possible. * * @param integer $number credit card number * @param string|array $type card type, or an array of card types * @return boolean * @uses Valid::luhn */ public static function credit_card($number, $type = NULL) { // Remove all non-digit characters from the number if (($number = preg_replace('/\\D+/', '', $number)) === '') { return FALSE; } if ($type == NULL) { // Use the default type $type = 'default'; } elseif (is_array($type)) { foreach ($type as $t) { // Test each type for validity if (Valid::credit_card($number, $t)) { return TRUE; } } return FALSE; } $cards = array('default' => array('length' => '13,14,15,16,17,18,19', 'prefix' => '', 'luhn' => TRUE), 'american express' => array('length' => '15', 'prefix' => '3[47]', 'luhn' => TRUE), 'diners club' => array('length' => '14,16', 'prefix' => '36|55|30[0-5]', 'luhn' => TRUE), 'discover' => array('length' => '16', 'prefix' => '6(?:5|011)', 'luhn' => TRUE), 'jcb' => array('length' => '15,16', 'prefix' => '3|1800|2131', 'luhn' => TRUE), 'maestro' => array('length' => '16,18', 'prefix' => '50(?:20|38)|6(?:304|759)', 'luhn' => TRUE), 'mastercard' => array('length' => '16', 'prefix' => '5[1-5]', 'luhn' => TRUE), 'visa' => array('length' => '13,16', 'prefix' => '4', 'luhn' => TRUE)); // Check card type $type = strtolower($type); if (!isset($cards[$type])) { return FALSE; } // Check card number length $length = strlen($number); // Validate the card length by the card type if (!in_array($length, preg_split('/\\D+/', $cards[$type]['length']))) { return FALSE; } // Check card number prefix if (!preg_match('/^' . $cards[$type]['prefix'] . '/', $number)) { return FALSE; } // No Luhn check required if ($cards[$type]['luhn'] == FALSE) { return TRUE; } return Valid::luhn($number); }