type:
integration
system:
Campaign Monitor,
MailBuild
name:
Add customers to mailing list
description:
Automatically add your customers to your Campaign Monitor or MailBuild lists
tags:
mailing list,
emarketing
date:
2007-08-21
developer:
http://www.themancan.com/

Campaign Monitor (and MailBuild) Integration

We at FoxyCart really like Campaign Monitor. It's built for web-designers and developers, just like FoxyCart, and it's very easy to setup, use, and love.

Campaign Monitor recently updated their API examples, so check 'em out here: http://www.campaignmonitor.com/api/samples/default.aspx#php

The following is a basic script to automatically add your FoxyCart customers to your Campaign Monitor list. Here's the basic instructions:

  1. Set up or login to Campaign Monitor. If you're just setting it up, follow their instructions to set up a list and etc.
  2. Hit your “Account Settings” up in the top left.
    1. Grab the “Your API Key” value.
    2. Paste it into a copy of the script below in the appropriate location.
  3. Hit the “Manage Subscribers” tab.
    1. Click on the list you want to add your FoxyCart customers to.
    2. Find the “API List ID” code, copy it, and paste it into your copy of the code below.
  4. Replace the string of XXXXXX's in your copy of the code below with yoru FoxyCart datafeed key (as set in the FoxyCart Admin).
  5. Save your copy of the code below to your PHP capable webserver.
  6. Point FoxyCart's XML datafeed to the location of this script on your server.
  7. TEST! Then test it again. If you have issues, let us know in the forum.

The Code

Copy this code and modify it according to the directions above.

<?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 = 'this is your foxy cart data feed key'; // your foxy cart datafeed key
// Campaign Monitor Stuff
$APIKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //your apikey
$ListID = "XXXXXXX";	//your list ID
 
// Declare a class for a custom field. It must be called this to allow proper mapping when the service is called.
class SubscriberCustomField
{
     public $Key;
     public $Value;
}
 
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) {
		$customer_name = "";
		// got any custom stuff? - use this
		// $my_custom_code = "";
		$resultCode = "";
		$resultMessage = "";
		$customer_name .= $transaction->customer_first_name;
		$customer_name .= " " . $transaction->customer_last_name;
		$customer_email = $transaction->customer_email;
		$customer_id = $transaction->customer_id;
		// got some custom stuff in the details or the options? - use this
		/*
		foreach($transaction->transaction_details->transaction_detail AS $transaction_detail) {
			foreach($transaction_detail->transaction_detail_options->transaction_detail_option AS $transaction_detail_option) {
				if ($transaction_detail_option->product_option_name == "my_custom_code") {
					$my_custom_code = $transaction_detail_option->product_option_value;
				}
			}
		}
		*/
		// ok, now we have our data, let's shake hands with the nice folks at campaign monitor
		try {
 
			$client = new SoapClient("http://app.campaignmonitor.com/api/api.asmx?wsdl", array('trace' => 1));
 
			// Set the basic API request information.
			$params->ApiKey = $APIKey;
			$params->ListID = $ListID;
			$params->Email = $customer_email;
			$params->Name = $customer_name;
 
			// Adding a standard text type custom field. This also how you would add a number, or single select multi-option field.
			// For the single select, make sure the value you set in the form has been added in Campaign Monitor.
 
			$customeridClass = new SubscriberCustomField();
			$customeridClass->Key = '[customer_id]';
			$customeridClass->Value = $customer_id;
 
			$customfields = array($customeridClass);
 
			// Assign our custom field array to our parameter list
			$params->CustomFields = $customfields;
 
			// Make the call
			$result = get_object_vars($client->AddSubscriberWithCustomFields($params));		
 
			$resultCode = current($result)->Code;
			$resultMessage = current($result)->Message;
 
			// If not successful
			if ($resultCode > 0) {
				$isError = true;
			}
 
			// The following code produces the entire service request and response. It may be useful for debugging.
			/*
			print "<pre>\n";
	  		print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
	  		print "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
	  		print "</pre>";
	  		*/
 
		} catch (SoapFault $exception) {
			$isError = true;
		}
 
	}
	if ($isError) {
		// 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";
	}
} else {
	// Something didnt work right, return an error
	// This ensures that the transactions will still show up on the next datafeed. 
	print "error";
}
?>