Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/tmp
.DS_Store
/.idea/
/.idea/
/coverage/
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
AllCops:
TargetRubyVersion: 2.7
NewCops: enable
SuggestExtensions: false
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Layout/DotPosition:
EnforcedStyle: trailing
Style/Documentation:
Enabled: false
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
# frozen_string_literal: true

# Specify your gem's dependencies in trustly-client-ruby.gemspec
source 'https://rubygems.org/'
gemspec
110 changes: 95 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,113 @@ PATH
remote: .
specs:
trustly-client-ruby (0.1.95)
faraday
faraday_middleware
rake

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rake (11.2.2)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.2)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
crack (0.4.5)
rexml
debug (1.6.1)
irb (>= 1.3.6)
reline (>= 0.3.1)
diff-lcs (1.5.0)
docile (1.4.0)
faraday (1.10.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
faraday-httpclient (~> 1.0)
faraday-multipart (~> 1.0)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.0)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
faraday-retry (~> 1.0)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)
faraday (~> 1.0)
hashdiff (1.0.1)
io-console (0.5.11)
irb (1.4.1)
reline (>= 0.3.0)
json (2.6.2)
multipart-post (2.2.3)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
public_suffix (4.0.7)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.5.0)
reline (0.3.1)
io-console (~> 0.5)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.32.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.19.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.19.1)
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
unicode-display_width (2.2.0)
webmock (3.15.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby

DEPENDENCIES
rspec (>= 2.0.0)
debug
rspec
rubocop
simplecov
trustly-client-ruby!
webmock

BUNDLED WITH
1.10.6
2.3.7
135 changes: 70 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Trustly-client-ruby

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)
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 implements the standard Payments API as well as gives stubs for executing calls against the API used by the backoffice.

Expand All @@ -14,7 +14,7 @@ This code is provided as-is, use it as inspiration, reference or drop it directl
Add this line to your application's Gemfile:

```ruby
gem 'trustly-client-ruby',:require=>'trustly'
gem 'trustly-client-ruby', require: 'trustly'
```

And then execute:
Expand All @@ -41,72 +41,89 @@ You will need to copy test and live private certificates using this naming conve

## Usage

Currently only **Deposit** and **Refund** api calls. However, other calls can be implemented very easily.
Currently supports **Deposit**, **Refund**, **AccountPayout**, **RegisterAccount** and **SelectAccount** api calls. Other calls can be implemented very easily.

### Api

In order to use Trustly Api, we'll need to create a **Trustly::Api::Signed**. Example:

```ruby
api = Trustly::Api::Signed.new({
:username=>"yourusername",
:password=>"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
})
api = Trustly::Api::Signed.new(
username: 'yourusername',
password: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
)
```

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:
Also make sure you have ENV variables for certificates. Default variables are: MERCHANT_PRIVATE_KEY for the signing key and TRUSTLY_PUBLIC_KEY for the verifying key.

```ruby
api = Trustly::Api::Signed.new({
:username=>"yourusername",
:password=>"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
: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"
username: 'yourusername',
password: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
host: 'test.trustly.com',
port: 443,
is_https: true,
private_pem: ENV.fetch('MY_PRIVATE_KEY_VAR', nil),
public_pem: ENV.fetch('TRUSTLY_PUBLIC_KEY_VAR', nil)
})
```

## Examples of RPC calls
### Deposit call

Deposit is straightfoward call. Only required arguments example:
Deposit is a straightfoward call. Only required arguments example:

```ruby
deposit = api.deposit({"EndUserID"=>10002,"MessageID"=>12349,"Amount"=>3})
deposit = api.deposit(
'EndUserID' => 10002,
'MessageID' => 12349,
'Amount' => '30.0',
'ShopperStatement' => 'MyBrand.com',
'Locale' => 'es_ES',
'Country' => 'ES',
'Currency' => 'EUR',
'SuccessURL' => 'https://my-brand.com/thank_you.html',
'FailURL' => 'https://my-brand.com/failure.html',
'NotificationURL' => 'https://gateway.my-brand.com/notifications',
'Firstname' => 'John',
'Lastname' => 'Doe'
)
```
Optional arguments are:

- Locale: default value "es_ES"
- Country: default "ES"
- Currency default "EUR"
- AccountID
- SuggestedMinAmount
- SuggestedMaxAmount
- Amount
- Currency
- Country
- IP
- SuccessURL: default "https://www.trustly.com/success"
- FailURL : default "https://www.trustly.com/fail"
- TemplateURL
- URLTarget
- MobilePhone
- Firstname
- Lastname
- Email
- NationalIdentificationNumber
- ShopperStatement
- NotificationURL: default "https://test.trustly.com/demo/notifyd_test"
- UnchangeableNationalIdentificationNumber
- ShippingAddressCountry
- ShippingAddressPostalCode
- ShippingAddressLine1
- ShippingAddressLine2
- ShippingAddress
- RequestDirectDebitMandate
- ChargeAccountID
- QuickDeposit
- URLScheme
- ExternalReference
- PSPMerchant
- PSPMerchantURL
- MerchantCategoryCode
- RecipientInformation

This will return a **Trustly::Data::JSONRPCResponse**:

```ruby
> deposit.get_data('url')
> deposit.data_at('url')
=> "https://test.trustly.com/_/orderclient.php?SessionID=755ea475-dcf1-476e-ac70-07913501b34e&OrderID=4257552724&Locale=es_ES"

> deposit.get_data()
> deposit.data
=> {
"orderid" => "4257552724",
"url" => "https://test.trustly.com/_/orderclient.php?SessionID=755ea475-dcf1-476e-ac70-07913501b34e&OrderID=4257552724&Locale=es_ES"
'orderid' => '4257552724',
'url' => 'https://test.trustly.com/_/orderclient.php?SessionID=755ea475-dcf1-476e-ac70-07913501b34e&OrderID=4257552724&Locale=es_ES'
}
```

Expand All @@ -119,69 +136,57 @@ You can check if there was an error:
> deposit.success?
=> false

> deposit.error_msg
=> "ERROR_DUPLICATE_MESSAGE_ID"
> deposit.error_message
=> 'ERROR_DUPLICATE_MESSAGE_ID'
```

### Refund call

Required parameters:

- OrderID
- Amount
- Currency / default to "EUR"

Example:

```ruby
> api.refund({"OrderID"=>2205700591,"Amount"=>3,"Currency"=>"EUR"})
```


### Notifications

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:

```ruby
def controller_action
api = Trustly::Api::Signed.new({..})
notification = Trustly::JSONRPCNotificationRequest.new(params)
api = Trustly::Api::Signed.new({...})
notification = Trustly::Data::JSONRPCNotificationRequest.new(notification_body: params)
if api.verify_trustly_signed_notification(notification)
# do something with notification
# do something with the notification
...
# reply to trustly
response = api.notification_response(notification,true)
render :text => response.json()
response = api.notification_response(notification, success: true)
render text: response.to_json
else
render :nothing => true, :status => 200
render nothing: true, status: 200
end
end
```

You can use **Trustly::JSONRPCNotificationRequest** object to access data provided using the following methods:
You can use **Trustly::Data::JSONRPCNotificationRequest** object to access data provided using the following methods:

```ruby
notification.get_data
notification.data
=> {"amount"=>"902.50", "currency"=>"EUR", "messageid"=>"98348932", "orderid"=>"87654567", "enduserid"=>"32123", "notificationid"=>"9876543456", "timestamp"=>"2010-01-20 14:42:04.675645+01", "attributes"=>{}}

> notification.get_method
> notification.method
=> "credit"

> notification.get_uuid
> notification.uuid
=> "258a2184-2842-b485-25ca-293525152425"

> notification.get_signature
> notification.signature
=> "R9+hjuMqbsH0Ku ... S16VbzRsw=="

> notification.get_data('amount')
> notification.data_at('amount')
=> "902.50"

> notification.attribute_at('key')
=> nil
```



## Contributing

1. Fork it ( https://github.com/jcarreti/trustly-client-ruby/fork )
1. Fork it ( https://github.com/gapfish/trusty-client-ruby/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'

task :console do
exec "irb -r trustly -r 'active_support/core_ext/object/try' -r 'active_support/core_ext/hash/keys' -r 'JSON' -I ./lib"
exec 'irb -r trustly -I ./lib'
end
Loading