You are here

function commerce_avatax_credentials_settings_validate in Drupal Commerce Connector for AvaTax 7.5

Validate handler for the "Validate credentials" button.

1 string reference to 'commerce_avatax_credentials_settings_validate'
commerce_avatax_credentials_settings_form in includes/commerce_avatax.admin.inc
Builds the AvaTax credentials settings form.

File

includes/commerce_avatax.admin.inc, line 124
Administrative callbacks for the Commerce Avatax module.

Code

function commerce_avatax_credentials_settings_validate($form, &$form_state) {
  $var_prefix = COMMERCE_AVATAX_VAR_PREFIX;
  $api_mode = $form_state['values'][$var_prefix . 'api_mode'];
  $account_number_field = $var_prefix . $api_mode . '_' . COMMERCE_AVATAX_ACCOUNT_NUMBER;
  $license_key_field = $var_prefix . $api_mode . '_' . COMMERCE_AVATAX_LICENSE_KEY;
  $account_number = $form_state['values'][$account_number_field];
  if (empty($account_number)) {
    $account_number = variable_get($account_number_field, '');
  }
  $license_key = $form_state['values'][$license_key_field];
  if (empty($license_key)) {
    $license_key = variable_get($license_key_field, '');
  }
  $valid = FALSE;

  // Validate the credentials provided.
  if (!empty($account_number) && !empty($license_key)) {
    $api_key = base64_encode("{$account_number}:{$license_key}");
    if ($avatax = commerce_avatax_object($api_key, $api_mode)) {
      $ping_request = $avatax
        ->ping();
      if ($ping_request['success'] && !empty($ping_request['result']['authenticated'])) {
        $valid = TRUE;
      }
    }
    if (!$valid) {
      form_set_error($account_number_field);
      form_set_error($license_key_field);
      drupal_set_message(t('Could not authenticate to the Avatax API.'), 'error');
    }
    else {
      $form_state['credentials_validated'] = TRUE;
      drupal_set_message(t('AvaTax response confirmed using the account and license key above.'));
    }
    $form_state['rebuild'] = TRUE;
  }
}