Class: Buckaroo::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/buckaroo/response.rb

Direct Known Subclasses

StatusResponse, TransactionResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Response

Returns a new instance of Response.



11
12
13
14
15
16
17
# File 'lib/buckaroo/response.rb', line 11

def initialize(response, options = {})
  body = response.body
  @response = Hash[Addressable::URI.form_unencode(body)]
  @status_code = @response['BRQ_STATUSCODE'].to_i
  @success = !error_occurred?
  @test = @response['BRQ_TEST'].downcase == 'true' ? true : false
end

Instance Attribute Details

#status_codeObject

Returns the value of attribute status_code.



9
10
11
# File 'lib/buckaroo/response.rb', line 9

def status_code
  @status_code
end

Instance Method Details

#success?Boolean

Returns whether the request was a success

Returns:

  • (Boolean)


25
26
27
# File 'lib/buckaroo/response.rb', line 25

def success?
  @success
end

#test?Boolean

Returns whether we’re running in test mode

Returns:

  • (Boolean)


20
21
22
# File 'lib/buckaroo/response.rb', line 20

def test?
  @test
end

#verified?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/buckaroo/response.rb', line 29

def verified?
  input = @response.dup
  given_hash = input['BRQ_SIGNATURE']
  input.delete('BRQ_SIGNATURE')
  # This might actually need some explanation why we are converting do lowercase here
  # Buckaroo 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 Buckaroo 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 << Buckaroo::Gateway.secret

  Digest::SHA512.hexdigest(to_hash) == given_hash
end