Skip to content

Instantly share code, notes, and snippets.

@notacouch
Created September 13, 2012 20:48
Show Gist options
  • Save notacouch/3717534 to your computer and use it in GitHub Desktop.
Save notacouch/3717534 to your computer and use it in GitHub Desktop.
FoxyCart XML Datafeed to MySQL by cowtan
<?php
// @author cowtan
// @url http://forum.foxycart.com/discussion/4578/xml-datafeed-to-mysql-via-php/p1
// @url http://pastie.org/1868428
// ======================================================================================
// CHANGE THIS DATA:
// Set the key you entered in your FoxyCart.com admin.
// ======================================================================================
$myKey = 'CHANGE THIS TEXT to your own datafeed keyphrase'; // your foxy cart datafeed key
$link = mysql_connect('localhost', 'username', 'password');
$dbSelected = mysql_select_db('database', $link);
// You can change the following data if you want to customize what data gets written.
if (isset($_POST["FoxyData"])) {
// Get the raw data and initialize variables
$FoxyData_encrypted = urldecode($_POST["FoxyData"]);
$FoxyData_decrypted = rc4crypt::decrypt($myKey,$FoxyData_encrypted);
$xml = new SimpleXMLElement($FoxyData_decrypted);
foreach ($xml->transactions->transaction as $transaction) {
$success = "foxy";
// Loop through to get the product code, name, customer name, date, and transaction ID
$orderid = $transaction->id;
$orderDate = $transaction->transaction_date;
$customerFirstName = mysql_real_escape_string($transaction->customer_first_name);
$customerLastName = mysql_real_escape_string($transaction->customer_last_name);
$customerAddress1 = mysql_real_escape_string($transaction->customer_address1);
$customerAddress2 = mysql_real_escape_string($transaction->customer_address2);
$customerCity = mysql_real_escape_string($transaction->customer_city);
$customerState = mysql_real_escape_string($transaction->customer_state);
$customerPostCode = mysql_real_escape_string($transaction->customer_postal_code);
$customerCountry = mysql_real_escape_string($transaction->customer_country);
$customerPhone = mysql_real_escape_string($transaction->customer_phone);
$customerEmail = mysql_real_escape_string($transaction->customer_email);
$sql = "INSERT INTO `cms_module_foxycart_customers` (orderid, orderDate, customerFirstName, customerLastName, customerAddress1, customerAddress2, customerCity, customerState, customerPostCode, customerCountry, customerPhone, customerEmail)
VALUES ($orderid, orderDate, '$customerFirstName', '$customerLastName', '$customerAddress1', '$customerAddress2', '$customerCity', '$customerState', '$customerPostCode', '$customerCountry', '$customerPhone', '$customerEmail')";
echo $sql;
$result = mysql_query($sql);
if ($result) {
$shippingFirstName = mysql_real_escape_string($transaction->shipping_first_name);
$shippingLastName = mysql_real_escape_string($transaction->shipping_last_name);
$shippingAddress1 = mysql_real_escape_string($transaction->shipping_address1);
$shippingAddress2 = mysql_real_escape_string($transaction->shipping_address2);
$shippingCity = mysql_real_escape_string($transaction->shipping_city);
$shippingState = mysql_real_escape_string($transaction->shipping_state);
$shippingPostCode = mysql_real_escape_string($transaction->shipping_postal_code);
$shippingCountry = mysql_real_escape_string($transaction->shipping_country);
$shippingPhone = mysql_real_escape_string($transaction->shipping_phone);
$sql = "INSERT INTO `cms_module_foxycart_shipping` (orderid, shippingFirstName, shippingLastName, shippingAddress1, shippingAddress2, shippingCity, shippingState, shippingPostCode, shippingCountry, shippingPhone)
VALUES ($orderid, '$shippingFirstName', '$shippingLastName', '$shippingAddress1', '$shippingAddress2', '$shippingCity', '$shippingState', '$shippingPostCode', '$shippingCountry', '$shippingPhone')";
$result = mysql_query($sql);
if ($result) {
$orderProduct = mysql_real_escape_string($transaction->product_total);
$orderTax = mysql_real_escape_string($transaction->tax_total);
$orderShipping = mysql_real_escape_string($transaction->shipping_total);
$orderTotal = mysql_real_escape_string($transaction->order_total);
$sql = "INSERT INTO `cms_module_foxycart_orders` (orderid, orderProduct, orderTax, orderShipping, orderTotal)
VALUES ($orderid, $orderProduct, $orderTax, $orderShipping, $orderTotal)";
$result = mysql_query($sql);
if ($result) {
foreach ($transaction->transaction_details->transaction_detail as $product) {
// Get the product details
$productName = $product->product_name;
$productQuantity = $product->product_quantity;
$productPrice = $product->product_price;
$sql = "INSERT INTO `cms_module_foxycart_products` (orderid, productName, productQuantity, productPrice)
VALUES ($orderid, '$productName', $productQuantity, $productPrice)";
$result = mysql_query($sql);
if (($result) && ($success == "foxy")) {
$sucess = "foxy";
} else {
$success = "error";
}
}
} else {
$success = "error";
}
} else {
$success = "error";
}
} else {
$success = "error";
}
// Return value of $success to Foxycart so it know where it stands
echo $success;
}
}
// ======================================================================================
// 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 <[email protected]>
* @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);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment