Skip to content

Instantly share code, notes, and snippets.

@overture8
Last active May 16, 2016 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overture8/36f8ab9bc817af5f03c15cde807217b2 to your computer and use it in GitHub Desktop.
Save overture8/36f8ab9bc817af5f03c15cde807217b2 to your computer and use it in GitHub Desktop.
Ruby Cardstream Payment Gateway code
require "net/https"
require "uri"
require "cgi"
require "digest"
class Cardstream
def initialize(post_data)
url_params = escape_params(post_data)
@post_data = hash_post_data(url_params)
end
def post
uri = URI.parse("https://gateway.cardstream.com/direct/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new uri
request.add_field('Content-Type', 'application/x-www-form-urlencoded')
request.add_field('Content-Length', @post_data.size)
request.body = @post_data
response = http.request request
return CGI::parse(response.body)
end
private
def escape_params(post_data)
post_data
.sort.to_h
.map { |k, v| "#{k}=#{CGI::escape(v.to_s)}" }
.reduce { |acc, v| "#{acc}&#{v}" }
.gsub(/\(/, "%28")
.gsub(/\)/, "%29")
.gsub(/%20/, "+")
end
def hash_post_data(url_params)
url_params + '&signature=' + Digest::SHA2.new(512).hexdigest("#{url_params}#{SIG_KEY}")
end
end
post_data = {
action: 'REFUND',
amount: 4900,
merchantID: 12345678,
type: 1,
xref: '1234567'
}
Cardstream::SIG_KEY = 'mysigkey'
cs = Cardstream.new(post_data)
response = cs.post
puts response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment