Class: Adyen::REST::Client
- Inherits:
-
Object
- Object
- Adyen::REST::Client
- Includes:
- AuthorisePayment, ModifyPayment
- Defined in:
- lib/adyen/rest/client.rb
Overview
The Client class acts as a client to Adyen’s REST webservice.
Instance Attribute Summary collapse
-
#environment ⇒ String
readonly
The adyen environment to interact with.
-
#merchant ⇒ Object
readonly
Returns the value of attribute merchant.
Instance Method Summary collapse
-
#close ⇒ void
Closes the client.
-
#execute_request(request) ⇒ Adyen::REST::Response
Executes an API request, and returns a reponse of the given type.
-
#http ⇒ Net::HTTP
The underlying
Net::HTTPinstance that is used to execute HTTP request against the API. -
#initialize(environment, username, password, merchant = nil) ⇒ Client
constructor
A new instance of Client.
-
#session { ... } ⇒ void
Runs a client session against the Adyen REST service for the duration of the block, and closes the connection afterwards.
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, merchant = nil) ⇒ Client
Returns a new instance of Client.
30 31 32 |
# File 'lib/adyen/rest/client.rb', line 30 def initialize(environment, username, password, merchant = nil) @environment, @username, @password, @merchant = environment, username, password, merchant end |
Instance Attribute Details
#environment ⇒ String (readonly)
The adyen environment to interact with. Either 'live' or 'test'.
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 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 126 127 128 129 |
# File 'lib/adyen/rest/client.rb', line 19 class Client include AuthorisePayment include ModifyPayment attr_reader :environment, :merchant # @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 merchant [String] def initialize(environment, username, password, merchant = nil) @environment, @username, @password, @merchant = environment, username, password, merchant 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(endpoint.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::HTTPInternalServerError raise Adyen::REST::ResponseError.new(response.body) when Net:: raise Adyen::REST::Error.new("Webservice credentials are incorrect") else raise Adyen::REST::Error.new("Unexpected HTTP response: #{response.code}") end end # The endpoint URI for this client. # @return [URI] The endpoint to use for the environment. def endpoint @endpoint ||= if merchant && environment.to_sym == :live URI(ENDPOINT_MERCHANT_SPECIFIC % [merchant]) else URI(ENDPOINT % [environment]) end end # @see Adyen::REST::Client#endpoint ENDPOINT = 'https://pal-%s.adyen.com/pal/adapter/httppost' ENDPOINT_MERCHANT_SPECIFIC = 'https://%s.pal-live.adyenpayments.com/pal/adapter/httppost' private_constant :ENDPOINT, :ENDPOINT_MERCHANT_SPECIFIC end |
#merchant ⇒ Object (readonly)
Returns the value of attribute merchant.
23 24 25 |
# File 'lib/adyen/rest/client.rb', line 23 def merchant @merchant end |
Instance Method Details
#close ⇒ void
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.
41 42 43 44 |
# File 'lib/adyen/rest/client.rb', line 41 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.
83 84 85 86 87 |
# File 'lib/adyen/rest/client.rb', line 83 def execute_request(request) request.validate! http_response = execute_http_request(request) request.build_response(http_response) end |
#http ⇒ Net::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.
67 68 69 70 71 |
# File 'lib/adyen/rest/client.rb', line 67 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.
52 53 54 55 56 |
# File 'lib/adyen/rest/client.rb', line 52 def session yield(self) ensure close end |