Class: Adyen::HPP::Request

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

Constant Summary collapse

MANDATORY_ATTRIBUTES =
%i(currency_code payment_amount merchant_account skin_code ship_before_date session_validity).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, skin: nil, environment: nil, shared_secret: nil) ⇒ Request

Initialize the HPP request

Parameters:

  • parameters (Hash)

    The payment parameters You must not provide the :merchant_sig parameter: it will be calculated automatically.

  • skin (Hash|String) (defaults to: nil)

    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.

  • environment (String) (defaults to: nil)

    The Adyen environment to use. When not set, the environment specified in the configuration will be used.

  • shared_secret (String) (defaults to: nil)

    The shared secret to use for signing the request. When not set, the shared secret of the skin will be used.



25
26
27
28
# File 'lib/adyen/hpp/request.rb', line 25

def initialize(parameters, skin: nil, environment: nil, shared_secret: nil)
  @parameters, @skin, @environment, @shared_secret = parameters, skin, environment, shared_secret
  @skin = Adyen.configuration.form_skin_by_name(@skin) unless skin.nil? || skin.is_a?(Hash)
end

Instance Attribute Details

#environmentString

Returns the Adyen environment the request will be directed to

Returns:

  • (String)

    environment if set, configuration default otherwise



41
42
43
# File 'lib/adyen/hpp/request.rb', line 41

def environment
  @environment || Adyen.configuration.environment
end

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#shared_secretString

Returns the shared secret to use for signing the request

Returns:

  • (String)

    shared secret if set, the skin’s shared secret otherwise



48
49
50
# File 'lib/adyen/hpp/request.rb', line 48

def shared_secret
  @shared_secret || skin[:shared_secret]
end

#skinHash

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

returned by Adyen::Configuration.register_form_skin

Returns:

  • (Hash)

    skin if set, configuration default otherwise



34
35
36
# File 'lib/adyen/hpp/request.rb', line 34

def skin
  @skin || Adyen.configuration.form_skin_by_name(Adyen.configuration.default_skin) || {}
end

Instance Method Details

#domainString

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

Returns:

  • (String)

    The domain of the Adyen payment system that can be used for payment forms or redirects.

See Also:

  • redirect_url


57
58
59
# File 'lib/adyen/hpp/request.rb', line 57

def domain
  Adyen.configuration.payment_flow_domain || HPP_DOMAIN % [environment.to_s]
end

#flat_payment_parametersHash

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

Returns:

  • (Hash)

    The payment parameters, with camelized and prefixed key, stringified values and the :merchant_signature parameter set.



108
109
110
# File 'lib/adyen/hpp/request.rb', line 108

def flat_payment_parameters
  Adyen::HPP::Signature.sign(Adyen::Util.flatten(formatted_parameters), shared_secret)
end

#formatted_parametersHash

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.

Returns:

  • (Hash)

    Completed and formatted payment parameters.

Raises:

  • (ArgumentError)

    Thrown if some parameter health check fails.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/adyen/hpp/request.rb', line 82

def formatted_parameters
  raise ArgumentError, "Cannot generate request: parameters should be a hash!" unless parameters.is_a?(Hash)

  formatted_parameters = parameters
  default_form_parameters = Adyen.configuration.default_form_params
  unless skin.empty?
    formatted_parameters[:skin_code] ||= skin[:skin_code]
    default_form_parameters = default_form_parameters.merge(skin[:default_form_params] || {})
  end
  formatted_parameters = default_form_parameters.merge(formatted_parameters)

  MANDATORY_ATTRIBUTES.each do |attribute|
    raise ArgumentError, "Cannot generate request: :#{attribute} attribute not found!" unless formatted_parameters[attribute]
  end

  formatted_parameters[:recurring_contract] = 'RECURRING' if formatted_parameters.delete(:recurring) == true
  formatted_parameters[:order_data]         = Adyen::Util.gzip_base64(formatted_parameters.delete(:order_data_raw)) if formatted_parameters[:order_data_raw]
  formatted_parameters[:ship_before_date]   = Adyen::Util.format_date(formatted_parameters[:ship_before_date])
  formatted_parameters[:session_validity]   = Adyen::Util.format_timestamp(formatted_parameters[:session_validity])
  formatted_parameters
end

#hidden_fieldsString

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 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.

 %>

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

Examples:


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

Returns:

  • (String)

    An HTML snippet that can be included in a form that POSTs to the Adyen payment system.



183
184
185
186
187
188
189
190
191
# File 'lib/adyen/hpp/request.rb', line 183

def hidden_fields

  # Generate a hidden input tag per parameter, join them by newlines.
  form_str = flat_payment_parameters.map { |key, value|
    "<input type=\"hidden\" name=\"#{CGI.escapeHTML(key)}\" value=\"#{CGI.escapeHTML(value)}\" />"
  }.join("\n")

  form_str.respond_to?(:html_safe) ? form_str.html_safe : form_str
end

#payment_methods_urlString

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

Returns:

  • (String)

    An absolute URL to redirect to the Adyen payment system.

See Also:

  • redirect_url


149
150
151
152
153
# File 'lib/adyen/hpp/request.rb', line 149

def payment_methods_url
  url(:directory) + '?' + flat_payment_parameters.map { |(k, v)|
    "#{CGI.escape(k)}=#{CGI.escape(v)}"
  }.join('&')
end

#redirect_urlString

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.

Examples:


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

Returns:

  • (String)

    An absolute URL to redirect to the Adyen payment system.



136
137
138
139
140
# File 'lib/adyen/hpp/request.rb', line 136

def redirect_url
  url + '?' + flat_payment_parameters.map { |(k, v)|
    "#{CGI.escape(k)}=#{CGI.escape(v)}"
  }.join('&')
end

#url(payment_flow = nil) ⇒ String

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

Parameters:

  • payment_flow (String) (defaults to: nil)

    The Adyen payment type to use. This parameter can be left out, in which case the default payment type will be used.

Returns:

  • (String)

    The absolute URL of the Adyen payment system that can be used for payment forms or redirects.

See Also:

  • domain
  • redirect_url
  • payment_methods_url


70
71
72
73
# File 'lib/adyen/hpp/request.rb', line 70

def url(payment_flow = nil)
  payment_flow ||= Adyen.configuration.payment_flow
  HPP_URL % [domain, payment_flow.to_s]
end