inter_api 2.1.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 +4 -4
- data/README.md +17 -27
- data/lib/inter_api/client_production.rb +17 -40
- data/lib/inter_api/client_sandbox.rb +12 -0
- data/lib/inter_api/version.rb +1 -1
- data/lib/inter_api.rb +4 -14
- metadata +2 -2
- data/lib/inter_api/payment.rb +0 -103
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b9bc4efa4928bc559eb90959ecbcdc3b5b389c3bcbb226f440ba01c29970b35
|
4
|
+
data.tar.gz: 486818dd33835e598952b289f82bc798857a2268e091d8829de8de39e93bafd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
38
|
-
# retorna um objeto <
|
39
|
-
puts
|
40
|
-
puts
|
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
|
-
|
44
|
-
# retorna um objeto <
|
45
|
-
puts
|
46
|
-
puts
|
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
|
-
|
50
|
-
|
51
|
-
|
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
|
-
|
70
|
-
# retorna
|
71
|
-
|
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,7 +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
|
-
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)
|
9
9
|
@crt = crt
|
10
10
|
@key = key
|
11
11
|
@client_id = client_id
|
@@ -15,6 +15,7 @@ module InterApi
|
|
15
15
|
@scopes = scopes
|
16
16
|
@access_token= access_token
|
17
17
|
@token_expires_at = token_expires_at
|
18
|
+
@test_mode = test_mode
|
18
19
|
end
|
19
20
|
|
20
21
|
def default_options
|
@@ -43,37 +44,12 @@ module InterApi
|
|
43
44
|
@token_expires_at = Time.now + response.expires_in
|
44
45
|
end
|
45
46
|
|
46
|
-
def
|
47
|
-
get("/pix/v2/cob/#{payment_id}")
|
48
|
-
end
|
49
|
-
|
50
|
-
def get_payment(payment_id)
|
51
|
-
response = get("/pix/v2/cob/#{payment_id}")
|
52
|
-
InterApi::Payment.new(response, self)
|
53
|
-
end
|
54
|
-
|
55
|
-
def create_payment_request(**args_body)
|
56
|
-
body = {
|
57
|
-
calendario: {
|
58
|
-
expiracao: args_body[:expiration]
|
59
|
-
},
|
60
|
-
devedor: build_devedor(args_body[:payer_tax_id], args_body[:payer_name]),
|
61
|
-
valor: {
|
62
|
-
original: format("%.2f", args_body[:amount]),
|
63
|
-
modalidadeAlteracao: 0
|
64
|
-
},
|
65
|
-
chave: @chave_pix,
|
66
|
-
solicitacaoPagador: args_body[:solicitacao_pagador],
|
67
|
-
infoAdicionais: args_body[:info_adicionais]
|
68
|
-
}
|
69
|
-
post("/pix/v2/cob", body:)
|
70
|
-
end
|
71
|
-
|
72
|
-
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: [])
|
73
48
|
body = {
|
74
49
|
calendario: {
|
75
50
|
expiracao: expiration
|
76
51
|
},
|
52
|
+
devedor: build_devedor(payer_tax_id, payer_name),
|
77
53
|
valor: {
|
78
54
|
original: format("%.2f", amount),
|
79
55
|
modalidadeAlteracao: 0
|
@@ -81,33 +57,33 @@ module InterApi
|
|
81
57
|
chave: @chave_pix,
|
82
58
|
solicitacaoPagador: solicitacao_pagador,
|
83
59
|
infoAdicionais: info_adicionais
|
84
|
-
}
|
85
|
-
body[:devedor] = build_devedor(payer_tax_id, payer_name) if payer_tax_id && payer_name
|
60
|
+
}.compact
|
86
61
|
|
87
|
-
|
88
|
-
InterApi::Payment.new(response, self)
|
62
|
+
post("/pix/v2/cob", body:)
|
89
63
|
end
|
90
64
|
|
91
|
-
def
|
92
|
-
|
65
|
+
def get_payment(payment_id)
|
66
|
+
get("/pix/v2/cob/#{payment_id}")
|
93
67
|
end
|
94
68
|
|
95
|
-
def update_payment(payment_id
|
96
|
-
|
97
|
-
|
69
|
+
def update_payment(payment_id:, status:)
|
70
|
+
body = {
|
71
|
+
status: status
|
72
|
+
}
|
73
|
+
patch("/pix/v2/cob/#{payment_id}", body:)
|
98
74
|
end
|
99
75
|
|
100
|
-
def get_refund(end_to_end_id
|
76
|
+
def get_refund(end_to_end_id:, id:)
|
101
77
|
get("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}")
|
102
78
|
end
|
103
79
|
|
104
|
-
def refund_payment(end_to_end_id
|
80
|
+
def refund_payment(end_to_end_id:, amount: nil, refund_nature: nil, description: nil)
|
105
81
|
id = SecureRandom.hex(10)
|
106
82
|
body = {
|
107
83
|
valor: format("%.2f", amount),
|
108
84
|
natureza: refund_nature,
|
109
85
|
descricao: description
|
110
|
-
}
|
86
|
+
}.compact
|
111
87
|
put("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", body:)
|
112
88
|
end
|
113
89
|
|
@@ -125,6 +101,7 @@ module InterApi
|
|
125
101
|
private
|
126
102
|
|
127
103
|
def build_devedor(payer_tax_id, payer_name)
|
104
|
+
return unless payer_tax_id && payer_name
|
128
105
|
formatted_tax_id = payer_tax_id.gsub(/\D/, "")
|
129
106
|
document_type = (formatted_tax_id.length == 11) ? "cpf" : "cnpj"
|
130
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
|
data/lib/inter_api/version.rb
CHANGED
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/
|
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
|
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
|
32
|
-
|
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:
|
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/
|
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
|
data/lib/inter_api/payment.rb
DELETED
@@ -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
|