class Adyen::HPP::Request

Constants

MANDATORY_ATTRIBUTES

Attributes

environment[W]
parameters[RW]
shared_secret[W]
skin[W]

Public Class Methods

new(parameters, skin: nil, environment: nil, shared_secret: nil) click to toggle source

Initialize the HPP request

@param [Hash] parameters The payment parameters

You must not provide the +:merchant_sig+ parameter: it will be calculated automatically.

@param [Hash|String] skin A skin hash in the same format that is returned by

Adyen::Configuration.register_form_skin, or the name of a registered skin.
When not set, the default skin specified in the configuration will be used.

@param [String] environment The Adyen environment to use.

When not set, the environment specified in the configuration will be used.

@param [String] shared_secret The shared secret to use for signing the request.

When not set, the shared secret of the skin will be used.
   # File lib/adyen/hpp/request.rb
25 def initialize(parameters, skin: nil, environment: nil, shared_secret: nil)
26   @parameters, @skin, @environment, @shared_secret = parameters, skin, environment, shared_secret
27   @skin = Adyen.configuration.form_skin_by_name(@skin) unless skin.nil? || skin.is_a?(Hash)
28 end

Public Instance Methods

domain() click to toggle source

Returns the DOMAIN of the Adyen payment system, adjusted for an Adyen environment.

@return [String] The domain of the Adyen payment system that can be used

for payment forms or redirects.

@see Adyen::HPP::Request.redirect_url

   # File lib/adyen/hpp/request.rb
57 def domain
58   Adyen.configuration.payment_flow_domain || HPP_DOMAIN % [environment.to_s]
59 end
environment() click to toggle source

Returns the Adyen environment the request will be directed to

@return [String] environment if set, configuration default otherwise

   # File lib/adyen/hpp/request.rb
41 def environment
42   @environment || Adyen.configuration.environment
43 end
flat_payment_parameters() click to toggle source

Transforms and flattens payment parameters to be in the correct format which is understood and accepted by adyen

@return [Hash] The payment parameters, with camelized and prefixed key, stringified values and

the +:merchant_signature+ parameter set.
    # File lib/adyen/hpp/request.rb
108 def flat_payment_parameters
109   Adyen::HPP::Signature.sign(Adyen::Util.flatten(formatted_parameters), shared_secret)
110 end
formatted_parameters() click to toggle source

Transforms the payment parameters hash to be in the correct format. It will also include the Adyen::Configuration#default_form_params hash and it will include the :skin_code parameter and the default attributes of the skin Any default parameter value will be overrided if another value is provided in the request.

@return [Hash] Completed and formatted payment parameters. @raise [ArgumentError] Thrown if some parameter health check fails.

    # File lib/adyen/hpp/request.rb
 82 def formatted_parameters
 83   raise ArgumentError, "Cannot generate request: parameters should be a hash!" unless parameters.is_a?(Hash)
 84 
 85   formatted_parameters = parameters
 86   default_form_parameters = Adyen.configuration.default_form_params
 87   unless skin.empty?
 88     formatted_parameters[:skin_code] ||= skin[:skin_code]
 89     default_form_parameters = default_form_parameters.merge(skin[:default_form_params] || {})
 90   end
 91   formatted_parameters = default_form_parameters.merge(formatted_parameters)
 92 
 93   MANDATORY_ATTRIBUTES.each do |attribute|
 94     raise ArgumentError, "Cannot generate request: :#{attribute} attribute not found!" unless formatted_parameters[attribute]
 95   end
 96 
 97   formatted_parameters[:recurring_contract] = 'RECURRING' if formatted_parameters.delete(:recurring) == true
 98   formatted_parameters[:order_data]         = Adyen::Util.gzip_base64(formatted_parameters.delete(:order_data_raw)) if formatted_parameters[:order_data_raw]
 99   formatted_parameters[:ship_before_date]   = Adyen::Util.format_date(formatted_parameters[:ship_before_date])
100   formatted_parameters[:session_validity]   = Adyen::Util.format_timestamp(formatted_parameters[:session_validity])
101   formatted_parameters
102 end
hidden_fields() click to toggle source

Returns a HTML snippet of hidden INPUT tags with the provided payment parameters. The snippet can be included in a payment form that POSTs to the Adyen payment system.

The payment parameters that are provided to this method will be merged with the {Adyen::Configuration#default_form_params} hash. The default parameter values will be overrided if another value is provided to this method.

You do not have to provide the :merchant_sig parameter: it will be calculated automatically.

@example

  <%
    payment_parameters = {
      :currency_code => 'USD',
      :payment_amount => 1000,
      :merchant_account => 'MyMerchant',
      ...
    }
    hpp_request = Adyen::HPP::Request.new(payment_parameters, skin: :my_skin, environment: :test)
 %>

<%= form_tag(hpp_request.url, authenticity_token: false, enforce_utf8: false) do %>
  <%= hpp_request.hidden_fields %>
  <%= submit_tag("Pay invoice")
<% end %>

@return [String] An HTML snippet that can be included in a form that POSTs to the

Adyen payment system.
    # File lib/adyen/hpp/request.rb
183 def hidden_fields
184 
185   # Generate a hidden input tag per parameter, join them by newlines.
186   form_str = flat_payment_parameters.map { |key, value|
187     "<input type=\"hidden\" name=\"#{CGI.escapeHTML(key)}\" value=\"#{CGI.escapeHTML(value)}\" />"
188   }.join("\n")
189 
190   form_str.respond_to?(:html_safe) ? form_str.html_safe : form_str
191 end
payment_methods_url() click to toggle source

@see Adyen::HPP::Request.redirect_url

Returns an absolute URL very similar to the one returned by Adyen::HPP::Request.redirect_url except that it uses the directory.shtml call which returns a list of all available payment methods

@return [String] An absolute URL to redirect to the Adyen payment system.

    # File lib/adyen/hpp/request.rb
149 def payment_methods_url
150   url(:directory) + '?' + flat_payment_parameters.map { |(k, v)|
151     "#{CGI.escape(k)}=#{CGI.escape(v)}"
152   }.join('&')
153 end
redirect_url() click to toggle source

Returns an absolute URL to the Adyen payment system, with the payment parameters included as GET parameters in the URL. The URL also depends on the Adyen enviroment

Note that Internet Explorer has a maximum length for URLs it can handle (2083 characters). Make sure that the URL is not longer than this limit if you want your site to work in IE.

@example

def pay
  # Generate a URL to redirect to Adyen's payment system.
  payment_parameters = {
    :currency_code => 'USD',
    :payment_amount => 1000,
    :merchant_account => 'MyMerchant',
    ...
  }
  hpp_request = Adyen::HPP::Request.new(payment_parameters, skin: :my_skin, environment: :test)

  respond_to do |format|
    format.html { redirect_to(hpp_request.redirect_url) }
  end
end

@return [String] An absolute URL to redirect to the Adyen payment system.

    # File lib/adyen/hpp/request.rb
136 def redirect_url
137   url + '?' + flat_payment_parameters.map { |(k, v)|
138     "#{CGI.escape(k)}=#{CGI.escape(v)}"
139   }.join('&')
140 end
shared_secret() click to toggle source

Returns the shared secret to use for signing the request

@return [String] shared secret if set, the skin's shared secret otherwise

   # File lib/adyen/hpp/request.rb
48 def shared_secret
49   @shared_secret || skin[:shared_secret]
50 end
skin() click to toggle source

Returns the Adyen skin to use for the request, in the same format that is

returned by Adyen::Configuration.register_form_skin

@return [Hash] skin if set, configuration default otherwise

   # File lib/adyen/hpp/request.rb
34 def skin
35   @skin || Adyen.configuration.form_skin_by_name(Adyen.configuration.default_skin) || {}
36 end
url(payment_flow = nil) click to toggle source

Returns the URL of the Adyen payment system, adjusted for an Adyen environment.

@param [String] payment_flow The Adyen payment type to use. This parameter can be

left out, in which case the default payment type will be used.

@return [String] The absolute URL of the Adyen payment system that can be used

for payment forms or redirects.

@see Adyen::HPP::Request.domain @see Adyen::HPP::Request.redirect_url @see Adyen::HPP::Request.payment_methods_url

   # File lib/adyen/hpp/request.rb
70 def url(payment_flow = nil)
71   payment_flow ||= Adyen.configuration.payment_flow
72   HPP_URL % [domain, payment_flow.to_s]
73 end