type:
integration
system:
ASP.Net, C#
name:
ASP.NET C# API + Datafeed Code
description:
Functions to interact with the FoxyCart API and XML Datafeed
tag:
api, datafeed, xml, c#, csharp, integration
date:
2011-06-01
version:
1.1

ASP.NET with C# XML Datafeed E-Commerce Integration

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

Functions to interact with the FoxyCart API and XML Datafeeds. This code will decrypt the XML stream into a string. From there you can store off or do other processing. Per the DataFeed API, the only response from the page is the string “foxy”. This can be used as a foundation to build a more advanced FoxyCart + .NET e-commerce integration. Don't forget to look at Transaction XML Datafeeds: Instant Transaction Notification for the XML format.

Requirements

Add the following code to the code file of a C# ASP.Net page called “ProcessTransaction.aspx”. You may need to set ValidateRequest=“false” in the Page directives of the .aspx page.

If you are using .net 4.0 or newer then you may also need to add this to the web.config for your ASPX (inside the System.Web tags)

<httpRuntime requestValidationMode=“2.0” />

Failure to do so may give you “Internal server error” responses and you may also see “A validation error has occured” errors in the application log of the server running your ASPX page.

Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class ProcessTransaction : System.Web.UI.Page
{
    private string API_KEY = "Your FoxyCart store secret (aka API Key)";
 
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var foxyFeed = Request.Form["FoxyData"];
            //Changed from ASCII to UTF8. Not sure why you'd get ASCII bytes on UTF-8 encoded data
            var ffBytes = Encoding.UTF8.GetBytes(foxyFeed);
            //This takes the UTF-8 encoded bytes and decodes them to Latin-1. If the decoded data is not Latin-1, the RC4.Decrypt, doesn't seem to function properly.
            var unencodedFeed = HttpUtility.UrlDecode(ffBytes, Encoding.GetEncoding(1252));     
            var decryptedTransaction = RC4.Decrypt(API_KEY, unecodedUTF8Feed, false);
            //At this point, we've decrypted UTF-8 data using the Latin-1 encoding, which functions properly, unless there are special characters in your data. 
            //http://www.weblogism.com/item/270/why-does-e-become-a
            //In order to fix the special characters, you need to turn the string back into Latin-1 bytes, and convert it to a UTF-8 string. 
            var transaction = Encoding.UTF8.GetString(Encoding.GetEncoding(1252).GetBytes(decryptedTransaction));
 
            //Now you can process the XML string "transaction" as desired.
 
            Response.Write("foxy");
        }
        catch (Exception) { }   //Just eat all exceptions.  You can log this internally if you'd like.
    }
 
    /**
     * RC4.NET 1.0
     *
     * RC4.NET is a petite library that allows you to use RC4
     * encryption easily in the .NET Platform. It's OO and can
     * produce outputs in binary and hex.
     *
     * (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]
     *	   All Rights Reserved
     *
     * @link http://rc4dotnet.devhome.org
     * @author Mukul Sabharwal <mjsabby@gmail.com>
     * @version $Id: RC4.cs,v 1.0 2006/03/19 15:35:24 mukul Exp $
     * @copyright Copyright &copy; 2006 Mukul Sabharwal
     * @license http://www.gnu.org/copyleft/lesser.html
     * @package RC4.NET
     */
    public class RC4
    {
        /**
         * Get ASCII Integer Code
         *
         * @param char ch Character to get ASCII value for
         * @access private
         * @return int
         */
        private static int ord(char ch)
        {
            return (int)(Encoding.GetEncoding(1252).GetBytes(ch + "")[0]);
        }
        /**
         * Get character representation of ASCII Code
         *
         * @param int i ASCII code
         * @access private
         * @return char
         */
        private static char chr(int i)
        {
            byte[] bytes = new byte[1];
            bytes[0] = (byte)i;
            return Encoding.GetEncoding(1252).GetString(bytes)[0];
        }
        /**
         * Convert Hex to Binary (hex2bin)
         *
         * @param string packtype Rudimentary in this implementation
         * @param string datastring Hex to be packed into Binary
         * @access private
         * @return string
         */
        private static string pack(string packtype, string datastring)
        {
            int i, j, datalength, packsize;
            byte[] bytes;
            char[] hex;
            string tmp;
 
            datalength = datastring.Length;
            packsize = (datalength / 2) + (datalength % 2);
            bytes = new byte[packsize];
            hex = new char[2];
 
            for (i = j = 0; i < datalength; i += 2)
            {
                hex[0] = datastring[i];
                if (datalength - i == 1)
                    hex[1] = '0';
                else
                    hex[1] = datastring[i + 1];
                tmp = new string(hex, 0, 2);
                try { bytes[j++] = byte.Parse(tmp, System.Globalization.NumberStyles.HexNumber); }
                catch { } /* grin */
            }
            return Encoding.GetEncoding(1252).GetString(bytes);
        }
        /**
         * Convert Binary to Hex (bin2hex)
         *
         * @param string bindata Binary data
         * @access public
         * @return string
         */
        public static string bin2hex(string bindata)
        {
            int i;
            byte[] bytes = Encoding.GetEncoding(1252).GetBytes(bindata);
            string hexString = "";
            for (i = 0; i < bytes.Length; i++)
            {
                hexString += bytes[i].ToString("x2");
            }
            return hexString;
        }
        /**
         * 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 string Encrypt(string pwd, string data, bool ispwdHex)
        {
            int a, i, j, k, tmp, pwd_length, data_length;
            int[] key, box;
            byte[] cipher;
            //string cipher;
 
            if (ispwdHex)
                pwd = pack("H*", pwd); // valid input, please!
            pwd_length = pwd.Length;
            data_length = data.Length;
            key = new int[256];
            box = new int[256];
            cipher = new byte[data.Length];
            //cipher = "";
 
            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[i] = (byte)(ord(data[i]) ^ k);
                //cipher += chr(ord(data[i]) ^ k);
            }
            return Encoding.GetEncoding(1252).GetString(cipher);
            //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 string Decrypt(string pwd, string data, bool ispwdHex)
        {
            return Encrypt(pwd, data, ispwdHex);
        }
    }
}

Site Tools