Module: BuckarooClient::Gateway::NVP::Signature

Included in:
BuckarooClient::Gateway::NVP, Response
Defined in:
lib/buckaroo_client/gateway/nvp/signature.rb

Instance Method Summary collapse

Instance Method Details

#secret_keyObject



7
8
9
# File 'lib/buckaroo_client/gateway/nvp/signature.rb', line 7

def secret_key
  ENV['BUCKAROO_CLIENT_SECRET'] || raise("BUCKAROO_CLIENT_SECRET not set")
end

#signature(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/buckaroo_client/gateway/nvp/signature.rb', line 11

def signature(input)
  # Base logic and comments taken from github.com/inventid/buckaroo
  #
  # This might actually need some explanation why we are converting do lowercase here
  # BuckarooClient specifies to sort these parameters, although the exact matter of sorting
  # is quite ambigious. So after quite a while of debugging, I discovered that by
  # sorting they do not use the ASCII based sorting Ruby uses. In fact, the sorting
  # is specified to place symbols first (which ASCII does, except for the underscore (_)
  # which is located between the capitals and lowercase letters (jeej ASCII!).
  # So in this case, by converting everything to lowercase before comparing, we ensure
  # that all symbols are in the table before the letters.
  #
  # Actual case where it went wrong: keys BRQ_TRANSACTIONS and BRQ_TRANSACTION_CANCELABLE
  # Ruby would sort these in this exact order, whereas BuckarooClient would reverse them. And
  # since for hashing the reversal generates a totally different sequence, that would
  # break message validation.
  #
  # TLDR; Leave it with a downcase
  sorted_data = input.sort_by { |key, _| key.to_s.downcase }
  to_hash = ''
  sorted_data.each { |key, value| to_hash << key.to_s+'='+value.to_s }
  to_hash << secret_key
  Digest::SHA1.hexdigest(to_hash)
end