You are here

protected function PaymentMethodAddForm::validateCreditCardForm in Commerce Core 8.2

Validates the credit card form.

Parameters

array $element: The credit card form element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the complete form.

1 call to PaymentMethodAddForm::validateCreditCardForm()
PaymentMethodAddForm::validateConfigurationForm in modules/payment/src/PluginForm/PaymentMethodAddForm.php
Form validation handler.

File

modules/payment/src/PluginForm/PaymentMethodAddForm.php, line 180

Class

PaymentMethodAddForm

Namespace

Drupal\commerce_payment\PluginForm

Code

protected function validateCreditCardForm(array &$element, FormStateInterface $form_state) {
  $values = $form_state
    ->getValue($element['#parents']);
  $card_type = CreditCard::detectType($values['number']);
  if (!$card_type) {
    $form_state
      ->setError($element['number'], t('You have entered a credit card number of an unsupported card type.'));
    return;
  }
  if (!CreditCard::validateNumber($values['number'], $card_type)) {
    $form_state
      ->setError($element['number'], t('You have entered an invalid credit card number.'));
  }
  if (!CreditCard::validateExpirationDate($values['expiration']['month'], $values['expiration']['year'])) {
    $form_state
      ->setError($element['expiration'], t('You have entered an expired credit card.'));
  }
  if (!CreditCard::validateSecurityCode($values['security_code'], $card_type)) {
    $form_state
      ->setError($element['security_code'], t('You have entered an invalid CVV.'));
  }

  // Persist the detected card type.
  $form_state
    ->setValueForElement($element['type'], $card_type
    ->getId());
}