Class: Trustly::Api::Signed

Inherits:
Trustly::Api show all
Defined in:
lib/trustly/api/signed.rb

Instance Attribute Summary collapse

Attributes inherited from Trustly::Api

#api_host, #api_is_https, #api_port, #last_request, #trustly_publickey, #trustly_verifyer

Instance Method Summary collapse

Methods inherited from Trustly::Api

#load_trustly_publickey, #serialize_data, #verify_trustly_signed_notification

Constructor Details

#initialize(_options) ⇒ Signed

Returns a new instance of Signed.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#api_passwordObject

Returns the value of attribute api_password.



3
4
5
# File 'lib/trustly/api/signed.rb', line 3

def api_password
  @api_password
end

#api_usernameObject

Returns the value of attribute api_username.



3
4
5
# File 'lib/trustly/api/signed.rb', line 3

def api_username
  @api_username
end

#is_httpsObject

Returns the value of attribute is_https.



3
4
5
# File 'lib/trustly/api/signed.rb', line 3

def is_https
  @is_https
end

#merchant_privatekeyObject

Returns the value of attribute merchant_privatekey.



3
4
5
# File 'lib/trustly/api/signed.rb', line 3

def merchant_privatekey
  @merchant_privatekey
end

#url_path(request = nil) ⇒ Object

Returns the value of attribute url_path.



3
4
5
# File 'lib/trustly/api/signed.rb', line 3

def url_path
  @url_path
end

Instance Method Details

#call_rpc(request) ⇒ Object



69
70
71
72
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object



37
38
39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
# 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) ⇒ Object



33
34
35
# 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) ⇒ Object



125
126
127
128
129
# 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) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/trustly/api/signed.rb', line 113

def refund(_options)
  options = {
    "OrderID" => "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('Deposit',options,nil)
  return self.call_rpc(request)
end

#sign_merchant_request(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



74
75
76
77
# 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) ⇒ Object



131
132
133
# File 'lib/trustly/api/signed.rb', line 131

def withdraw(_options)

end