inter_api 0.1.2 → 0.1.4

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: 6c9ecfff5db0dc4478eccff3eb84f4396b702c3e2cc9e4ce18581fc7752e17b0
4
- data.tar.gz: d45bbd34051b389eacb045243db1df83192a44b769ed8b51137510a8b6c98948
3
+ metadata.gz: ebf4c931a13edda0c526f8020fc65d91d355997fc403968ba8cf8d468d4b5423
4
+ data.tar.gz: 6f7183cf731bf26705f0ba6ece04f25b22fed56fdb90355d715919a55c430de9
5
5
  SHA512:
6
- metadata.gz: 7bb5bcae235c1d6cc5f47099517ac973e022657500d951215452e8054ef8d6bf7d5247e2293c941451ce9ff70e304d0951ba7de355fca38537e6ed5aa1c843df
7
- data.tar.gz: 2bbf29df1b4ba0b151107f65f9f18fdb81cf2a7101745aa4f9e07562622a3ec91f2d3d5a9171b93b05b486ff1bd0dcb5df5bcb5faadd282b775aae47dabc4bd2
6
+ metadata.gz: 2993e344a72e2511be6adf96fe94d03739a79fff71c36e58ba9cd83a933a80218a67f4e520c2daaf5b641132a19ce8d0047c81c1255dfff3de49ac2744c9b177
7
+ data.tar.gz: 83ba77625890a8abd45935da2f28e946fa4aa3b07103d95c0eed44c4a59b057b151401c4d29c240df49b9a868baefc117d7b0288f4d48d998157b24fbfbe6ec0
@@ -2,8 +2,9 @@ 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, :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
5
+ attr_reader :crt, :key, :client_id, :client_secret, :chave_pix, :conta_corrente, :scopes
6
+ attr_accessor :token, :token_expires_at
7
+ 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
8
  @crt = crt
8
9
  @key = key
9
10
  @client_id = client_id
@@ -33,38 +34,65 @@ module InterApi
33
34
  @token_expires_at = Time.now + response.json["expires_in"]
34
35
  end
35
36
 
37
+ def get_payment_request(payment_id)
38
+ get_request("/pix/v2/cob/#{payment_id}", headers: build_headers, sslcert: @crt, sslkey: @key)
39
+ end
40
+
36
41
  def get_payment(payment_id)
37
- headers = build_headers
38
- response = get("/pix/v2/cob/#{payment_id}", headers: headers, sslcert: @crt, sslkey: @key)
39
- InterApi::Payment.new(response.json)
42
+ response = get_payment_request(payment_id).run
43
+ InterApi::Payment.new(response.json, self)
40
44
  end
41
45
 
42
46
  def create_payment_request(**args_body)
43
- headers = build_headers
44
47
  body = {
45
48
  calendario: {
46
49
  expiracao: args_body[:expiration]
47
50
  },
48
51
  devedor: build_devedor(args_body[:payer_tax_id], args_body[:payer_name]),
49
52
  valor: {
50
- original: args_body[:amount],
53
+ original: format("%.2f", args_body[:amount]),
51
54
  modalidadeAlteracao: 0
52
55
  },
53
56
  chave: @chave_pix,
54
57
  solicitacaoPagador: args_body[:solicitacao_pagador],
55
58
  infoAdicionais: args_body[:info_adicionais]
56
59
  }
57
- post_request("/pix/v2/cob", headers: headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body))
60
+ post_request("/pix/v2/cob", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body))
58
61
  end
59
62
 
60
63
  def create_payment(amount:, payer_tax_id:, payer_name:, expiration: 3600, solicitacao_pagador: nil, info_adicionais: [])
61
64
  response = create_payment_request(amount: amount, payer_tax_id: payer_tax_id, payer_name: payer_name, expiration: expiration, solicitacao_pagador: solicitacao_pagador, info_adicionais: info_adicionais).run
62
- InterApi::Payment.new(response.json)
65
+ InterApi::Payment.new(response.json, self)
66
+ end
67
+
68
+ def update_payment_request(payment_id, body)
69
+ patch_request("/pix/v2/cob/#{payment_id}", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body))
70
+ end
71
+
72
+ def update_payment(payment_id, body, amount)
73
+ response = update_payment_request(payment_id, JSON.dump(body)).run
74
+ InterApi::Payment.new(response.json, self)
75
+ end
76
+
77
+ def get_refund(end_to_end_id, id)
78
+ response = get("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", headers: build_headers, sslcert: @crt, sslkey: @key)
79
+ response.json
80
+ end
81
+
82
+ def refund_payment(end_to_end_id, amount, refund_nature, description)
83
+ id = SecureRandom.hex(10)
84
+ body = {
85
+ valor: format("%.2f", amount),
86
+ natureza: refund_nature,
87
+ descricao: description
88
+ }
89
+ response = put("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body))
90
+ response.json
63
91
  end
64
92
 
65
93
  private
66
94
 
67
- def build_devedor payer_tax_id, payer_name
95
+ def build_devedor(payer_tax_id, payer_name)
68
96
  formatted_tax_id = payer_tax_id.gsub(/\D/, "")
69
97
  document_type = (formatted_tax_id.length == 11) ? "cpf" : "cnpj"
70
98
  {
@@ -1,52 +1,72 @@
1
1
  module InterApi
2
2
  class Payment
3
-
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
- def initialize response
3
+ attr_reader :response, :txid, :end_to_end_id, :devolucoes, :copia_e_cola, :status, :solicitacao, :info_adicionais, :valor_original, :valor_pago, :criacao, :expiracao, :devedor_nome, :devedor_tax_id, :internal_error
4
+ def initialize(response, client)
5
+ @client = client
6
6
  @response = response
7
7
  @txid = response.dig("txid")
8
+ @end_to_end_id = response.dig("pix", 0, "endToEndId")
9
+ @devolucoes = response.dig("pix", 0, "devolucoes")
8
10
  @copia_e_cola = response.dig("pixCopiaECola")
9
11
  @status = response.dig("status")
10
12
  @solicitacao = response.dig("solicitacaoPagador")
11
13
  @info_adicionais = response.dig("infoAdicionais")
12
14
  @valor_original = response.dig("valor", "original")
13
15
  @valor_pago = response.dig("pix", 0, "valor")
14
- @criacao = date_creation
15
- @expiracao = date_expiration
16
+ @criacao = creation_date
17
+ @expiracao = expiration_date
16
18
  @devedor_nome = response.dig("devedor", "nome")
17
19
  @devedor_tax_id = response.dig("devedor", "cpf") || response.dig("devedor", "cnpj")
18
20
  @internal_error = PaymentError.new(response).internal_error
19
21
  end
20
22
 
21
23
  def paid?
22
- @valor_original.to_d == @valor_pago.to_d
24
+ @status == "CONCLUIDA" && @valor_original.to_d == @valor_pago.to_d
23
25
  end
24
26
 
25
- def qr_code module_size: 4
27
+ def valid?
28
+ @status == "ATIVA" && @expiracao > Time.now
29
+ end
30
+
31
+ def reload!
32
+ new_payment = @client.get_payment(@txid)
33
+ instance_variables.each do |variable|
34
+ instance_variable_set(variable, new_payment.instance_variable_get(variable))
35
+ end
36
+ self
37
+ end
38
+
39
+ def update(body)
40
+ @client.update_payment(@txid, body)
41
+ end
42
+
43
+ def invalidate!
44
+ update({status: "REMOVIDA_PELO_USUARIO_RECEBEDOR"})
45
+ reload!
46
+ end
47
+
48
+ def refund(amount: nil, refund_nature: nil, description: nil)
49
+ amount ||= @valor_pago
50
+ refund_nature ||= "ORIGINAL"
51
+ @client.refund_payment(@end_to_end_id, amount, refund_nature, description)
52
+ end
53
+
54
+ def qr_code(module_size: 4)
26
55
  return false unless @copia_e_cola
27
56
  qr = RQRCode::QRCode.new(@copia_e_cola, size: 10, level: :l)
28
57
  qr.as_svg(module_size: module_size)
29
58
  end
30
59
 
31
- def valid?
32
- @expiracao > Time.now
33
- end
34
-
35
60
  private
36
61
 
37
- def date_creation
62
+ def creation_date
38
63
  return nil unless @response.dig("calendario", "criacao")
39
64
  Time.new(@response.dig("calendario", "criacao"))
40
65
  end
41
66
 
42
- def date_expiration
67
+ def expiration_date
43
68
  return nil unless @response.dig("calendario", "criacao")
44
69
  Time.new(@response.dig("calendario", "criacao")) + @response.dig("calendario", "expiracao")
45
70
  end
46
-
47
- # def reload!
48
- # @response = api_inter.find_by_id(id)
49
- # end
50
-
51
71
  end
52
72
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InterApi
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
  end
data/lib/inter_api.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bigdecimal/util"
4
+ require "securerandom"
4
5
  require "rqrcode"
5
6
  require "debug"
6
7
  require "ac"
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.2
4
+ version: 0.1.4
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-27 00:00:00.000000000 Z
11
+ date: 2024-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ac
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: securerandom
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: ''
56
70
  email:
57
71
  - ulyssesh.20@gmail.com