This is an old revision of the document!


type:
integration
supports-foxycart-version-to:
0.7.2
system:
SmartTurn
name:
SmartTurn
description:
Set the FoxyCart datafeed to pass your data to SmartTurn's fulfillment system
tag:
fulfillment, datafeed
date:
2012-03-23
version:
1.0
developer:
http://www.sparkweb.net/

smartturn

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.

SmartTurn Fulfillment is now included natively in the FoxyTools Order Desk integration.

Description

Hook the FoxyCart datafeed up to SmartTurn. This solution works with single-ship stores only. (Multi-ship code might be available - please contact developer for more details.)

Requirements

  • This has been tested with 0.7.1 and 0.7.2 but should work fine back to 0.7.0 as well.

Code

<?php
/*
Written by David Hollander (david@sparkweb.net)
This script is designed for a single-ship store. Please contact me if you need multi-ship support.
2012-03-24
v1.0, For FoxyCart 0.7.2
*/
 
 
//Setup
$api_key = "YOUR_FOXYCART_API_KEY_HERE";
$smartturn_login = "YOUR_LOGIN";
$smartturn_password  = "YOUR_PASSWORD";
$external_no_prefix = "W";		//required so order number are unique
$owner_code = "YOUR_OWNER_CODE";	// Must be Owner Code as assigned in SmartTurn
$wsdl = "https://services.smartturn.com/occam/services/OccamService?wsdl";
date_default_timezone_set('America/Los_Angeles');
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
 
 
//Get Transaction Data Post From FoxyCart
if (isset($_POST["FoxyData"])) {
    	$FoxyData_encrypted = urldecode($_POST["FoxyData"]);
	$FoxyData_decrypted = rc4crypt::decrypt($api_key,$FoxyData_encrypted);
 
//Nothing to see here... move along
} else {
	die('No Content Received');
}
 
//Import FoxyData Response and Parse with SimpleXML
$xml = simplexml_load_string($FoxyData_decrypted, NULL, LIBXML_NOCDATA);
 
//For Each Transaction
foreach($xml->transactions->transaction as $transaction) {
 
	//Smart Turn
	$smart_turn_item_array = array();
 
	//Get FoxyCart Customer Information
	$customer_id = (string)$transaction->customer_id;
	$customer_first_name = (string)$transaction->customer_first_name;
	$customer_last_name = (string)$transaction->customer_last_name;
	$customer_email = (string)$transaction->customer_email;
	$customer_password = (string)$transaction->customer_password;
 
	//For Each Transaction Detail
	foreach($transaction->transaction_details->transaction_detail as $transactiondetails) {
		$product_name = (string)$transactiondetails->product_name;
		$product_code = (string)$transactiondetails->product_code;
		$product_quantity = (int)$transactiondetails->product_quantity;
		$product_discount = 0;
		$shipto = 'DEFAULT';
		if ((string)$transactiondetails->shipto != "") $shipto = (string)$transactiondetails->shipto;
		foreach($transactiondetails->transaction_detail_options->transaction_detail_option as $transaction_detail_option) {
			$product_discount += (double)$transaction_detail_option->price_mod;
		}
 
		//Smart Turn Product
		$smart_turn_item_array[$shipto][] = array(
			"itemMasterId" => $product_code
			,"description" => $product_name
			,"details" => ''
			,"orderedQuantity" => array(
				"value" => $product_quantity
				,"unitAbbreviation" => 'ea')
			,"customerRequestDate" => date('c', strtotime((string)$transaction->transaction_date))
			,"price" => array(
				"value" => (double)$transactiondetails->product_price + $product_discount
				,"type" => '$')
			);
	}
 
 
	//Submit SmartTurn
	$address_id = "DEFAULT";
 
	$client = new SoapClient($wsdl, array('trace' => true, 'exceptions' => true));
	$custom_fields_array = array(
		array("name" => 'Email', "textValue" => $customer_email),
		array("name" => 'NEW Method of Shipment', "textValue" => (string)$transaction->shipto_shipping_service_description),
		array("name" => 'Sales Terms', "textValue" => 'Credit Card'),
		array("name" => 'Order Code', "textValue" => 'Web Store')
	);
 
	$shipping_first_name = ((string)$transaction->shipping_first_name ? (string)$transaction->shipping_first_name : (string)$transaction->customer_first_name);
	$shipping_last_name = ((string)$transaction->shipping_last_name ? (string)$transaction->shipping_last_name : (string)$transaction->customer_last_name);
	$shipping_company = ((string)$transaction->shipping_company ? (string)$transaction->shipping_company : (string)$transaction->customer_company);
	$shipping_address1 = ((string)$transaction->shipping_address1 ? (string)$transaction->shipping_address1 : (string)$transaction->customer_address1);
	$shipping_address2 = ((string)$transaction->shipping_address2 ? (string)$transaction->shipping_address2 : (string)$transaction->customer_address2);
	$shipping_city = ((string)$transaction->shipping_city ? (string)$transaction->shipping_city : (string)$transaction->customer_city);
	$shipping_state = ((string)$transaction->shipping_state ? (string)$transaction->shipping_state : (string)$transaction->customer_state);
	$shipping_postal_code = ((string)$transaction->shipping_postal_code ? (string)$transaction->shipping_postal_code : (string)$transaction->customer_postal_code);
	$shipping_country = ((string)$transaction->shipping_country ? (string)$transaction->shipping_country : (string)$transaction->customer_country);
	$shipping_phone = ((string)$transaction->shipping_phone ? (string)$transaction->shipping_phone : (string)$transaction->customer_phone);
 
	$customer_name = normalize((string)$transaction->customer_first_name . ' ' . (string)$transaction->customer_last_name);
	$shipping_name = normalize($shipping_first_name . ' ' . $shipping_last_name);
 
	$order_array[] = array(
		"externalNumber" => $external_no_prefix . (string)$transaction->id . '-' . $address_id
		,"type" => 'EXTERNAL'
		,"date" => date('c', strtotime((string)$transaction->transaction_date))
		,"dateDue" => date('c', strtotime((string)$transaction->transaction_date))
		,"ownerCode" => $owner_code
		,"priority" => (string)$transaction->shipto_shipping_service_description
		,"salesRep" => 'WEB'
		,"customerName" => $customer_first_name . " " . $customer_last_name
		,"customerContact" => $customer_name
		,"customerContactPhone" => (string)$transaction->customer_phone
		,"customerAddress" => array(
			"addressLine1" => normalize(substr((string)$transaction->customer_address1, 0, 50))
			,"addressLine2" => normalize(substr((string)$transaction->customer_address2, 0, 50))
			,"city" => normalize((string)$transaction->customer_city)
			,"state" => normalize((string)$transaction->customer_state)
			,"country" => normalize((string)$transaction->customer_country)
			,"postalCode" => normalize((string)$transaction->customer_postal_code)
		)
		,"ShipTo" => array(
			"addressLine1" => normalize(substr($shipping_address1, 0, 50))
			,"addressLine2" => normalize(substr($shipping_address2, 0, 50))
			,"city" => normalize($shipping_city)
			,"state" => normalize($shipping_state)
			,"country" => normalize($shipping_country)
			,"postalCode" => normalize($shipping_postal_code)
		)
		,"shipToName" => $shipping_company ? $shipping_company : $shipping_name
		,"shipToContact" => $shipping_name
		,"shipToContactPhone" => $shipping_phone
		,"comments" => (string)$transaction->shipping_shipping_service_description
		,"useShipToAsBillAddress" =>FALSE
		,"allowPartialShipment" =>TRUE
		,"status" =>'NEW'
		,"soCustomFields" => $custom_fields_array
		,"item" => $smart_turn_item_array['primary']
	);
 
	//Transmit Order to SmartTurn
	$save_salesorder = $client->saveSalesOrder(array(
		"inCredential" => array("UserId" => $smartturn_login, "Password" => $smartturn_password),
		"inSalesOrders"=>$order_array)
	);
 
	if ($save_salesorder) {
		if ($save_salesorder->uploadResponse->status == 'SUCCESS') {
			//print "Order " . $save_salesorder->uploadResponse->externalRefId . " successfully saved as " . $save_salesorder->uploadResponse->systemId . "\n";
		} else {
			//print_r($response_each);
			print "ERROR uploading order " . $save_salesorder->uploadResponse->externalRefId . "\n";
			print "...error returned was: " . $save_salesorder->uploadResponse->error . "\n";
		}
	}
 
 
}
 
 
//Done
die("foxy");
 
 
 
 
 
 
 
function normalize ($string) {
    $table = array(
        '?'=>'S', '?'=>'s', '?'=>'Dj', '?'=>'dj', '?'=>'Z', '?'=>'z', '?'=>'C', '?'=>'c', '?'=>'C', '?'=>'c',
        'Ë'=>'A', 'ç'=>'A', 'å'=>'A', 'Ì'=>'A', '€'=>'A', ''=>'A', '®'=>'A', '‚'=>'C', 'é'=>'E', 'ƒ'=>'E',
        'æ'=>'E', 'è'=>'E', 'í'=>'I', 'ê'=>'I', 'ë'=>'I', 'ì'=>'I', '„'=>'N', 'ñ'=>'O', 'î'=>'O', 'ï'=>'O',
        'Í'=>'O', '…'=>'O', '¯'=>'O', 'ô'=>'U', 'ò'=>'U', 'ó'=>'U', '†'=>'U', '?'=>'Y', '?'=>'B', '§'=>'Ss',
        'ˆ'=>'a', '‡'=>'a', '‰'=>'a', '‹'=>'a', 'Š'=>'a', 'Œ'=>'a', '¾'=>'a', ''=>'c', ''=>'e', 'Ž'=>'e',
        ''=>'e', '‘'=>'e', '“'=>'i', '’'=>'i', '”'=>'i', '•'=>'i', '?'=>'o', '–'=>'n', '˜'=>'o', '—'=>'o',
        '™'=>'o', '›'=>'o', 'š'=>'o', '¿'=>'o', ''=>'u', 'œ'=>'u', 'ž'=>'u', '?'=>'y', '?'=>'y', '?'=>'b',
        'Ø'=>'y', '?'=>'R', '?'=>'r',
    );
    return strtr($string, $table);
}
 
 
// ======================================================================================
// RC4 ENCRYPTION CLASS
// Do not modify.
// ======================================================================================
/**
 * 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);
	}
}
 
?>

Site Tools