trustly-client-ruby 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca9ff0abd3eadbde9d7ada98e1c25507e00fa534
4
- data.tar.gz: 03cc19c4253d50be02ee4db20903f17a050fa366
3
+ metadata.gz: 20e58d7cb248d8adfa149f89eaba36b8bfe9c520
4
+ data.tar.gz: 71ef2a4aeef7e028b8054387fa72055cb7fdff0a
5
5
  SHA512:
6
- metadata.gz: e75d6fdd5131303f9eb378f9bf908045f5ebcae42c50fde53e518c0dc5a0fdf2d63ac7641d113f4e2b7f9075ea28f208e1ae21b55d620adba43fe7a495ea6e4c
7
- data.tar.gz: 11df02b98fe608c64eec1bb883f47f2996ece22a457b9931a5f7f0704667e9d7589fa5c1a408b2d91f1941e7b692164ed92516fe844dc448da36b057f1a406a4
6
+ metadata.gz: 547c1f23f5a1564f0c575bf7a3cbf0a76df3a46e850b0c3d3e290122ebdda9774b1328a03c91b9494dce20c6772b340a694420305e0c2ea6a749f5446633b82d
7
+ data.tar.gz: 5784007b972a3af3ef8febe82b93490985067490a3ee3f945654ae711c562f437b3da398653132cbe265f3f8a1c79185a869a18fc483566df00e07e0d343d07a
data/README.md CHANGED
@@ -1,38 +1,176 @@
1
- # Trustly::Client::Ruby
1
+ # Trustly-client-ruby
2
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.
3
+ This is an example implementation of communication with the Trustly API using Ruby. This a ruby gem that allows you use Trustly Api calls in ruby. It's based on [trustly-client-python] (https://github.com/trustly/trustly-client-python) and [turstly-client-php] (https://github.com/trustly/trustly-client-php)
4
+
5
+ It implements the standard Payments API as well as gives stubs for executing calls against the API used by the backoffice.
6
+
7
+ For full documentation on the Trustly API internals visit our developer website: http://trustly.com/developer . All information about software flows and call patters can be found on that site. The documentation within this code will only cover the code itself, not how you use the Trustly API.
8
+
9
+ This code is provided as-is, use it as inspiration, reference or drop it directly into your own project and use it.
4
10
 
5
- TODO: Delete this and the text above, and describe your gem
6
11
 
7
12
  ## Installation
8
13
 
9
14
  Add this line to your application's Gemfile:
10
15
 
11
16
  ```ruby
12
- gem 'trustly-client-ruby'
17
+ gem 'trustly-client-ruby',:require=>'trustly'
13
18
  ```
14
19
 
15
20
  And then execute:
16
21
 
17
- $ bundle
22
+ $ bundle install
18
23
 
19
24
  Or install it yourself as:
20
25
 
21
26
  $ gem install trustly-client-ruby
22
27
 
28
+ If you use rails, you can use this generator in order to let trustly find your certificates:
29
+
30
+ $ rails g trustly:install
31
+
32
+ This will copy trustly public certificates under certs/trustly folder:
33
+
34
+ certs/trustly/test.trustly.public.pem
35
+ certs/trustly/live.trustly.public.pem
36
+
37
+ You will need to copy test and live private certificates using this naming convention (if you want Trustly to load them automatically but you can use different path and names):
38
+
39
+ certs/trustly/test.merchant.private.pem
40
+ certs/trustly/live.merchant.private.pem
41
+
23
42
  ## Usage
24
43
 
25
- TODO: Write usage instructions here
44
+ Currently only **Deposit** and **Refund** api calls. However, other calls can be implemented very easily.
45
+
46
+ ### Api
47
+
48
+ In order to use Trustly Api, we'll need to create a **Trustly::Api::Signed**. Example:
49
+
50
+ ```ruby
51
+ api = Trustly::Api::Signed.new({
52
+ :username=>"yourusername",
53
+ :password=>"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
54
+ })
55
+ ```
56
+
57
+ This will automatically load pem files from **certs/trustly** with default optons. If you want to specify other paths or options then you can call:
58
+
59
+ ```ruby
60
+ api = Trustly::Api::Signed.new({
61
+ :username=>"yourusername",
62
+ :password=>"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
63
+ :host => 'test.trustly.com',
64
+ :port => 443,
65
+ :is_https => true,
66
+ :private_pem => "#{Rails.root}/certs/trustly/test.merchant.private.pem",
67
+ :public_pem => "#{Rails.root}/certs/trustly/test.trustly.public.pem"
68
+ })
69
+ ```
70
+
71
+ ### Deposit call
72
+
73
+ Deposit is straightfoward call. Only required arguments example:
74
+
75
+ ```ruby
76
+ deposit = api.deposit({"EndUserID"=>10002,"MessageID"=>12349,"Amount"=>3})
77
+ ```
78
+ Optional arguments are:
79
+
80
+ - Locale: default value "es_ES"
81
+ - Country: default "ES"
82
+ - Currency default "EUR"
83
+ - SuggestedMinAmount
84
+ - SuggestedMaxAmount
85
+ - Amount
86
+ - Currency
87
+ - Country
88
+ - IP
89
+ - SuccessURL: default "https://www.trustly.com/success"
90
+ - FailURL : default "https://www.trustly.com/fail"
91
+ - TemplateURL
92
+ - URLTarget
93
+ - MobilePhone
94
+ - Firstname
95
+ - Lastname
96
+ - NationalIdentificationNumber
97
+ - ShopperStatement
98
+ - NotificationURL: default "https://test.trustly.com/demo/notifyd_test"
99
+
100
+ This will return a **Trustly::Data::JSONRPCResponse**:
101
+
102
+ ```ruby
103
+ > deposit.get_data('url')
104
+ => "https://test.trustly.com/_/orderclient.php?SessionID=755ea475-dcf1-476e-ac70-07913501b34e&OrderID=4257552724&Locale=es_ES"
105
+
106
+ > deposit.get_data()
107
+ => {
108
+ "orderid" => "4257552724",
109
+ "url" => "https://test.trustly.com/_/orderclient.php?SessionID=755ea475-dcf1-476e-ac70-07913501b34e&OrderID=4257552724&Locale=es_ES"
110
+ }
111
+ ```
112
+
113
+ You can check if there was an error:
114
+
115
+ ```ruby
116
+ > deposit.error?
117
+ => true
118
+
119
+ > deposit.success?
120
+ => false
121
+
122
+ > deposit.error_msg
123
+ => "ERROR_DUPLICATE_MESSAGE_ID"
124
+ ```
125
+
126
+ ### Refund call
127
+
26
128
 
27
- ## Development
28
129
 
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.
130
+ ### Notifications
131
+
132
+ After a **deposit** or **refund** call, Trustly will send a notification to **NotificationURL**. If you are using rails the execution flow will look like this:
133
+
134
+ ```ruby
135
+ def controller_action
136
+ api = Trustly::Api::Signed.new({..})
137
+ notification = Trustly::JSONRPCNotificationRequest.new(params)
138
+ if api.verify_trustly_signed_notification(notification)
139
+ # do something with notification
140
+ ...
141
+ # reply to trustly
142
+ response = api.notification_response(notification,true)
143
+ render :text => response.json()
144
+ else
145
+ render :nothing => true, :status => 200
146
+ end
147
+ end
148
+ ```
149
+
150
+ You can use **Trustly::JSONRPCNotificationRequest** object to access data provided using the following methods:
151
+
152
+ ```ruby
153
+ notification.get_data
154
+ => {"amount"=>"902.50", "currency"=>"EUR", "messageid"=>"98348932", "orderid"=>"87654567", "enduserid"=>"32123", "notificationid"=>"9876543456", "timestamp"=>"2010-01-20 14:42:04.675645+01", "attributes"=>{}}
155
+
156
+ > notification.get_method
157
+ => "credit"
158
+
159
+ > notification.get_uuid
160
+ => "258a2184-2842-b485-25ca-293525152425"
161
+
162
+ > notification.get_signature
163
+ => "R9+hjuMqbsH0Ku ... S16VbzRsw=="
164
+
165
+ > notification.get_data('amount')
166
+ => "902.50"
167
+ ```
168
+
30
169
 
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
170
 
33
171
  ## Contributing
34
172
 
35
- 1. Fork it ( https://github.com/[my-github-username]/trustly-client-ruby/fork )
173
+ 1. Fork it ( https://github.com/jcarreti/trustly-client-ruby/fork )
36
174
  2. Create your feature branch (`git checkout -b my-new-feature`)
37
175
  3. Commit your changes (`git commit -am 'Add some feature'`)
38
176
  4. Push to the branch (`git push origin my-new-feature`)
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+
3
+ module Trustly
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ desc 'Create cert folder and trustly pem files'
7
+ source_root ::File.expand_path('../templates', __FILE__)
8
+
9
+ def copy_files
10
+ copy_file "test.trustly.public.pem", "certs/trustly/test.trustly.public.pem"
11
+ copy_file "live.trustly.public.pem", "certs/trustly/live.trustly.public.pem"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ -----BEGIN PUBLIC KEY-----
2
+ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoZhnqiELeoX3QNSg7jpU
3
+ kbLV4BU32LoSMuABAaPdxhpZaccFYud2z4QUlMq/j46vdVDpaCFaCZ+qNT5+tHbQ
4
+ BFgcrx82u7r+aMHvKy4FEczT5aev0NxRlQKHmNQygvp3hNkqeOw4nJy3PoD4cgCp
5
+ SlLTiPOBy2ZsWUHQpSVJdDUiLwAQfNV90jMqa3zq1nTfmTBmd6NR1XAjg5eS6SWr
6
+ to1nVS1b7XKGv7Cc1kt0RVd54tWqoCMDHwEiU0st66BCKdYk3r5woDZxGZUUjVdm
7
+ g9O2xpqRRDcdJGm8HIOVHGSNT9R7LMucH/PGwrfpdWmBDjy0BkuDlssuBgh731l2
8
+ cwIDAQAB
9
+ -----END PUBLIC KEY-----
@@ -0,0 +1,9 @@
1
+ -----BEGIN PUBLIC KEY-----
2
+ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy7h/yX8DEA2m588SrWye
3
+ AC8rTMbErwHt2hoTiP9fte/iOo0FXIZSmNsNu422L+iJyvZQu19ebeL7XgB0UXqt
4
+ zA6KtXBMXIKwuMCZhbdeR8sb7OKbX2nlWM+e2Hmrr9CTfkZkFBeSC+iN9fAU6PoR
5
+ X0i5PWm0uZnaoWXcZnk5CxQCgnfYgsx7xsd8Au+mrqE8SHeT8zi/Inw0Xp6ba25G
6
+ YsZhHfIPD2rcZQOpWbmHRS4Jk4aGzSOBHbAZhKlP97PxoVfUcPI3iCA1+3jMs1l2
7
+ PYsHUbP60NMVwkGPjFOTv4m1a1wKsue0mhspDdvswZUeKE+POGOuewqTQJ+gIhXw
8
+ mQIDAQAB
9
+ -----END PUBLIC KEY-----
data/lib/trustly.rb CHANGED
@@ -13,8 +13,8 @@ require "trustly/data/request"
13
13
  require "trustly/data/response"
14
14
  require "trustly/data/jsonrpc_request"
15
15
  require "trustly/data/jsonrpc_response"
16
- require "trustly/data/jsonrpcnotificationrequest"
17
- require "trustly/data/jsonrpcnotificationrequest"
16
+ require "trustly/data/jsonrpcnotification_request"
17
+ require "trustly/data/jsonrpcnotification_response"
18
18
 
19
19
  require "trustly/api"
20
20
  require "trustly/api/signed"
data/lib/trustly/api.rb CHANGED
@@ -46,6 +46,14 @@ class Trustly::Api
46
46
  raise NotImplementedError
47
47
  end
48
48
 
49
+ def verify_trustly_signed_notification(response)
50
+ method = response.get_method()
51
+ uuid = response.get_uuid()
52
+ signature = response.get_signature()
53
+ data = response.get_data()
54
+ return self._verify_trustly_signed_data(method, uuid, signature, data)
55
+ end
56
+
49
57
  protected
50
58
 
51
59
  def _verify_trustly_signed_data(method, uuid, signature, data)
@@ -64,13 +72,6 @@ class Trustly::Api
64
72
  return self._verify_trustly_signed_data(method, uuid, signature, data)
65
73
  end
66
74
 
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
 
75
76
  def set_host(host=nil,port=nil,is_https=nil)
76
77
  self.api_host = host unless host.nil?
@@ -8,7 +8,7 @@ class Trustly::Api::Signed < Trustly::Api
8
8
  :host => 'test.trustly.com',
9
9
  :port => 443,
10
10
  :is_https => true,
11
- :private_pem => "#{Rails.root}/certs/trustly/test.acuerdalo.private.pem",
11
+ :private_pem => "#{Rails.root}/certs/trustly/test.merchant.private.pem",
12
12
  :public_pem => "#{Rails.root}/certs/trustly/test.trustly.public.pem"
13
13
  }.merge(_options)
14
14
 
@@ -110,6 +110,24 @@ class Trustly::Api::Signed < Trustly::Api
110
110
  #options["HoldNotifications"] = "1" unless
111
111
  end
112
112
 
113
+ def refund(_options)
114
+ options = {
115
+ "OrderID" => "EUR"
116
+ }.merge(_options)
117
+
118
+ # check for required options
119
+ ["OrderID","Amount","Currency"].each{|req_attr| raise Trustly::Exception::DataError, "Option not valid '#{req_attr}'" if options.try(:[],req_attr).nil? }
120
+
121
+ request = Trustly::Data::JSONRPCRequest.new('Deposit',options,nil)
122
+ return self.call_rpc(request)
123
+ end
124
+
125
+ def notification_response(notification,success=true)
126
+ response = Trustly::JSONRPCNotificationResponse.new(notification,success)
127
+ response.set_signature(self.sign_merchant_request(notification))
128
+ return notification
129
+ end
130
+
113
131
  def withdraw(_options)
114
132
 
115
133
  end
@@ -3,15 +3,19 @@ class Trustly::JSONRPCNotificationRequest < Trustly::Data
3
3
  attr_accessor :notification_body, :payload
4
4
 
5
5
  def initialize(notification_body)
6
- super
6
+ super()
7
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
8
+ unless self.notification_body.is_a?(Hash)
9
+ begin
10
+ self.payload = JSON.parse(self.notification_body)
11
+ rescue JSON::ParserError => e
12
+ raise Trustly::Exception::DataError, e.message
13
+ end
14
+
15
+ raise Trustly::Exception::JSONRPCVersionError, 'JSON RPC Version #{(self.get_version()} is not supported' if self.get_version() != '1.1'
16
+ else
17
+ self.payload = self.notification_body.deep_stringify_keys
12
18
  end
13
-
14
- raise Trustly::Exception::JSONRPCVersionError, 'JSON RPC Version #{(self.get_version()} is not supported' if self.get_version() != '1.1'
15
19
  end
16
20
 
17
21
  def get_version()
@@ -1,3 +1,3 @@
1
1
  module Trustly
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trustly-client-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Carretie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-30 00:00:00.000000000 Z
11
+ date: 2015-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -46,15 +46,17 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - LICENSE.txt
48
48
  - README.md
49
+ - lib/generators/trustly/install_generator.rb
50
+ - lib/generators/trustly/templates/live.trustly.public.pem
51
+ - lib/generators/trustly/templates/test.trustly.public.pem
49
52
  - lib/trustly.rb
50
- - lib/trustly.rb~
51
53
  - lib/trustly/api.rb
52
54
  - lib/trustly/api/signed.rb
53
55
  - lib/trustly/data.rb
54
56
  - lib/trustly/data/jsonrpc_request.rb
55
57
  - lib/trustly/data/jsonrpc_response.rb
56
- - lib/trustly/data/jsonrpcnotificationrequest.rb
57
- - lib/trustly/data/jsonrpcnotificationresponse.rb
58
+ - lib/trustly/data/jsonrpcnotification_request.rb
59
+ - lib/trustly/data/jsonrpcnotification_response.rb
58
60
  - lib/trustly/data/request.rb
59
61
  - lib/trustly/data/response.rb
60
62
  - lib/trustly/exception.rb
@@ -64,7 +66,6 @@ files:
64
66
  - lib/trustly/exception/jsonrpc_version_error.rb
65
67
  - lib/trustly/exception/signature_error.rb
66
68
  - lib/trustly/version.rb
67
- - lib/trustly/version.rb~
68
69
  homepage: https://github.com/jcarreti/trusty-client-ruby
69
70
  licenses:
70
71
  - MIT
data/lib/trustly.rb~ DELETED
@@ -1,19 +0,0 @@
1
- module Trustly
2
- end
3
-
4
- require "trustly/exception/authentification_error"
5
- require "trustly/exception/connection_error"
6
- require "trustly/exception/data_error"
7
- require "trustly/exception/jsonrpc_version_error"
8
- require "trustly/exception/signature_error"
9
-
10
- require "trustly/data"
11
- require "trustly/data/jsonrpc_request"
12
- require "trustly/data/jsonrpc_response"
13
- require "trustly/data/jsonrpcnotificationrequest"
14
- require "trustly/data/jsonrpcnotificationrequest"
15
-
16
- require "trustly/api"
17
- require "trustly/api/signed"
18
- require "trustly/version"
19
-
@@ -1,3 +0,0 @@
1
- module Trustly
2
- VERSION = "0.1.6"
3
- end