You are here

protected function CreditCardPaymentMethodBase::validateCardNumber in Ubercart 8.4

Validates a credit card number during checkout.

Parameters

string $number: Credit card number as a string.

Return value

bool TRUE if card number is valid according to the Luhn algorithm.

See also

https://en.wikipedia.org/wiki/Luhn_algorithm

2 calls to CreditCardPaymentMethodBase::validateCardNumber()
CreditCardPaymentMethodBase::cartDetails in payment/uc_credit/src/CreditCardPaymentMethodBase.php
Returns the form or render array to be displayed at checkout.
CreditCardPaymentMethodBase::cartProcess in payment/uc_credit/src/CreditCardPaymentMethodBase.php
Called when checkout is submitted with this payment method selected.

File

payment/uc_credit/src/CreditCardPaymentMethodBase.php, line 695

Class

CreditCardPaymentMethodBase
Defines a base credit card payment method plugin implementation.

Namespace

Drupal\uc_credit

Code

protected function validateCardNumber($number) {
  $id = substr($number, 0, 1);
  $types = $this
    ->getEnabledTypes();
  if ($id == 3 && empty($types['amex']) || $id == 4 && empty($types['visa']) || $id == 5 && empty($types['mastercard']) || $id == 6 && empty($types['discover']) || !ctype_digit($number)) {
    return FALSE;
  }
  $total = 0;
  for ($i = 0; $i < strlen($number); $i++) {
    $digit = substr($number, $i, 1);
    if ((strlen($number) - $i - 1) % 2) {
      $digit *= 2;
      if ($digit > 9) {
        $digit -= 9;
      }
    }
    $total += $digit;
  }
  if ($total % 10 != 0) {
    return FALSE;
  }
  return TRUE;
}