@@ -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 }
133194end
0 commit comments