diff -crB commerce_civicrm_old/commerce_civicrm.api.php commerce_civicrm/commerce_civicrm.api.php
*** commerce_civicrm_old/commerce_civicrm.api.php	2014-03-14 07:39:28.000000000 -0600
--- commerce_civicrm/commerce_civicrm.api.php	2015-02-02 10:36:36.474475036 -0600
***************
*** 32,39 ****
    $profile = $order_wrapper->commerce_customer_billing->value();
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
  
    // Then alter $params as required to add any custom commerce fields to send to CiviCRM.
!   $params['job_title'] = $profile_wrapper->field_job_title->value();
    $params['current_employer'] = $profile_wrapper->field_organisation->value();
    $params['phone'] = array(
      array(
--- 32,40 ----
    $profile = $order_wrapper->commerce_customer_billing->value();
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
  
+   // DOING THIS IN commerce_civicrm.module now
    // Then alter $params as required to add any custom commerce fields to send to CiviCRM.
!   /*$params['job_title'] = $profile_wrapper->field_job_title->value();
    $params['current_employer'] = $profile_wrapper->field_organisation->value();
    $params['phone'] = array(
      array(
***************
*** 41,49 ****
        'phone' => $profile_wrapper->field_phone->value(),
        'phone_type_id' => 1,
        'location_type' => 'Home',
!     		'sequential' => 0
      )
!   );
  
    // Update the contact, need this for details like employer, job title etc.
    $params['id'] = $params['contact_id'];
--- 42,50 ----
        'phone' => $profile_wrapper->field_phone->value(),
        'phone_type_id' => 1,
        'location_type' => 'Home',
!       'sequential' => 0
      )
!   );*/
  
    // Update the contact, need this for details like employer, job title etc.
    $params['id'] = $params['contact_id'];
diff -crB commerce_civicrm_old/commerce_civicrm.info commerce_civicrm/commerce_civicrm.info
*** commerce_civicrm_old/commerce_civicrm.info	2014-03-14 07:43:26.000000000 -0600
--- commerce_civicrm/commerce_civicrm.info	2014-12-18 15:29:00.000000000 -0600
***************
*** 13,20 ****
  
  
  ; Information added by Drupal.org packaging script on 2014-03-14
! version = "7.x-1.0-beta1+40-dev"
  core = "7.x"
  project = "commerce_civicrm"
! datestamp = "1394804606"
  
--- 13,20 ----
  
  
  ; Information added by Drupal.org packaging script on 2014-03-14
! version = "7.x-1.0-beta2"
  core = "7.x"
  project = "commerce_civicrm"
! datestamp = "1394804905"
  
diff -crB commerce_civicrm_old/commerce_civicrm.module commerce_civicrm/commerce_civicrm.module
*** commerce_civicrm_old/commerce_civicrm.module	2014-03-14 07:39:28.000000000 -0600
--- commerce_civicrm/commerce_civicrm.module	2015-02-02 10:40:07.059398317 -0600
***************
*** 185,192 ****
    $form_state['commerce_order']->data['cid'] = $cid;
  
    // Get the CiviCRM Contact object.
!   $params = array('id' => $cid, 'version' => 3, 'sequential' => 0);
!   $result = civicrm_api('contact', 'get', $params);
    $contact = $result['values'][$cid];
  
    // Set the order primary mail to the contacts email
--- 185,194 ----
    $form_state['commerce_order']->data['cid'] = $cid;
  
    // Get the CiviCRM Contact object.
!   $params = array(
!     'id' => $cid,
!   );
!   $result = civicrm_api3('contact', 'get', $params);
    $contact = $result['values'][$cid];
  
    // Set the order primary mail to the contacts email
***************
*** 260,284 ****
    if (!civicrm_initialize()) {
      return;
    }
-   require_once 'api/v2/Group.php';
    require_once 'CRM/Core/Config.php';
  
-   $params = array();
-   $groups = civicrm_group_get($params);
    $options = array();
!   foreach ($groups as $group) {
!     $options[$group['id']] = $group['title'];
!   }
!   natsort($options);
  
!   // Get contribution types.
!   // Note, no {} on table name because it's in civicrm db.
!   $sql = "SELECT id, name FROM civicrm_contribution_type WHERE is_active = 1";
!   $dao =& CRM_Core_DAO::executeQuery($sql, array());
!   $types = array(0 => t('Select a type...'));
!   while ($dao->fetch()) {
!     $types[$dao->id] = $dao->name;
    }
  
    $form = array();
    $form['commerce_civicrm_contribution_type'] = array(
--- 261,291 ----
    if (!civicrm_initialize()) {
      return;
    }
    require_once 'CRM/Core/Config.php';
  
    $options = array();
!   try {
!     // Get groups.
!     $params = array();
!     $result = civicrm_api3('Group', 'get', $params);
!     if ($result['values']) {
!       foreach ($result['values'] as $group) {
!         $options[$group['id']] = $group['title'];
!       }
!     }
  
!     // Get contribution types.
!     $sql = 'SELECT id, name FROM civicrm_financial_type';
!     $dao =& CRM_Core_DAO::executeQuery($sql, array());
!     $types = array(0 => t('Select a type...'));
!     while ($dao->fetch()) {
!       $types[$dao->id] = $dao->name;
!     }
!   }
!   catch (Exception $e) {
!     drupal_set_message(t('CiviCRM API error: %message', array('%message' => $e->getMessage())), 'error');
    }
+   natsort($options);
  
    $form = array();
    $form['commerce_civicrm_contribution_type'] = array(
***************
*** 353,377 ****
   *   CRM_Contact_BAO_Group object describing the contact to add
   */
  function _commerce_civicrm_add_to_groups($cid) {
-   require_once 'api/v2/GroupContact.php';
  
    $groups = variable_get('commerce_civicrm_groups', '');
    if (empty($groups)) {
      return;
    }
  
!   foreach ($groups as $id => $key) {
!     if ($key != 0) {
!       $params = array('contact_id' => $cid, 'group_id' => $key);
!       $result = civicrm_group_contact_add($params);
!       if (!empty($result['is_error'])) {
!         watchdog('commerce_civicrm', 'Error adding contact to group: %error', array('%error' => $result['error_message']), WATCHDOG_ERROR);
        }
      }
    }
  }
  
- 
  /**
   * Get contact id for the customer.
   *
--- 359,389 ----
   *   CRM_Contact_BAO_Group object describing the contact to add
   */
  function _commerce_civicrm_add_to_groups($cid) {
  
    $groups = variable_get('commerce_civicrm_groups', '');
    if (empty($groups)) {
      return;
    }
  
!   try {
!     foreach ($groups as $id => $key) {
!       if ($key != 0) {
!         $params = array(
!       'contact_id' => $cid, 
!       'group_id' => $key
!     );
!         $result = civicrm_api3('GroupContact', 'create', $params);
!         if (!empty($result['is_error'])) {
!           watchdog('commerce_civicrm', 'Error adding contact to group: %error', array('%error' => $result['error_message']), WATCHDOG_ERROR);
!         }
        }
      }
    }
+   catch (Exception $e) {
+      watchdog('commerce_civicrm', 'civicrm_contact_search(): %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR);
+   }
  }
  
  /**
   * Get contact id for the customer.
   *
***************
*** 386,394 ****
      return;
    }
  
-   require_once 'CRM/Core/BAO/UFMatch.php';
-   require_once 'api/v2/Contact.php';
- 
    // Order created from CiviCRM
    if (isset($order->data['cid'])) {
      return $order->data['cid'];
--- 398,403 ----
***************
*** 396,426 ****
  
    $mail = _commerce_civicrm_get_mail_from_order($order);
  
!   // Logged in user.
!   if ($order->uid) {
!     global $user;
!     $match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $order->uid,  $mail, 'Drupal', FALSE, 'Individual');
!     if (!is_object($match)) {
!       return FALSE;
!     }
!     return $match->contact_id;
!   }
! 
!   // Anonymous user.
!   // Try to find a match based on email.
!   $contact_search_results = civicrm_api('contact', 'get', array(
!     'email' => $mail,
!     'version' => 3,
!     'return' => array(
!       'contact_id',
!       'contact_source',
!     )
!   ));
!   if (!empty($contact_search_results['is_error'])) {
!     watchdog('commerce_civicrm', 'civicrm_contact_search(): %error', array('%error' => $contact_search_results['error_message']), WATCHDOG_ERROR);
!     return FALSE;
    }
  
    switch (count($contact_search_results['values'])) {
      case 0:
        return 0;
--- 405,442 ----
  
    $mail = _commerce_civicrm_get_mail_from_order($order);
  
!   try {
!     // Logged in user.
!     if ($order->uid) {
!       $params = array(
!         'uf_id' => $order->uid,
!       );
!     $result = civicrm_api3('UFMatch', 'get', $params);
!       if (!empty($results['values'])) {
!          $match = reset($result['values']);
!          return $match['contact_id'];
!       }
!     }
! 
!     // Anonymous user.
!     // Look in the CiviCRM contacts table for a contact that matches the primary email.
!   $params = array(
!       'email' => $order->mail,
!       'return.contact_id' => TRUE
!   );
!     $result = civicrm_api3('Contact', 'get', $params);
!     if (!empty($result['values'])) {
!       $match = reset($result['values']);
!       return $match['contact_id'];
!     }
!   }
!   catch (Exception $e) {
!     watchdog('commerce_civicrm', 'civicrm_contact_search(): %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR);
    }
+   return 0;
  
+ /*
+   // may be needed for results from email
    switch (count($contact_search_results['values'])) {
      case 0:
        return 0;
***************
*** 434,439 ****
--- 450,456 ----
        }
        return 0;
    }
+   */
  }
  
  
***************
*** 443,539 ****
   * @params $order Drupal order object
   */
  function _commerce_civicrm_update_contact($cid, $order) {
!   if (!civicrm_initialize()) {
!     return;
!   }
! 
!   // Needed for civicrm_location_update
!   require_once 'api/v2/Location.php';
  
!   // Ensure we have a contact
!   if (!$cid) {
!     return;
!   }
  
!   // Get the contact object from the contact id
!   $result = civicrm_api('contact', 'get', array('id' => $cid, 'version' => 3, 'sequential' => 0));
!   $contact = $result['values'][$result['id']];
  
!   // Get customer profile information.
!   $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
!   $billing_profile = $order_wrapper->commerce_customer_billing->value();
!   $billing_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $billing_profile);
!   $billing_address = $billing_profile_wrapper->commerce_customer_address->value();
! 
!   // If we have a full name field, split it.
!   if (!empty($billing_address['name_line'])) {
!     $contact['display_name'] = $billing_address['name_line'];
!     // Assumes user only has one first name and one surname... not ideal.
!     $names = preg_split('/\s+/', $billing_address['name_line'], 2);
!     $first_name = $names[0];
!     $last_name = !empty($names[1]) ? $names[1] : '';
!   }
!   // Otherwise just pull out the first and last names.
!   else {
!     $first_name = $billing_address['first_name'];
!     $last_name = $billing_address['last_name'];
!     $contact['display_name'] = $first_name .' '. $last_name;
!   }
! 
!   // Prepare array to update contact via Civi API.
!   $contact['last_name'] = $last_name;
!   $contact['first_name'] = $first_name;
!   $contact['sort_name'] = "{$last_name}, {$first_name}";
!   $contact['display_name'] = $billing_address['first_name'] . ' ' . $billing_address['last_name'];
!   $contact['email'] = _commerce_civicrm_get_mail_from_order($order);
!   if (empty($contact['source'])) {
!     $contact['source'] = t('Drupal Commerce purchase');
!   }
! 
!   // Update the contact object
!   $contact['version'] = 3;
!   $contact['sequential'] = 0;
!   $result = civicrm_api('contact', 'update', $contact);
! 
!   // Billing location.
!   $address = array(
!     1 => array(
!       'location_type'          => 'Billing',
        'is_primary'             => TRUE,
        'city'                   => $billing_address['locality'],
        'state_province'         => $billing_address['administrative_area'],
        'postal_code'            => $billing_address['postal_code'],
        'street_address'         => $billing_address['thoroughfare'],
        'supplemental_address_1' => $billing_address['premise'],
        'country'                => $billing_address['country'],
!     )
!   );
! 
!   // TODO Phone @see hook_commerce_civicrm_params() for example.
! 
!   // Add / update the location.
!   $params = array('version' => '3.0', 'contact_id' => $cid, 'address' => $address);
! 
!   // Allow other modules to alter the data before being sent to CiviCRM.
!   foreach (module_implements('commerce_civicrm_params') as $module) {
!     $function = $module . '_commerce_civicrm_params';
!     $function($params, $order, $cid);
!   }
! 
!   $new_location = civicrm_location_update($params);
!   if ($new_location['is_error'] && strpos($new_location['error_message'], "Invalid Location Type(s)") !== FALSE) {
!     $new_location = civicrm_location_add($params);
!   }
! 
!   // Check if we have shipping information.
!   if ($shipping_profile = $order_wrapper->commerce_customer_shipping->value()) {
!     $shipping_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $shipping_profile);
!     $shipping_address = $shipping_profile_wrapper->commerce_customer_address->value();
  
!     // Shipping location.
!     $address = array(
!       1 => array(
!         'location_type'          => 'Home',
          'is_primary'             => TRUE,
          'city'                   => $shipping_address['locality'],
          'state_province'         => $shipping_address['administrative_area'],
--- 460,609 ----
   * @params $order Drupal order object
   */
  function _commerce_civicrm_update_contact($cid, $order) {
!   try {
!     if (!civicrm_initialize()) {
!       return;
!     }
!   
!     // Ensure we have a contact
!     if (!$cid) {
!       throw(new ErrorException('Contact id required to update contact.'));
!     }
!   
!     // Get the contact object from the contact id
!     $params = array(
!       'id' => $cid,
!     );
!     $result = civicrm_api3('contact', 'get', $params);
!     $contact = $result['values'][$result['id']];
  
!     // Get customer profile information.
!     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
!     $billing_profile = $order_wrapper->commerce_customer_billing->value();
!     $billing_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $billing_profile);
!     $billing_address = $billing_profile_wrapper->commerce_customer_address->value();
  
!     // If we have a full name field, split it.
!     if (!empty($billing_address['name_line'])) {
!       $contact['display_name'] = $billing_address['name_line'];
!       // Assumes user only has one first name and one surname... not ideal.
!       $names = preg_split('/\s+/', $billing_address['name_line'], 2);
!       $first_name = $names[0];
!       $last_name = !empty($names[1]) ? $names[1] : '';
!     }
!     // Otherwise just pull out the first and last names.
!     else {
!       $first_name = $billing_address['first_name'];
!       $last_name = $billing_address['last_name'];
!       $contact['display_name'] = $first_name .' '. $last_name;
!     }
  
!     // Prepare array to update contact via Civi API.
!     $contact['last_name'] = $last_name;
!     $contact['first_name'] = $first_name;
!     $contact['sort_name'] = "{$last_name}, {$first_name}";
!     $contact['display_name'] = $billing_address['first_name'] . ' ' . $billing_address['last_name'];
!     $contact['email'] = _commerce_civicrm_get_mail_from_order($order);
!     if (empty($contact['source'])) {
!       $contact['source'] = t('Drupal Commerce purchase');
!     }
!     
!     // @TODO update civicrm contact's job info if set.
!     // This is creating duplicate employer entries for some reason, disabling for now.
!     //$contact['job_title'] = $billing_profile_wrapper->field_job_title->value();
!     //$contact['current_employer'] = $billing_profile_wrapper->field_organisation->value();
!     
!     // Update the contact object
!     $result = civicrm_api3('contact', 'create', $contact);
! 
!     // Get location type. This could be configurable, but for now we'll
!     // presume 'Billing'.
!     $params = array(
!       'name' => 'Billing',
!     );
!     $result = civicrm_api3('LocationType', 'get', $params);
!     if (!empty($result['values'])) {
!       $location_type = reset($result['values']);
!       $billing_location_type_id = $location_type['id'];
!     }
!     
!     // Get location type. This could be configurable, but for now we'll
!     // presume 'Home'.
!     $params = array(
!       'name' => 'Home',
!     );
!     $result = civicrm_api3('LocationType', 'get', $params);
!     if (!empty($result['values'])) {
!       $location_type = reset($result['values']);
!       $home_location_type_id = $location_type['id'];
!     }
!     
!     // Billing location.
!     $params = array(
!       'contact_id'             => $cid,
!       'location_type_id'       => $billing_location_type_id,
        'is_primary'             => TRUE,
+       'is_billing'             => TRUE,
        'city'                   => $billing_address['locality'],
        'state_province'         => $billing_address['administrative_area'],
        'postal_code'            => $billing_address['postal_code'],
        'street_address'         => $billing_address['thoroughfare'],
        'supplemental_address_1' => $billing_address['premise'],
        'country'                => $billing_address['country'],
!     );
!     // Check to see if a billing address already exists
!     $result = civicrm_api3('Address', 'get', array(
!       'location_type_id' => $billing_location_type_id,
!       'contact_id' => $cid,
!     ));
!     // If so, update it
!     if( !empty($result['values']) ) {
!       $address = reset($result['values']);
!       $address_id = $address['id'];
!       $params['id'] = $address_id;
!     }
!     civicrm_api3('Address', 'create', $params);
!     
!     // Create a phone
!     $params = array(
!       'contact_id' => $cid,
!       'location_type_id' => $billing_location_type_id,
!       'phone' => $billing_profile_wrapper->field_phone->value(),
!       'is_primary' => TRUE,
!       'is_billing' => TRUE,
!       'phone_type_id' => 1,
!     );
!     // Check to see if a phone already exists
!     $result = civicrm_api3('Phone', 'get', array(
!       'location_type_id' => $billing_location_type_id,
!       'contact_id' => $cid,
!     ));
!     // If so, updated it
!     if( !empty($result['values']) ) {
!       $phone = reset($result['values']);
!       $phone_id = $phone['id'];
!       $params['id'] = $phone_id;
!     }
!     civicrm_api3('Phone', 'create', $params);
!     
!     // Then alter $params as required to add any custom commerce fields to send to CiviCRM.
!     
!     // Allow other modules to alter the data before being sent to CiviCRM.
!     foreach (module_implements('commerce_civicrm_params') as $module) {
!       $function = $module . '_commerce_civicrm_params';
!       $function($params, $order, $cid);
!     }
  
!     // Check if we have shipping information.
!     if (!empty($order_wrapper->commerce_customer_shipping)) {
!       $shipping_profile = $order_wrapper->commerce_customer_shipping->value();
!       $shipping_profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $shipping_profile);
!       $shipping_address = $shipping_profile_wrapper->commerce_customer_address->value();
! 
!       // Shipping location.
!       $params = array(
!         'contact_id'             => $cid,
!         'location_type_id'       => $home_location_type_id,
          'is_primary'             => TRUE,
          'city'                   => $shipping_address['locality'],
          'state_province'         => $shipping_address['administrative_area'],
***************
*** 541,560 ****
          'street_address'         => $shipping_address['thoroughfare'],
          'supplemental_address_1' => $shipping_address['premise'],
          'country'                => $shipping_address['country'],
!       )
      );
! 
!     // Add / update the location.
!     $params = array('version' => '3.0', 'contact_id' => $cid, 'address' => $address);
!     $new_location = civicrm_location_update($params);
!     if ($new_location['is_error'] && strpos($new_location['error_message'], "Invalid Location Type(s) : Home") !== FALSE) {
!       $new_location = civicrm_location_add($params);
      }
!   }
! 
!   // Log the error, but continue.
!   if (civicrm_error($new_location)) {
!     watchdog('commerce_civicrm', 'civicrm_location_update(): %error', array('%error' => $new_location['error_message']), WATCHDOG_ERROR);
    }
  }
  
--- 611,682 ----
          'street_address'         => $shipping_address['thoroughfare'],
          'supplemental_address_1' => $shipping_address['premise'],
          'country'                => $shipping_address['country'],
!       );
!       
!       // Check to see if a billing address already exists
!       $result = civicrm_api3('Address', 'get', array(
!         'location_type_id' => $home_location_type_id,
!         'contact_id' => $cid,
!       ));
!       // If so, updated it
!       if( !empty($result['values']) ) {
!         $address = reset($result['values']);
!         $address_id = $address['id'];
!         $params['id'] = $address_id;
!       }
!       civicrm_api3('Address', 'create', $params);
!       
!       // Create a phone
!       $params = array(
!         'contact_id' => $cid,
!         'location_type_id' => $home_location_type_id,
!         'phone' => $shipping_profile_wrapper->field_phone->value(),
!         'is_primary' => TRUE,
!         'phone_type_id' => 1,
!       );
!       // Check to see if a phone already exists
!       $result = civicrm_api3('Phone', 'get', array(
!         'location_type_id' => $home_location_type_id,
!         'contact_id' => $cid,
!       ));
!       // If so, updated it
!       if( !empty($result['values']) ) {
!         $phone = reset($result['values']);
!         $phone_id = $phone['id'];
!         $params['id'] = $phone_id;
!       }
!       civicrm_api3('Phone', 'create', $params);
!     }
!     
!     // @TODO Create new civicrm activity for order
!     // Get Order activity. This could be configurable, but for now we'll
!     // presume 'Online Order Form'.
!     /*$params = array(
!       'name' => 'Online Order Form',
      );
!     $result = civicrm_api3('OptionValue', 'get', $params);
!     if (!empty($result['values'])) {
!       $activity_type = reset($result['values']);
!       $order_form_type_id = $activity_type['value'];
      }
!     
!     // Create new activity.
!     $params = array(
!       'source_contact_id' => $cid,
!       'activity_type_id' => 'order_form_type_id',
!       'subject' => 'test activity type id',
!       'activity_date_time' => '2011-06-02 14:36:13',
!       'status_id' => 2,
!       'priority_id' => 1,
!       'duration' => 120,
!       'location' => 'Pensulvania',
!       'details' => 'a test activity',
!       'custom_1' => 'custom string',
!     );
!     civicrm_api3('Activity', 'create', $params);*/
!     
!   } catch (Exception $e) {
!     watchdog('commerce_civicrm', 'civicrm_location_update(): %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR);
    }
  }
  
***************
*** 568,617 ****
      return;
    }
  
!   require_once 'CRM/Core/Config.php';
!   require_once 'api/v2/CustomGroup.php';
! 
!   // First we need to check if the Sales Tax and Shipping custom fields have
!   // already been created.
!   $params = array(
!     'title'            => 'Drupal Commerce Purchases',
!     'name'             => 'commerce_purchases',
!     'extends'          => array('Contribution'),
!     'weight'           => 1,
!     'collapse_display' => 0,
!     'is_active'        => 1,
!   );
!   $custom_group = civicrm_custom_group_create($params);
!   variable_set('commerce_civicrm_contribution_group_id', $custom_group['id']);
! 
!   $params = array(
!     'custom_group_id' => $custom_group['id'],
!     'label'           => 'Sales Tax',
!     'html_type'       => 'Text',
!     'data_type'       => 'String',
!     'weight'          => 1,
!     'is_required'     => 0,
!     'is_searchable'   => 0,
!     'is_active'       => 1,
!   );
!   $tax_field = civicrm_custom_field_create($params);
!   variable_set('commerce_civicrm_tax_field_id', $tax_field['result']['customFieldId']);
  
!   $params = array(
!     'custom_group_id' => $custom_group['id'],
!     'label'           => 'Shipping Cost',
!     'html_type'       => 'Text',
!     'data_type'       => 'String',
!     'weight'          => 2,
!     'is_required'     => 0,
!     'is_searchable'   => 0,
!     'is_active'       => 1,
!   );
!   $shipping_field = civicrm_custom_field_create($params);
!   variable_set('commerce_civicrm_shipping_field_id', $shipping_field['result']['customFieldId']);
  }
  
- 
  /**
   * Process transactions on an order.
   *
--- 690,749 ----
      return;
    }
  
!   try {
!     // First we need to check if the Sales Tax and Shipping custom fields have
!     // already been created.
!     $params = array(
!       'title'            => 'Drupal Commerce Purchases',
!       'name'             => 'commerce_purchases',
!       'extends'          => array('Contribution'),
!       'weight'           => 1,
!       'collapse_display' => 0,
!       'is_active'        => 1,
!     );
!     $result = civicrm_api3('CustomGroup', 'create', $params);
!     if (!empty($result['values'])) {
!       $custom_group = reset($result['values']);
!       variable_set('commerce_civicrm_contribution_group_id', $custom_group['id']);
! 
!       $params = array(
!         'custom_group_id' => $custom_group['id'],
!         'label'           => 'Sales Tax',
!         'html_type'       => 'Text',
!         'data_type'       => 'String',
!         'weight'          => 1,
!         'is_required'     => 0,
!         'is_searchable'   => 0,
!         'is_active'       => 1,
!       );
!       $result = civicrm_api3('CustomField', 'create', $params);
!       if (!empty($result['values'])) {
!         $tax_field = reset($result['values']);
!         variable_set('commerce_civicrm_tax_field_id', $tax_field['id']);
!       }
  
!       $params = array(
!         'custom_group_id' => $custom_group['id'],
!         'label'           => 'Shipping Cost',
!         'html_type'       => 'Text',
!         'data_type'       => 'String',
!         'weight'          => 2,
!         'is_required'     => 0,
!         'is_searchable'   => 0,
!         'is_active'       => 1,
!       );
!       $result = civicrm_api3('CustomField', 'create', $params);
!       if (!empty($result['values'])) {
!         $shipping_field = reset($result['values']);
!         variable_set('commerce_civicrm_shipping_field_id', $shipping_field['id']);
!       }
!     }
!   }
!   catch (Exception $e) {
!     watchdog('commerce_civicrm', 'civicrm_location_update(): %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR);
!   }
  }
  
  /**
   * Process transactions on an order.
   *
***************
*** 653,694 ****
    // TODO: figure out where to get the shipping total from.
    $shipping_total = 0 * $transaction->amount / $total;
  
!   $params = array(
!     'version' => 3,
!     'contact_id' => $cid,
!     'receive_date' => date('Y-m-d h:i:s'),
!     'total_amount' => $transaction->amount / 100,
!     'financial_type_id' => variable_get('commerce_civicrm_contribution_type', ''), // @FIXME this needs a sensible default
!     'payment_instrument_id' => _commerce_civicrm_map_payment_instrument($transaction->payment_method),
!     'non_deductible_amount' => 00.00,
!     'fee_amount' => 00.00,
!     'net_amount' => $transaction->amount / 100,
!     'trxn_id' => $transaction->transaction_id . '_dc',
!     'invoice_id' => $transaction->transaction_id . '_dc',
!     'source' => variable_get('commerce_civicrm_source_name', 'Drupal Commerce'),
!     'contribution_status_id' => _commerce_civicrm_map_contribution_status($transaction->status),
!     'note' => _commerce_civicrm_create_detail_string($order_wrapper),
!   );
! 
!   if (!empty($tax_field_id)) {
!     $params['custom_' . $tax_field_id] = $transaction_tax;
!   }
! 
!   if (!empty($shipping_field_id)) {
!     $params['custom_' . $shipping_field_id] = $shipping_total;
!   }
! 
!   // Allow other modules to alter the contribution data before being sent to CiviCRM.
!   foreach (module_implements('commerce_civicrm_contribution_params') as $module) {
!     $function = $module . '_commerce_civicrm_contribution_params';
!     $function($params, $order, $cid, $transaction);
    }
! 
!   $contribution = civicrm_api('contribution', 'create', $params);
! 
!   // Log any error.
!   if (civicrm_error($contribution)) {
!     watchdog('commerce_civicrm', 'civicrm_contribution_add(): %error', array('%error' => $contribution['error_message']), WATCHDOG_ERROR);
    }
  }
  
--- 785,826 ----
    // TODO: figure out where to get the shipping total from.
    $shipping_total = 0 * $transaction->amount / $total;
  
!   try {
!     $params = array(
!       'contact_id' => $cid,
!       'receive_date' => date('Y-m-d h:i:s'),
!       'total_amount' => $transaction->amount / 100,
!       'financial_type_id' => variable_get('commerce_civicrm_contribution_type', ''), // @FIXME this needs a sensible default
!       'payment_instrument_id' => _commerce_civicrm_map_payment_instrument($transaction->payment_method),
!       'non_deductible_amount' => 00.00,
!       'fee_amount' => 00.00,
!       'net_amount' => $transaction->amount / 100,
!       'trxn_id' => $transaction->transaction_id . '_dc',
!       'invoice_id' => $transaction->transaction_id . '_dc',
!       'source' => variable_get('commerce_civicrm_source_name', 'Drupal Commerce'),
!       'contribution_status_id' => _commerce_civicrm_map_contribution_status($transaction->status),
!       'note' => _commerce_civicrm_create_detail_string($order_wrapper),
!     );
!   
!     if (!empty($tax_field_id)) {
!       $params['custom_' . $tax_field_id] = $transaction_tax;
!     }
!   
!     if (!empty($shipping_field_id)) {
!       $params['custom_' . $shipping_field_id] = $shipping_total;
!     }
!   
!     // Allow other modules to alter the contribution data before being sent to CiviCRM.
!     foreach (module_implements('commerce_civicrm_contribution_params') as $module) {
!       $function = $module . '_commerce_civicrm_contribution_params';
!       $function($params, $order, $cid, $transaction);
!     }
!   
!     $contribution = civicrm_api3('Contribution', 'create', $params);
!   
    }
!   catch (Exception $e) {
!     watchdog('commerce_civicrm', 'CiviCRM API error: %error', array('%error' => $e->getMessage()), WATCHDOG_ERROR);
    }
  }
  
