passes($attribute, $value)) { $fail('The :attribute is not a valid credit card number.'); } } public function passes($attribute, $value) { // Remove non-numeric characters $cardNumber = preg_replace('/\D/', '', $value); $sum = 0; $shouldDouble = false; // Process the digits from right to left for ($i = strlen($cardNumber) - 1; $i >= 0; $i--) { $digit = (int)$cardNumber[$i]; if ($shouldDouble) { $digit *= 2; if ($digit > 9) { $digit -= 9; } } $sum += $digit; $shouldDouble = !$shouldDouble; } // Return true if the total modulo 10 is 0 return ($sum % 10 === 0); } }