Class: MultibancoIfthenpay::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/multibanco_ifthenpay/calculator.rb

Overview

Multibanco reference calculation formula

Constant Summary collapse

CHK_ARRAY =
[3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38,
89, 17, 73, 51].freeze

Class Method Summary collapse

Class Method Details

.get_chk_digits(chk_str) ⇒ String

Parameters:

  • chk_str (String)

Returns:

  • (String)


36
37
38
39
40
41
42
43
44
# File 'lib/multibanco_ifthenpay/calculator.rb', line 36

def self.get_chk_digits(chk_str)
  chk_val = CHK_ARRAY.each_with_index.reduce(0) do |val, (chk_item, index)|
    val + (chk_str[CHK_ARRAY.length - index - 1].to_i % 10) * chk_item
  end

  chk_val %= 97

  format('%02.0f', (98 - chk_val))
end

.get_chk_string(entity, sub_entity, order_id, order_value) ⇒ String

Parameters:

  • entity (String)
  • sub_entity (String)
  • order_id (String)
  • order_value (Float)

Returns:

  • (String)


29
30
31
32
# File 'lib/multibanco_ifthenpay/calculator.rb', line 29

def self.get_chk_string(entity, sub_entity, order_id, order_value)
  entity + sub_entity + order_id[-(7 - sub_entity.length)..-1] +
    format('%08.0f', (order_value.to_f * 100))
end

.get_multibanco_reference(entity, sub_entity, order_id, order_value) ⇒ String

Parameters:

  • entity (String)
  • sub_entity (String)
  • order_id (String)
  • order_value (Float)

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/multibanco_ifthenpay/calculator.rb', line 12

def self.get_multibanco_reference(entity, sub_entity, order_id, order_value)
  order_id = '0000000000' + order_id

  order_value = format('%.2f', order_value)

  chk_str = get_chk_string(entity, sub_entity, order_id, order_value)

  chk_digits = get_chk_digits(chk_str)

  chk_str[5, 3] + chk_str[8, 3] + chk_str[11, 1] + chk_digits
end