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
81 changes: 31 additions & 50 deletions lib/adyen/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,17 @@ def service_url(service, action, version)
def call_adyen_api(service, action, request_data, headers, version, _with_application_info: false)
# get URL for requested endpoint
url = service_url(service, action.is_a?(String) ? action : action.fetch(:url), version)

auth_type = auth_type(service, request_data)

# get method from action or default to post
method = action.is_a?(String) ? 'post' : action.fetch(:method)

call_adyen_api_url(url, method, request_data, headers)
end

# send request to adyen API with a given full URL
def call_adyen_api_url(url, method, request_data, headers)
# make sure valid authentication has been provided, without a specific service
validate_auth(request_data)

# initialize Faraday connection object
conn = Faraday.new(url, @connection_options) do |faraday|
Expand All @@ -154,7 +163,7 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic
faraday.headers['User-Agent'] = "#{Adyen::NAME}/#{Adyen::VERSION}"

# set header based on auth_type and service
auth_header(auth_type, faraday)
add_auth_header(faraday)

# add optional headers if specified in request
# will overwrite default headers if overlapping
Expand All @@ -173,49 +182,27 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic
# convert to json
request_data = request_data.to_json

if action.is_a?(::Hash)
if action.fetch(:method) == 'get'
begin
response = conn.get
rescue Faraday::ConnectionFailed => e
raise e, "Connection to #{url} failed"
end
end
if action.fetch(:method) == 'delete'
begin
response = conn.delete
rescue Faraday::ConnectionFailed => e
raise e, "Connection to #{url} failed"
end
end
if action.fetch(:method) == 'patch'
begin
response = conn.patch do |req|
begin
response = case method
when 'get'
conn.get
when 'delete'
conn.delete
when 'patch'
conn.patch do |req|
req.body = request_data
end
rescue Faraday::ConnectionFailed => e
raise e, "Connection to #{url} failed"
end
end
if action.fetch(:method) == 'post'
# post request to Adyen
begin
response = conn.post do |req|
when 'post'
conn.post do |req|
req.body = request_data
end
rescue Faraday::ConnectionFailed => e
raise e, "Connection to #{url} failed"
end
end
else
begin
response = conn.post do |req|
req.body = request_data
else
raise ArgumentError, "Invalid HTTP method: #{method}"
end
rescue Faraday::ConnectionFailed => e
raise e, "Connection to #{url} failed"
end
rescue Faraday::ConnectionFailed => e
raise e, "Connection to #{url} failed"
end

# check for API errors
case response.status
when 400
Expand Down Expand Up @@ -326,10 +313,10 @@ def balance_control
@balance_control ||= Adyen::BalanceControl.new(self)
end


private

def auth_header(auth_type, faraday)
def add_auth_header(faraday)
case auth_type
when "basic"
if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
Expand All @@ -346,28 +333,22 @@ def auth_header(auth_type, faraday)
end
end

def auth_type(service, request_data)
# make sure valid authentication has been provided
validate_auth_type(service, request_data)
def auth_type
# 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)
def validate_auth(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

# build the error message from the response payload
Expand Down
Loading