class Paytrail

Public Class Methods

new(apiName, apiKey, secret) click to toggle source
# File lib/paytrail.rb, line 10
def initialize(apiName, apiKey, secret)
    @apiName = apiName
    @apiKey = apiKey
    @secret = secret
end

Public Instance Methods

delete(uri, headers) click to toggle source
# File lib/paytrail.rb, line 48
def delete(uri, headers)
    uri = URI(uri)
    request = Net::HTTP::Delete.new(uri, headers)

    makeRequest(uri, request)
end
makeAuthorization(method, location, timestamp, contentMD5) click to toggle source
# File lib/paytrail.rb, line 20
def makeAuthorization(method, location, timestamp, contentMD5)
    signatureData = "%s\n%s\n%s %s\n%s\n%s" % [method, location, @apiName, @apiKey, timestamp, contentMD5]
    digest = OpenSSL::Digest::SHA256.new
    hmac = OpenSSL::HMAC.digest(digest, @secret, signatureData)

    Base64.strict_encode64(hmac)
end
makeContentMD5(content) click to toggle source
# File lib/paytrail.rb, line 28
def makeContentMD5(content)
    Digest::MD5.base64digest content
end
makeHeaders(method, location, timestamp, body) click to toggle source
# File lib/paytrail.rb, line 32
def makeHeaders(method, location, timestamp, body)
    {
        'Timestamp' => timestamp,
        'Content-MD5' => makeContentMD5(body),
        'Authorization' => "%s %s:%s" % [@apiName, @apiKey, makeAuthorization(method, location, timestamp, makeContentMD5(body))]
    }
end
makeRequest(uri, request) click to toggle source
# File lib/paytrail.rb, line 55
def makeRequest(uri, request)
    http = Net::HTTP.start(uri.hostname, uri.port)

    http.request(request)
end
makeTimestamp() click to toggle source
# File lib/paytrail.rb, line 16
def makeTimestamp()
    Time.now.iso8601
end
post(uri, headers, body) click to toggle source
# File lib/paytrail.rb, line 40
def post(uri, headers, body)
    uri = URI(uri)
    request = Net::HTTP::Post.new(uri, headers)
    request.body = body

    makeRequest(uri, request)
end