type:
integration
system:
Ruby on Rails
name:
Datafeed Decryption Example
description:
A basic example of how to decrypt a foxycart v060 datafeed
tag:
datafeed, encryption
date:
2010-06-25
version:
1.0
developer:
http://obledesign.com

Datafeed Decryption Example

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

A simple example of how to implement a datafeed listener in Ruby on Rails. It especially focuses on decrypting the RC4 encryption.

Installation

  1. Create a new Datafeeds controller with the code below or paste the method into your own controller.
  2. Edit the API_KEY with the API key from your own store.

Requirements

  • You must have ruby-rc4 installed.
  • You must have Nokogiri installed.
  • This example is for a v060 datafeed. It may have to be adapted for any other version.

Gotchas

  • Be sure to use CGI::unescape instead of URI::unescape or URI::decode. The URI methods will screw up the decoding and you'll get weird artifacts in your XML.
  • If you get an InvalidAuthenticityToken error in Rails you'll have to disable protect_from_forgery for the action being called.

Code

class DatafeedsController < ApplicationController
 
  API_KEY = 'YOUR_API_KEY_GOES_HERE'
 
  # Parses Foxycart datafeed v060
  # see http://wiki.foxycart.com/docs/datafeed for details
  def create
 
    require 'rc4'
    require 'nokogiri'
 
    decryptor = RC4.new(API_KEY) 
    xml = decryptor.decrypt(CGI::unescape(params['FoxyData']))
 
    feed = Nokogiri::XML(xml)
    feed.xpath('.//transaction').each do |transaction|
 
      # PROCESS THE FEED AS YOU LIKE HERE
 
    end
 
    render :text => 'foxy'
 
    rescue Exception=>e
      render :text => e.to_s
 
  end
 
end

Site Tools