You are here

function commerce_avatax_validate_address in Drupal Commerce Connector for AvaTax 7.5

Same name and namespace in other branches
  1. 7.4 commerce_avatax.module \commerce_avatax_validate_address()

Validate the address entered on checkout form.

1 call to commerce_avatax_validate_address()
commerce_avatax_checkout_validate in ./commerce_avatax.module
Checkout form validation callback.

File

includes/commerce_avatax.address.inc, line 6

Code

function commerce_avatax_validate_address($address) {
  $avatax = commerce_avatax_object();
  if (!$avatax) {
    watchdog('commerce_avatax', 'Could not connect to AvaTax for address validation.');
    return NULL;
  }

  // Prepare the address resolve request, we need to map the addressfield
  // components to the format expected by Avatax.
  $parameters = array();
  $address_mapping = commerce_avatax_address_fields_mapping();

  // Prepare the address to be validated.
  foreach ($address as $key => $value) {
    if (!isset($address_mapping[$key])) {
      continue;
    }
    $parameters[$address_mapping[$key]] = $value;
  }

  // Call the Avatax API for validating the address.
  $result = $avatax
    ->addressesResolve($parameters);
  if (empty($result['success'])) {
    return array();
  }

  // If the address could not validated, return an empty array.
  if (is_array($result['result']) && isset($result['result']['messages'])) {
    $message = reset($result['result']['messages']);
    if ($message['severity'] === 'Error') {
      return array();
    }
  }

  // Return the list of validated addresses returned by the API call.
  if (!empty($result['result']['validatedAddresses'])) {
    return $result['result']['validatedAddresses'];
  }
  return array();
}