lib/adyen/form.rb in adyen-1.6.0 vs lib/adyen/form.rb in adyen-2.0.0.pre1

- old
+ new

@@ -84,13 +84,13 @@ parameters[:shared_secret] ||= skin[:shared_secret] parameters.merge!(skin[:default_form_params]) end parameters[:recurring_contract] = 'RECURRING' if parameters.delete(:recurring) == true - parameters[:order_data] = Adyen::Encoding.gzip_base64(parameters.delete(:order_data_raw)) if parameters[:order_data_raw] - parameters[:ship_before_date] = Adyen::Formatter::DateTime.fmt_date(parameters[:ship_before_date]) - parameters[:session_validity] = Adyen::Formatter::DateTime.fmt_time(parameters[:session_validity]) + parameters[:order_data] = Adyen::Util.gzip_base64(parameters.delete(:order_data_raw)) if parameters[:order_data_raw] + parameters[:ship_before_date] = Adyen::Util.format_date(parameters[:ship_before_date]) + parameters[:session_validity] = Adyen::Util.format_timestamp(parameters[:session_validity]) end # Transforms the payment parameters to be in the correct format and calculates the merchant # signature parameter. It also does some basic health checks on the parameters hash. # @@ -131,11 +131,11 @@ # # @param [Hash] parameters The payment parameters. The parameters set in the # {Adyen::Configuration#default_form_params} hash will be included automatically. # @return [Hash] The payment parameters flatten, with camelized and prefixed key, stringified value def flat_payment_parameters(parameters = {}) - flatten(payment_parameters(parameters)) + Adyen::Util.flatten(payment_parameters(parameters)) end # 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 current Adyen enviroment. # @@ -250,11 +250,11 @@ # @return [String] The signature of the payment request # @raise [ArgumentError] Thrown if shared_secret is empty def calculate_signature(parameters, shared_secret = nil) shared_secret ||= parameters.delete(:shared_secret) raise ArgumentError, "Cannot calculate payment request signature with empty shared_secret" if shared_secret.to_s.empty? - Adyen::Encoding.hmac_base64(shared_secret, calculate_signature_string(parameters)) + Adyen::Util.hmac_base64(shared_secret, calculate_signature_string(parameters)) end # Generates the string that is used to calculate the request signature. This signature # is used by Adyen to check whether the request is genuinely originating from you. # @param [Hash] parameters The parameters that will be included in the billing address request. @@ -280,11 +280,11 @@ # @return [String] The signature of the billing address request # @raise [ArgumentError] Thrown if shared_secret is empty def calculate_billing_address_signature(parameters, shared_secret = nil) shared_secret ||= parameters.delete(:shared_secret) raise ArgumentError, "Cannot calculate billing address request signature with empty shared_secret" if shared_secret.to_s.empty? - Adyen::Encoding.hmac_base64(shared_secret, calculate_billing_address_signature_string(parameters[:billing_address])) + Adyen::Util.hmac_base64(shared_secret, calculate_billing_address_signature_string(parameters[:billing_address])) end # shopperSig: shopper.firstName + shopper.infix + shopper.lastName + shopper.gender + shopper.dateOfBirthDayOfMonth + shopper.dateOfBirthMonth + shopper.dateOfBirthYear + shopper.telephoneNumber # (Note that you can send only shopper.firstName and shopper.lastName if you like. Do NOT include shopperSocialSecurityNumber in the shopperSig!) def calculate_shopper_signature_string(parameters) @@ -294,23 +294,23 @@ end def calculate_shopper_signature(parameters, shared_secret = nil) shared_secret ||= parameters.delete(:shared_secret) raise ArgumentError, "Cannot calculate shopper request signature with empty shared_secret" if shared_secret.to_s.empty? - Adyen::Encoding.hmac_base64(shared_secret, calculate_shopper_signature_string(parameters[:shopper])) + Adyen::Util.hmac_base64(shared_secret, calculate_shopper_signature_string(parameters[:shopper])) end ###################################################### # REDIRECT SIGNATURE CHECKING ###################################################### # Generates the string for which the redirect signature is calculated, using the request paramaters. # @param [Hash] params A hash of HTTP GET parameters for the redirect request. # @return [String] The signature string. def redirect_signature_string(params) - params[:authResult].to_s + params[:pspReference].to_s + params[:merchantReference].to_s + - params[:skinCode].to_s + params[:merchantReturnData].to_s + params['authResult'].to_s + params['pspReference'].to_s + params['merchantReference'].to_s + + params['skinCode'].to_s + params['merchantReturnData'].to_s end # Computes the redirect signature using the request parameters, so that the # redirect can be checked for forgery. # @@ -319,13 +319,13 @@ # the original payment form. You can leave this out of the skin is registered # using the {Adyen::Form.register_skin} method. # @return [String] The redirect signature # @raise [ArgumentError] Thrown if shared_secret is empty def redirect_signature(params, shared_secret = nil) - shared_secret ||= Adyen.configuration.form_skin_shared_secret_by_code(params[:skinCode]) + shared_secret ||= Adyen.configuration.form_skin_shared_secret_by_code(params['skinCode']) raise ArgumentError, "Cannot compute redirect signature with empty shared_secret" if shared_secret.to_s.empty? - Adyen::Encoding.hmac_base64(shared_secret, redirect_signature_string(params)) + Adyen::Util.hmac_base64(shared_secret, redirect_signature_string(params)) end # Checks the redirect signature for this request by calcultating the signature from # the provided parameters, and comparing it to the signature provided in the +merchantSig+ # parameter. @@ -356,45 +356,10 @@ # the original payment form. You can leave this out of the skin is registered # using the {Adyen::Configuration#register_form_skin} method. # @return [true, false] Returns true only if the signature in the parameters is correct. def redirect_signature_check(params, shared_secret = nil) raise ArgumentError, "params should be a Hash" unless params.is_a?(Hash) - raise ArgumentError, "params should contain :merchantSig" unless params.key?(:merchantSig) - params[:merchantSig] == redirect_signature(params, shared_secret) - end - - # Returns the camelized version of a string. - # @param [:to_s] identifier The identifier to turn to camelcase - # @return [String] The camelcase version of the identifier provided. - def camelize(identifier) - identifier.to_s.gsub(/_(.)/) { $1.upcase } - end - - # Transforms the nested parameters Hash into a 'flat' Hash which is understood by adyen. This is: - # * all keys are camelized - # * all keys are stringified - # * nested hash is flattened, keys are prefixed with root key - # - # @example - # flatten {:billing_address => { :street => 'My Street'}} - # - # # resolves in: - # {'billingAddress.street' => 'My Street'} - # - # @param [Hash] parameters The payment parameters which to transform - # @param [String] prefix The prefix to add to the key - # @param [Hash] return_hash The new hash which is retruned (needed for recursive calls) - # @return [Hash] The return_hash filled with camelized and prefixed key, stringified value - def flatten(parameters, prefix = "", return_hash = {}) - parameters ||= {} - parameters.inject(return_hash) do |hash, (key, value)| - key = "#{prefix}#{camelize(key)}" - if value.is_a?(Hash) - flatten(value, "#{key}.", return_hash) - else - hash[key] = value.to_s - end - hash - end + raise ArgumentError, "params should contain :merchantSig" unless params.key?('merchantSig') + params['merchantSig'] == redirect_signature(params, shared_secret) end end end