Skip to content

Commit e4694a3

Browse files
Merge pull request #176 from Adyen/automation/release
Release v7.3.0
2 parents 97759b0 + a48b934 commit e4694a3

File tree

7 files changed

+778
-57
lines changed

7 files changed

+778
-57
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ statusRequest =
215215
response = adyen.terminal_cloud_api.sync(statusRequest)
216216
```
217217

218+
## OAuth usage (for Partners)
219+
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:
220+
221+
~~~~ruby
222+
adyen = Adyen::Client.new
223+
224+
adyen.oauth_token = "oauth_token"
225+
226+
~~~~
227+
228+
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).
229+
218230
## Feedback
219231
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.
220232

lib/adyen/client.rb

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212

1313
module Adyen
1414
class Client
15-
attr_accessor :ws_user, :ws_password, :api_key, :client, :adapter
16-
attr_reader :env, :connection_options
15+
attr_accessor :ws_user, :ws_password, :api_key, :oauth_token, :client, :adapter
16+
attr_reader :env, :connection_options, :adapter_options
1717

18-
def initialize(ws_user: nil, ws_password: nil, api_key: nil, env: :live, adapter: nil, mock_port: 3001,
19-
live_url_prefix: nil, mock_service_url_base: nil, connection_options: nil)
18+
def initialize(ws_user: nil, ws_password: nil, api_key: nil, oauth_token: nil, env: :live, adapter: nil, mock_port: 3001,
19+
live_url_prefix: nil, mock_service_url_base: nil, connection_options: nil, adapter_options: nil)
2020
@ws_user = ws_user
2121
@ws_password = ws_password
2222
@api_key = api_key
23+
@oauth_token = oauth_token
2324
@env = env
2425
@adapter = adapter || Faraday.default_adapter
26+
@adapter_options = adapter_options || Faraday.default_adapter_options
2527
@mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
2628
@live_url_prefix = live_url_prefix
2729
@connection_options = connection_options || Faraday::ConnectionOptions.new
@@ -113,44 +115,16 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic
113115
# get URL for requested endpoint
114116
url = service_url(service, action.is_a?(String) ? action : action.fetch(:url), version)
115117

116-
# make sure right authentication has been provided
117-
# will use api_key if present, otherwise ws_user and ws_password
118-
if @api_key.nil?
119-
if service == 'PaymentSetupAndVerification'
120-
raise Adyen::AuthenticationError.new('Checkout service requires API-key', request_data),
121-
'Checkout service requires API-key'
122-
elsif @ws_password.nil? || @ws_user.nil?
123-
raise Adyen::AuthenticationError.new(
124-
'No authentication found - please set api_key or ws_user and ws_password',
125-
request_data
126-
),
127-
'No authentication found - please set api_key or ws_user and ws_password'
128-
else
129-
auth_type = 'basic'
130-
end
131-
else
132-
auth_type = 'api-key'
133-
end
118+
auth_type = auth_type(service, request_data)
134119

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

141-
# set auth type based on service
142-
case auth_type
143-
when 'basic'
144-
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
145-
# for faraday 2.0 and higher
146-
faraday.request :authorization, :basic, @ws_user, @ws_password
147-
else
148-
# for faraday 1.x
149-
faraday.basic_auth(@ws_user, @ws_password)
150-
end
151-
when 'api-key'
152-
faraday.headers['x-api-key'] = @api_key
153-
end
126+
# set header based on auth_type and service
127+
auth_header(auth_type, faraday)
154128

155129
# add optional headers if specified in request
156130
# will overwrite default headers if overlapping
@@ -298,6 +272,49 @@ def balance_control_service
298272
def terminal_cloud_api
299273
@terminal_cloud_api ||= Adyen::TerminalCloudAPI.new(self)
300274
end
275+
276+
private
277+
278+
def auth_header(auth_type, faraday)
279+
case auth_type
280+
when "basic"
281+
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
282+
# for faraday 2.0 and higher
283+
faraday.request :authorization, :basic, @ws_user, @ws_password
284+
else
285+
# for faraday 1.x
286+
faraday.basic_auth(@ws_user, @ws_password)
287+
end
288+
when "api-key"
289+
faraday.headers["x-api-key"] = @api_key
290+
when "oauth"
291+
faraday.headers["Authorization"] = "Bearer #{@oauth_token}"
292+
end
293+
end
294+
295+
def auth_type(service, request_data)
296+
# make sure valid authentication has been provided
297+
validate_auth_type(service, request_data)
298+
# Will prioritize authentication methods in this order:
299+
# api-key, oauth, basic
300+
return "api-key" unless @api_key.nil?
301+
return "oauth" unless @oauth_token.nil?
302+
"basic"
303+
end
304+
305+
def validate_auth_type(service, request_data)
306+
# ensure authentication has been provided
307+
if @api_key.nil? && @oauth_token.nil? && (@ws_password.nil? || @ws_user.nil?)
308+
raise Adyen::AuthenticationError.new(
309+
'No authentication found - please set api_key, oauth_token, or ws_user and ws_password',
310+
request_data
311+
)
312+
end
313+
if service == "PaymentSetupAndVerification" && @api_key.nil? && @oauth_token.nil? && @ws_password.nil? && @ws_user.nil?
314+
raise Adyen::AuthenticationError.new('Checkout service requires API-key or oauth_token', request_data),
315+
'Checkout service requires API-key or oauth_token'
316+
end
317+
end
301318
end
302319
end
303320
# rubocop:enable all

lib/adyen/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module Adyen
22
NAME = 'adyen-ruby-api-library'.freeze
3-
VERSION = '7.2.0'.freeze
3+
VERSION = '7.3.0'.freeze
44
end

0 commit comments

Comments
 (0)