You are here

public static function CreditCard::validateNumber in Commerce Core 8.2

Validates the given credit card number.

Parameters

string $number: The credit card number.

\Drupal\commerce_payment\CreditCardType $type: The credit card type.

Return value

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

2 calls to CreditCard::validateNumber()
CreditCardTest::testValidateNumber in modules/payment/tests/src/Unit/CreditCardTest.php
@covers ::detectType @covers ::matchPrefix @covers ::validateNumber @covers ::validateLuhn @dataProvider cardsProvider
PaymentMethodAddForm::validateCreditCardForm in modules/payment/src/PluginForm/PaymentMethodAddForm.php
Validates the credit card form.

File

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

Class

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

Namespace

Drupal\commerce_payment

Code

public static function validateNumber(string $number, CreditCardType $type) : bool {
  if (!is_numeric($number)) {
    return FALSE;
  }
  if (!in_array(strlen($number), $type
    ->getNumberLengths())) {
    return FALSE;
  }
  if ($type
    ->usesLuhn() && !self::validateLuhn($number)) {
    return FALSE;
  }
  return TRUE;
}