class Payonline::Signature

Public Class Methods

new(params, keys, add_merchant_id = true) click to toggle source
# File lib/payonline/signature.rb, line 3
def initialize(params, keys, add_merchant_id = true)
  @keys = keys

  # The order of parameter keys matters
  @params = prepare_params(params, add_merchant_id)

  # Permitted content_type values: text, xml
  @params[:security_key] = digest
  @params[:content_type] = 'text'
end

Public Instance Methods

digest() click to toggle source
# File lib/payonline/signature.rb, line 19
def digest
  Digest::MD5.hexdigest(digest_data)
end
sign() click to toggle source

A signed copy of params

# File lib/payonline/signature.rb, line 15
def sign
  @params.transform_keys { |key| key.to_s.camelize }
end

Private Instance Methods

digest_data() click to toggle source

Prepare params for digest

# File lib/payonline/signature.rb, line 36
def digest_data
  digest_params = @params.slice(*@keys) if @keys.present?
  digest_params[:private_security_key] = Payonline.configuration.private_security_key

  # HACK: PayOnline sends OrderId but TransactionID (notice the letter case)
  digest_params
    .transform_keys { |key| key.to_s.camelize }
    .transform_keys { |key| key == 'TransactionId' ? 'TransactionID' : key }
    .map { |key, value| "#{key}=#{value}" }
    .join('&')
end
prepare_params(params, add_merchant_id = true) click to toggle source

Prepend params hash with merchant_id

# File lib/payonline/signature.rb, line 26
def prepare_params(params, add_merchant_id = true)
  if add_merchant_id
    params.reverse_merge!(merchant_id: Payonline.configuration.merchant_id)
    @keys.unshift(:merchant_id)
  end

  params.with_indifferent_access
end