Class: Adyen::HPP::Request
- Inherits:
-
Object
- Object
- Adyen::HPP::Request
- 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
-
#environment ⇒ String
Returns the Adyen environment the request will be directed to.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#shared_secret ⇒ String
Returns the shared secret to use for signing the request.
-
#skin ⇒ Hash
Returns the Adyen skin to use for the request, in the same format that is returned by Adyen::Configuration.register_form_skin.
Instance Method Summary collapse
-
#domain ⇒ String
Returns the DOMAIN of the Adyen payment system, adjusted for an Adyen environment.
-
#flat_payment_parameters ⇒ Hash
Transforms and flattens payment parameters to be in the correct format which is understood and accepted by adyen.
-
#formatted_parameters ⇒ Hash
Transforms the payment parameters hash to be in the correct format.
-
#hidden_fields ⇒ String
Returns a HTML snippet of hidden INPUT tags with the provided payment parameters.
-
#initialize(parameters, skin: nil, environment: nil, shared_secret: nil) ⇒ Request
constructor
Initialize the HPP request.
-
#payment_methods_url ⇒ String
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.
-
#redirect_url ⇒ String
Returns an absolute URL to the Adyen payment system, with the payment parameters included as GET parameters in the URL.
-
#url(payment_flow = nil) ⇒ String
Returns the URL of the Adyen payment system, adjusted for an Adyen environment.
Constructor Details
#initialize(parameters, skin: nil, environment: nil, shared_secret: nil) ⇒ Request
Initialize the HPP request
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
#environment ⇒ String
Returns the Adyen environment the request will be directed to
41 42 43 |
# File 'lib/adyen/hpp/request.rb', line 41 def environment @environment || Adyen.configuration.environment end |
#parameters ⇒ Object
Returns the value of attribute parameters.
9 10 11 |
# File 'lib/adyen/hpp/request.rb', line 9 def parameters @parameters end |
#shared_secret ⇒ String
Returns the shared secret to use for signing the request
48 49 50 |
# File 'lib/adyen/hpp/request.rb', line 48 def shared_secret @shared_secret || skin[:shared_secret] end |
#skin ⇒ Hash
Returns the Adyen skin to use for the request, in the same format that is
returned by Adyen::Configuration.register_form_skin
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
#domain ⇒ String
Returns the DOMAIN of the Adyen payment system, adjusted for an Adyen environment.
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_parameters ⇒ Hash
Transforms and flattens payment parameters to be in the correct format which is understood and accepted by adyen
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_parameters ⇒ Hash
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.
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.(formatted_parameters[:session_validity]) formatted_parameters end |
#hidden_fields ⇒ String
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 %>
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_url ⇒ String
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
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_url ⇒ String
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.
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.
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 |