Skip to content

Commit 4305d05

Browse files
authored
Merge pull request #6 from jonatanrdsantos/add-cobranca
Add "cobranca boleto" to the sdk
2 parents 1e6aaa2 + eb9961a commit 4305d05

File tree

20 files changed

+531
-3
lines changed

20 files changed

+531
-3
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
name: Test
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Elixir
18+
uses: erlef/setup-beam@v1
19+
with:
20+
elixir-version: '1.15.7' # Set this to the version you use
21+
otp-version: '26.2.1' # Set this to the version you use
22+
23+
- name: Restore dependencies cache
24+
uses: actions/cache@v3
25+
with:
26+
path: |
27+
deps
28+
_build
29+
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
30+
restore-keys: ${{ runner.os }}-mix-
31+
32+
- name: Install dependencies
33+
run: mix deps.get
34+
35+
- name: Run tests
36+
run: mix test
37+
env:
38+
# Add any environment variables your tests need here
39+
INTER_CLIENT_ID: ${{ secrets.INTER_CLIENT_ID }}
40+
INTER_CLIENT_SECRET: ${{ secrets.INTER_CLIENT_SECRET }}
41+
INTER_SCOPE: ${{ secrets.INTER_SCOPE }}
42+
INTER_API_CERT: ${{ secrets.INTER_API_CERT }}
43+
INTER_API_KEY: ${{ secrets.INTER_API_KEY }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ inter-*.tar
2424

2525
# Temporary files, for example, from tests.
2626
/tmp/
27+
28+
.env
29+
config

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
To execute the container locally, please create a new integration in the https://developers.inter.co/sandbox/my-integrations download the key and cert, rename the files and put inside a folder called `config` like:
2+
3+
elixir-inder-sdk/
4+
├── docker-compose.yml
5+
├── config/
6+
├── certificate.crt
7+
└── certificate.key

Dockerfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM elixir:1.15
1+
FROM elixir:1.17
22

33
RUN apt-get update && \
44
apt-get install -y inotify-tools && \

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ services:
66
command: "bash"
77
volumes:
88
- .:/app
9+
- ./config/certificate.crt:/secrets/certificate.crt:ro
10+
- ./config/certificate.key:/secrets/certificate.key:ro
11+
entrypoint: ./entrypoint.sh
12+
env_file:
13+
- .env

entrypoint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
export INTER_API_CERT="$(cat /secrets/certificate.crt)"
4+
export INTER_API_KEY="$(cat /secrets/certificate.key)"
5+
6+
exec "$@"

lib/inter.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ defmodule Inter do
1717
end
1818
end
1919

20+
def cobranca_charge(%Inter.Client{} = client, %Inter.Cobranca.Charge.Request{} = request) do
21+
client
22+
|> Inter.Client.token()
23+
|> Inter.Client.cobranca_charge(request)
24+
end
25+
26+
def get_cobranca(%Inter.Client{} = client, cod, conta_corrente) do
27+
case cod do
28+
_ -> client |> Inter.Client.token() |> Inter.Client.get_cobranca(cod, conta_corrente)
29+
end
30+
end
31+
2032
def pix_qr_code(%Inter.Client{} = client) do
2133
case client.response do
2234
%Inter.Pix.Charge.Response{} = response ->

lib/inter/client.ex

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ defmodule Inter.Client do
3131
key_file: "key_file"
3232
}
3333
"""
34-
def new(client_id, client_secret, scope, grant_type, cert_file, key_file) do
34+
def new(
35+
client_id,
36+
client_secret,
37+
scope,
38+
grant_type,
39+
cert_file,
40+
key_file,
41+
url \\ "https://cdpj.partners.bancointer.com.br/"
42+
) do
3543
{type, encoded, _atom} = key_file |> :public_key.pem_decode() |> hd()
3644

3745
%__MODULE__{
46+
base_url: url,
3847
client_id: client_id,
3948
client_secret: client_secret,
4049
scope: scope,
@@ -117,6 +126,49 @@ defmodule Inter.Client do
117126
}
118127
end
119128

129+
def get_cobranca(%__MODULE__{} = client, cod, conta_corrente) do
130+
headers = [
131+
{"Content-Type", "application/json"},
132+
{"Authorization", "Bearer " <> client.token.access_token},
133+
{"X-Conta-Corrente", conta_corrente}
134+
]
135+
136+
response =
137+
HTTPoison.get(
138+
client.base_url <> "cobranca/v3/cobrancas/#{cod}",
139+
headers,
140+
client.request_options
141+
)
142+
143+
%__MODULE__{
144+
client
145+
| request: %{},
146+
response: handle_response(response, Inter.Cobranca.Charge.Response)
147+
}
148+
end
149+
150+
def cobranca_charge(%__MODULE__{} = client, %Inter.Cobranca.Charge.Request{} = request) do
151+
headers = [
152+
{"Content-Type", "application/json"},
153+
{"Authorization", "Bearer " <> client.token.access_token},
154+
{"X-Conta-Corrente", request.contaCorrente}
155+
]
156+
157+
response =
158+
HTTPoison.post(
159+
client.base_url <> "cobranca/v3/cobrancas",
160+
Poison.encode!(request |> Nestru.encode!()),
161+
headers,
162+
client.request_options
163+
)
164+
165+
%__MODULE__{
166+
client
167+
| request: request,
168+
response: handle_response(response, Inter.Cobranca.Charge.Response.SimpleResponse)
169+
}
170+
end
171+
120172
defp handle_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}, type),
121173
do: body |> Jason.decode!() |> Nestru.decode!(type)
122174

@@ -129,5 +181,14 @@ defmodule Inter.Client do
129181
),
130182
do: {:error, body, response}
131183

184+
defp handle_response(
185+
{:ok, %HTTPoison.Response{status_code: 400, body: body}} = response,
186+
_type
187+
),
188+
do: {:error, body |> Jason.decode!(), response}
189+
190+
defp handle_response({:ok, %HTTPoison.Response{status_code: 429}} = response, _type),
191+
do: {:error, "You've been rate-limited, try again later (429 error)", response}
192+
132193
defp handle_response(response, _type), do: {:error, "Failed to obtain OAuth token", response}
133194
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule Inter.Cobranca.Charge.Request do
2+
@moduledoc """
3+
Documentation for `Inter.Cobranca.Charge.Request`.
4+
"""
5+
6+
@derive [
7+
{Nestru.Encoder,
8+
hint: %{
9+
pagador: Inter.Cobranca.Charge.Request.Pagador,
10+
desconto: Inter.Cobranca.Charge.Request.Desconto,
11+
multa: Inter.Cobranca.Charge.Request.Multa,
12+
mora: Inter.Cobranca.Charge.Request.Mora,
13+
mensagem: Inter.Cobranca.Charge.Request.Mensagem,
14+
beneficiarioFinal: Inter.Cobranca.Charge.Request.BeneficiarioFinal
15+
}},
16+
{Nestru.Decoder,
17+
hint: %{
18+
pagador: Inter.Cobranca.Charge.Request.Pagador,
19+
desconto: Inter.Cobranca.Charge.Request.Desconto,
20+
multa: Inter.Cobranca.Charge.Request.Multa,
21+
mora: Inter.Cobranca.Charge.Request.Mora,
22+
mensagem: Inter.Cobranca.Charge.Request.Mensagem,
23+
beneficiarioFinal: Inter.Cobranca.Charge.Request.BeneficiarioFinal
24+
}}
25+
]
26+
defstruct contaCorrente: nil,
27+
seuNumero: nil,
28+
valorNominal: nil,
29+
dataVencimento: nil,
30+
numDiasAgenda: nil,
31+
pagador: nil,
32+
desconto: nil,
33+
multa: nil,
34+
mora: nil,
35+
mensagem: nil,
36+
beneficiarioFinal: nil
37+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Inter.Cobranca.Charge.Request.BeneficiarioFinal do
2+
@moduledoc """
3+
Documentation for `Inter.Cobranca.Charge.Request.BeneficiarioFinal`.
4+
"""
5+
6+
@derive [Nestru.Encoder, Nestru.Decoder]
7+
defstruct cpfCnpj: nil,
8+
tipoPessoa: nil,
9+
nome: nil,
10+
endereco: nil,
11+
bairro: nil,
12+
cidade: nil,
13+
uf: nil,
14+
cep: nil
15+
end

0 commit comments

Comments
 (0)