inter_api 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1b557a668eb322c52dae171a4ac52b78b098ef1f9b3a18583f1d67d4e6bf526
4
- data.tar.gz: 2935946dd4574822ad03916624143a04d773e0c9eeca0c96abb25effedd1fb85
3
+ metadata.gz: 6c9ecfff5db0dc4478eccff3eb84f4396b702c3e2cc9e4ce18581fc7752e17b0
4
+ data.tar.gz: d45bbd34051b389eacb045243db1df83192a44b769ed8b51137510a8b6c98948
5
5
  SHA512:
6
- metadata.gz: 61449b97f533dfe179d2814b461709a424ca0b2e48fe9595eacb831601170d46ee797e7e4d51a3a915731a797bd816cd5d949b908550e1beda096c72319a7964
7
- data.tar.gz: f8ecbf9148def1ebab3f402a0e9d6881c6cdd6157f82f4900fe4bf02e2ab1b98c7af3ca61370a3055f3cb3891dc447acd2ec14ecab99704d1fdd006363504411
6
+ metadata.gz: 7bb5bcae235c1d6cc5f47099517ac973e022657500d951215452e8054ef8d6bf7d5247e2293c941451ce9ff70e304d0951ba7de355fca38537e6ed5aa1c843df
7
+ data.tar.gz: 2bbf29df1b4ba0b151107f65f9f18fdb81cf2a7101745aa4f9e07562622a3ec91f2d3d5a9171b93b05b486ff1bd0dcb5df5bcb5faadd282b775aae47dabc4bd2
@@ -2,17 +2,18 @@ module InterApi
2
2
  class Client < Ac::Base
3
3
  BASE_URL = "https://cdpj.partners.bancointer.com.br/"
4
4
 
5
- attr_accessor :crt, :key, :client_id, :client_secret, :chave_pix, :conta_corrente, :token, :token_expires_at
6
- def initialize crt:, key:, client_id:, client_secret:, chave_pix:, conta_corrente:, token: nil, token_expires_at: nil
5
+ attr_accessor :crt, :key, :client_id, :client_secret, :chave_pix, :conta_corrente, :scopes, :token, :token_expires_at
6
+ def initialize crt:, key:, client_id:, client_secret:, chave_pix:, conta_corrente:, scopes: "cobv.write cobv.read cob.write cob.read pix.write pix.read", token: nil, token_expires_at: nil
7
7
  @crt = crt
8
8
  @key = key
9
9
  @client_id = client_id
10
10
  @client_secret = client_secret
11
11
  @chave_pix = chave_pix
12
12
  @conta_corrente = conta_corrente
13
+ @scopes = scopes
13
14
  @token = token
14
15
  @token_expires_at = token_expires_at
15
- if @token.nil? || @token_expires_at.nil?
16
+ if @token.nil? || @token_expires_at < Time.now
16
17
  authenticate!
17
18
  end
18
19
  end
@@ -25,7 +26,7 @@ module InterApi
25
26
  client_id: @client_id,
26
27
  client_secret: @client_secret,
27
28
  grant_type: "client_credentials",
28
- scope: "cobv.write cob.write cob.read pix.write pagamento-pix.write"
29
+ scope: @scopes
29
30
  }
30
31
  response = post("/oauth/v2/token", headers: headers, sslcert: @crt, sslkey: @key, body: body)
31
32
  @token = response.json["access_token"]
@@ -64,9 +65,10 @@ module InterApi
64
65
  private
65
66
 
66
67
  def build_devedor payer_tax_id, payer_name
67
- document_type = (payer_tax_id.gsub(/\D/, "").length == 11) ? "cpf" : "cnpj"
68
+ formatted_tax_id = payer_tax_id.gsub(/\D/, "")
69
+ document_type = (formatted_tax_id.length == 11) ? "cpf" : "cnpj"
68
70
  {
69
- document_type => payer_tax_id,
71
+ document_type => formatted_tax_id,
70
72
  "nome" => payer_name
71
73
  }
72
74
  end
@@ -1,7 +1,7 @@
1
1
  module InterApi
2
2
  class Payment
3
3
 
4
- attr_reader :response, :txid, :copia_e_cola, :status, :solicitacao, :info_adicionais, :valor_original, :valor_pago, :criacao, :expiracao, :devedor_nome, :devedor_tax_id
4
+ attr_reader :response, :txid, :copia_e_cola, :status, :solicitacao, :info_adicionais, :valor_original, :valor_pago, :criacao, :expiracao, :devedor_nome, :devedor_tax_id, :internal_error
5
5
  def initialize response
6
6
  @response = response
7
7
  @txid = response.dig("txid")
@@ -11,10 +11,11 @@ module InterApi
11
11
  @info_adicionais = response.dig("infoAdicionais")
12
12
  @valor_original = response.dig("valor", "original")
13
13
  @valor_pago = response.dig("pix", 0, "valor")
14
- @criacao = Time.new(response.dig("calendario", "criacao"))
15
- @expiracao = Time.new(response.dig("calendario", "criacao")) + response.dig("calendario", "expiracao")
14
+ @criacao = date_creation
15
+ @expiracao = date_expiration
16
16
  @devedor_nome = response.dig("devedor", "nome")
17
17
  @devedor_tax_id = response.dig("devedor", "cpf") || response.dig("devedor", "cnpj")
18
+ @internal_error = PaymentError.new(response).internal_error
18
19
  end
19
20
 
20
21
  def paid?
@@ -22,6 +23,7 @@ module InterApi
22
23
  end
23
24
 
24
25
  def qr_code module_size: 4
26
+ return false unless @copia_e_cola
25
27
  qr = RQRCode::QRCode.new(@copia_e_cola, size: 10, level: :l)
26
28
  qr.as_svg(module_size: module_size)
27
29
  end
@@ -30,6 +32,18 @@ module InterApi
30
32
  @expiracao > Time.now
31
33
  end
32
34
 
35
+ private
36
+
37
+ def date_creation
38
+ return nil unless @response.dig("calendario", "criacao")
39
+ Time.new(@response.dig("calendario", "criacao"))
40
+ end
41
+
42
+ def date_expiration
43
+ return nil unless @response.dig("calendario", "criacao")
44
+ Time.new(@response.dig("calendario", "criacao")) + @response.dig("calendario", "expiracao")
45
+ end
46
+
33
47
  # def reload!
34
48
  # @response = api_inter.find_by_id(id)
35
49
  # end
@@ -0,0 +1,25 @@
1
+ module InterApi
2
+ class PaymentError
3
+ STATUS_ERROR = [400, 403, 404, 503]
4
+ attr_accessor :json_response
5
+ def initialize json_response
6
+ @json_response = json_response
7
+ end
8
+
9
+ def internal_error
10
+ return nil unless json_response["violacoes"] || STATUS_ERROR.include?(json_response["status"])
11
+ failed_message_error || rejected_message_error || "Pagamento recusado."
12
+ end
13
+
14
+ private
15
+
16
+ def rejected_message_error
17
+ "#{json_response["title"]} => #{json_response["detail"]}"
18
+ end
19
+
20
+ def failed_message_error
21
+ return false unless json_response["violacoes"]
22
+ "#{json_response["violacoes"][0]["razao"]} => #{json_response["violacoes"][0]["propriedade"]}"
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InterApi
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/inter_api.rb CHANGED
@@ -8,6 +8,7 @@ require "ac"
8
8
  require_relative "inter_api/version"
9
9
  require "inter_api/client"
10
10
  require "inter_api/payment"
11
+ require "inter_api/payment_error"
11
12
 
12
13
  module InterApi
13
14
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inter_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ulysses
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-23 00:00:00.000000000 Z
11
+ date: 2024-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ac
@@ -68,6 +68,7 @@ files:
68
68
  - lib/inter_api.rb
69
69
  - lib/inter_api/client.rb
70
70
  - lib/inter_api/payment.rb
71
+ - lib/inter_api/payment_error.rb
71
72
  - lib/inter_api/version.rb
72
73
  - sig/inter_api.rbs
73
74
  homepage: https://github.com/ulysses-bull/inter_api