Sha256: 82a2741785b7077eab5ceed3ae8150a4d871d906c60a3f40afe5a5c90b4304fa

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8

require 'wirecard_checkout_page/value_handling'

module WirecardCheckoutPage
  class ResponseChecksum
    include WirecardCheckoutPage::Utils
    include WirecardCheckoutPage::ValueHandling

    def initialize(values = {})
      @values = stringify_keys(values)
      @secret = @values.delete('secret') or
        raise WirecardCheckoutPage::ValueMissing, 'value "secret" is missing'
      # This rails form value is escaped as an html entity by
      # WirecardCheckoutPage, so set it back to the original UTF-8 here if it
      # exists:
      @values['utf8'] and @values['utf8'] = '✓'
      @values.freeze
      reset_missing_keys
    end

    attr_reader :values

    attr_reader :expected_fingerprint

    attr_reader :computed_fingerprint

    def fingerprint
      values = @values.dup
      values['secret'] ||= @secret
      if seed = responseFingerprintSeed(responseFingerprintOrder(values), values)
        Digest::MD5.hexdigest seed
      end
    end

    def valid?
      reset_missing_keys
      @expected_fingerprint = values.fetch('responseFingerprint') do |k|
        add_missing_key k
      end
      @computed_fingerprint = fingerprint
      !missing_keys? && computed_fingerprint == @expected_fingerprint
    end

    private

    def responseFingerprintOrder(values)
      order = values.fetch('responseFingerprintOrder') do |k|
        add_missing_key k
        return []
      end
      order.split(',')
    end

    def responseFingerprintSeed(keys, values)
      fingerprint = keys.map do |k|
        values.fetch(k) do
          add_missing_key k
        end
      end * ''
      fingerprint unless missing_keys?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wirecard_checkout_page-0.0.1 lib/wirecard_checkout_page/response_checksum.rb
wirecard_checkout_page-0.0.0 lib/wirecard_checkout_page/response_checksum.rb