type:
integration
system:
SalesForce
name:
Create Customer in Salesforce
description:
Adds a generic lead to SalesForce using the customer information from the FoxyCart order.
tag:
crm

Salesforce + FoxyCart

Please note: The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Description

This is a modification of campaign_monitor for use with http://salesforce.com. It adds a generic lead to SalesForce using data from a FoxyCart order. It uses fopen as opposed to curl because curl does not seem to like more than four characters after a period in the file name.

FoxyCart + SalesForce

<?php
/**
 * RC4Crypt 3.2
 *
 * RC4Crypt is a petite library that allows you to use RC4
 * encryption easily in PHP. It's OO and can produce outputs
 * in binary and hex.
 *
 * (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]
 *     All Rights Reserved
 *
 * @link http://rc4crypt.devhome.org
 * @author Mukul Sabharwal <mjsabby@gmail.com>
 * @version $Id: class.rc4crypt.php,v 3.2 2006/03/10 05:47:24 mukul Exp $
 * @copyright Copyright &copy; 2006 Mukul Sabharwal
 * @license http://www.gnu.org/copyleft/gpl.html
 * @package RC4Crypt
 */
 
/**
 * RC4 Class
 * @package RC4Crypt
 */
class rc4crypt {
	/**
	 * The symmetric encryption function
	 *
	 * @param string $pwd Key to encrypt with (can be binary of hex)
	 * @param string $data Content to be encrypted
	 * @param bool $ispwdHex Key passed is in hexadecimal or not
	 * @access public
	 * @return string
	 */
	function encrypt ($pwd, $data, $ispwdHex = 0)
	{
		if ($ispwdHex)
			$pwd = @pack('H*', $pwd); // valid input, please!
 
		$key[] = '';
		$box[] = '';
		$cipher = '';
 
		$pwd_length = strlen($pwd);
		$data_length = strlen($data);
 
		for ($i = 0; $i < 256; $i++)
		{
			$key[$i] = ord($pwd[$i % $pwd_length]);
			$box[$i] = $i;
		}
		for ($j = $i = 0; $i < 256; $i++)
		{
			$j = ($j + $box[$i] + $key[$i]) % 256;
			$tmp = $box[$i];
			$box[$i] = $box[$j];
			$box[$j] = $tmp;
		}
		for ($a = $j = $i = 0; $i < $data_length; $i++)
		{
			$a = ($a + 1) % 256;
			$j = ($j + $box[$a]) % 256;
			$tmp = $box[$a];
			$box[$a] = $box[$j];
			$box[$j] = $tmp;
			$k = $box[(($box[$a] + $box[$j]) % 256)];
			$cipher .= chr(ord($data[$i]) ^ $k);
		}
		return $cipher;
	}
	/**
	 * Decryption, recall encryption
	 *
	 * @param string $pwd Key to decrypt with (can be binary of hex)
	 * @param string $data Content to be decrypted
	 * @param bool $ispwdHex Key passed is in hexadecimal or not
	 * @access public
	 * @return string
	 */
	function decrypt ($pwd, $data, $ispwdHex = 0)
	{
		return rc4crypt::encrypt($pwd, $data, $ispwdHex);
	}
}
 
// Foxy Stuff
$DataFeedKey = 'XXXXXXX'; // your foxy cart datafeed key
//SalesForce Stuff
$sfga = 'XXXXXXX'; //SalesForce Google Ads key
$oid = 'XXXXXXXX'; //SalesForce Key
$lead_source = 'Web Order'; // lead source e.g. Web Info Request
$custom = ''; //any custom options/inputs
//$inputs = array('customer_first_name','customer_last_name','customer_email','customer_address1','customeraddress2'); -- possible automation of setting data for future
 
 
if (isset($_POST["FoxyData"])) {
	$FoxyData_encrypted = urldecode($_POST["FoxyData"]);
	$FoxyData_decrypted = rc4crypt::decrypt($DataFeedKey,$FoxyData_encrypted);
	$FoxyDataArray = new SimpleXMLElement($FoxyData_decrypted);
	foreach($FoxyDataArray->transactions->transaction AS $transaction) {
		$company_name = "";
		$shipping_address = "";
		$customer_first_name = $transaction->customer_first_name;
		$customer_last_name = $transaction->customer_last_name;
		$customer_email = $transaction->customer_email;
 
		//billing address
		$customer_address1 = $transaction->customer_address1;
		$customer_address2 = $transaction->customer_address2;
		$customer_city = $transaction->customer_city;
		$customer_state = $transaction->customer_state;
		$customer_postal_code = $transaction->customer_postal_code;
		$customer_country = $transaction->customer_country;
		$customer_phone = $transaction->customer_phone;
		if(!empty($customer_address2)){
			$customer_address1 .= " " . $customer_address2; //%0D%0A
		}
		$billing_address = "&street=" .$customer_address1 .  "&city=" . $customer_city . "&state=" . $customer_state . "&zip=" . $customer_postal_code . "&country=" . $customer_country;
 
		//shipping address
		$shipping_address1 = $transaction->shipping_address1;
		$shipping_address2 = $transaction->shipping_address2;
		$shipping_city = $transaction->shipping_city;
		$shipping_state = $transaction->shipping_state;
		$shipping_postal_code = $transaction->shipping_postal_code;
		$shipping_country = $transaction->shipping_country;
		$shipping_phone = $transaction->shipping_phone;
		if(!empty($shipping_address2)){
			$shipping_address1 .= "%0D%0A" . $shipping_address2;
		}
		if(!empty($shipping_address1)){
			$shipping_address = $shipping_address1 .  "%0D%0A" . $shipping_city . ", " . $shipping_state . " " . $shipping_postal_code . "%0D%0A" . $shipping_country . "%0D%0A" . $shipping_phone;
		}
		// got some custom stuff in the details or the options? - use this
 
		foreach($transaction->custom_fields->custom_field AS $custom_field) {
 			if($custom_field->custom_field_name == "company_name"){
 				$company_name = $custom_field->custom_field_value;
 			}
		}
 
		/**/
		// ok, now we have our data, let's shake hands with the nice folks at SALES FORCE WOO PARTY ALL THE TIME!
 
		//form the url
		$url = "http://www.salesforce.com/servlet/servlet.WebToLead?"
			. "encoding=UTF-8" . "&sfga=" . $sfga  . "&lead_source=" . $lead_source . "&oid=" . $oid
			. "&email=" . $customer_email . "&first_name=" . $customer_first_name . "&last_name=" . $customer_last_name . "&company=" . $company_name
			. $billing_address . "&phone=" . $customer_phone . "&description=" . $shipping_address . $custom;
 
		$url = str_replace(array(' ','\n','\r'),'+',$url);
		$fp = fopen($url,"r");
	}
	if (!$fp) {
		// Something didnt work right, return an error
		// This ensures that the transactions will still show up on the next datafeed.
		print "error";
	} else {
		// we're good. Mark it fed.
		print "foxy";
		fclose($fp);
	}
} else {
	// Something didnt work right, return an error
	// This ensures that the transactions will still show up on the next datafeed.
	print "error";
}
?>

Site Tools