Class: Adyen::REST::Client

Inherits:
Object
  • Object
show all
Includes:
AuthorisePayment, ModifyPayment, Payout
Defined in:
lib/adyen/rest/client.rb

Overview

The Client class acts as a client to Adyen’s REST webservice.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Payout

#confirm_payout, #confirm_payout_third_party, #decline_payout, #decline_payout_third_party, #store_payout, #submit_and_store_payout, #submit_and_store_payout_third_party, #submit_payout, #submit_payout_third_party

Methods included from ModifyPayment

#cancel_or_refund_payment, #cancel_or_refund_payment_request, #cancel_payment, #cancel_payment_request, #capture_payment, #capture_payment_request, #refund_payment, #refund_payment_request

Methods included from AuthorisePayment

#authorise_payment, #authorise_payment_3dsecure, #authorise_payment_3dsecure_request, #authorise_payment_request, #authorise_recurring_payment, #authorise_recurring_payment_request, #list_recurring_details, #list_recurring_details_request, #reauthorise_recurring_payment, #reauthorise_recurring_payment_request

Constructor Details

#initialize(environment, username, password) ⇒ Client

Returns a new instance of Client.

Parameters:

  • environment (String)

    The Adyen environment to interface with. Either 'live' or 'test'.

  • username (String)

    The webservice username, e.g. [email protected]

  • password (String)

    The password associated with the username

  • default_api_params (Hash)

    Default arguments that will be used for every API call



32
33
34
# File 'lib/adyen/rest/client.rb', line 32

def initialize(environment, username, password)
  @environment, @username, @password = environment, username, password
end

Instance Attribute Details

#environmentString (readonly)

The adyen environment to interact with. Either 'live' or 'test'.

Returns:

  • (String)


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/adyen/rest/client.rb', line 20

class Client
  include AuthorisePayment
  include ModifyPayment
  include Payout

  attr_reader :environment

  # @param environment [String] The Adyen environment to interface with. Either
  #   <tt>'live'</tt> or <tt>'test'</tt>.
  # @param username [String] The webservice username, e.g. <tt>[email protected]</tt>
  # @param password [String] The password associated with the username
  # @param default_api_params [Hash] Default arguments that will be used for every API call
  def initialize(environment, username, password)
    @environment, @username, @password = environment, username, password
  end

  # Closes the client.
  #
  # - This will terminate the HTTP connection.
  # - After calling this method, the behavior of any further method calls against
  #   this client instance is undefined.
  #
  # @return [void]
  def close
    @http.finish if @http && @http.started?
    @http = nil
  end

  # Runs a client session against the Adyen REST service for the duration of the block,
  # and closes the connection afterwards.
  #
  # @yield The provided block will be called in which you can interact with the API using
  #   the provided client. The client will be closed after the block returns.
  # @return [void]
  def session
    yield(self)
  ensure
    close
  end


  # The underlying <tt>Net::HTTP</tt> instance that is used to execute HTTP
  # request against the API.
  #
  # You can use this to set options on the Net::HTTP instance, like <tt>read_timeout</tt>.
  # Many of these options will only work if you set them before the HTTP connection is
  # opened, i.e. before doing the first API call.
  #
  # @return [Net::HTTP] The underlying Net::HTTP instance the client uses to perform HTTP request.
  def http
    @http ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |http|
      http.use_ssl = endpoint.scheme == 'https'
    end
  end

  # Executes an API request, and returns a reponse of the given type.
  #
  # @param request [Adyen::REST::Request] The API request to execute.
  #   <tt>validate!</tt> will be called on the this object before the
  #   request is made.
  # @param response_type [Class] The response type to use. Use either
  #   <tt>Adyen::REST::Response</tt> or a subclass.
  # @return [Adyen::REST::Response] A response instance of the provided type
  # @see execute_http_request The <tt>execute_http_request</tt> takes care
  #   of  executing the underlying HTTP request.
  def execute_request(request)
    request.validate!
    http_response = execute_http_request(request)
    request.build_response(http_response)
  end

  protected

  # Executes a HTTP request against Adyen's REST webservice.
  # @param request [Adyen::REST::Request] The request to execute.
  # @return [Net::HTTPResponse] The response from the server.
  # @raise [Adyen::REST::Error] if the HTTP response code was not 200.
  # @see #http Use the <tt>http</tt> method to set options on the underlying
  #   <tt>Net::HTTP</tt> object, like timeouts.
  def execute_http_request(request)
    http_request = Net::HTTP::Post.new(request.path)
    http_request.basic_auth(@username, @password)
    http_request.set_form_data(request.form_data)

    case response = http.request(http_request)
    when Net::HTTPOK
      return response
    when Net::HTTPUnprocessableEntity, Net::HTTPInternalServerError
      raise Adyen::REST::ResponseError.new(response.body)
    when Net::HTTPUnauthorized
      raise Adyen::REST::Error.new("Webservice credentials are incorrect")
    else
      raise Adyen::REST::Error.new("Unexpected HTTP response code: #{response.code} | request path: #{request.path} | response body: #{response.body}")
    end
  end

  # The endpoint URI for this client.
  # @return [URI] The endpoint to use for the environment.
  def endpoint
    @endpoint ||= URI(ENDPOINT % [environment])
  end

  # @see Adyen::REST::Client#endpoint
  ENDPOINT = 'https://pal-%s.adyen.com/'
  private_constant :ENDPOINT
end

Instance Method Details

#closevoid

This method returns an undefined value.

Closes the client.

  • This will terminate the HTTP connection.

  • After calling this method, the behavior of any further method calls against this client instance is undefined.



43
44
45
46
# File 'lib/adyen/rest/client.rb', line 43

def close
  @http.finish if @http && @http.started?
  @http = nil
end

#execute_request(request) ⇒ Adyen::REST::Response

Executes an API request, and returns a reponse of the given type.

Parameters:

  • request (Adyen::REST::Request)

    The API request to execute. validate! will be called on the this object before the request is made.

  • response_type (Class)

    The response type to use. Use either Adyen::REST::Response or a subclass.

Returns:

See Also:

  • The execute_http_request takes care of executing the underlying HTTP request.


85
86
87
88
89
# File 'lib/adyen/rest/client.rb', line 85

def execute_request(request)
  request.validate!
  http_response = execute_http_request(request)
  request.build_response(http_response)
end

#httpNet::HTTP

The underlying Net::HTTP instance that is used to execute HTTP request against the API.

You can use this to set options on the Net::HTTP instance, like read_timeout. Many of these options will only work if you set them before the HTTP connection is opened, i.e. before doing the first API call.

Returns:

  • (Net::HTTP)

    The underlying Net::HTTP instance the client uses to perform HTTP request.



69
70
71
72
73
# File 'lib/adyen/rest/client.rb', line 69

def http
  @http ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |http|
    http.use_ssl = endpoint.scheme == 'https'
  end
end

#session { ... } ⇒ void

This method returns an undefined value.

Runs a client session against the Adyen REST service for the duration of the block, and closes the connection afterwards.

Yields:

  • The provided block will be called in which you can interact with the API using the provided client. The client will be closed after the block returns.



54
55
56
57
58
# File 'lib/adyen/rest/client.rb', line 54

def session
  yield(self)
ensure
  close
end