Skip to content

Commit

Permalink
Merge pull request Adyen#176 from Adyen/automation/release
Browse files Browse the repository at this point in the history
Release v7.3.0
  • Loading branch information
AdyenAutomationBot authored Sep 25, 2023
2 parents 97759b0 + a48b934 commit e4694a3
Show file tree
Hide file tree
Showing 7 changed files with 778 additions and 57 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ statusRequest =
response = adyen.terminal_cloud_api.sync(statusRequest)
```

## OAuth usage (for Partners)
If you are using our OAuth service to make API requests on your customer's behalf, and you already got your Access Token as explained in the [OAuth Integration Guide](https://docs.adyen.com/development-resources/oauth/integration/#page-introduction), you can setup your Client like in the following example:

~~~~ruby
adyen = Adyen::Client.new

adyen.oauth_token = "oauth_token"

~~~~

The APIs available to use through OAuth in this library depend on the [OAuth Scopes](https://docs.adyen.com/development-resources/oauth/scopes/) defined when [Registering your OAuth client](https://docs.adyen.com/development-resources/oauth/integration/#step-1-register-your-client).

## Feedback
We value your input! Help us enhance our API Libraries and improve the integration experience by providing your feedback. Please take a moment to fill out [our feedback form](https://forms.gle/A4EERrR6CWgKWe5r9) to share your thoughts, suggestions or ideas.

Expand Down
89 changes: 53 additions & 36 deletions lib/adyen/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@

module Adyen
class Client
attr_accessor :ws_user, :ws_password, :api_key, :client, :adapter
attr_reader :env, :connection_options
attr_accessor :ws_user, :ws_password, :api_key, :oauth_token, :client, :adapter
attr_reader :env, :connection_options, :adapter_options

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, connection_options: nil)
def initialize(ws_user: nil, ws_password: nil, api_key: nil, oauth_token: nil, env: :live, adapter: nil, mock_port: 3001,
live_url_prefix: nil, mock_service_url_base: nil, connection_options: nil, adapter_options: nil)
@ws_user = ws_user
@ws_password = ws_password
@api_key = api_key
@oauth_token = oauth_token
@env = env
@adapter = adapter || Faraday.default_adapter
@adapter_options = adapter_options || Faraday.default_adapter_options
@mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
@live_url_prefix = live_url_prefix
@connection_options = connection_options || Faraday::ConnectionOptions.new
Expand Down Expand Up @@ -113,44 +115,16 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic
# get URL for requested endpoint
url = service_url(service, action.is_a?(String) ? action : action.fetch(:url), version)

# make sure right authentication has been provided
# will use api_key if present, otherwise ws_user and ws_password
if @api_key.nil?
if service == 'PaymentSetupAndVerification'
raise Adyen::AuthenticationError.new('Checkout service requires API-key', request_data),
'Checkout service requires API-key'
elsif @ws_password.nil? || @ws_user.nil?
raise Adyen::AuthenticationError.new(
'No authentication found - please set api_key or ws_user and ws_password',
request_data
),
'No authentication found - please set api_key or ws_user and ws_password'
else
auth_type = 'basic'
end
else
auth_type = 'api-key'
end
auth_type = auth_type(service, request_data)

# initialize Faraday connection object
conn = Faraday.new(url, @connection_options) do |faraday|
faraday.adapter @adapter
faraday.adapter @adapter, **@adapter_options
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['User-Agent'] = "#{Adyen::NAME}/#{Adyen::VERSION}"

# set auth type based on service
case auth_type
when 'basic'
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
# for faraday 2.0 and higher
faraday.request :authorization, :basic, @ws_user, @ws_password
else
# for faraday 1.x
faraday.basic_auth(@ws_user, @ws_password)
end
when 'api-key'
faraday.headers['x-api-key'] = @api_key
end
# set header based on auth_type and service
auth_header(auth_type, faraday)

# add optional headers if specified in request
# will overwrite default headers if overlapping
Expand Down Expand Up @@ -298,6 +272,49 @@ def balance_control_service
def terminal_cloud_api
@terminal_cloud_api ||= Adyen::TerminalCloudAPI.new(self)
end

private

def auth_header(auth_type, faraday)
case auth_type
when "basic"
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
# for faraday 2.0 and higher
faraday.request :authorization, :basic, @ws_user, @ws_password
else
# for faraday 1.x
faraday.basic_auth(@ws_user, @ws_password)
end
when "api-key"
faraday.headers["x-api-key"] = @api_key
when "oauth"
faraday.headers["Authorization"] = "Bearer #{@oauth_token}"
end
end

def auth_type(service, request_data)
# make sure valid authentication has been provided
validate_auth_type(service, request_data)
# Will prioritize authentication methods in this order:
# api-key, oauth, basic
return "api-key" unless @api_key.nil?
return "oauth" unless @oauth_token.nil?
"basic"
end

def validate_auth_type(service, request_data)
# ensure authentication has been provided
if @api_key.nil? && @oauth_token.nil? && (@ws_password.nil? || @ws_user.nil?)
raise Adyen::AuthenticationError.new(
'No authentication found - please set api_key, oauth_token, or ws_user and ws_password',
request_data
)
end
if service == "PaymentSetupAndVerification" && @api_key.nil? && @oauth_token.nil? && @ws_password.nil? && @ws_user.nil?
raise Adyen::AuthenticationError.new('Checkout service requires API-key or oauth_token', request_data),
'Checkout service requires API-key or oauth_token'
end
end
end
end
# rubocop:enable all
2 changes: 1 addition & 1 deletion lib/adyen/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Adyen
NAME = 'adyen-ruby-api-library'.freeze
VERSION = '7.2.0'.freeze
VERSION = '7.3.0'.freeze
end
Loading

0 comments on commit e4694a3

Please sign in to comment.