Integrating Ruby and Linkpoint

Aug 25, 2007

Well, after a decent summer hiatus, I’ve returned to the blog, brushed the dust off, and am ready to start writing again.

To kick things off code must be posted. A recent project involved using Linkpoint to make credit card transactions over the internet. Now yes, Active Merchant does support linkpoint as a gateway, I just couldn’t get it work right. I tried several times, futzed a bit, and eventually just wrote my own code since I wasn’t needing to do anything fancy.

Hats off to the guys behind Shopify and Active Merchant, I am very impressed with how Active Merchant has evolved, but sometimes it just ends up being easier to roll your own.

Here’s the proof of concept code I wrote that ended up giving me a “Hello World!” as far as the realm of e-commerce goes. This also demonstrates using Net::HTTPS, which might possibly be the most poorly documented part of the ruby standard library. I’d go so far to say that it is downright hidden.

Obviously I wouldn’t recommend just copy/pasting this into a production environment.

require 'net/https'
require 'rubygems'
require 'builder'

url = 'https://staging.linkpt.net:1129/'
login = '1234567890' # Your store number goes here
pem_file = File.open('1234567890.pem').read # your cert/key goes here

data = "" 
b = Builder::XmlMarkup.new(:target => data, :indent => 2)

b.instruct!
b.order do
  b.merchantinfo do
    b.configfile login
  end
  b.orderoptions do
    b.ordertype 'SALE'
  end
  b.payment do
    b.chargetotal '1.00'
  end
  b.creditcard do
    b.cardnumber '4111111111111111'
    b.cardexpmonth '1'
    b.cardexpyear '08'
  end
  b.periodic do
    b.action 'SUBMIT'
    b.installments '12'
    b.threshold '3'
    b.startdate 'immediate'
    b.periodicity 'monthly'
  end
end

puts data

uri = URI.parse(url)

http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true
http.verify_mode    = OpenSSL::SSL::VERIFY_NONE
http.cert = OpenSSL::X509::Certificate.new(pem_file)
http.key = OpenSSL::PKey::RSA.new(pem_file)

response = http.post(uri.path, data)

puts response.body