You are here

function commerce_avatax_post in Drupal Commerce Connector for AvaTax 7.4

Sends HTTP POST request to endpoint.

Return value

array Returns an associative array containing 'meta' and 'body' elements.

2 calls to commerce_avatax_post()
commerce_avatax_get_tax in includes/commerce_avatax_calc.inc
Gets the tax amount for the order based on the delivery address.
_commerce_avatax_update in ./commerce_avatax.rules.inc
Send Commit/Cancel operation to AvaTax.

File

./commerce_avatax.module, line 676
Calculate Sales Tax using AvaTax service from Avalara, Inc.

Code

function commerce_avatax_post($endpoint, $data, $base_url = '', $account = '', $license = '') {
  $curl_opts = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => FALSE,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_SSL_VERIFYPEER => TRUE,
  );

  // Commerce Avatax requires SSL peer verification, which may prevent out of
  // date servers from successfully processing API requests. If you get an error
  // related to peer verification, you may need to download the CA certificate
  // bundle file from http://curl.haxx.se/docs/caextract.html, place it in a
  // safe location on your web server, and update your settings.php to set the
  // commerce_avatax_cacert variable to contain the absolute path of the file.
  // Alternately, you may be able to update your php.ini to point to the file
  // with the curl.cainfo setting.
  if (variable_get('commerce_avatax_cacert', FALSE)) {
    $curl_opts[CURLOPT_CAINFO] = variable_get('commerce_avatax_cacert', '');
  }
  list($use_mode, $product_version, $account, $license, $base_url) = commerce_avatax_get_config($account, $license, $base_url);
  if (is_array($data)) {
    $data = json_encode($data);
  }
  $curl_opts[CURLOPT_POSTFIELDS] = $data;
  $curl_opts[CURLOPT_HTTPHEADER] = array(
    'Content-Length: ' . strlen($data),
    'Content-Type: text/json',
    'Authorization: Basic ' . base64_encode("{$account}:{$license}"),
    'Date: ' . date(DATE_RFC1123, REQUEST_TIME),
  );
  $url = rtrim($base_url, '/') . '/' . ltrim($endpoint, '/');
  $curl = curl_init($url);
  foreach ($curl_opts as $opt => $val) {
    curl_setopt($curl, $opt, $val);
  }
  $body = curl_exec($curl);
  $meta = curl_getinfo($curl);
  curl_close($curl);
  if ($body === FALSE) {
    watchdog('commerce_avatax', 'AvaTax request failed. message: %msg', array(
      '%msg' => curl_error($curl),
    ), WATCHDOG_ERROR);
    return array(
      'body' => '',
      'meta' => $meta,
    );
  }
  if ($use_mode == COMMERCE_AVATAX_DEVELOPMENT_MODE) {
    watchdog('commerce_avatax', 'Request info: !url !headers !body !response !meta', array(
      '!url' => "<pre>URL : {$url}</pre>",
      '!headers' => "<pre>Request Headers:\n" . var_export($curl_opts[CURLOPT_HTTPHEADER], TRUE) . '</pre>',
      '!body' => "<pre>Request body:\n" . $data . '</pre>',
      '!response' => "<pre>Response:\n" . check_plain(var_export($body, TRUE)) . '</pre>',
      '!meta' => "<pre>Response Meta:\n" . var_export($meta, TRUE) . '</pre>',
    ), WATCHDOG_DEBUG);
  }
  if ($body) {
    $body_parsed = json_decode($body, TRUE);
    return array(
      'body' => $body_parsed,
      'meta' => $meta,
    );
  }
  else {
    return array(
      'body' => '',
      'meta' => $meta,
    );
  }
}