This is an old revision of the document!


Table of Contents

type:
integration
supports-foxycart-version-from:
1.1
system:
Zapier
name:
Zapier Datafeed
description:
Set your datafeed to push orders to Zapier
tags:
zapier, api
date:
2014-10-07
version:
1.0
developer:
http://www.sparkweb.net/

Zapier

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

Use the FoxyCart datafeed to push your orders to Zapier for processing and submitting to third-party API's.

Requirements

PHP, cURL

Code

<?php
//Zapier Webhook Script by David Hollander, www.sparkweb.net
//Version 1.0, 2014-10-07
 
//Setup Instructions
//-------------------------------------------------------------------------------------------------------------------
 
//Go to this url to enable the Zapier app: https://zapier.com/developer/invite/6466/2e388f1a688ec8ed2976c539d9ab71c4/
 
//Get this field from the FoxyCart > Advanced Settings page
$foxycart_api_key = "";
 
//You'll be presented with a url when setting up your Zapier integration. Enter it here:
$zapier_static_webhook_url = "";
 
//This file needs to be saved on your webserver. Then go to Advanced Settings and check the box to enable store datafeed.
//Put the url for this file in the datafeed box.
 
//When Zapier asks you to create a brand new transaction in FoxyCart, just refeed a transaction.
//You can do this by expanding the transaction on the FoxyCart transactions page.
 
//-------------------------------------------------------------------------------------------------------------------
 
 
 
 
//Processing The Datafeed
if (isset($_POST["FoxyData"])) {
 
	//Make sure we have everything we need
	if (!$foxycart_api_key) {
		die("Please enter your FoxyCart API key.");
	} elseif (!$zapier_static_webhook_url) {
		die("Please enter your static webhook url from Zapier.");
	}
 
	//Decrypt the Datafeed
	$decrypted = foxycart_decrypt($_POST["FoxyData"]);
	$xml_string = simplexml_load_string($decrypted, NULL, LIBXML_NOCDATA);
	$json = json_encode($xml_string);
 
	//Send to Zapier
	$ch = curl_init();
	curl_setopt_array($ch, array(
		CURLOPT_URL => $zapier_static_webhook_url,
		CURLOPT_FAILONERROR => 1,
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_TIMEOUT => 5,
		CURLOPT_POSTFIELDS => $json,
		CURLOPT_POST => true,
	));
	$result = curl_exec($ch);
	curl_close($ch);
 
	//Process Response
	$json = json_decode($result, 1);
 
	//No JSON Returned
	if (!is_array($json)) {
		die("Invalid Response: $result");
 
	//Failure
	} elseif ($json['status'] != "success") {
		die("Unsuccessful Status: $result");
 
	//All Good!
	} else {
		die("foxy");
	}
 
} else {
	die('No Content Received From Datafeed');
}
 
 
 
 
//Decrypt Data From Source
function foxycart_decrypt($src) {
    global $foxycart_api_key;
	return rc4crypt::decrypt($foxycart_api_key, urldecode($src));
}
 
 
// ======================================================================================
// 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
 */
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
	 */
	public static 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
	 */
	public static function decrypt ($pwd, $data, $ispwdHex = 0) {
		return rc4crypt::encrypt($pwd, $data, $ispwdHex);
	}
}

Site Tools