You are here

function commerce_avatax_commerce_payment_order_paid_in_full in Drupal Commerce Connector for AvaTax 7.3

Same name and namespace in other branches
  1. 7.5 commerce_avatax.module \commerce_avatax_commerce_payment_order_paid_in_full()

Implements hook_payment_order_paid_in_full().

File

./commerce_avatax_erp.inc, line 88
AvaTax Sales Order Processing - sample module.

Code

function commerce_avatax_commerce_payment_order_paid_in_full($transaction) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $transaction);

  // Get state for sales tax address
  if (variable_get('commerce_avatax_tax_address', '') == 'Billing') {
    if (isset($order_wrapper->commerce_customer_billing->commerce_customer_address)) {
      $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address
        ->value();
      $state = $billing_address['administrative_area'];
    }
  }
  if (variable_get('commerce_avatax_tax_address', '') == 'Shipping') {
    if (isset($order_wrapper->commerce_customer_shipping->commerce_customer_address)) {
      $shipping_address = $order_wrapper->commerce_customer_shipping->commerce_customer_address
        ->value();
      $state = $shipping_address['administrative_area'];
    }
  }

  // exit if delivery address state is not in list of active states
  $avatax_states = array();
  $avatax_states_str = variable_get('commerce_avatax_select_states', '');
  if ($avatax_states_str) {
    $avatax_states = explode(", ", $avatax_states_str);
    if (!in_array($state, $avatax_states)) {
      return FALSE;
    }
  }

  // Required for Avalara Scripts.
  $library = libraries_load('avalara_avatax');
  if (!$library || empty($library['loaded'])) {
    drupal_set_message('Please install Avatax library.', 'warning');
    return FALSE;
  }

  // Get Company code and Company Use Mode
  $company_code = variable_get('commerce_avatax_company_code', '');
  $use_mode = variable_get('commerce_avatax_use_mode', '');

  // Get sales tax amount from avatax line item
  foreach ($order_wrapper->commerce_line_items as $line => $line_item_wrapper) {
    $line_item = $line_item_wrapper
      ->value();
    if ($line_item->type == 'avatax') {
      $tax = $line_item->commerce_unit_price['und']['0']['amount'] / 100;
    }
  }
  $ava_order_total = $transaction->commerce_order_total['und']['0']['amount'] / 100 - $tax;
  $client = new TaxServiceSoap($use_mode);
  $request = new PostTaxRequest();

  // Locate Document by Invoice Number.
  $request
    ->setDocCode('dc-' . $transaction->order_id . '');
  $date_time = new DateTime();
  $request
    ->setDocDate(date_format($date_time, "Y-m-d"));
  $request
    ->setDocType('SalesInvoice');
  $request
    ->setCompanyCode($company_code);
  $request
    ->setTotalAmount($ava_order_total);
  $request
    ->setTotalTax($tax);
  try {
    $result = $client
      ->postTax($request);
    if ($result
      ->getResultCode() != SeverityLevel::$Success) {
      foreach ($result
        ->getMessages() as $msg) {
        drupal_set_message(t('AvaTax error: ' . $msg
          ->getName() . " - " . $msg
          ->getSummary() . " - " . $msg
          ->getDetails() . ''), 'error');
      }
    }
  } catch (SoapFault $exception) {
    $msg = 'SOAP Exception: ';
    if ($exception) {
      $msg .= $exception->faultstring;
    }
    drupal_set_message(filter_xss('AvaTax message is: ' . $msg . '.'), 'error');
    drupal_set_message(filter_xss('AvaTax last request is: ' . $client
      ->__getLastRequest() . ''), 'error');
    drupal_set_message(filter_xss('AvaTax last response is: ' . $client
      ->__getLastResponse() . ''), 'error');
  }
}