You are here

function commerce_avatax_ui_admin_form_validate_credentials in Drupal Commerce Connector for AvaTax 7.4

Validate AvaTax account and license by testing the primary address.

1 call to commerce_avatax_ui_admin_form_validate_credentials()
commerce_avatax_ui_admin_settings in includes/commerce_avatax_ui.admin.inc
Admin settings menu callback.

File

includes/commerce_avatax_ui.admin.inc, line 735
Admin settings for commerce_avatax.

Code

function commerce_avatax_ui_admin_form_validate_credentials($form, $form_state) {

  // Only validate if the user has clicked on the 'Validate credentials' button.
  if ($form_state['clicked_button']['#value'] != $form['options']['credentials']['fields']['validate_btn']['#value']) {
    return;
  }
  $parameters = array(
    'line1' => $form_state['values']['commerce_avatax_primary_street1'],
    'line2' => $form_state['values']['commerce_avatax_primary_street2'],
    'city' => $form_state['values']['commerce_avatax_primary_city'],
    'region' => $form_state['values']['commerce_avatax_primary_state'],
    'country' => $form_state['values']['commerce_avatax_primary_country'],
    'postalcode' => $form_state['values']['commerce_avatax_primary_zip'],
  );
  $product_version = $form_state['values']['commerce_avatax_product_version'];
  $use_mode = $form_state['values']['commerce_avatax_use_mode'];
  $account = $form_state['values']['commerce_avatax_' . $product_version . '_' . $use_mode . '_account'];
  $license = $form_state['values']['commerce_avatax_' . $product_version . '_' . $use_mode . '_license'];
  $base_url = 'https://development.avalara.net/1.0';
  if ($use_mode == COMMERCE_AVATAX_PRODUCTION_MODE) {
    $base_url = 'https://rest.avalara.net/1.0';
  }
  $querystring = http_build_query($parameters);
  $querystring = str_replace("amp;", "", $querystring);
  $curl_opts = array(
    // Return result instead of echoing.
    CURLOPT_RETURNTRANSFER => TRUE,
    // Follow redirects, Location: headers.
    CURLOPT_FOLLOWLOCATION => FALSE,
    // But do not redirect more than 10 times.
    CURLOPT_MAXREDIRS => 10,
    // Abort if network connection takes more than 5 seconds.
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_SSL_VERIFYPEER => TRUE,
  );
  $curl_opts[CURLOPT_HTTPHEADER] = array(
    'Content-Type: text/json',
    'Authorization: Basic ' . base64_encode("{$account}:{$license}"),
    'Date: ' . date(DATE_RFC1123, REQUEST_TIME),
  );
  $url = rtrim($base_url, '/') . '/address/validate';
  if ($querystring) {
    $url .= '?' . $querystring;
  }
  $curl = curl_init($url);
  foreach ($curl_opts as $opt => $val) {
    curl_setopt($curl, $opt, $val);
  }
  $body = curl_exec($curl);
  curl_close($curl);
  if ($body === FALSE) {
    return array(
      FALSE,
      t('AvaTax request failed. This may be an out of date SSL certificates on your server.'),
    );
  }
  elseif ($body) {
    $body_parsed = json_decode($body, TRUE);
    if ($body_parsed['ResultCode'] == 'Success') {
      return array(
        TRUE,
        t('AvaTax response confirmed using the account and license key above.'),
      );
    }
  }
  if ($body_parsed['Messages'][0]['Summary']) {
    return array(
      FALSE,
      t('Validate credentials failed: @body', array(
        '@body' => $body_parsed['Messages'][0]['Summary'],
      )),
    );
  }
  else {
    return array(
      FALSE,
      t('Validate credentials failed: AvaTax was not able to return an error message'),
    );
  }
}