You are here

function uc_recurring_authorizenet_cim_update_form_validate in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 includes/uc_recurring.uc_authorizenet.inc \uc_recurring_authorizenet_cim_update_form_validate()

Implements update form validation for the authorizenet CIM gateway.

File

includes/uc_recurring.uc_authorizenet.inc, line 159
Uc recurring implementation for the test gateway module.

Code

function uc_recurring_authorizenet_cim_update_form_validate(&$form, &$form_state) {
  $has_errors = FALSE;
  $values = $form_state['values'];

  // Make sure an owner value was entered.
  if (variable_get('uc_credit_owner_enabled', FALSE) && empty($values['cc_data']['cc_owner'])) {
    form_set_error('cc_data][cc_owner', t('Enter the owner name as it appears on the card.'));
    $has_errors = TRUE;
  }

  // Validate the CC number if that's turned on/check for non-digits.
  if (variable_get('uc_credit_validate_numbers', TRUE)) {

    // Remove the non-numeric characters
    $cc_number = preg_replace('/[^0-9]/', '', $values['cc_data']['cc_number']);
    form_set_value($form['cc_data']['cc_number'], $cc_number, $form_state);
    if (!_uc_credit_valid_card_number($cc_number) || !ctype_digit($cc_number)) {
      form_set_error('cc_data][cc_number', t('You have entered an invalid credit card number.'));
      $has_errors = TRUE;
    }
  }

  // Validate the start date (if entered).
  if (variable_get('uc_credit_start_enabled', FALSE) && !_uc_credit_valid_card_start($values['cc_data']['cc_start_month'], $values['cc_data']['cc_start_year'])) {
    form_set_error('cc_data][cc_start_month', t('The start date you entered is invalid.'));
    form_set_error('cc_data][cc_start_year', t('The start date you entered is invalid.'));
    $has_errors = TRUE;
  }

  // Validate the card expiration date.
  if (!_uc_credit_valid_card_expiration($values['cc_data']['cc_exp_month'], $values['cc_data']['cc_exp_year'])) {
    form_set_error('cc_data][cc_exp_month', t('The credit card you entered has expired.'));
    form_set_error('cc_data][cc_exp_year', t('The credit card you entered has expired.'));
    $has_errors = TRUE;
  }

  // Validate the issue number (if entered).  With issue numbers, '01' is
  // different from '1', but is_numeric() is still appropriate.
  if (variable_get('uc_credit_issue_enabled', FALSE) && !_uc_credit_valid_card_issue($values['cc_data']['cc_issue'])) {
    form_set_error('cc_data][cc_issue', t('The issue number you entered is invalid.'));
    $has_errors = TRUE;
  }

  // Validate the CVV number if enabled.
  if (variable_get('uc_credit_cvv_enabled', TRUE) && !_uc_credit_valid_cvv($values['cc_data']['cc_cvv'])) {
    form_set_error('cc_data][cc_cvv', t('You have entered an invalid CVV number.'));
    $has_errors = TRUE;
  }

  // Validate the bank name if enabled.
  if (variable_get('uc_credit_bank_enabled', FALSE) && empty($values['cc_data']['cc_bank'])) {
    form_set_error('cc_data][cc_bank', t('You must enter the issuing bank for that card.'));
    $has_errors = TRUE;
  }

  //Form requires rebuilding form so codes and expiration blocks will display correctly
  if ($has_errors) {

    // remove duplicate error messages when two fields share a message
    $_SESSION['messages']['error'] = array_unique($_SESSION['messages']['error']);
    $form_state["rebuild"] = TRUE;
  }
}