[Drupal] How can we integrate Drupal with infusionsoft

| | 3 min read

Infusionsoft is a all-in-one sales and marketing software.One of our client requirement is to integrate Drupal with infusionsoft using infusionsoft API. Steps to implement this as follows

  1. To integrate infusionsoft, we need an application name and apikey.

    Application name - Your Infusionsoft application name: xxx in http://xxx.infusionsoft.com

    Apikey - You can generate your Apikey
    Goto Infusionsoft => Admin => Settings => Click on Application in the settings menu => Then go to the API section and enter an API Passphrase(optional). Also you can enter list of allowed IPaddresses. Then save to generate APIkey.
  2. In Drupal we can implement this via creating a custom module,
    1. In custom module , create a custom menu for infusionsoft admin settings form with option to enter Application name and Apikey.
      
          function infusionsoft_menu() {
              $items['admin/settings/infusionsoft'] = array(
                   'title' => 'Infusionsoft',
                   'description' => 'Adjust infusionsoft settings.',
                   'page callback' => 'drupal_get_form',
                   'page arguments' => array('infusionsoft_admin_settings'),
                   'access arguments' => array('administer infusionsoft'),
                   'file' => 'includes/infusionsoft.admin.inc',
             );
             return $items;
          }
          //In includes/infusionsoft.admin.inc file
          function infusionsoft_admin_settings() {
              $form = array();
              $form['Infusionsoft settings'] = array(
                 '#type' => 'fieldset',
                 '#title' => t('Infusionsoft settings'),
                 '#collapsible' => TRUE,
                 '#collapsed' => FALSE,
               );
               $form['Infusionsoft settings']['infusionsoft_appname'] = array(
                 '#type' => 'textfield',
                 '#title' => t('Application Name'),
                 '#size' => 70,
                 '#default_value' => variable_get('infusionsoft_appname', ''),
                 '#description' => t('Your Infusionsoft application name: xxx in http://xxx.infusionsoft.com'),
                 '#required' => TRUE,
               );
               $form['Infusionsoft settings']['infusionsoft_apikey'] = array(
                 '#type' => 'textfield',
                 '#title' => t('API Key'),
                 '#size' => 70,
                 '#default_value' => variable_get('infusionsoft_apikey', ''),
                 '#description' => t('Copy and paste your Infusionsoft API key .'),
                 '#required' => TRUE,
               );
               
               $form['Infusionsoft settings']['custom_field'] = array(
                '#type' => 'radios',
                '#title' => t('Is Sync Field a custom field?'),
                '#maxlength' => 1,
                '#description' => t('If the Sync field used is a custom field , then click yes.'),
                '#default_value' => variable_get('custom_field', ''),
                '#options' => array(1 => t('Yes'), 0 => t('No'),),
                '#required' => TRUE,
               ); 
               
               $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
               $form['#validate'][] = 'infusionsoft_admin_settings_validate';
               return system_settings_form($form);
          }
          
    2. Then create a function to call a service from infusionsoft. All services are documented in the link http://help.infusionsoft.com/developers/services-methods.
      For example if we want to add users to infusionsoft whenever new user created in Drupal, call a function in login form_submit_handler
      
          function infusionsoft_add_new_contact($form, &$form_state) {
           	$contact_data = array(
      		                    "FirstName" => $form_state['values']['field_name_first']['und'][0] ['value'],
      		                    "LastName" => $form_state['values']['field_name_last']['und'][0] ['value'],
      		                    "Email" => $form_state['values']['mail'],
      		                    "Username" => $form_state['values']['mail'],
      		                    "Password" => $form_state['values']['pass'],
      	                    );
      
             $result = infusionsoft_update_contact($contact_data);
          }
          function infusionsoft_update_contact($contact_data, $contact_id = NULL, $options = array()) {
      
           // Remove empty contact data from update array.
           foreach ($contact_data as $key => $value) {
             if (empty($value)) {
               unset($contact_data[$key]);
             }
           }
      
           // Return if there is no contact data.
           if (empty($contact_data)) {
             return FALSE;
           }
      
           $defaults = array(
             'opt_in' => TRUE,
             'opt_reason' => 'Drupal Auto Opt In',
             'no_email_update' => FALSE,
             'dup_check_type' => 'Email',
           );
      
           $options = array_merge($defaults, $options);
      
           // If we have a new user and we have an email address, or no email update
           // is selected, create a new user with the supplied info.
           if (!empty($contact_data['Email']) || $options['no_email_update'] == TRUE) {
             // There is no email in infusionsoft, we have a new user!
      		   $contact_id = infusionsoft_send_request('ContactService.addWithDupCheck', $contact_data, $options['dup_check_type']);
      		   
      		   return $contact_id;
      	   }
      
           // There was not enough data to create a new user.
           return FALSE;
         }
         
    3. Then create another function to send request to infusionsoft. See the function modulename_send_request() given below. In this we are using built in drupal version of XML-RPC to send request to infusionsoft.
      
       function modulename_send_request($request_type = 'ContactService.load') {
        $values = func_get_args();
        $app_name = variable_get('infusionsoft_appname', '');
        $server = "https://$app_name.infusionsoft.com/api/xmlrpc";
        $api_key = variable_get('infusionsoft_apikey', '');
      
        // Return early if no API key has been set.
        if (empty($api_key)) {
          return;
        }
        $i = 0;
        foreach ($values as $key => $value) {
         if($i == 0) {
          $args[$values[0]][$i] = $api_key;
         } 
         else {
          $args[$values[0]][$i] = $value;
         } 
         $i++;
        }
        
        // Remove the $request_type variable from the values array.
        array_shift($values);
      
        $result = xmlrpc($server, $args, $options = array() );
       
        // XML-RPC error handler
        
        if ($error = xmlrpc_error()) { 
          if ($error->code <= 0) {
            // Error code of 0 does not necessarily mean that a connection could not be reached.
            // Could also mean a misspelled service or method call.
            //if (!_infusionsoft_test_connection()) {
              $error->message .= t('TCP/IP Socket could not be opened. Your connection to the infusionsoft server may be down.');
            //}
          }
          $message = sprintf(t('XML-RPC error: %s (%d).'), $error->message, $error->code);
          watchdog('infusionsoft-error', 'Error Message: ' . $error->message . '
      Error Code: ' . $error->code); return FALSE; } else { return $result; } }