trustly-client-ruby 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/lib/trustly.rb +3 -0
- data/lib/trustly/api.rb +108 -0
- data/lib/trustly/api/signed.rb +117 -0
- data/lib/trustly/data.rb +65 -0
- data/lib/trustly/data/jsonrpc_request.rb +92 -0
- data/lib/trustly/data/jsonrpc_response.rb +15 -0
- data/lib/trustly/data/jsonrpcnotificationrequest.rb +48 -0
- data/lib/trustly/data/jsonrpcnotificationresponse.rb +59 -0
- data/lib/trustly/data/request.rb +33 -0
- data/lib/trustly/data/response.rb +80 -0
- data/lib/trustly/exception/authentification_error.rb +2 -0
- data/lib/trustly/exception/connection_error.rb +2 -0
- data/lib/trustly/exception/data_error.rb +3 -0
- data/lib/trustly/exception/jsonrpc_version_error.rb +3 -0
- data/lib/trustly/exception/signature_error.rb +2 -0
- data/lib/trustly/version.rb +3 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c90e99723b00ef20870db739054224a2a736e30a
|
4
|
+
data.tar.gz: d11c19c43749183b921c8278b56572ee29d07ca0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac472acbd680ffcdb1696dab28c91031dc70f1f50b1002c4c55854c7c92233703c76597b59a54e1692d36556bfce1087285ec3565d1f767329850de9f2e9b2a4
|
7
|
+
data.tar.gz: 036fd6b47420f61653eaf025cca5f7d4edb20cd020fd31d9f2a414c0f136a66b1deb2e4ea287413de1e0e60d7059ea473101146cd182baa32552cac77d2a1fe3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Trustly::Client::Ruby
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/trustly/client/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'trustly-client-ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install trustly-client-ruby
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/trustly-client-ruby/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/lib/trustly.rb
ADDED
data/lib/trustly/api.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
|
2
|
+
class Trustly::Api
|
3
|
+
|
4
|
+
attr_accessor :api_host, :api_port, :api_is_https,:last_request,:trustly_publickey,:trustly_verifyer
|
5
|
+
|
6
|
+
def serialize_data(object)
|
7
|
+
serialized = ""
|
8
|
+
if object.is_a?(Array)
|
9
|
+
# Its an array
|
10
|
+
object.each do |obj|
|
11
|
+
serialized.concat(obj.is_a?(Hash) ? serialize_data(obj) : obj.to_s)
|
12
|
+
end
|
13
|
+
elsif object.is_a?(Hash)
|
14
|
+
# Its a Hash
|
15
|
+
object.sort.to_h.each do |key,value|
|
16
|
+
serialized.concat(key.to_s).concat(serialize_data(value))
|
17
|
+
end
|
18
|
+
else
|
19
|
+
# Anything else: numbers, symbols, values
|
20
|
+
serialized.concat object.to_s
|
21
|
+
end
|
22
|
+
return serialized
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(host,port,is_https,pem_file)
|
26
|
+
self.api_host = host
|
27
|
+
self.api_port = port
|
28
|
+
self.api_is_https = is_https
|
29
|
+
|
30
|
+
self.load_trustly_publickey(pem_file)
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_trustly_publickey(file)
|
34
|
+
self.trustly_publickey = OpenSSL::PKey::RSA.new(File.read(file)) #.public_key
|
35
|
+
end
|
36
|
+
|
37
|
+
def url_path(request=nil)
|
38
|
+
raise NotImplementedError
|
39
|
+
end
|
40
|
+
|
41
|
+
def handle_response(request,httpcall)
|
42
|
+
raise NotImplementedError
|
43
|
+
end
|
44
|
+
|
45
|
+
def insert_credentials(request)
|
46
|
+
raise NotImplementedError
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
def _verify_trustly_signed_data(method, uuid, signature, data)
|
52
|
+
method = '' if method.nil?
|
53
|
+
uuid = '' if uuid.nil?
|
54
|
+
serial_data = "#{method}#{uuid}#{self.serialize_data(data)}"
|
55
|
+
raw_signature = Base64.decode64(signature)
|
56
|
+
return self.trustly_publickey.public_key.verify(OpenSSL::Digest::SHA1.new, raw_signature, serial_data)
|
57
|
+
end
|
58
|
+
|
59
|
+
def verify_trustly_signed_response(response)
|
60
|
+
method = response.get_method()
|
61
|
+
uuid = response.get_uuid()
|
62
|
+
signature = response.get_signature()
|
63
|
+
data = response.get_data()
|
64
|
+
return self._verify_trustly_signed_data(method, uuid, signature, data)
|
65
|
+
end
|
66
|
+
|
67
|
+
def verify_trustly_signed_notification(response)
|
68
|
+
method = response.get_method()
|
69
|
+
uuid = response.get_uuid()
|
70
|
+
signature = response.get_signature()
|
71
|
+
data = response.get_data()
|
72
|
+
return self._verify_trustly_signed_data(method, uuid, signature, data)
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_host(host=nil,port=nil,is_https=nil)
|
76
|
+
self.api_host = host unless host.nil?
|
77
|
+
self.load_trustly_publickey() unless host.nil?
|
78
|
+
self.api_port = port unless port.nil?
|
79
|
+
self.is_https = is_https unless is_https.nil?
|
80
|
+
end
|
81
|
+
|
82
|
+
def base_url
|
83
|
+
if self.api_is_https
|
84
|
+
return (self.api_port == 443) ? "https://#{self.api_host}" : "https://#{self.api_host}:#{self.api_port}"
|
85
|
+
else
|
86
|
+
return (self.api_port == 80) ? "http://#{self.api_host}" : "http://#{self.api_host}:#{self.api_port}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def uri(request)
|
91
|
+
return URI("#{self.base_url}#{self.url_path(request)}")
|
92
|
+
end
|
93
|
+
|
94
|
+
def call_rpc(request)
|
95
|
+
self.insert_credentials(request)
|
96
|
+
self.last_request = request
|
97
|
+
uri = self.uri(request)
|
98
|
+
http_req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
|
99
|
+
http_req.body = request.json()
|
100
|
+
http_res = Net::HTTP.start(uri.hostname, uri.port,{use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE}) { |http| http.request(http_req) }
|
101
|
+
return self.handle_response(request,http_res)
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
|
@@ -0,0 +1,117 @@
|
|
1
|
+
class Trustly::Api::Signed < Trustly::Api
|
2
|
+
|
3
|
+
attr_accessor :url_path,:api_username, :api_password, :merchant_privatekey, :is_https
|
4
|
+
|
5
|
+
|
6
|
+
def initialize(_options)
|
7
|
+
options = {
|
8
|
+
:host => 'test.trustly.com',
|
9
|
+
:port => 443,
|
10
|
+
:is_https => true,
|
11
|
+
:private_pem => "#{Rails.root}/certs/trustly/test.acuerdalo.private.pem",
|
12
|
+
:public_pem => "#{Rails.root}/certs/trustly/test.trustly.public.pem"
|
13
|
+
}.merge(_options)
|
14
|
+
|
15
|
+
|
16
|
+
raise Trustly::Exception::SignatureError, "File '#{options[:private_pem]}' does not exist" unless File.file?(options[:private_pem])
|
17
|
+
raise Trustly::Exception::SignatureError, "File '#{options[:public_pem]}' does not exist" unless File.file?(options[:public_pem])
|
18
|
+
|
19
|
+
super(options[:host],options[:port],options[:is_https],options[:public_pem])
|
20
|
+
|
21
|
+
self.api_username = options.try(:[],:username)
|
22
|
+
self.api_password = options.try(:[],:password)
|
23
|
+
self.is_https = options.try(:[],:is_https)
|
24
|
+
self.url_path = '/api/1'
|
25
|
+
|
26
|
+
raise Trustly::Exception::AuthentificationError, "Username not valid" if self.api_username.nil?
|
27
|
+
raise Trustly::Exception::AuthentificationError, "Password not valid" if self.api_password.nil?
|
28
|
+
|
29
|
+
self.load_merchant_privatekey(options[:private_pem])
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_merchant_privatekey(filename)
|
34
|
+
self.merchant_privatekey = OpenSSL::PKey::RSA.new(File.read(filename))
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_response(request,httpcall)
|
38
|
+
response = Trustly::Data::JSONRPCResponse.new(httpcall)
|
39
|
+
raise Trustly::Exception::SignatureError,'Incoming message signature is not valid' unless self.verify_trustly_signed_response(response)
|
40
|
+
raise Trustly::Exception::DataError, 'Incoming response is not related to request. UUID mismatch.' if response.get_uuid() != request.get_uuid()
|
41
|
+
return response
|
42
|
+
end
|
43
|
+
|
44
|
+
def insert_credentials(request)
|
45
|
+
request.set_data( 'Username' , self.api_username)
|
46
|
+
request.set_data( 'Password' , self.api_password)
|
47
|
+
request.set_param('Signature', self.sign_merchant_request(request))
|
48
|
+
end
|
49
|
+
|
50
|
+
def sign_merchant_request(data)
|
51
|
+
raise Trustly::Exception::SignatureError, 'No private key has been loaded' if self.merchant_privatekey.nil?
|
52
|
+
method = data.get_method()
|
53
|
+
method = '' if method.nil?
|
54
|
+
uuid = data.get_uuid()
|
55
|
+
uuid = '' if uuid.nil?
|
56
|
+
data = data.get_data()
|
57
|
+
data = {} if data.nil?
|
58
|
+
|
59
|
+
serial_data = "#{method}#{uuid}#{self.serialize_data(data)}"
|
60
|
+
sha1hash = OpenSSL::Digest::SHA1.new
|
61
|
+
signature = self.merchant_privatekey.sign(sha1hash,serial_data)
|
62
|
+
return Base64.encode64(signature).chop #removes \n
|
63
|
+
end
|
64
|
+
|
65
|
+
def url_path(request=nil)
|
66
|
+
return '/api/1'
|
67
|
+
end
|
68
|
+
|
69
|
+
def call_rpc(request)
|
70
|
+
request.set_uuid(SecureRandom.uuid) if request.get_uuid().nil?
|
71
|
+
return super(request)
|
72
|
+
end
|
73
|
+
|
74
|
+
def void(orderid)
|
75
|
+
request = Trustly::Data::JSONRPCRequest.new('Void',{"OrderID"=>orderid},nil)
|
76
|
+
return self.call_rpc(request)
|
77
|
+
end
|
78
|
+
|
79
|
+
def deposit(_options)
|
80
|
+
options = {
|
81
|
+
"Locale" => "es_ES",
|
82
|
+
"Country" => "ES",
|
83
|
+
"Currency" => "EUR",
|
84
|
+
"SuccessURL" => "https://www.trustly.com/success",
|
85
|
+
"FailURL" => "https://www.trustly.com/fail",
|
86
|
+
"NotificationURL" => "https://test.trustly.com/demo/notifyd_test",
|
87
|
+
"Amount" => 0
|
88
|
+
}.merge(_options)
|
89
|
+
|
90
|
+
["Locale","Country","Currency","SuccessURL","FailURL","Amount","NotificationURL","EndUserID","MessageID"].each do |req_attr|
|
91
|
+
raise Trustly::Exception::DataError, "Option not valid '#{req_attr}'" if options.try(:[],req_attr).nil?
|
92
|
+
end
|
93
|
+
|
94
|
+
raise Trustly::Exception::DataError, "Amount is 0" if options["Amount"].nil? || options["Amount"].to_f <= 0.0
|
95
|
+
|
96
|
+
attributes = options.slice(
|
97
|
+
"Locale","Country","Currency",
|
98
|
+
"SuggestedMinAmount","SuggestedMaxAmount","Amount",
|
99
|
+
"Currency","Country","IP",
|
100
|
+
"SuccessURL","FailURL","TemplateURL","URLTarget",
|
101
|
+
"MobilePhone","Firstname","Lastname","NationalIdentificationNumber",
|
102
|
+
"ShopperStatement"
|
103
|
+
)
|
104
|
+
|
105
|
+
data = options.slice("NotificationURL","EndUserID","MessageID")
|
106
|
+
|
107
|
+
# check required fields
|
108
|
+
request = Trustly::Data::JSONRPCRequest.new('Deposit',data,attributes)
|
109
|
+
return self.call_rpc(request)
|
110
|
+
#options["HoldNotifications"] = "1" unless
|
111
|
+
end
|
112
|
+
|
113
|
+
def withdraw(_options)
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
data/lib/trustly/data.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
class Trustly::Data
|
2
|
+
|
3
|
+
attr_accessor :payload
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
self.payload = {}
|
7
|
+
end
|
8
|
+
# Vacuum out all keys being set to Nil in the data to be communicated
|
9
|
+
def vacumm(data)
|
10
|
+
if data.is_a? Array
|
11
|
+
ret = []
|
12
|
+
data.each do |elem|
|
13
|
+
unless elem.nil?
|
14
|
+
v = self.vacumm elem
|
15
|
+
ret.append(v) unless v.nil?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
return nil if ret.length == 0
|
19
|
+
return ret
|
20
|
+
elsif data.is_a? Hash
|
21
|
+
ret = {}
|
22
|
+
data.each do |key,elem|
|
23
|
+
unless elem.nil?
|
24
|
+
v = self.vacumm elem
|
25
|
+
ret[key.to_s] = elem unless v.nil?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return nil if ret.length == 0
|
29
|
+
return ret
|
30
|
+
else
|
31
|
+
return data
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def get(name=nil)
|
36
|
+
return name.nil? ? self.payload.dup : self.payload.try(:[],name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_from(sub,name)
|
40
|
+
return nil if sub.nil? || name.nil? || self.payload.try(:[],sub).nil? || self.payload[sub].try(:[],name).nil?
|
41
|
+
return self.payload[sub][name]
|
42
|
+
end
|
43
|
+
|
44
|
+
def set(name,value)
|
45
|
+
self.payload[name] = value
|
46
|
+
return value
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_in(sub,name,value,parent=nil)
|
50
|
+
return nil if sub.nil? || name.nil?
|
51
|
+
self.payload[sub] = {} if self.payload.try(:[],sub).nil?
|
52
|
+
self.payload[sub][name] = value
|
53
|
+
end
|
54
|
+
|
55
|
+
def pop(name)
|
56
|
+
v = self.payload.try(:[],name)
|
57
|
+
delete self.payload[name] unless v.nil?
|
58
|
+
return v
|
59
|
+
end
|
60
|
+
|
61
|
+
def json()
|
62
|
+
self.payload.to_json
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
class Trustly::Data::JSONRPCRequest < Trustly::Data::Request
|
2
|
+
|
3
|
+
def initialize(method=nil,data=nil,attributes=nil)
|
4
|
+
|
5
|
+
if !data.nil? || !attributes.nil?
|
6
|
+
self.payload = {"params"=>{}}
|
7
|
+
unless data.nil?
|
8
|
+
if !data.is_a?(Hash) && !attributes.nil?
|
9
|
+
raise TypeError, "Data must be a Hash if attributes is provided"
|
10
|
+
else
|
11
|
+
self.payload["params"]["Data"] = data
|
12
|
+
end
|
13
|
+
else
|
14
|
+
self.payload["params"]["Data"] = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
self.payload["params"]["Data"]["Attributes"] = attributes unless attributes.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
self.payload['method'] = method unless method.nil?
|
21
|
+
self.payload['params'] = {} unless self.get('params')
|
22
|
+
self.payload['version'] = '1.1'
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def get_param(name)
|
28
|
+
return self.payload['params'].try(:[],name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_data(name=nil)
|
32
|
+
data = self.get_param('Data')
|
33
|
+
return data if name.nil?
|
34
|
+
raise KeyError, "Not found #{name} in data" if data.nil?
|
35
|
+
return data.dup if name.nil?
|
36
|
+
return data.try(:[],name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def get_attribute(name)
|
40
|
+
data = self.get_param('Data')
|
41
|
+
if data.nil?
|
42
|
+
attributes = nil
|
43
|
+
else
|
44
|
+
attributes = data.try(:[],'Attributes')
|
45
|
+
end
|
46
|
+
raise KeyError, "Not found 'Attributes' in data" if attributes.nil?
|
47
|
+
return attributes.dup if name.nil?
|
48
|
+
return attributes.try(:[],name)
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_param(name,value)
|
52
|
+
self.payload['params'][name] = value
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_data(name,value)
|
56
|
+
unless name.nil?
|
57
|
+
self.payload['params']['Data'] = {} if self.payload['params'].try(:[],'Data').nil?
|
58
|
+
self.payload['params']['Data'][name] = value
|
59
|
+
end
|
60
|
+
return value
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_attributes(name,value)
|
64
|
+
unless name.nil?
|
65
|
+
self.payload['params']['Data'] = {} if self.payload['params'].try(:[],'Data').nil?
|
66
|
+
self.payload['params']['Data']['Attributes'] = {} if self.payload['params']['Data'].try(:[],'Attributes').nil?
|
67
|
+
self.payload['params']['Data']['Attributes'][name] = value
|
68
|
+
end
|
69
|
+
return value
|
70
|
+
end
|
71
|
+
|
72
|
+
def set_uuid(uuid)
|
73
|
+
return self.set_param('UUID',uuid)
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_uuid
|
77
|
+
return self.get_param('UUID')
|
78
|
+
rescue KeyError => e
|
79
|
+
return nil
|
80
|
+
end
|
81
|
+
|
82
|
+
def set_method(method)
|
83
|
+
return self.set('method',method)
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_method()
|
87
|
+
return self.get('method')
|
88
|
+
rescue KeyError => e
|
89
|
+
return nil
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Trustly::Data::JSONRPCResponse < Trustly::Data::Response
|
2
|
+
|
3
|
+
def initialize(http_response)
|
4
|
+
super(http_response)
|
5
|
+
version = self.get("version")
|
6
|
+
raise Trustly::Exception::JSONRPCVersionError, "JSON RPC Version is not supported" if version != '1.1'
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_data(name=nil)
|
10
|
+
return self.response_result.try(:[],"data") if name.nil?
|
11
|
+
return Trustly::Exception::DataError, "Data not found or key is null" if self.response_result.try(:[],"data").nil? || name.nil?
|
12
|
+
return self.response_result["data"][name]
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class Trustly::JSONRPCNotificationRequest < Trustly::Data
|
2
|
+
|
3
|
+
attr_accessor :notification_body, :payload
|
4
|
+
|
5
|
+
def initialize(notification_body)
|
6
|
+
super
|
7
|
+
self.notification_body = notification_body
|
8
|
+
begin
|
9
|
+
self.payload = JSON.parse(self.notification_body)
|
10
|
+
rescue JSON::ParserError => e
|
11
|
+
raise Trustly::Exception::DataError, e.message
|
12
|
+
end
|
13
|
+
|
14
|
+
raise Trustly::Exception::JSONRPCVersionError, 'JSON RPC Version #{(self.get_version()} is not supported' if self.get_version() != '1.1'
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_version()
|
18
|
+
return self.get('version')
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_method()
|
22
|
+
return self.get('method')
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_uuid()
|
26
|
+
return self.get_params('uuid')
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_signature()
|
30
|
+
return self.get_params('signature')
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_params(name)
|
34
|
+
raise KeyError,"#{name} is not present in params" if name.nil? || self.payload.try(:[],"params").nil? || self.payload["params"].try(:[],name).nil?
|
35
|
+
return self.payload["params"][name]
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_data(name=nil)
|
39
|
+
if name.nil?
|
40
|
+
raise KeyError,"Data not present" if self.payload.try(:[],"params").nil? || self.payload["params"].try(:[],"data").nil?
|
41
|
+
return self.payload["params"]["data"]
|
42
|
+
else
|
43
|
+
raise KeyError,"#{name} is not present in data" if name.nil? || self.payload.try(:[],"params").nil? || self.payload["params"].try(:[],"data").nil? || self.payload["params"]["data"].try(:[],name).nil?
|
44
|
+
return self.payload["params"]["data"][name]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class Trustly::JSONRPCNotificationResponse < Trustly::Data
|
2
|
+
|
3
|
+
def initialize(request,success=nil)
|
4
|
+
super
|
5
|
+
uuid = request.get_uuid()
|
6
|
+
method = request.get_method()
|
7
|
+
|
8
|
+
self.set('version')
|
9
|
+
self.set_result('uuid',uuid) unless uuid.nil?
|
10
|
+
self.set_result('method',method) unless method.nil?
|
11
|
+
self.set_data('success', (!success.nil? && !success ? 'FAILED' : 'OK' ))
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_result(name,value)
|
15
|
+
return nil if name.nil? || value.nil?
|
16
|
+
self.payload["result"] = {} if self.payload.try(:[],"result").nil?
|
17
|
+
self.payload["result"][name] = value
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_data(name,value)
|
21
|
+
return nil if name.nil? || value.nil?
|
22
|
+
self.payload["result"] = {} if self.payload.try(:[],"result").nil?
|
23
|
+
self.payload["result"]["data"] = {} if self.payload["result"].try(:[],"data").nil?
|
24
|
+
self.payload["result"]["data"][name] = value
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_result(name)
|
28
|
+
raise KeyError,"#{name} is not present in result" if name.nil? || self.payload.try(:[],"result").nil? || self.payload["result"].try(:[],name).nil?
|
29
|
+
return self.payload["result"][name]
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_data(name=nil)
|
33
|
+
raise KeyError,"#{name} is not present in data" if name.nil? || self.payload.try(:[],"result").nil? || self.payload["result"].try(:[],"data").nil? || self.payload["result"]["data"].try(:[],name).nil?
|
34
|
+
return self.payload["result"]["data"][name]
|
35
|
+
end
|
36
|
+
|
37
|
+
def get_data(name=nil)
|
38
|
+
if name.nil?
|
39
|
+
raise KeyError,"Data not present" if self.payload.try(:[],"result").nil? || self.payload["result"].try(:[],"data").nil?
|
40
|
+
return self.payload["result"]["data"]
|
41
|
+
else
|
42
|
+
raise KeyError,"#{name} is not present in data" if name.nil? || self.payload.try(:[],"result").nil? || self.payload["result"].try(:[],"data").nil? || self.payload["result"]["data"].try(:[],name).nil?
|
43
|
+
return self.payload["result"]["data"][name]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_method
|
48
|
+
return self.get_result('method')
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_uuid
|
52
|
+
return self.get_result('uuid')
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_signature
|
56
|
+
return self.get_result('signature')
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Trustly::Data::Request < Trustly::Data
|
2
|
+
|
3
|
+
attr_accessor :method
|
4
|
+
|
5
|
+
def initialize(method=nil,payload=nil)
|
6
|
+
super
|
7
|
+
self.payload = self.vacuum(payload) unless payload.nil?
|
8
|
+
unless method.nil?
|
9
|
+
self.method = method
|
10
|
+
else
|
11
|
+
self.method = self.payload.get('method')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_method
|
16
|
+
return self.method
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_method(method)
|
20
|
+
self.method = method
|
21
|
+
return method
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_uuid
|
25
|
+
return self.payload.get('uuid')
|
26
|
+
end
|
27
|
+
|
28
|
+
def set_uuid
|
29
|
+
self.set('uuid',uuid)
|
30
|
+
return uuid
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
class Trustly::Data::Response < Trustly::Data
|
2
|
+
attr_accessor :response_status, :response_reason, :response_body, :response_result
|
3
|
+
|
4
|
+
def initialize(http_response) #called from Net::HTTP.get_response("trustly.com","/api_path") -> returns Net::HTTPResponse
|
5
|
+
super()
|
6
|
+
self.response_status = http_response.code
|
7
|
+
self.response_reason = http_response.class.name
|
8
|
+
self.response_body = http_response.body
|
9
|
+
begin
|
10
|
+
self.payload = JSON.parse(self.response_body)
|
11
|
+
rescue JSON::ParserError => e
|
12
|
+
if self.response_status != 200
|
13
|
+
raise Trustly::Exception::ConnectionError, "#{self.response_status}: #{self.response_reason} [#{self.response_body}]"
|
14
|
+
else
|
15
|
+
raise Trustly::Exception::DataError, e.message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
begin
|
20
|
+
self.response_result = self.get('result')
|
21
|
+
rescue IndexError::KeyError => e
|
22
|
+
self.response_result = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
if self.response_result.nil?
|
26
|
+
begin
|
27
|
+
self.response_result = self.payload["error"]["error"]
|
28
|
+
rescue IndexError::KeyError => e
|
29
|
+
end
|
30
|
+
end
|
31
|
+
raise Trustly::Exception::DataError, "No result or error in response #{self.payload}" if self.response_result.nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
def error?
|
35
|
+
return !self.get('error').nil?
|
36
|
+
rescue IndexError::KeyError => e
|
37
|
+
return false
|
38
|
+
end
|
39
|
+
|
40
|
+
def error_code
|
41
|
+
return nil unless self.error?
|
42
|
+
return self.response_result["data"].try(:[],'code')
|
43
|
+
end
|
44
|
+
|
45
|
+
def error_msg
|
46
|
+
return nil unless self.error?
|
47
|
+
return self.response_result["data"].try(:[],'message')
|
48
|
+
end
|
49
|
+
|
50
|
+
def success?
|
51
|
+
return !self.get('result').nil?
|
52
|
+
rescue IndexError::KeyError => e
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_uuid
|
57
|
+
return self.response_result.try(:[],'uuid')
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_method
|
61
|
+
return self.response_result.try(:[],'method')
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_signature
|
65
|
+
return self.response_result.try(:[],"signature")
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_result
|
69
|
+
unless name.nil?
|
70
|
+
if self.response_result.is_a?(Hash)
|
71
|
+
return self.response_result.try(:[],name)
|
72
|
+
else
|
73
|
+
raise StandardError::TypeError, "Result is not a Hash"
|
74
|
+
end
|
75
|
+
else
|
76
|
+
return self.response_result.dup
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trustly-client-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jorge Carretie
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.0
|
41
|
+
description: Support for Ruby use of trustly API
|
42
|
+
email: jorge@carretie.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.md
|
49
|
+
- lib/trustly.rb
|
50
|
+
- lib/trustly/api.rb
|
51
|
+
- lib/trustly/api/signed.rb
|
52
|
+
- lib/trustly/data.rb
|
53
|
+
- lib/trustly/data/jsonrpc_request.rb
|
54
|
+
- lib/trustly/data/jsonrpc_response.rb
|
55
|
+
- lib/trustly/data/jsonrpcnotificationrequest.rb
|
56
|
+
- lib/trustly/data/jsonrpcnotificationresponse.rb
|
57
|
+
- lib/trustly/data/request.rb
|
58
|
+
- lib/trustly/data/response.rb
|
59
|
+
- lib/trustly/exception/authentification_error.rb
|
60
|
+
- lib/trustly/exception/connection_error.rb
|
61
|
+
- lib/trustly/exception/data_error.rb
|
62
|
+
- lib/trustly/exception/jsonrpc_version_error.rb
|
63
|
+
- lib/trustly/exception/signature_error.rb
|
64
|
+
- lib/trustly/version.rb
|
65
|
+
homepage: https://github.com/jcarreti/trusty-client-ruby
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.4.5
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Trustly Client Ruby Support
|
89
|
+
test_files: []
|