You are here

public static function CreditCard::validateLuhn in Commerce Core 8.2

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 CreditCard::validateLuhn()
CreditCard::validateNumber in modules/payment/src/CreditCard.php
Validates the given credit card number.

File

modules/payment/src/CreditCard.php, line 201

Class

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

Namespace

Drupal\commerce_payment

Code

public static function validateLuhn(string $number) : bool {
  $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;
}