|
12 | 12 |
|
13 | 13 | module Adyen |
14 | 14 | 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 |
17 | 17 |
|
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) |
20 | 20 | @ws_user = ws_user |
21 | 21 | @ws_password = ws_password |
22 | 22 | @api_key = api_key |
| 23 | + @oauth_token = oauth_token |
23 | 24 | @env = env |
24 | 25 | @adapter = adapter || Faraday.default_adapter |
| 26 | + @adapter_options = adapter_options || Faraday.default_adapter_options |
25 | 27 | @mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}" |
26 | 28 | @live_url_prefix = live_url_prefix |
27 | 29 | @connection_options = connection_options || Faraday::ConnectionOptions.new |
@@ -113,44 +115,16 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic |
113 | 115 | # get URL for requested endpoint |
114 | 116 | url = service_url(service, action.is_a?(String) ? action : action.fetch(:url), version) |
115 | 117 |
|
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) |
134 | 119 |
|
135 | 120 | # initialize Faraday connection object |
136 | 121 | conn = Faraday.new(url, @connection_options) do |faraday| |
137 | | - faraday.adapter @adapter |
| 122 | + faraday.adapter @adapter, **@adapter_options |
138 | 123 | faraday.headers['Content-Type'] = 'application/json' |
139 | 124 | faraday.headers['User-Agent'] = "#{Adyen::NAME}/#{Adyen::VERSION}" |
140 | 125 |
|
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) |
154 | 128 |
|
155 | 129 | # add optional headers if specified in request |
156 | 130 | # will overwrite default headers if overlapping |
@@ -298,6 +272,49 @@ def balance_control_service |
298 | 272 | def terminal_cloud_api |
299 | 273 | @terminal_cloud_api ||= Adyen::TerminalCloudAPI.new(self) |
300 | 274 | 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 |
301 | 318 | end |
302 | 319 | end |
303 | 320 | # rubocop:enable all |
0 commit comments