You are here

function _uc_credit_valid_card_number in Ubercart 7.3

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 _uc_credit_valid_card_number()
uc_payment_method_credit in payment/uc_credit/uc_credit.module
Callback function for the Credit Card payment method.
uc_payment_method_credit_form in payment/uc_credit/uc_credit.module
Displays the credit card details form on the checkout screen.

File

payment/uc_credit/uc_credit.module, line 839
Defines the credit card payment method and hooks in payment gateways.

Code

function _uc_credit_valid_card_number($number) {
  $id = substr($number, 0, 1);
  if ($id == 3 && !variable_get('uc_credit_amex', TRUE) || $id == 4 && !variable_get('uc_credit_visa', TRUE) || $id == 5 && !variable_get('uc_credit_mastercard', TRUE) || $id == 6 && !variable_get('uc_credit_discover', TRUE) || !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;
}