inter_api 0.1.1 → 0.1.3

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: e75b105e99c1775c30079e2a9ce4effe000e705a21d7d1c60bc2ebd27c9c7d1c
4
- data.tar.gz: 9e568d8e45786a71c413a536d731bd40f0d091b7807747a487d598ef5d35d34e
3
+ metadata.gz: 909c71db897c6ace26bd6ca8d6ef799ebbbf67be2be9f5cdfb5e0c5a64cc607e
4
+ data.tar.gz: bda1a06da9d5949d9638cd97a04e5640c6f6fb5f3751c96cb4466d7b38980d98
5
5
  SHA512:
6
- metadata.gz: e8fc8c8435f0b5b1e739629909d640efb5722464b0ed1c636e161d558b6272ab73b1b39b7959332c08aa29f8da764577533fffc75838cfef69bc44fb3913f029
7
- data.tar.gz: fbe1956363c1327892c8393a01ce54da67fb43e9b64221a48bf663d5af7a8ad14e68e8e2d8cec0e5659b827b9f9b859d795622c8e46120cbc5765290dbdbb505
6
+ metadata.gz: a0573df2a464d46d279d7da1f9b49768b568806212ebe90c9405a55256e4f20cb523db3d7a2679d2ad7babcda508bdc0053dda286351bfd5e620a602cb2eb718
7
+ data.tar.gz: 35e8aeeac198412d4d1dfd4bc420d4cb44ca22647500801135eb6d332b0d34147d3c6f7ded1882630272e7c572c86a358e83920a8a4969017f91cd114cb75551
@@ -2,14 +2,15 @@ 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
16
  if @token.nil? || @token_expires_at < Time.now
@@ -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"]
@@ -46,7 +47,7 @@ module InterApi
46
47
  },
47
48
  devedor: build_devedor(args_body[:payer_tax_id], args_body[:payer_name]),
48
49
  valor: {
49
- original: args_body[:amount],
50
+ original: format("%.2f", args_body[:amount]),
50
51
  modalidadeAlteracao: 0
51
52
  },
52
53
  chave: @chave_pix,
@@ -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.1"
4
+ VERSION = "0.1.3"
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.1
4
+ version: 0.1.3
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-28 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