class Trustly::Api::Signed

Attributes

api_password[RW]
api_username[RW]
is_https[RW]
merchant_privatekey[RW]
url_path[RW]

Public Class Methods

new(_options) click to toggle source
Calls superclass method Trustly::Api::new
# File lib/trustly/api/signed.rb, line 6
def initialize(_options)
  options = {
    :host        => 'test.trustly.com',
    :port        => 443,
    :is_https    => true,
    :private_pem => "#{Rails.root}/certs/trustly/test.merchant.private.pem",
    :public_pem  => "#{Rails.root}/certs/trustly/test.trustly.public.pem"
  }.merge(_options)


  raise Trustly::Exception::SignatureError, "File '#{options[:private_pem]}' does not exist" unless File.file?(options[:private_pem])
  raise Trustly::Exception::SignatureError, "File '#{options[:public_pem]}' does not exist"  unless File.file?(options[:public_pem])

  super(options[:host],options[:port],options[:is_https],options[:public_pem])

  self.api_username = options.try(:[],:username)
  self.api_password = options.try(:[],:password)
  self.is_https     = options.try(:[],:is_https)
  self.url_path     = '/api/1'

  raise Trustly::Exception::AuthentificationError, "Username not valid" if self.api_username.nil?
  raise Trustly::Exception::AuthentificationError, "Password not valid" if self.api_password.nil?

  self.load_merchant_privatekey(options[:private_pem])

end

Public Instance Methods

call_rpc(request) click to toggle source
Calls superclass method Trustly::Api#call_rpc
# File lib/trustly/api/signed.rb, line 69
def call_rpc(request)
  request.set_uuid(SecureRandom.uuid) if request.get_uuid().nil?
  return super(request)
end
deposit(_options) click to toggle source
# File lib/trustly/api/signed.rb, line 79
def deposit(_options)
  options = {
    "Locale"            => "es_ES",
    "Country"           => "ES",
    "Currency"          => "EUR",
    "SuccessURL"        => "https://www.trustly.com/success",
    "FailURL"           => "https://www.trustly.com/fail",
    "NotificationURL"   => "https://test.trustly.com/demo/notifyd_test",
    "Amount"            => 0
  }.merge(_options)

  ["Locale","Country","Currency","SuccessURL","FailURL","Amount","NotificationURL","EndUserID","MessageID"].each do |req_attr|
    raise Trustly::Exception::DataError, "Option not valid '#{req_attr}'" if options.try(:[],req_attr).nil?
  end

  raise Trustly::Exception::DataError, "Amount is 0" if options["Amount"].nil? || options["Amount"].to_f <= 0.0

  attributes = options.slice(
    "Locale","Country","Currency",
    "SuggestedMinAmount","SuggestedMaxAmount","Amount",
    "Currency","Country","IP",
    "SuccessURL","FailURL","TemplateURL","URLTarget",
    "MobilePhone","Firstname","Lastname","NationalIdentificationNumber",
    "ShopperStatement"
  )

  data       = options.slice("NotificationURL","EndUserID","MessageID")

  # check required fields
  request = Trustly::Data::JSONRPCRequest.new('Deposit',data,attributes)
  return self.call_rpc(request)
  #options["HoldNotifications"] = "1" unless
end
handle_response(request,httpcall) click to toggle source
# File lib/trustly/api/signed.rb, line 37
def handle_response(request,httpcall)
  response = Trustly::Data::JSONRPCResponse.new(httpcall)
  raise   Trustly::Exception::SignatureError,'Incoming message signature is not valid' unless self.verify_trustly_signed_response(response)
  raise   Trustly::Exception::DataError,     'Incoming response is not related to request. UUID mismatch.' if response.get_uuid() != request.get_uuid()
  return  response
end
insert_credentials(request) click to toggle source
# File lib/trustly/api/signed.rb, line 44
def insert_credentials(request)
  request.set_data( 'Username' , self.api_username)
  request.set_data( 'Password' , self.api_password)
  request.set_param('Signature', self.sign_merchant_request(request))
end
load_merchant_privatekey(filename) click to toggle source
# File lib/trustly/api/signed.rb, line 33
def load_merchant_privatekey(filename)
  self.merchant_privatekey = OpenSSL::PKey::RSA.new(File.read(filename))
end
notification_response(notification,success=true) click to toggle source
# File lib/trustly/api/signed.rb, line 125
def notification_response(notification,success=true)
  response = Trustly::JSONRPCNotificationResponse.new(notification,success)
  response.set_signature(self.sign_merchant_request(response))
  return response
end
refund(_options) click to toggle source
# File lib/trustly/api/signed.rb, line 113
def refund(_options)
  options = {
    "Currency" => "EUR"
  }.merge(_options)

  # check for required options
  ["OrderID","Amount","Currency"].each{|req_attr| raise Trustly::Exception::DataError, "Option not valid '#{req_attr}'" if options.try(:[],req_attr).nil? }

  request = Trustly::Data::JSONRPCRequest.new('Refund',options,nil)
  return self.call_rpc(request)
end
sign_merchant_request(data) click to toggle source
# File lib/trustly/api/signed.rb, line 50
def sign_merchant_request(data)
  raise Trustly::Exception::SignatureError, 'No private key has been loaded' if self.merchant_privatekey.nil?
  method      = data.get_method()
  method      = '' if method.nil?
  uuid        = data.get_uuid()
  uuid        = '' if uuid.nil?
  data        = data.get_data()
  data        = {} if data.nil?

  serial_data = "#{method}#{uuid}#{self.serialize_data(data)}"
  sha1hash    = OpenSSL::Digest::SHA1.new
  signature   = self.merchant_privatekey.sign(sha1hash,serial_data)
  return Base64.encode64(signature).chop #removes \n
end
void(orderid) click to toggle source
# File lib/trustly/api/signed.rb, line 74
def void(orderid)
  request = Trustly::Data::JSONRPCRequest.new('Void',{"OrderID"=>orderid},nil)
  return self.call_rpc(request)
end
withdraw(_options) click to toggle source
# File lib/trustly/api/signed.rb, line 131
def withdraw(_options)

end