Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.