You are here

public static function CommercePaymentCreditCard::validateLuhn in Commerce Core 7

Validates the given credit card number using the Luhn algorithm.

Parameters

string $number: The credit card number.

Return value

bool TRUE if the credit card number is valid, FALSE otherwise.

1 call to CommercePaymentCreditCard::validateLuhn()
CommercePaymentCreditCard::validateNumber in modules/payment/includes/commerce_payment.credit_card.inc
Validates the given credit card number.

File

modules/payment/includes/commerce_payment.credit_card.inc, line 588
Credit-card helper functions for Drupal commerce.

Class

CommercePaymentCreditCard
Provides logic for listing card types and validating card details.

Code

public static function validateLuhn($number) {
  $total = 0;
  foreach (array_reverse(str_split($number)) as $i => $digit) {
    $digit = $i % 2 ? $digit * 2 : $digit;
    $digit = $digit > 9 ? $digit - 9 : $digit;
    $total += $digit;
  }
  return $total % 10 === 0;
}