Skip to content

Commit

Permalink
4.0.0 - Add AdyenResponse class
Browse files Browse the repository at this point in the history
4.0.0 - Add AdyenResponse class

*Note this is a breaking change from 3.x.x
  • Loading branch information
Colin Rood authored Nov 12, 2019
2 parents e832000 + 7a1f2cf commit 2beb3d7
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 31 deletions.
20 changes: 20 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Contribution guidelines

## How to contribute step-by-step

1. Fork the `Adyen/adyen-ruby-api-library` repository.
2. Create a new branch from `develop` in your fork. This makes it easier for you to keep track of your changes.
3. Make the desired changes to the code.
* If you are adding new functionality or fixing a bug, we recommend you add unit tests that cover it.
4. Push the changes to your fork.
5. Create a pull request to the `Adyen/adyen-ruby-api-library` repository.
6. In your pull request, please describe in detail:
* What problem you’re solving
* Your approach to fixing the problem
* Any tests you wrote
7. Check Allow edits from maintainers.
8. Create the pull request.
9. Ensure that all checks have passed.

After you create your pull request, one of the code owners will review your code.
We aim to review your request within 2-3 business days.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ response = adyen.checkout.payments({

### Change API Version
```ruby
adyen.checkout.version = 49
adyen.checkout.version = 50
```

## List of supported methods
Expand Down Expand Up @@ -139,7 +139,15 @@ adyen.checkout.version = 49
## Support

If you have any problems, questions or suggestions, create an issue here or send your inquiry to [email protected].


## Contributing
We strongly encourage you to join us in contributing to this repository so everyone can benefit from:
* New features and functionality
* Resolved bug fixes and issues
* Any general improvements

Read our [**contribution guidelines**](CONTRIBUTING.md) to find out how.

## Licence

MIT license. For more information, see the LICENSE file.
14 changes: 8 additions & 6 deletions lib/adyen/client.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require "faraday"
require "json"
require_relative "./errors"
require_relative "./result"
require_relative "./util"

module Adyen
class Client
attr_accessor :ws_user, :ws_password, :api_key, :client, :adapter, :live_url_prefix
attr_reader :env

def initialize(ws_user: nil, ws_password: nil, api_key: nil, env: :live, adapter: nil, mock_port: 3001, live_url_prefix: nil)
def initialize(ws_user: nil, ws_password: nil, api_key: nil, env: :live, adapter: nil, mock_port: 3001, live_url_prefix: nil, mock_service_url_base: nil)
@ws_user = ws_user
@ws_password = ws_password
@api_key = api_key
@env = env
@adapter = adapter || Faraday.default_adapter
@mock_port = mock_port
@mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
@live_url_prefix = live_url_prefix
end

Expand All @@ -35,7 +37,7 @@ def live_url_prefix=(value)
def service_url_base(service)
raise ArgumentError, "Please set Client.live_url_prefix to the portion of your merchant-specific URL prior to '-[service]-live.adyenpayments.com'" if @live_url_prefix.nil? and @env == :live
if @env == :mock
"http://localhost:#{@mock_port}"
@mock_service_url_base
else
case service
when "Checkout"
Expand Down Expand Up @@ -111,8 +113,6 @@ def call_adyen_api(service, action, request_data, headers, version)
faraday.headers[key] = value
end
end


# if json string convert to hash
# needed to add applicationInfo
if request_data.is_a?(String)
Expand Down Expand Up @@ -144,7 +144,9 @@ def call_adyen_api(service, action, request_data, headers, version)
raise Adyen::PermissionError.new("Missing user permissions; https://docs.adyen.com/user-management/user-roles", request_data)
end

response
formatted_response = AdyenResult.new(response.body, response.headers, response.status)

formatted_response
end

# add application_info for analytics
Expand Down
16 changes: 16 additions & 0 deletions lib/adyen/result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'json'

module Adyen
class AdyenResult
attr_reader :response, :header, :status

def initialize(response, header, status)
@response = JSON.parse(response)

# `header` in Faraday response is not a JSON string, but rather a
# Faraday `Headers` object. Convert first before parsing
@header = JSON.parse(header.to_json)
@status = status
end
end
end
2 changes: 1 addition & 1 deletion lib/adyen/services/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Adyen
class Checkout < Service
DEFAULT_VERSION = 49
DEFAULT_VERSION = 50

def initialize(client, version = DEFAULT_VERSION)
service = 'Checkout'
Expand Down
2 changes: 1 addition & 1 deletion lib/adyen/services/payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Adyen
class Payments < Service
attr_accessor :version
DEFAULT_VERSION = 49
DEFAULT_VERSION = 50

def initialize(client, version = DEFAULT_VERSION)
service = 'Payment'
Expand Down
21 changes: 21 additions & 0 deletions lib/adyen/util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This utility method monkey-patches Ruby's Hash class to allow keys to be read
# and updated with dot notation. Usage is entirely optional (i.e., hash values
# can still be accessed via symbol and string keys).
#
# Credit: https://gist.github.com/winfred/2185384#file-ruby-dot-hash-access-rb

class Hash
class NoKeyOrMethodError < NoMethodError; end
def method_missing(method,*args)
m = method.to_s
string_key = m.gsub(/=$/,'')
sym_key = string_key.to_sym
if self.has_key? string_key
m.match(/=$/) ? self.send("[#{string_key}]=", *args) : self[string_key]
elsif self.has_key? sym_key
m.match(/=$/) ? self.send("[#{sym_key}]=", *args) : self[sym_key]
else
raise NoKeyOrMethodError
end
end
end
4 changes: 2 additions & 2 deletions lib/adyen/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Adyen
module Adyen
NAME = "adyen-ruby-api-library"
VERSION = "3.0.2".freeze
VERSION = "4.0.0".freeze
end
29 changes: 17 additions & 12 deletions spec/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,24 @@
to_return(
body: response_body
)
response = @shared_values[:client].checkout.payments.details(request_body)

result = @shared_values[:client].checkout.payments.details(request_body)
# result.response is already a Ruby hash (rather than an unparsed JSON string)
response_hash = result.response

expect(request_body[:applicationInfo][:adyenLibrary][:name]).
to eq(Adyen::NAME)
expect(request_body[:applicationInfo][:adyenLibrary][:version]).
to eq(Adyen::VERSION)
expect(request_body[:applicationInfo][:adyenPaymentSource][:name]).
to eq("adyen-test")
expect(response.status).
expect(result.status).
to eq(200)
expect(response.body).
to eq(response_body)
expect((parsed_body = JSON.parse(response.body)).class).
expect(response_hash).
to eq(JSON.parse(response_body))
expect(response_hash.class).
to be Hash
expect(parsed_body["resultCode"]).
expect(response_hash["resultCode"]).
to eq("RedirectShopper")
end

Expand All @@ -78,15 +81,17 @@
to_return(
body: response_body
)
response = @shared_values[:client].checkout.payments.result(request_body)

expect(response.status).
result = @shared_values[:client].checkout.payments.result(request_body)
response_hash = result.response

expect(result.status).
to eq(200)
expect(response.body).
to eq(response_body)
expect((parsed_body = JSON.parse(response.body)).class).
expect(response_hash).
to eq(JSON.parse(response_body))
expect(response_hash.class).
to be Hash
expect(parsed_body["resultCode"]).
expect(response_hash["resultCode"]).
to eq("Authorised")
end

Expand Down
18 changes: 18 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
@shared_values[:client].api_key = "api_key"
end

it "uses the specified mock service URL" do
client = Adyen::Client.new(env: :mock, mock_service_url_base: "https://mock.test")
expect(client.service_url_base("Account")).
to eq("https://mock.test")
end

it "generates localhost service URL when a mock port is specified" do
client = Adyen::Client.new(env: :mock, mock_port: 3005)
expect(client.service_url_base("Account")).
to eq("http://localhost:3005")
end

it "prefers the mock service URL when both mock service URL and port are specified" do
client = Adyen::Client.new(env: :mock, mock_port: 3005, mock_service_url_base: "https://this-url-wins.test")
expect(client.service_url_base("Account")).
to eq("https://this-url-wins.test")
end

it "generates the correct service URL base for CAL TEST" do
client = Adyen::Client.new(env: :test)
client.live_url_prefix = "abcdef1234567890-TestCompany"
Expand Down
16 changes: 9 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ def create_test(client, service, method_name, parent_object)
to_return(
body: response_body
)
response = parent_object.public_send(method_name, request_body)
result = parent_object.public_send(method_name, request_body)

# result.response is already a Ruby hash (rather than an unparsed JSON string)
response_hash = result.response

# boilerplate error checks
expect(response.status).
expect(result.status).
to eq(200)
expect(response.body).
to eq(response_body)
expect((parsed_body = JSON.parse(response.body)).class).
expect(response_hash).
to eq(JSON.parse(response_body))
expect(response_hash.class).
to be Hash

# return hash of response
parsed_body
response_hash
end

# creates tests from an array of arrays
Expand Down

0 comments on commit 2beb3d7

Please sign in to comment.