I want to have a subscription page using infusionsoft and post it to my drupal site. After the subscription application is successfully complete, I want to add the user on my drupal site. Have any idea on how to work this up??

Comments

julie.metivier’s picture

Hello,

I'm looking for something similar with Drupal 6:
1. Create a Web Form using Infusionsoft
2. Customize the user register form by adding these new fields
3. Send the values to infusionsoft using their library isdk.php and xmlrpc2.0
4. Create the new user

Here is a first step:
http://drupal.org/node/547796

But I need some more help to call these functions when the form is submitted.

Do we have to insert these functions in the template.php?
How to customize the submit button to send the values?

Thank you

julie.metivier’s picture

I've done this...


/**
 * Implementation of HOOK_theme().
 */
function estc_theme(&$existing, $type, $theme, $path) {
  $hooks = zen_theme($existing, $type, $theme, $path);

  // Add your theme hooks like this:
  $hooks['user_register'] = array(
	 'template' => 'user_register',
	 'arguments' => array('form' => NULL),
	);

  // @TODO: Needs detailed comments. Patches welcome!
  return $hooks;
}

function estc_preprocess_user_register(&$variables) {
  $variables['intro_text'] = t('This is my awesome register form');
  $variables['form']['#submit'][] = 'custom_register_submit';
  $variables['rendered'] = drupal_render($variables['form']);
}
function custom_register_submit(&$variables) {
  $conDat = array('FirstName' => '123123123',
                    'LastName'  => '123123123',
                    'Email'     => '123123123');
  return infusionsoft_send_request('addCon', $conDat);
}
/**
* Primary callback function to send an API request to infusionsoft.
* $request_type - the API function to call
*/
function infusionsoft_send_request($request_type) {
  $values = func_get_args();
  //Remove the $request_type variable
  array_shift($values);
 
  $vals = array();
  //Put the values in the correct form. Each entry in $vals looks like $values[0], $values[1], etc
  foreach ($values as $key => $value) $vals[] = '$values['. $key .']';
  //Create the function call.
  $function_call = 'return $myApp->'. $request_type .'('. implode(', ', $vals) .');';
 
  //Don't alert users of issues with infusionsoft
  ob_start();
  $myApp = infusionsoft_load_myApp();
  if ($return = $myApp->cfgCon("demo")) {
    $return = eval($function_call);
  }
 
  if (!$return && ($error = ob_get_contents())) {
    watchdog('infusionsoft', 'Unable to send request of type !type(!error).', array('!type' => $request_type, '!error' => $error));
  }
  ob_end_clean();
 
  return $return;
}

/**
* Function to load the sdk so as to not create a million instances.
*/
function infusionsoft_load_myApp($new = 0) {
  static $myApp = NULL;
  if ($new || !$myApp) {
    require_once('sites/all/libraries/infusionsoft/isdk.php');
    $myApp = new iSDK;
  }
  return $myApp;
}

But the function custom_register_submit is never called by submiting the form!

Is there someone to help?
Thank you

julie.metivier’s picture

Hope this will help someone :)

I've created my own module (mymodule) in sites/all/module
In this folder I've created mymodule.module and mymodule.info

// $Id: mymodule.info,v 0.1 2010/04/12 09:06:06 julie.metivier Exp $

/**
*  Implementation of hook_form_alter().
*/
function custom_host_form_alter(&$form, $form_state, $form_id) {
	global $user;
  switch($form_id) {
		// Edit my Profile Page
		case 'user_profile_form':
			$form['#submit'][] = 'custom_host_update_submit';
		break;
		
		// Sign Up form modifications
  	case 'user_register':
			$form['#submit'][] = 'custom_host_submit';
  	break;
  }
}

/**
* Customized submit function to create a new host in Infusionsoft
*/
function custom_host_submit($form, &$form_state) {
	$returninfo = array();
	
	$conDat = array('FirstName' => $form_state['values']['profile_firstname'],
									'LastName'  => $form_state['values']['profile_lastname'],
									'Email'     => $form_state['values']['mail']);

	// Add a new user or update the user data if the user has got already an infusionid
	if (isset($form_state['values']['profile_infusionid'])){
		$infusionID = (int)$form_state['values']['profile_infusionid'];
		$returninfo = infusionsoft_send_request('updateCon', $infusionID, $conDat);
	} else {
		$infusionID = infusionsoft_send_request('addCon', $conDat);
	}

	// register infusionid in a Drupal field
	$success = db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES ('11', %d, '%s')", $form_state['user']->uid, $infusionID);
	if (!$success) {
		// The query failed - better to abort the save than risk further data loss.
		drupal_set_message('error: not registered');
	}
}

/**
* Customized submit function to update an host in Infusionsoft
*/
function custom_host_update_submit($form, &$form_state) {
  // Update Email
	if (isset($form_state['values']['mail'])) {
		$hostid = $form['_account']['#value']->uid;
		$result = db_query("SELECT v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE f.fid = '11' AND uid = %d", $hostid);
    $infusionid = db_fetch_array($result);
		
		if (!$infusionid) {
      // The query failed - better to abort the save than risk further data loss.
      drupal_set_message('error: not registered');
    } else { // change infusionsoft record if infusionid is not empty
			//remove all tags before assigning a new one
			$conDat = array('Email'     => $form_state['values']['mail']);
			$returninfo = infusionsoft_send_request('updateCon', (int)$infusionid['value'], $conDat);
		}
  }
	
	// Update Firstname and Lastname
	else if (isset($form_state['values']['profile_firstname'])) {
		$hostid = $form['_account']['#value']->uid;
		$result = db_query("SELECT v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE f.fid = '11' AND uid = %d", $hostid);
    $infusionid = db_fetch_array($result);
		if (!$infusionid) {
      // The query failed - better to abort the save than risk further data loss.
      drupal_set_message('error: not registered');
    } else {
			$conDat = array('FirstName' => $form_state['values']['profile_firstname'],
							'LastName'  => $form_state['values']['profile_lastname']);
			$returninfo = infusionsoft_send_request('updateCon', (int)$infusionid['value'], $conDat);
		}
	}
}

/**
* Primary callback function to send an API request to infusionsoft.
* $request_type - the API function to call
*/
function infusionsoft_send_request($request_type) {
  $values = func_get_args();
  //Remove the $request_type variable
  array_shift($values);
 
  $vals = array();
  //Put the values in the correct form. Each entry in $vals looks like $values[0], $values[1], etc
  foreach ($values as $key => $value) $vals[] = '$values['. $key .']';
  //Create the function call.
  $function_call = 'return $myApp->'. $request_type .'('. implode(', ', $vals) .');';
 
  //Don't alert users of issues with infusionsoft
  ob_start();
  $myApp = infusionsoft_load_myApp();
  if ($return = $myApp->cfgCon("demo")) {
    $return = eval($function_call);
  }
 
  if (!$return && ($error = ob_get_contents())) {
    watchdog('infusionsoft', 'Unable to send request of type !type(!error).', array('!type' => $request_type, '!error' => $error));
  }
  ob_end_clean();
 
  return $return;
}

/**
* Function to load the sdk so as to not create a million instances.
*/
function infusionsoft_load_myApp($new = 0) {
  static $myApp = NULL;
  if ($new || !$myApp) {
    require_once('sites/all/libraries/infusionsoft/isdk.php');
    $myApp = new iSDK;
  }
  return $myApp;
}
; $Id: mymodule.info,v 0.1 2010/04/12 09:06:06 julie.metivier Exp $
name = My Module
description = Changing user forms to register new users in Infusionsoft
core = 6.x

I've done some more development for adding the location details (using the module location) and the user role (using the tags in Infusionsoft to simulate the role).

If someone needs that too, I could make an update.

Enjoy

corypark’s picture

I am just getting started on Infusionsoft integration...
Any other code you can share would be helpful :) thanks!