Packages

Elixir package to validate bank account numbers of Brazilian banks.

Retired package: Security issue - Not published by owners

Current section

Files

Jump to
bran_checker lib banks santander.ex
Raw

lib/banks/santander.ex

defmodule BRAN.Banks.Santander do
alias BRAN.DigitCalculator
import BRAN.Banks.Util
@moduledoc """
Documentation `BRAN.Banks.Santander`
"""
@weigths [9, 7, 3, 1, 0, 0, 9, 7, 1, 3, 1, 9, 7, 3]
@account_types [
"01",
"02",
"03",
"05",
"07",
"09",
"13",
"27",
"35",
"37",
"43",
"45",
"46",
"48",
"50",
"53",
"60",
"92"
]
@doc """
Returns a boolean, after checking if the combination of agency_number, account_number and digit is valid
##Examples
iex> BRAN.Santander.is_valid?([2,5,4,5], [0,2,3,6,6,0,2,3], 1)
:true
"""
@spec is_valid?(List.t(), List.t(), Integer.t()) :: Boolean.t()
def is_valid?(agency_number, account_number, digit) do
with true <- is_valid_agency_number?(agency_number),
true <- is_valid_account_number?(account_number),
true <- is_valid_account_type?(account_number) do
full_account_number = agency_number ++ [0, 0] ++ account_number
validating_digit =
full_account_number
|> DigitCalculator.calc_numbers(@weigths, false)
|> rem(10)
|> calc_digit()
validating_digit == digit
else
_ -> false
end
end
defp is_valid_account_number?(account_number), do: length(account_number) == 8
defp is_valid_account_type?(account_number) do
account_type =
account_number
|> Enum.take(2)
|> Enum.join()
Enum.member?(@account_types, account_type)
end
defp calc_digit(digit) when digit == 0, do: digit
defp calc_digit(digit), do: 10 - digit
end