You are here

function commerce_avatax_checkout_validate in Drupal Commerce Connector for AvaTax 7.5

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

Checkout form validation callback.

1 string reference to 'commerce_avatax_checkout_validate'
commerce_avatax_form_alter in ./commerce_avatax.module
Implements hook_form_alter().

File

./commerce_avatax.module, line 944
AvaTax service integration from Avalara, Inc.

Code

function commerce_avatax_checkout_validate($form, &$form_state) {

  // If the rebuild flag is set to TRUE by Commerce Checkout skip the address
  // validation.
  if (!empty($form_state['rebuild'])) {
    return;
  }
  $form_state['commerce_avatax'] = array();
  $field_name = commerce_avatax_get_customer_profile_field();

  // Skip the address validation if we couldn't find the field.
  if (!$field_name || !($field = field_info_field($field_name))) {
    return;
  }
  $profile_type = $field['settings']['profile_type'];
  $pane_id = 'customer_profile_' . $profile_type;

  // Addressbook 3.x integration
  // Check if the profile can be found in the $form_state object.
  if (isset($form_state['pane_' . $pane_id]['profile'])) {
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $form_state['pane_' . $pane_id]['profile']);
  }
  else {

    // The form state order object is stale, we need to reload it.
    $order = commerce_order_load($form_state['order']->order_id);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $profile_wrapper = $order_wrapper->{$field_name};
  }
  if (!isset($profile_wrapper->commerce_customer_address)) {
    return;
  }
  $address = $profile_wrapper->commerce_customer_address
    ->value();
  $country = $address['country'];
  $enabled_countries = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'address_validate_countries', array(
    'US',
  ));
  if (!in_array($country, $enabled_countries)) {
    return;
  }

  // Include the address validation functions.
  module_load_include('inc', 'commerce_avatax', 'includes/commerce_avatax.address');
  $validated_addresses = commerce_avatax_validate_address($address);
  if ($validated_addresses === NULL) {
    return;
  }
  $result = commerce_avatax_address_compare($address, $validated_addresses);
  if ($result['result'] != 'valid') {
    $form_state['commerce_avatax']['address_validation_failed'] = TRUE;
    $form_state['commerce_avatax']['address_validation_result'] = $result;

    // Store the customer profile ID to update.
    $form_state['commerce_avatax']['customer_profile_to_update'] = $profile_wrapper
      ->getIdentifier();
    $form_state['rebuild'] = TRUE;
  }
}