Class: Adyen::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/adyen/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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, terminal_region: nil, application_name: nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/adyen/client.rb', line 18

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, terminal_region: nil,
               application_name: nil)
  @ws_user = ws_user
  @ws_password = ws_password
  @api_key = api_key
  @oauth_token = oauth_token
  self.env = env
  @application_name = application_name
  @adapter = adapter || Faraday.default_adapter
  if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.1')
    # for faraday 2.1 and higher
    @adapter_options = adapter_options || Faraday.default_adapter_options
  else
    # for faraday 1.x and 2.0
    @adapter_options = adapter_options || {}
  end
  @mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
  self.live_url_prefix = live_url_prefix
  if RUBY_VERSION >= '3.2'
    # set default timeouts
    @connection_options = connection_options || Faraday::ConnectionOptions.new(
      request: Faraday::RequestOptions.new(
        open_timeout: 30,
        timeout: 60
      )
    )
  else
    @connection_options = connection_options || Faraday::ConnectionOptions.new
  end

  @terminal_region = terminal_region
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



15
16
17
# File 'lib/adyen/client.rb', line 15

def adapter
  @adapter
end

#adapter_optionsObject (readonly)

Returns the value of attribute adapter_options.



16
17
18
# File 'lib/adyen/client.rb', line 16

def adapter_options
  @adapter_options
end

#api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/adyen/client.rb', line 15

def api_key
  @api_key
end

#application_nameObject

Returns the value of attribute application_name.



15
16
17
# File 'lib/adyen/client.rb', line 15

def application_name
  @application_name
end

#clientObject

Returns the value of attribute client.



15
16
17
# File 'lib/adyen/client.rb', line 15

def client
  @client
end

#connection_optionsObject (readonly)

Returns the value of attribute connection_options.



16
17
18
# File 'lib/adyen/client.rb', line 16

def connection_options
  @connection_options
end

#envObject

Returns the value of attribute env.



16
17
18
# File 'lib/adyen/client.rb', line 16

def env
  @env
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



15
16
17
# File 'lib/adyen/client.rb', line 15

def oauth_token
  @oauth_token
end

#terminal_regionObject (readonly)

Returns the value of attribute terminal_region.



16
17
18
# File 'lib/adyen/client.rb', line 16

def terminal_region
  @terminal_region
end

#ws_passwordObject

Returns the value of attribute ws_password.



15
16
17
# File 'lib/adyen/client.rb', line 15

def ws_password
  @ws_password
end

#ws_userObject

Returns the value of attribute ws_user.



15
16
17
# File 'lib/adyen/client.rb', line 15

def ws_user
  @ws_user
end

Instance Method Details

#balance_controlObject



332
333
334
# File 'lib/adyen/client.rb', line 332

def balance_control
  @balance_control ||= Adyen::BalanceControl.new(self)
end

#balance_platformObject



300
301
302
# File 'lib/adyen/client.rb', line 300

def balance_platform
  @balance_platform ||= Adyen::BalancePlatform.new(self)
end

#bin_lookupObject



292
293
294
# File 'lib/adyen/client.rb', line 292

def bin_lookup
  @bin_lookup ||= Adyen::BinLookup.new(self)
end

#call_adyen_api(service, action, request_data, headers, version, _with_application_info: false) ⇒ Object

send request to adyen API



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/adyen/client.rb', line 149

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)

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

    # 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
    headers.map do |key, value|
      faraday.headers[key] = value
    end

    # add library headers
    faraday.headers['adyen-library-name'] = Adyen::NAME
    faraday.headers['adyen-library-version'] = Adyen::VERSION
  end
  # if json string convert to hash
  # needed to add applicationInfo
  request_data = JSON.parse(request_data) if request_data.is_a?(String)

  # 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|
          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|
          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
      end
    rescue Faraday::ConnectionFailed => e
      raise e, "Connection to #{url} failed"
    end
  end
  # check for API errors
  case response.status
    when 400
      full_message = build_error_message(response.body, 'Invalid format or fields')
      raise Adyen::FormatError.new(full_message, request_data, response.body)
    when 401
      full_message = build_error_message(response.body, 'Authentication error')
      raise Adyen::AuthenticationError.new(full_message, request_data)
    when 403
      full_message = build_error_message(response.body, 'Authorisation error')
      raise Adyen::PermissionError.new(full_message, request_data, response.body)
    when 404
      full_message = build_error_message(response.body, 'Not found error')
      raise Adyen::NotFoundError.new(full_message, request_data, response.body)
    when 422
      full_message = build_error_message(response.body, 'Validation error')
      raise Adyen::ValidationError.new(full_message, request_data, response.body)
    when 500..599
      full_message = build_error_message(response.body, 'Internal server error')
      raise Adyen::ServerError.new(full_message, request_data, response.body)
  end

  # delete has no response.body (unless it throws an error)
  if response.body.nil? || response.body === ''
    AdyenResult.new('{}', response.headers, response.status)
  # terminal API async call returns always 'ok'
  elsif response.body === 'ok'
    AdyenResult.new('{}', response.headers, response.status)
  else
    AdyenResult.new(response.body, response.headers, response.status)
  end
end

#capitalObject



336
337
338
# File 'lib/adyen/client.rb', line 336

def capital
  @capital ||= Adyen::Capital.new(self)
end

#checkoutObject

services



260
261
262
# File 'lib/adyen/client.rb', line 260

def checkout
  @checkout ||= Adyen::Checkout.new(self)
end

#data_protectionObject



284
285
286
# File 'lib/adyen/client.rb', line 284

def data_protection
  @data_protection ||= Adyen::DataProtection.new(self)
end

#disputesObject



288
289
290
# File 'lib/adyen/client.rb', line 288

def disputes
  @disputes ||= Adyen::Disputes.new(self)
end


296
297
298
# File 'lib/adyen/client.rb', line 296

def legal_entity_management
  @legal_entity_management ||= Adyen::LegalEntityManagement.new(self)
end

#live_url_prefix=(value) ⇒ Object

remove ‘https’ from live_url_prefix if necessary



62
63
64
# File 'lib/adyen/client.rb', line 62

def live_url_prefix=(value)
  @live_url_prefix = value.nil? ? value : value.sub('https://', '')
end

#managementObject



308
309
310
# File 'lib/adyen/client.rb', line 308

def management
  @management ||= Adyen::Management.new(self)
end

#marketpayObject



276
277
278
# File 'lib/adyen/client.rb', line 276

def marketpay
  @marketpay ||= Adyen::Marketpay::Marketpay.new(self)
end

#open_bankingObject



324
325
326
# File 'lib/adyen/client.rb', line 324

def open_banking
  @open_banking ||= Adyen::OpenBanking.new(self)
end

#paymentObject



264
265
266
# File 'lib/adyen/client.rb', line 264

def payment
  @payment ||= Adyen::Payment.new(self)
end

#payoutObject



268
269
270
# File 'lib/adyen/client.rb', line 268

def payout
  @payout ||= Adyen::Payout.new(self)
end

#pos_mobileObject



320
321
322
# File 'lib/adyen/client.rb', line 320

def pos_mobile
  @pos_mobile ||= Adyen::PosMobile.new(self)
end

#pos_terminal_managementObject



280
281
282
# File 'lib/adyen/client.rb', line 280

def pos_terminal_management
  @pos_terminal_management ||= Adyen::PosTerminalManagement.new(self)
end

#recurringObject



272
273
274
# File 'lib/adyen/client.rb', line 272

def recurring
  @recurring ||= Adyen::Recurring.new(self)
end

#service_url(service, action, version) ⇒ Object

construct full URL from service and endpoint



138
139
140
141
142
143
144
145
146
# File 'lib/adyen/client.rb', line 138

def service_url(service, action, version)
  if service == "Checkout" && @env == :live
    return "#{service_url_base(service)}/checkout/v#{version}/#{action}"
  elsif version == nil
    return "#{service_url_base(service)}/#{action}"
  else
    return "#{service_url_base(service)}/v#{version}/#{action}"
  end
end

#service_url_base(service) ⇒ Object

base URL for API given service and @env



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/adyen/client.rb', line 67

def service_url_base(service)
  if @env == :mock
    @mock_service_url_base
  else
    case service
    when 'Checkout'
      url = "https://checkout-#{@env}.adyen.com"
      supports_live_url_prefix = true
    when 'Account', 'Fund', 'Notification', 'Hop'
      url = "https://cal-#{@env}.adyen.com/cal/services/#{service}"
      supports_live_url_prefix = false
    when 'Recurring', 'Payment', 'Payout', 'BinLookup', 'StoredValue', 'BalanceControl'
      url = "https://pal-#{@env}.adyen.com/pal/servlet/#{service}"
      supports_live_url_prefix = true
    when 'PosTerminalManagement'
      url = "https://postfmapi-#{@env}.adyen.com/postfmapi/terminal"
      supports_live_url_prefix = false
    when 'Disputes'
      url = "https://ca-#{@env}.adyen.com/ca/services/DisputeService"
      supports_live_url_prefix = false
    when 'DataProtection'
      url = "https://ca-#{@env}.adyen.com/ca/services/DataProtectionService"
      supports_live_url_prefix = false
    when 'LegalEntityManagement'
      url = "https://kyc-#{@env}.adyen.com/lem"
      supports_live_url_prefix = false
    when 'BalancePlatform'
      url = "https://balanceplatform-api-#{@env}.adyen.com/bcl"
      supports_live_url_prefix = false
    when 'Transfers'
      url = "https://balanceplatform-api-#{@env}.adyen.com/btl"
      supports_live_url_prefix = false
    when 'Capital'
      url = "https://balanceplatform-api-#{@env}.adyen.com/capital"
      supports_live_url_prefix = false
    when 'Management'
      url = "https://management-#{@env}.adyen.com"
      supports_live_url_prefix = false
    when 'TerminalCloudAPI'
      url = if !terminal_region.nil?
        "https://terminal-api-#{@env}-#{terminal_region}.adyen.com"
      else
        "https://terminal-api-#{@env}.adyen.com"
      end
      supports_live_url_prefix = false
    when 'PosMobile'
      url = "https://checkout-#{@env}.adyen.com/checkout/possdk"
      supports_live_url_prefix = true
    when 'SessionAuthentication'
      subdomain = @env == :live ? "authe-#{@env}" : @env
      url = "https://#{subdomain}.adyen.com/authe/api"
      supports_live_url_prefix = false
    else
      raise ArgumentError, 'Invalid service specified'
    end

    if @live_url_prefix.nil? && (@env == :live) && supports_live_url_prefix
      raise ArgumentError,
            "Please set Client.live_url_prefix to the portion \n          of your merchant-specific URL prior to '-[service]-live.adyenpayments.com'"
    end

    if @env == :live && supports_live_url_prefix
      url.insert(8, "#{@live_url_prefix}-")
      url['adyen.com'] = 'adyenpayments.com'
    end

    url
  end
end

#session_authenticationObject



328
329
330
# File 'lib/adyen/client.rb', line 328

def session_authentication
  @open_banking ||= Adyen::SessionAuthentication.new(self)
end

#stored_valueObject



312
313
314
# File 'lib/adyen/client.rb', line 312

def stored_value
  @stored_value ||= Adyen::StoredValue.new(self)
end

#terminal_cloud_apiObject



316
317
318
# File 'lib/adyen/client.rb', line 316

def terminal_cloud_api
  @terminal_cloud_api ||= Adyen::TerminalCloudAPI.new(self)
end

#transfersObject



304
305
306
# File 'lib/adyen/client.rb', line 304

def transfers
  @transfers ||= Adyen::Transfers.new(self)
end