inter_api 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 916894b13dfca9706d937b33783c82abadfa0b38cd8e453c16cf38722cbf91bd
4
- data.tar.gz: cfe2a096a7ff9b744e30710f750caac01a29b32d84e8bbe9a55d2d062650967f
3
+ metadata.gz: 9b9bc4efa4928bc559eb90959ecbcdc3b5b389c3bcbb226f440ba01c29970b35
4
+ data.tar.gz: 486818dd33835e598952b289f82bc798857a2268e091d8829de8de39e93bafd6
5
5
  SHA512:
6
- metadata.gz: c7767f24ea9c94da6f4fba3e5a8e11b5314999a897fcd04c36470dcf8bf5c9da6154ebdc4fb8501cf339c3746c6f3d86e2ae9aadfbb1379b37268751de7b4151
7
- data.tar.gz: 3d2be806b3bc8c4ce6323bebc0a64d19ed2050e373ef785da9d9292b690016843cdb8c76a7a0e618a597f857751bb5a12c90500113ad615611dc7eb67d68aa1d
6
+ metadata.gz: 6bd1dff9e43c35e4455254d3c47156110d4972eb83e6d0dc33df49dba850adcd3b723242afa2acc41ef4e720fb323a8353c5b9f68b4d962a4ff646aafa096825
7
+ data.tar.gz: 14fa2a073a1b0a34cbc28860f1d6e333175e6b3cbf76c56d7014387e8e14936697a16009558e9a5472548d96c86a79e8366610cfd38bac79a722fc600c3f4072
data/README.md CHANGED
@@ -31,42 +31,32 @@ bin/rails generate inter_api:install
31
31
  ```ruby
32
32
  # Instanciar o API client
33
33
  api_client = InterApi::Client.new(crt: "./certificado_api_inter.crt", key: "./chave_api_inter.key", client_id: "123456", client_secret: "abcde", chave_pix: "1011121314", conta_corrente: "9876", scopes: "cobv.write cobv.read cob.write cob.read pix.write pix.read")
34
+ # É possível passar o parâmetro "test_mode" na instanciação do client, que vai definir se ele vai usar ambiente de teste ou produção. Por padrão é "false" (produção).
34
35
  # Ao instanciar o objeto, cria um access_token e/ou valida a partir da data de expiração
35
36
 
36
- # Criar um pagamento
37
- payment = api_client.create_payment(amount: 50.00, payer_tax_id: "123.123.123-01", payer_name: "João da Silva", expiration: 3600, solicitacao_pagador: "Compra de produto", info_adicionais: [ nome: "Produto", valor: "Produto 1"])
38
- # retorna um objeto <InterApi::Payment>
39
- puts payment.txid # "123123"
40
- puts payment.status # "ATIVA"
37
+ # Criar um pagamento
38
+ response = api_client.create_payment(amount: 50.00, payer_tax_id: "123.123.123-01", payer_name: "João da Silva", expiration: 3600, solicitacao_pagador: "Compra de produto", info_adicionais: [ nome: "Produto", valor: "Produto 1"])
39
+ # retorna um objeto <Ac::AcObject>
40
+ puts response.txid # "123123"
41
+ puts response.status # "ATIVA"
41
42
 
42
43
  # Obter um pagamento a partir do id
43
- payment = api_client.get_payment(payment.txid)
44
- # retorna um objeto <InterApi::Payment>
45
- puts payment.txid # "123123"
46
- puts payment.status # "ATIVA"
44
+ response = api_client.get_payment(payment.txid)
45
+ # retorna um objeto <Ac::AcObject>
46
+ puts response.txid # "123123"
47
+ puts response.status # "ATIVA"
47
48
 
48
49
  # Atualizar um pagamento
49
- body = { status: "REMOVIDA_PELO_USUARIO_RECEBEDOR" }
50
- api_client.update(payment.txid, body)
51
- # retorna o objeto <InterApi::Payment> atualizado
52
- puts payment.status # "REMOVIDA_PELO_USUARIO_RECEBEDOR"
50
+ response = api_client.update_payment(payment_id: payment.txid, status: "REMOVIDA_PELO_USUARIO_RECEBEDOR")
51
+ # retorna o objeto <Ac::AcObject> com o pagamento atualizado
52
+ puts response.status # "REMOVIDA_PELO_USUARIO_RECEBEDOR"
53
53
 
54
54
  ```
55
55
 
56
56
  #### Métodos do Pagamento
57
57
  ```ruby
58
- payment.valid? # true ou false
59
-
60
- payment.paid? # true ou false
61
-
62
- payment.qr_code # retorna o source do SVG
63
-
64
- payment.reload! # retorna payment atualizado
65
-
66
- payment.invalidate! # invalida e retorna o payment atualizado
67
-
68
58
  # Reebolso de pagamento
69
- reembolso = payment.refund(amount: 50.00, refund_nature: "ORIGINAL", description: "Reembolso")
70
- # retorna o JSON da resposta
71
- reembolso["status"] # "EM_PROCESSAMENTO"
72
- ```
59
+ response = api_client.refund_payment(end_to_end_id: "123", amount: 50.00, refund_nature: "ORIGINAL", description: "Reembolso")
60
+ # retorna um objeto <Ac::AcObject>
61
+ response.status # "EM_PROCESSAMENTO"
62
+ ```
@@ -5,8 +5,7 @@ module InterApi
5
5
  SAVE_RESPONSES = true
6
6
 
7
7
  attr_reader :crt, :key, :client_id, :client_secret, :chave_pix, :conta_corrente, :scopes
8
- attr_accessor :access_token, :token_expires_at
9
- 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 webhook.read webhook.write", access_token: nil, token_expires_at: nil)
8
+ 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 webhook.read webhook.write", access_token: nil, token_expires_at: nil, test_mode: false)
10
9
  @crt = crt
11
10
  @key = key
12
11
  @client_id = client_id
@@ -16,15 +15,7 @@ module InterApi
16
15
  @scopes = scopes
17
16
  @access_token= access_token
18
17
  @token_expires_at = token_expires_at
19
- end
20
-
21
- def access_token
22
- authorize if should_refresh?
23
- @access_token
24
- end
25
-
26
- def should_refresh?
27
- @access_token.blank? || @token_expires_at&.past?
18
+ @test_mode = test_mode
28
19
  end
29
20
 
30
21
  def default_options
@@ -38,7 +29,7 @@ module InterApi
38
29
  }
39
30
  end
40
31
 
41
- def authorize
32
+ def refresh_token
42
33
  headers = {
43
34
  "Content-Type" => "application/x-www-form-urlencoded"
44
35
  }
@@ -53,37 +44,12 @@ module InterApi
53
44
  @token_expires_at = Time.now + response.expires_in
54
45
  end
55
46
 
56
- def get_payment_request(payment_id)
57
- get("/pix/v2/cob/#{payment_id}")
58
- end
59
-
60
- def get_payment(payment_id)
61
- response = get("/pix/v2/cob/#{payment_id}")
62
- InterApi::Payment.new(response, self)
63
- end
64
-
65
- def create_payment_request(**args_body)
66
- body = {
67
- calendario: {
68
- expiracao: args_body[:expiration]
69
- },
70
- devedor: build_devedor(args_body[:payer_tax_id], args_body[:payer_name]),
71
- valor: {
72
- original: format("%.2f", args_body[:amount]),
73
- modalidadeAlteracao: 0
74
- },
75
- chave: @chave_pix,
76
- solicitacaoPagador: args_body[:solicitacao_pagador],
77
- infoAdicionais: args_body[:info_adicionais]
78
- }
79
- post("/pix/v2/cob", body:)
80
- end
81
-
82
- def create_payment(amount:, payer_tax_id: nil, payer_name: nil, expiration: 3600, solicitacao_pagador: nil, info_adicionais: [])
47
+ def create_payment(expiration: 3600, payer_tax_id: nil, payer_name: nil, amount:, solicitacao_pagador: nil, info_adicionais: [])
83
48
  body = {
84
49
  calendario: {
85
50
  expiracao: expiration
86
51
  },
52
+ devedor: build_devedor(payer_tax_id, payer_name),
87
53
  valor: {
88
54
  original: format("%.2f", amount),
89
55
  modalidadeAlteracao: 0
@@ -91,33 +57,33 @@ module InterApi
91
57
  chave: @chave_pix,
92
58
  solicitacaoPagador: solicitacao_pagador,
93
59
  infoAdicionais: info_adicionais
94
- }
95
- body[:devedor] = build_devedor(payer_tax_id, payer_name) if payer_tax_id && payer_name
60
+ }.compact
96
61
 
97
- response = post("/pix/v2/cob", body:)
98
- InterApi::Payment.new(response, self)
62
+ post("/pix/v2/cob", body:)
99
63
  end
100
64
 
101
- def update_payment_request(payment_id, body)
102
- patch("/pix/v2/cob/#{payment_id}", body:)
65
+ def get_payment(payment_id)
66
+ get("/pix/v2/cob/#{payment_id}")
103
67
  end
104
68
 
105
- def update_payment(payment_id, body)
106
- response = patch("/pix/v2/cob/#{payment_id}", body:)
107
- InterApi::Payment.new(response, self)
69
+ def update_payment(payment_id:, status:)
70
+ body = {
71
+ status: status
72
+ }
73
+ patch("/pix/v2/cob/#{payment_id}", body:)
108
74
  end
109
75
 
110
- def get_refund(end_to_end_id, id)
76
+ def get_refund(end_to_end_id:, id:)
111
77
  get("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}")
112
78
  end
113
79
 
114
- def refund_payment(end_to_end_id, amount, refund_nature, description)
80
+ def refund_payment(end_to_end_id:, amount: nil, refund_nature: nil, description: nil)
115
81
  id = SecureRandom.hex(10)
116
82
  body = {
117
83
  valor: format("%.2f", amount),
118
84
  natureza: refund_nature,
119
85
  descricao: description
120
- }
86
+ }.compact
121
87
  put("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", body:)
122
88
  end
123
89
 
@@ -135,6 +101,7 @@ module InterApi
135
101
  private
136
102
 
137
103
  def build_devedor(payer_tax_id, payer_name)
104
+ return unless payer_tax_id && payer_name
138
105
  formatted_tax_id = payer_tax_id.gsub(/\D/, "")
139
106
  document_type = (formatted_tax_id.length == 11) ? "cpf" : "cnpj"
140
107
  {
@@ -0,0 +1,12 @@
1
+ module InterApi
2
+ class ClientSandbox < ClientProduction
3
+ BASE_URL = "https://cdpj-sandbox.partners.uatinter.co/"
4
+
5
+ def pay_pix payment_id:, amount:
6
+ body = {
7
+ valor: format("%.2f", amount)
8
+ }
9
+ post("/pix/v2/cob/pagar/#{payment_id}", body:)
10
+ end
11
+ end
12
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InterApi
4
- VERSION = "2.0.0"
4
+ VERSION = "3.0.0"
5
5
  end
data/lib/inter_api.rb CHANGED
@@ -2,34 +2,24 @@
2
2
 
3
3
  require "bigdecimal/util"
4
4
  require "securerandom"
5
- require "rqrcode"
6
- # require "debug"
7
5
  require "ac"
8
6
 
9
7
  require_relative "inter_api/version"
10
8
  require "inter_api/client_production"
11
- require "inter_api/payment"
9
+ require "inter_api/client_sandbox"
12
10
 
13
11
  module InterApi
14
12
  Typhoeus::Config.timeout = 10
15
13
 
16
14
  class Error < StandardError; end
17
15
 
18
- class ClientTeste < ClientProduction
19
- BASE_URL = "https://cdpj-sandbox.partners.uatinter.co/"
20
-
21
- def pay_pix payment_id, amount
22
- body = {
23
- valor: format("%.2f", amount)
24
- }
25
- post("/pix/v2/cob/pagar/#{payment_id}", body:)
26
- end
16
+ class ClientSandbox < ClientProduction
27
17
  end
28
18
 
29
19
  class Client
30
20
  def self.new **args
31
- if args.delete(:test_mode)
32
- ClientTeste.new(**args)
21
+ if args[:test_mode]
22
+ ClientSandbox.new(**args)
33
23
  else
34
24
  ClientProduction.new(**args)
35
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inter_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todas Essas Coisas
@@ -80,7 +80,7 @@ files:
80
80
  - Rakefile
81
81
  - lib/inter_api.rb
82
82
  - lib/inter_api/client_production.rb
83
- - lib/inter_api/payment.rb
83
+ - lib/inter_api/client_sandbox.rb
84
84
  - lib/inter_api/version.rb
85
85
  - sig/inter_api.rbs
86
86
  homepage: https://github.com/todasessascoisas/inter_api
@@ -1,103 +0,0 @@
1
- module InterApi
2
- class Payment
3
- attr_reader :response, :txid, :end_to_end_id, :devolucoes, :copia_e_cola, :status, :solicitacao, :info_adicionais, :valor_original, :valor_pago, :criacao, :expiracao, :pago_em, :devedor_nome, :devedor_tax_id
4
- def initialize(response, client)
5
- @client = client
6
- @response = response
7
- @txid = response.txid
8
- @end_to_end_id = end_to_end
9
- @devolucoes = devolutions
10
- @copia_e_cola = response.pixCopiaECola
11
- @status = response.status
12
- @solicitacao = response["solicitacaoPagador"]
13
- @info_adicionais = response.infoAdicionais
14
- @valor_original = response.valor.original
15
- @valor_pago = total_paid
16
- @criacao = creation_date
17
- @expiracao = expiration_date
18
- @pago_em = paid_date
19
- @devedor_nome = debtor_name
20
- @devedor_tax_id = debtor_tax_id
21
- end
22
-
23
- def paid? external_value = nil
24
- external_value = @valor_original if external_value.nil?
25
- @status == "CONCLUIDA" && @valor_original.to_d == @valor_pago.to_d && external_value.to_d == @valor_pago.to_d
26
- end
27
-
28
- def valid?
29
- @status == "ATIVA" && @expiracao > Time.now
30
- end
31
-
32
- def reload!
33
- new_payment = @client.get_payment(@txid)
34
- instance_variables.each do |variable|
35
- instance_variable_set(variable, new_payment.instance_variable_get(variable))
36
- end
37
- self
38
- end
39
-
40
- def update(body)
41
- @client.update_payment(@txid, body)
42
- end
43
-
44
- def invalidate!
45
- update({status: "REMOVIDA_PELO_USUARIO_RECEBEDOR"})
46
- reload!
47
- end
48
-
49
- def refund(amount: nil, refund_nature: nil, description: nil)
50
- amount ||= @valor_pago
51
- refund_nature ||= "ORIGINAL"
52
- @client.refund_payment(@end_to_end_id, amount, refund_nature, description)
53
- end
54
-
55
- def qr_code(module_size: 4)
56
- return false unless @copia_e_cola
57
- qr = RQRCode::QRCode.new(@copia_e_cola, size: 10, level: :l)
58
- qr.as_svg(module_size: module_size)
59
- end
60
-
61
- private
62
-
63
- def creation_date
64
- return nil unless @response.calendario.criacao
65
- Time.new(@response.calendario.criacao)
66
- end
67
-
68
- def expiration_date
69
- return nil unless @response.calendario.criacao
70
- Time.new(@response.calendario.criacao) + @response.calendario.expiracao
71
- end
72
-
73
- def paid_date
74
- return nil unless @response["pix"] && @response.pix.any? && @response.pix[0].horario
75
- Time.new(@response.pix[0].horario)
76
- end
77
-
78
- def debtor_name
79
- return nil unless @response["devedor"]
80
- @response.devedor.nome
81
- end
82
-
83
- def debtor_tax_id
84
- return nil unless @response["devedor"]
85
- @response.devedor.cpf || @response.devedor.cnpj
86
- end
87
-
88
- def end_to_end
89
- return nil unless @response["pix"] && @response.pix.any?
90
- @response.pix[0].endToEndId
91
- end
92
-
93
- def devolutions
94
- return nil unless @response["pix"] && @response.pix.any?
95
- @response.pix[0].devolucoes
96
- end
97
-
98
- def total_paid
99
- return nil unless @response["pix"] && @response.pix.any?
100
- @response.pix[0].valor
101
- end
102
- end
103
- end