You are here

public static function CreditCard::detectType in Commerce Core 8.2

Detects the credit card type based on the number.

Parameters

string $number: The credit card number.

Return value

\Drupal\commerce_payment\CreditCardType|null The credit card type, or NULL if unknown.

2 calls to CreditCard::detectType()
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 128

Class

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

Namespace

Drupal\commerce_payment

Code

public static function detectType($number) {
  if (!is_numeric($number)) {
    return NULL;
  }
  $types = self::getTypes();
  foreach ($types as $type) {
    foreach ($type
      ->getNumberPrefixes() as $prefix) {
      if (self::matchPrefix($number, $prefix)) {
        return $type;
      }
    }
  }
  return NULL;
}