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, env: :live, adapter: nil, mock_port: 3001, live_url_prefix: nil, mock_service_url_base: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
# File 'lib/adyen/client.rb', line 11

def initialize(ws_user: nil, ws_password: nil, api_key: nil, env: :live, adapter: nil, mock_port: 3001, live_url_prefix: nil, mock_service_url_base: nil)
  @ws_user = ws_user
  @ws_password = ws_password
  @api_key = api_key
  @env = env
  @adapter = adapter || Faraday.default_adapter
  @mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
  @live_url_prefix = live_url_prefix
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



8
9
10
# File 'lib/adyen/client.rb', line 8

def adapter
  @adapter
end

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/adyen/client.rb', line 8

def api_key
  @api_key
end

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/adyen/client.rb', line 8

def client
  @client
end

#envObject

Returns the value of attribute env.



9
10
11
# File 'lib/adyen/client.rb', line 9

def env
  @env
end

#live_url_prefixObject

Returns the value of attribute live_url_prefix.



8
9
10
# File 'lib/adyen/client.rb', line 8

def live_url_prefix
  @live_url_prefix
end

#ws_passwordObject

Returns the value of attribute ws_password.



8
9
10
# File 'lib/adyen/client.rb', line 8

def ws_password
  @ws_password
end

#ws_userObject

Returns the value of attribute ws_user.



8
9
10
# File 'lib/adyen/client.rb', line 8

def ws_user
  @ws_user
end

Instance Method Details

#add_application_info(request_data) ⇒ Object

add application_info for analytics



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/adyen/client.rb', line 180

def add_application_info(request_data)
  adyenLibrary = {
    :name => Adyen::NAME,
    :version => Adyen::VERSION.to_s,
  }

  if request_data[:applicationInfo].nil?
    request_data[:applicationInfo] = {}
  end

  request_data[:applicationInfo][:adyenLibrary] = adyenLibrary
end

#bin_lookupObject



226
227
228
# File 'lib/adyen/client.rb', line 226

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



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
136
137
138
139
140
141
142
143
144
145
146
147
148
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
# File 'lib/adyen/client.rb', line 80

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)

  # make sure right authentication has been provided
  # will use api_key if present, otherwise ws_user and ws_password
  if @api_key.nil?
    if service == "PaymentSetupAndVerification"
      raise Adyen::AuthenticationError.new("Checkout service requires API-key", request_data), "Checkout service requires API-key"
    elsif @ws_password.nil? || @ws_user.nil?
      raise Adyen::AuthenticationError.new("No authentication found - please set api_key or ws_user and ws_password", request_data), "No authentication found - please set api_key or ws_user and ws_password"
    else
      auth_type = "basic"
    end
  else
    auth_type = "api-key"
  end

  # initialize Faraday connection object
  conn = Faraday.new(url: url) do |faraday|
    faraday.adapter @adapter
    faraday.headers["Content-Type"] = "application/json"
    faraday.headers["User-Agent"] = Adyen::NAME + "/" + Adyen::VERSION

    # set auth type based on service
    case auth_type
    when "basic"
      if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0')
        # for faraday 2.0 and higher
        faraday.request :authorization, :basic, @ws_user, @ws_password
      else
        # for faraday 1.x
        faraday.basic_auth(@ws_user, @ws_password)
      end
    when "api-key"
      faraday.headers["x-api-key"] = @api_key
    end

    # add optional headers if specified in request
    # will overwrite default headers if overlapping
    headers.map do |key, value|
      faraday.headers[key] = value
    end
  end
  # if json string convert to hash
  # needed to add applicationInfo
  if request_data.is_a?(String)
    request_data = JSON.parse(request_data)
  end

  # add application only on checkout service
  if with_application_info
    add_application_info(request_data)
  end

  # 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 => connection_error
        raise connection_error, "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 => connection_error
        raise connection_error, "Connection to #{url} failed"
      end
    end
  else
    # post request to Adyen
    begin
      response = conn.post do |req|
        req.body = request_data
      end # handle client errors
    rescue Faraday::ConnectionFailed => connection_error
      raise connection_error, "Connection to #{url} failed"
    end
  end

  # check for API errors
  case response.status
  when 401
    raise Adyen::AuthenticationError.new("Invalid API authentication; https://docs.adyen.com/user-management/how-to-get-the-api-key", request_data)
  when 403
    raise Adyen::PermissionError.new("Missing user permissions; https://docs.adyen.com/user-management/user-roles", request_data)
  end

  formatted_response = AdyenResult.new(response.body, response.headers, response.status)

  formatted_response
end

#checkoutObject

services



194
195
196
# File 'lib/adyen/client.rb', line 194

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

#data_protectionObject



218
219
220
# File 'lib/adyen/client.rb', line 218

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

#disputeObject



222
223
224
# File 'lib/adyen/client.rb', line 222

def dispute
  @dispute ||= Adyen::Dispute.new(self)
end

#marketpayObject



210
211
212
# File 'lib/adyen/client.rb', line 210

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

#paymentsObject



198
199
200
# File 'lib/adyen/client.rb', line 198

def payments
  @payments ||= Adyen::Payments.new(self)
end

#payoutsObject



202
203
204
# File 'lib/adyen/client.rb', line 202

def payouts
  @payouts ||= Adyen::Payouts.new(self)
end

#postfmapiObject



214
215
216
# File 'lib/adyen/client.rb', line 214

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

#recurringObject



206
207
208
# File 'lib/adyen/client.rb', line 206

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

#service_url(service, action, version) ⇒ Object

construct full URL from service and endpoint



71
72
73
74
75
76
77
# File 'lib/adyen/client.rb', line 71

def service_url(service, action, version)
  if service == "Checkout" || service == "Terminal"
    "#{service_url_base(service)}/v#{version}/#{action}"
  else
    "#{service_url_base(service)}/#{service}/v#{version}/#{action}"
  end
end

#service_url_base(service) ⇒ Object

base URL for API given service and @env

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/adyen/client.rb', line 36

def service_url_base(service)
  raise ArgumentError, "Please set Client.live_url_prefix to the portion of your merchant-specific URL prior to '-[service]-live.adyenpayments.com'" if @live_url_prefix.nil? and @env == :live
  if @env == :mock
    @mock_service_url_base
  else
    case service
    when "Checkout"
      url = "https://checkout-#{@env}.adyen.com/checkout"
      supports_live_url_prefix = true
    when "Account", "Fund", "Notification", "Hop"
      url = "https://cal-#{@env}.adyen.com/cal/services"
      supports_live_url_prefix = false
    when "Recurring", "Payment", "Payout", "BinLookup"
      url = "https://pal-#{@env}.adyen.com/pal/servlet"
      supports_live_url_prefix = true
    when "Terminal"
      url = "https://postfmapi-#{@env}.adyen.com/postfmapi/terminal"
      supports_live_url_prefix = false
    when "DataProtectionService", "DisputeService"
      url = "https://ca-#{@env}.adyen.com/ca/services"
      supports_live_url_prefix = false
    else
      raise ArgumentError, "Invalid service specified"
    end

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

    return url
  end
end