Skip to content

Instantly share code, notes, and snippets.

@fernyb
Created January 24, 2013 05:50
Show Gist options
  • Save fernyb/4618001 to your computer and use it in GitHub Desktop.
Save fernyb/4618001 to your computer and use it in GitHub Desktop.
class Transaction
attr_accessor :transaction_id
attr_accessor :url
SIGNATURE_KEYNAME = "Signature"
SIGNATURE_METHOD_KEYNAME = "SignatureMethod"
SIGNATURE_VERSION_KEYNAME = "SignatureVersion"
HMAC_SHA256_ALGORITHM = "HmacSHA256"
HMAC_SHA1_ALGORITHM = "HmacSHA1"
SERVICE_HOST = "fps.sandbox.amazonaws.com"
class << self
attr_accessor :access_key, :secret_key
end
def access_key
self.class.access_key
end
def secret_key
self.class.secret_key
end
def self.get transaction_id
tran = new
tran.transaction_id = transaction_id
tran.get
end
def current_timestamp
Time.now.utc.iso8601
end
def build_query_params
{
'Action' => 'GetTransactionStatus',
'AWSAccessKeyId' => access_key,
'Timestamp' => current_timestamp,
'TransactionId' => transaction_id,
'Version' => '2008-09-17',
SIGNATURE_METHOD_KEYNAME => HMAC_SHA256_ALGORITHM,
SIGNATURE_VERSION_KEYNAME => "2"
}.sort.map.inject({}) do |initial, item|
initial.merge!({ item.first => urlencode(item.last) })
end.map{|k,v| "#{k}=#{v}" }.join("&")
end
def get
query = build_query_params
sig = generate_signature(query)
self.url = "https://#{SERVICE_HOST}/?#{query}&#{SIGNATURE_KEYNAME}=#{sig}"
self.url
end
def urlencode(plaintext)
CGI.escape(plaintext.to_s).gsub("+", "%20").gsub("%7E", "~")
end
def generate_signature(query)
sign = "GET\n#{SERVICE_HOST}\n/\n#{query}"
algorithm = 'sha256'
digest = OpenSSL::Digest::Digest.new(algorithm)
sig = Base64.encode64(OpenSSL::HMAC.digest(digest, secret_key, sign)).chomp
urlencode(sig)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment