class MultibancoIfthenpay::Calculator

Multibanco reference calculation formula

Constants

CHK_ARRAY

Public Class Methods

get_chk_digits(chk_str) click to toggle source

@param [String] chk_str @return [String]

# 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) click to toggle source

@param [String] entity @param [String] sub_entity @param [String] order_id @param [Float] order_value @return [String]

# 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) click to toggle source

@param [String] entity @param [String] sub_entity @param [String] order_id @param [Float] order_value @return [String]

# 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