class BankApi::Clients::BancoDeChileCompanyClient

Constants

COMPANY_ACCOUNT_DEPOSITS_URL
COMPANY_CC_BALANCE_URL
COMPANY_DEPOSITS_TXT_URL
COMPANY_DEPOSITS_URL
COMPANY_LOGIN_URL
COMPANY_PREPARE_DEPOSITS_URL
COMPANY_PREPARE_WITHDRAWALS_URL
COMPANY_WITHDRAWALS_TXT_URL
COMPANY_WITHDRAWALS_URL

Public Class Methods

new(config = BankApi::Configuration.new) click to toggle source
Calls superclass method
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 19
def initialize(config = BankApi::Configuration.new)
  @bdc_company_rut = config.bdc_company_rut
  @bdc_user_rut = config.bdc_user_rut
  @bdc_password = config.bdc_password
  @bdc_account = config.bdc_account
  super
end

Public Instance Methods

bank_name() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 27
def bank_name
  :bancochile
end

Private Instance Methods

account_deposits_from_txt() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 153
def account_deposits_from_txt
  url = browser.search("#expoDato_child > a:nth-child(3)").attribute(:href)
  result = browser.download(url)
                  .content.encode("UTF-8", "iso-8859-3")
                  .delete("\r").split("\n").drop(2)
  format_account_transactions(result)
end
base_deposits_payload(from_date, to_date) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 286
def base_deposits_payload(from_date, to_date)
  {
    'initDate' => from_date,
    'endDate' => to_date,
    'ctaCorriente' => padded_account,
    'nada' => 'nada'
  }
end
base_withdrawals_payload(from_date, to_date) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 295
def base_withdrawals_payload(from_date, to_date)
  {
    "llavePre" => "",
    "llaveIns" => "",
    "rutIns" => "",
    "pag" => "",
    "campo" => "",
    'initDate' => from_date,
    'endDate' => to_date,
    "cuentaCargo" => padded_account,
    "destinatario" => "",
    "operacion" => "",
    "estado" => "12",
    'nada' => 'nada'
  }
end
click_fetch_balance_button() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 66
def click_fetch_balance_button
  browser.search('#btnSeleccionarCuenta').click
end
click_login_button() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 137
def click_login_button
  browser.search('.btn_amarillodegrade').click
end
cookies() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 329
def cookies
  selenium_browser.manage.all_cookies.map do |cookie|
    "#{cookie[:name]}=#{cookie[:value]}"
  end.join("; ")
end
date_range() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 339
def date_range
  @date_range ||= begin
    timezone = Timezone['America/Santiago']
    today = timezone.utc_to_local(Time.now).to_date
    { start: (today - @days_to_check).strftime("%d/%m/%Y"), end: today.strftime("%d/%m/%Y") }
  end
end
deposits_from_txt() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 167
def deposits_from_txt
  prepare_deposits
  response = RestClient::Request.execute(
    url: COMPANY_DEPOSITS_TXT_URL, method: :post, headers: session_headers,
    payload: deposits_txt_payload(date_range[:start], date_range[:end]), verify_ssl: true
  )
  raise "Banchile is down" if response.body.include? "no podemos atenderle"
  transactions = split_deposit_transactions(response.body)
  format_deposit_transactions(transactions)
rescue => e
  validate_banchile_status!
  raise e
end
deposits_txt_payload(from_date, to_date) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 274
def deposits_txt_payload(from_date, to_date)
  base_deposits_payload(from_date, to_date).merge('accion' => 'exportarTxtOperaciones')
end
format_account_transactions(transactions) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 244
def format_account_transactions(transactions)
  transactions.inject([]) do |memo, t|
    parts = t.split(";")
    amount = parts[3].to_i
    next memo if amount.zero?

    memo << {
      client: format_client_name(parts[1]),
      rut: nil,
      date: Date.parse(parts[0]),
      time: nil,
      amount: amount
    }

    memo
  end
end
format_client_name(name) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 262
def format_client_name(name)
  name.to_s.split("DE:").last.to_s.strip
end
format_deposit_transactions(transactions) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 220
def format_deposit_transactions(transactions)
  transactions.map do |t|
    {
      client: format_client_name(t[3]),
      rut: t[4],
      date: Date.parse(t[0]),
      time: nil,
      amount: t[6].to_i
    }
  end
end
format_withdrawal_transactions(transactions) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 232
def format_withdrawal_transactions(transactions)
  transactions.map do |t|
    {
      rut: Utils::BancoDeChile.format_rut(t[25..34]),
      client: format_client_name(t[35..64].strip),
      account_number: t[65..82].strip,
      amount: t[93..103].to_i,
      email: t[166..216].strip
    }
  end
end
get_balance(options) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 41
def get_balance(options)
  login
  goto_balance
  select_account(options[:account_number])
  click_fetch_balance_button
  {
    account_number: options[:account_number],
    available_balance: money_to_i(read_balance(:available)),
    countable_balance: money_to_i(read_balance(:countable))
  }
ensure
  browser.close
end
get_deposits(options = {}) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 84
def get_deposits(options = {})
  login

  if options[:source] == :account_details
    return get_deposits_from_balance_section
  end

  get_deposits_from_transfers_section
ensure
  browser.close
end
get_deposits_from_balance_section() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 104
def get_deposits_from_balance_section
  goto_account_deposits
  account_deposits_from_txt
end
get_deposits_from_transfers_section() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 109
def get_deposits_from_transfers_section
  goto_deposits
  deposits_from_txt
end
get_withdrawals() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 96
def get_withdrawals
  login

  get_withdrawals_from_transfers_section
ensure
  browser.close
end
get_withdrawals_from_transfers_section() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 114
def get_withdrawals_from_transfers_section
  goto_withdrawals
  withdrawals_from_txt
end
goto_account_deposits() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 141
def goto_account_deposits
  browser.goto COMPANY_ACCOUNT_DEPOSITS_URL
end
goto_balance() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 55
def goto_balance
  browser.goto COMPANY_CC_BALANCE_URL
end
goto_deposits() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 145
def goto_deposits
  browser.goto COMPANY_DEPOSITS_URL
end
goto_login_url() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 125
def goto_login_url
  browser.goto COMPANY_LOGIN_URL
end
goto_withdrawals() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 149
def goto_withdrawals
  browser.goto COMPANY_WITHDRAWALS_URL
end
login() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 119
def login
  goto_login_url
  set_login_values
  click_login_button
end
money_to_i(text) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 70
def money_to_i(text)
  text.delete(".").delete("$").delete(" ").to_i
end
padded_account() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 266
def padded_account
  "0" * (12 - @bdc_account.length) + @bdc_account
end
prepare_deposits() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 181
def prepare_deposits
  RestClient::Request.execute(
    url: COMPANY_PREPARE_DEPOSITS_URL, method: :post, headers: session_headers,
    payload: prepare_deposits_payload(date_range[:start], date_range[:end]),
    verify_ssl: true
  )
end
prepare_deposits_payload(from_date, to_date) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 270
def prepare_deposits_payload(from_date, to_date)
  base_deposits_payload(from_date, to_date).merge('accion' => 'buscarOperaciones')
end
prepare_withdrawals() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 203
def prepare_withdrawals
  RestClient::Request.execute(
    url: COMPANY_PREPARE_WITHDRAWALS_URL, method: :post, headers: session_headers,
    payload: prepare_withdrawals_payload(date_range[:start], date_range[:end])
  )
end
prepare_withdrawals_payload(from_date, to_date) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 278
def prepare_withdrawals_payload(from_date, to_date)
  base_withdrawals_payload(from_date, to_date).merge('accion' => 'consulta')
end
read_balance(balance_kind) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 74
def read_balance(balance_kind)
  return '' unless [:available, :countable].include? balance_kind

  if balance_kind == :available
    return browser.search('table.detalleSaldosMov tr:nth-child(2) > td.aRight.bold').text
  end

  browser.search('table.detalleSaldosMov tr:first-child > td.aRight.bold').text
end
select_account(account_number) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 59
def select_account(account_number)
  first_account = browser.search("select[name=cuenta] option").find do |account|
    account.value.include? account_number
  end.value
  browser.search("select[name=cuenta]").set by_value: first_account
end
session_headers() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 312
def session_headers
  {
    "Pragma" => "no-cache",
    "Connection" => "keep-alive",
    "Cache-Control" => "no-cache",
    "Upgrade-Insecure-Requests" => 1,
    "Content-Type" => "application/x-www-form-urlencoded",
    "Origin" => "https://www.empresas.bancochile.cl",
    "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 " +
      "(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9," +
      "image/webp,image/apng,*/*;q=0.8",
    "Accept-Encoding" => "gzip, deflate, br",
    "Cookie" => cookies
  }
end
set_login_values() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 129
def set_login_values
  browser.search('#rutemp1').set without_verifier_digit_or_separators(@bdc_company_rut)
  browser.search('#dvemp1').set verifier_digit(@bdc_company_rut)
  browser.search('#rut1').set without_verifier_digit_or_separators(@bdc_user_rut)
  browser.search('#verificador1').set verifier_digit(@bdc_user_rut)
  browser.search('#pin1').set @bdc_password
end
split_deposit_transactions(transactions_str) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 210
def split_deposit_transactions(transactions_str)
  transactions_str.delete("\r").split("\n").drop(1).map { |r| r.split(";") }.select do |t|
    t[7] == "Aprobada"
  end
end
split_withdrawal_transactions(transactions_str) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 216
def split_withdrawal_transactions(transactions_str)
  transactions_str.delete("\r").split("\n")
end
validate_banchile_status!() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 161
def validate_banchile_status!
  unless browser.search(".textoerror:contains('no podemos atenderle')").none?
    raise "Banchile is down"
  end
end
validate_credentials() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 33
def validate_credentials
  raise BankApi::MissingCredentialsError if [
    @bdc_company_rut,
    @bdc_user_rut,
    @bdc_password
  ].any?(&:nil?)
end
withdrawals_from_txt() click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 189
def withdrawals_from_txt
  prepare_withdrawals
  response = RestClient::Request.execute(
    url: COMPANY_WITHDRAWALS_TXT_URL, method: :post, headers: session_headers,
    payload: withdrawals_txt_payload(date_range[:start], date_range[:end])
  )
  raise "Banchile is down" if response.body.include? "no podemos atenderle"
  transactions = split_withdrawal_transactions(response.body)
  format_withdrawal_transactions(transactions)
rescue => e
  validate_banchile_status!
  raise e
end
withdrawals_txt_payload(from_date, to_date) click to toggle source
# File lib/bank_api/clients/banco_de_chile_company_client.rb, line 282
def withdrawals_txt_payload(from_date, to_date)
  base_withdrawals_payload(from_date, to_date).merge('accion' => 'exportarTxt')
end