From 11d4d0d80e3fccce2f627b625a1bc94e100d9c7c Mon Sep 17 00:00:00 2001 From: Sahil Gadimbayli Date: Mon, 11 May 2020 23:44:03 +0200 Subject: [PATCH 1/2] Build Boleto entities based on BS2 Schema. --- lib/entities/boleto.ex | 66 ++++++++++++++++++++++++++++++++ lib/entities/client.ex | 2 +- lib/entities/desconto.ex | 27 +++++++++++++ lib/entities/endreco.ex | 35 +++++++++++++++++ lib/entities/mensagem.ex | 27 +++++++++++++ lib/entities/multa.ex | 24 ++++++++++++ lib/entities/sacado.ex | 34 ++++++++++++++++ lib/entities/sacador_avalista.ex | 29 ++++++++++++++ 8 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 lib/entities/boleto.ex create mode 100644 lib/entities/desconto.ex create mode 100644 lib/entities/endreco.ex create mode 100644 lib/entities/mensagem.ex create mode 100644 lib/entities/multa.ex create mode 100644 lib/entities/sacado.ex create mode 100644 lib/entities/sacador_avalista.ex diff --git a/lib/entities/boleto.ex b/lib/entities/boleto.ex new file mode 100644 index 0000000..5bf8f0b --- /dev/null +++ b/lib/entities/boleto.ex @@ -0,0 +1,66 @@ +defmodule BancoBs2.Entities.Boleto do + @moduledoc """ + Struct holding information for Boleto + slips. + """ + + alias BancoBs2.Entities.Desconto + alias BancoBs2.Entities.Mensagem + alias BancoBs2.Entities.Multa + alias BancoBs2.Entities.Sacado + alias BancoBs2.Entities.SacadorAvalista + + defstruct id: nil, + sacado: nil, + status: nil, + nosso_numero: nil, + codigo_de_barra: nil, + linha_digitavel: nil, + seu_numero: nil, + cliente_id: nil, + sacador_avalista: nil, + vencimento: nil, + valor: nil, + canal: nil, + multa: nil, + desconto: nil, + mensagem: nil + + @typedoc """ + Type that represents `Boleto` struct with: + + id: String.t(), + sacado: Sacado.t(), + status: integer, + nosso_numero: integer, + codigo_de_barra: String.t(), + linha_digitavel: String.t(), + seu_numero: String.t(), + cliente_id: String.t(), + sacador_avalista: SacadorAvalista.t(), + vencimento: String.t(), + valor: integer, + canal: String.t(), + multa: Multa.t(), + desconto: Desconto.t(), + mensagem: Mensagem.t() + """ + + @type t :: %BancoBs2.Entities.Boleto{ + id: String.t(), + sacado: Sacado.t(), + status: integer, + nosso_numero: integer, + codigo_de_barra: String.t(), + linha_digitavel: String.t(), + seu_numero: String.t(), + cliente_id: String.t(), + sacador_avalista: SacadorAvalista.t(), + vencimento: String.t(), + valor: integer, + canal: String.t(), + multa: Multa.t(), + desconto: Desconto.t(), + mensagem: Mensagem.t() + } +end diff --git a/lib/entities/client.ex b/lib/entities/client.ex index a500582..0a53bea 100644 --- a/lib/entities/client.ex +++ b/lib/entities/client.ex @@ -1,6 +1,6 @@ defmodule BancoBs2.Entities.Client do @moduledoc """ - `Client` struct holding `OAuth` information. + Struct holding `OAuth` information. Filled after successful authentication. To be used in further API requests. diff --git a/lib/entities/desconto.ex b/lib/entities/desconto.ex new file mode 100644 index 0000000..f995094 --- /dev/null +++ b/lib/entities/desconto.ex @@ -0,0 +1,27 @@ +defmodule BancoBs2.Entities.Desconto do + @moduledoc """ + Struct representing Desconto. Usually part + of a Boleto entity. + """ + + defstruct percentual: nil, + valorFixo: nil, + valorDiario: nil, + limite: nil + + @typedoc """ + Type that represents Desconto with: + + percentual: float(), + valorFixo: float(), + valorDiario: float(), + limite: String.t() + """ + + @type t :: %BancoBs2.Entities.Desconto{ + percentual: float(), + valorFixo: float(), + valorDiario: float(), + limite: String.t() + } +end diff --git a/lib/entities/endreco.ex b/lib/entities/endreco.ex new file mode 100644 index 0000000..a8dca67 --- /dev/null +++ b/lib/entities/endreco.ex @@ -0,0 +1,35 @@ +defmodule BancoBs2.Entities.Endreco do + @moduledoc """ + Entity representing Endreco. + """ + + defstruct cep: nil, + estado: nil, + cidade: nil, + bairro: nil, + logradouro: nil, + numero: nil, + complemento: nil + + @typedoc """ + Type that represents `Endreco` with: + + cep: String.t(), + estado: String.t(), + cidade: String.t(), + bairro: String.t(), + logradouro: String.t(), + numero: String.t(), + complemento: String.t() + """ + + @type t :: %BancoBs2.Entities.Endreco{ + cep: String.t(), + estado: String.t(), + cidade: String.t(), + bairro: String.t(), + logradouro: String.t(), + numero: String.t(), + complemento: String.t() + } +end diff --git a/lib/entities/mensagem.ex b/lib/entities/mensagem.ex new file mode 100644 index 0000000..a4f4cb0 --- /dev/null +++ b/lib/entities/mensagem.ex @@ -0,0 +1,27 @@ +defmodule BancoBs2.Entities.Mensagem do + @moduledoc """ + Struct representing Mensagem. Usually part + of a Boleto entity. + """ + + defstruct linha1: nil, + linha2: nil, + linha3: nil, + linha4: nil + + @typedoc """ + Type that represents Desconto with: + + linha1: String.t(), + linha2: String.t(), + linha3: String.t(), + linha4: String.t() + """ + + @type t :: %BancoBs2.Entities.Mensagem{ + linha1: String.t(), + linha2: String.t(), + linha3: String.t(), + linha4: String.t() + } +end diff --git a/lib/entities/multa.ex b/lib/entities/multa.ex new file mode 100644 index 0000000..7856fe3 --- /dev/null +++ b/lib/entities/multa.ex @@ -0,0 +1,24 @@ +defmodule BancoBs2.Entities.Multa do + @moduledoc """ + Struct representing Multa. Usually part + of a Boleto entity. + """ + + defstruct valor: nil, + data: nil, + juros: nil + + @typedoc """ + Type that represents Multa with: + + valor: float(), + data: String.t(), + juros: float() + """ + + @type t :: %BancoBs2.Entities.Multa{ + valor: float(), + data: String.t(), + juros: float() + } +end diff --git a/lib/entities/sacado.ex b/lib/entities/sacado.ex new file mode 100644 index 0000000..49ef9c0 --- /dev/null +++ b/lib/entities/sacado.ex @@ -0,0 +1,34 @@ +defmodule BancoBs2.Entities.Sacado do + @moduledoc """ + Struct representing Sacado. Usually part + of a Boleto entity. + """ + + alias BancoBs2.Entities.Endreco + + defstruct email: nil, + telefone: nil, + tipo: nil, + documento: nil, + nome: nil, + endreco: nil + + @typedoc """ + Type that represents `Sacado` with: + email: String.t(), + telefone: String.t(), + tipo: integer, + documento: String.t(), + nome: String.t(), + endreco: Endreco.t() + """ + + @type t :: %BancoBs2.Entities.Sacado{ + email: String.t(), + telefone: String.t(), + tipo: integer, + documento: String.t(), + nome: String.t(), + endreco: Endreco.t() + } +end diff --git a/lib/entities/sacador_avalista.ex b/lib/entities/sacador_avalista.ex new file mode 100644 index 0000000..58a22ff --- /dev/null +++ b/lib/entities/sacador_avalista.ex @@ -0,0 +1,29 @@ +defmodule BancoBs2.Entities.SacadorAvalista do + @moduledoc """ + Struct representing SacadorAvalista. Usually + part of a Boleto entity. + """ + + alias BancoBs2.Entities.Endreco + + defstruct tipo: nil, + documento: nil, + nome: nil, + endreco: nil + + @typedoc """ + Type that represents SacadorAvalista with: + + tipo: integer, + documento: String.t(), + nome: String.t(), + endreco: Endreco.t() + """ + + @type t :: %BancoBs2.Entities.SacadorAvalista{ + tipo: integer, + documento: String.t(), + nome: String.t(), + endreco: Endreco.t() + } +end From 8ca7a1548ece53ae3976816af167ee59e00768df Mon Sep 17 00:00:00 2001 From: Sahil Gadimbayli Date: Wed, 20 May 2020 21:52:59 +0200 Subject: [PATCH 2/2] add plugin --- lib/apis/collection/boletos.ex | 46 ++++++++++++++++++++++++++++++++++ lib/plugins/auth_headers.ex | 21 ++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 lib/apis/collection/boletos.ex create mode 100644 lib/plugins/auth_headers.ex diff --git a/lib/apis/collection/boletos.ex b/lib/apis/collection/boletos.ex new file mode 100644 index 0000000..b756e49 --- /dev/null +++ b/lib/apis/collection/boletos.ex @@ -0,0 +1,46 @@ +defmodule BancoBs2.Apis.Collection.Boletos do + @moduledoc """ + Module to interact with Collection API. + """ + + @base_url Application.get_env(:banco_bs2, :base_url) + @base_endpoint "/pj/forintegration/cobranca/v1/boletos" + @default_headers %{"Content-type": "application/json"} + + alias BancoBs2.Entities.Client + alias BancoBs2.ApiError + alias BancoBs2.Entities.Boleto + alias HTTPoison.Response + alias HTTPoison.Error + + import BancoBs2.Plugins.AuthHeaders + + @doc """ + Creates a Boleto. See available parameters to pass + [here](https://devs.bs2.com/manual/cobranca/#incluir-boleto-de-cobran%C3%A7a) + + If succesful, returns {:ok, %Boleto{}}, else {:error, %ApiError{}}. + """ + + @spec create(Client.t(), map) :: {atom, Boleto.t() | ApiError.t()} + def create(client, create_params) do + case HTTPoison.post( + "#{@base_url}#{@base_endpoint}/simplificado", + Jason.encode!(create_params), + auth_headers(client, @default_headers) + ) do + {:ok, %Response{status_code: 200, body: body}} -> + Jason.decode!(body) + + {:ok, response} -> + handle_error(response) + + {:error, %Error{reason: reason}} -> + {:error, %ApiError{message: "Connection error occured: #{reason}"}} + end + end + + defp handle_error(response) do + "came here" + end +end diff --git a/lib/plugins/auth_headers.ex b/lib/plugins/auth_headers.ex new file mode 100644 index 0000000..ca99955 --- /dev/null +++ b/lib/plugins/auth_headers.ex @@ -0,0 +1,21 @@ +defmodule BancoBs2.Plugins.AuthHeaders do + @moduledoc """ + Contains shared functions to be used in API modules. + """ + + @doc """ + Generates HTTP headers with including access_token + from %Client{} entity. + + Returns %{} with necessary headers. + """ + + @spec auth_headers(Client.t(), map) :: map + def auth_headers(%{token_type: token_type, access_token: access_token}, default_headers \\ %{}) do + Map.put( + default_headers, + :Authorization, + token_type <> " " <> access_token + ) + end +end