Skip to content

Commit cf00d4b

Browse files
authored
Add applicationInfo only for requests which supports it (#78)
1 parent 29537e1 commit cf00d4b

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed

lib/adyen/client.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def service_url(service, action, version)
8080
end
8181

8282
# send request to adyen API
83-
def call_adyen_api(service, action, request_data, headers, version)
83+
def call_adyen_api(service, action, request_data, headers, version, with_application_info = false)
8484
# get URL for requested endpoint
8585
url = service_url(service, action, version)
8686

@@ -125,7 +125,7 @@ def call_adyen_api(service, action, request_data, headers, version)
125125
end
126126

127127
# add application only on checkout service
128-
if service == 'Checkout' || service == 'CheckoutUtility'
128+
if with_application_info
129129
add_application_info(request_data)
130130
end
131131

@@ -157,16 +157,15 @@ def call_adyen_api(service, action, request_data, headers, version)
157157
# add application_info for analytics
158158
def add_application_info(request_data)
159159
adyenLibrary = {
160-
:name => Adyen::NAME,
161-
:version => Adyen::VERSION.to_s
160+
:name => Adyen::NAME,
161+
:version => Adyen::VERSION.to_s,
162162
}
163163

164164
if request_data[:applicationInfo].nil?
165-
request_data[:applicationInfo] = {};
165+
request_data[:applicationInfo] = {}
166166
end
167167

168168
request_data[:applicationInfo][:adyenLibrary] = adyenLibrary
169-
request_data[:applicationInfo][:adyenLibraryTest] = adyenLibrary
170169
end
171170

172171
# services

lib/adyen/services/checkout.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
require_relative 'service'
1+
require_relative "service"
22

33
module Adyen
44
class Checkout < Service
55
DEFAULT_VERSION = 50
66

77
def initialize(client, version = DEFAULT_VERSION)
8-
service = 'Checkout'
8+
service = "Checkout"
99
method_names = [
1010
:payment_methods,
1111
:payment_session,
12-
:payment_links
12+
:payment_links,
13+
]
14+
with_application_info = [
15+
:payment_session,
16+
:payment_links,
1317
]
1418

15-
super(client, version, service, method_names)
19+
super(client, version, service, method_names, with_application_info)
1620
end
1721

1822
# This method can't be dynamically defined because
@@ -25,16 +29,16 @@ def payments(*args)
2529
when 0
2630
Adyen::CheckoutDetail.new(@client, @version)
2731
else
28-
action = 'payments'
32+
action = "payments"
2933
args[1] ||= {} # optional headers arg
30-
@client.call_adyen_api(@service, action, args[0], args[1], @version)
34+
@client.call_adyen_api(@service, action, args[0], args[1], @version, true)
3135
end
3236
end
3337
end
3438

3539
class CheckoutDetail < Service
3640
def initialize(client, version = DEFAULT_VERSION)
37-
@service = 'Checkout'
41+
@service = "Checkout"
3842
@client = client
3943
@version = version
4044
end

lib/adyen/services/payments.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
require_relative 'service'
1+
require_relative "service"
22

33
module Adyen
44
class Payments < Service
55
attr_accessor :version
66
DEFAULT_VERSION = 50
77

88
def initialize(client, version = DEFAULT_VERSION)
9-
service = 'Payment'
9+
service = "Payment"
1010
method_names = [
1111
:authorise,
1212
:authorise3d,
@@ -16,10 +16,15 @@ def initialize(client, version = DEFAULT_VERSION)
1616
:refund,
1717
:cancel_or_refund,
1818
:adjust_authorisation,
19-
:donate
19+
:donate,
20+
]
21+
with_application_info = [
22+
:authorise,
23+
:authorise3d,
24+
:authorise3ds2,
2025
]
2126

22-
super(client, version, service, method_names)
27+
super(client, version, service, method_names, with_application_info)
2328
end
2429
end
2530
end

lib/adyen/services/service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def self.action_for_method_name(method_name)
1111
method_name.to_s.gsub(/_./) { |x| x[1].upcase }
1212
end
1313

14-
def initialize(client, version, service, method_names)
14+
def initialize(client, version, service, method_names, with_application_info = [])
1515
@client = client
1616
@version = version
1717
@service = service
@@ -20,7 +20,7 @@ def initialize(client, version, service, method_names)
2020
method_names.each do |method_name|
2121
define_singleton_method method_name do |request, headers = {}|
2222
action = self.class.action_for_method_name(method_name)
23-
@client.call_adyen_api(@service, action, request, headers, @version)
23+
@client.call_adyen_api(@service, action, request, headers, @version, with_application_info.include?(method_name))
2424
end
2525
end
2626
end

spec/spec_helper.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@ def create_test(client, service, method_name, parent_object)
1818
request_body = JSON.parse(json_from_file("mocks/requests/#{service}/#{method_name}.json"))
1919
response_body = json_from_file("mocks/responses/#{service}/#{method_name}.json")
2020

21-
22-
if service == 'Checkout' || service == 'CheckoutUtility'
21+
with_application_info = [
22+
"authorise",
23+
"authorise3d",
24+
"authorise3ds2",
25+
"payments",
26+
"payment_session",
27+
"payment_links",
28+
]
29+
if with_application_info.include?(method_name)
2330
client.add_application_info(request_body)
2431
end
2532

2633
# client-generated headers
2734
headers = {
28-
'Content-Type'.to_sym => 'application/json'
35+
"Content-Type".to_sym => "application/json",
2936
}
3037

3138
# authentication headers
32-
if not client.api_key.nil? then
39+
if not client.api_key.nil?
3340
headers["x-api-key"] = client.api_key
34-
elsif not client.ws_user.nil? and not client.ws_password.nil? then
41+
elsif not client.ws_user.nil? and not client.ws_password.nil?
3542
auth_header = "Basic " + Base64::encode64("#{client.ws_user}:#{client.ws_password}")
3643
headers["Authorization"] = auth_header.strip
3744
else
@@ -43,12 +50,12 @@ def create_test(client, service, method_name, parent_object)
4350
url = client.service_url(service, action, parent_object.version)
4451
WebMock.stub_request(:post, url).
4552
with(
46-
body: request_body,
47-
headers: headers
48-
).
53+
body: request_body,
54+
headers: headers,
55+
).
4956
to_return(
50-
body: response_body
51-
)
57+
body: response_body,
58+
)
5259
result = parent_object.public_send(method_name, request_body)
5360

5461
# result.response is already a Ruby object (Adyen::HashWithAccessors) (rather than an unparsed JSON string)
@@ -86,10 +93,10 @@ def create_client(auth_type)
8693
client = Adyen::Client.new
8794
client.env = :mock
8895

89-
if auth_type == :basic then
96+
if auth_type == :basic
9097
client.ws_user = "user"
9198
client.ws_password = "password"
92-
elsif auth_type == :api_key then
99+
elsif auth_type == :api_key
93100
client.api_key = "api_key"
94101
else
95102
raise ArgumentError "Invalid auth type for test client"

0 commit comments

Comments
 (0)