Class: InterApi::ClientProduction

Inherits:
Ac::Base
  • Object
show all
Defined in:
lib/inter_api/client_production.rb

Direct Known Subclasses

ClientTeste

Constant Summary collapse

BASE_URL =
"https://cdpj.partners.bancointer.com.br/"
MAX_RETIES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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", token: nil, token_expires_at: nil) ⇒ ClientProduction

Returns a new instance of ClientProduction.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/inter_api/client_production.rb', line 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", token: nil, token_expires_at: nil)
  @crt = crt
  @key = key
  @client_id = client_id
  @client_secret = client_secret
  @chave_pix = chave_pix
  @conta_corrente = conta_corrente
  @scopes = scopes
  @token = token
  @token_expires_at = token_expires_at
  if @token.nil? || @token_expires_at < Time.now
    authenticate!
  end
end

Instance Attribute Details

#chave_pixObject (readonly)

Returns the value of attribute chave_pix.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def chave_pix
  @chave_pix
end

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def client_secret
  @client_secret
end

#conta_correnteObject (readonly)

Returns the value of attribute conta_corrente.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def conta_corrente
  @conta_corrente
end

#crtObject (readonly)

Returns the value of attribute crt.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def crt
  @crt
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def key
  @key
end

#scopesObject (readonly)

Returns the value of attribute scopes.



6
7
8
# File 'lib/inter_api/client_production.rb', line 6

def scopes
  @scopes
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/inter_api/client_production.rb', line 7

def token
  @token
end

#token_expires_atObject

Returns the value of attribute token_expires_at.



7
8
9
# File 'lib/inter_api/client_production.rb', line 7

def token_expires_at
  @token_expires_at
end

Instance Method Details

#authenticate!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/inter_api/client_production.rb', line 23

def authenticate!
  headers = {
    "Content-Type" => "application/x-www-form-urlencoded"
  }
  body = {
    client_id: @client_id,
    client_secret: @client_secret,
    grant_type: "client_credentials",
    scope: @scopes
  }
  response = post("/oauth/v2/token", headers: headers, sslcert: @crt, sslkey: @key, body: body) { |response| validate_response(response, "access_token") }
  @token = response.json["access_token"]
  @token_expires_at = Time.now + response.json["expires_in"]
end

#create_payment(amount:, payer_tax_id: nil, payer_name: nil, expiration: 3600, solicitacao_pagador: nil, info_adicionais: []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/inter_api/client_production.rb', line 64

def create_payment(amount:, payer_tax_id: nil, payer_name: nil, expiration: 3600, solicitacao_pagador: nil, info_adicionais: [])
  body = {
    calendario: {
      expiracao: expiration
    },
    valor: {
      original: format("%.2f", amount),
      modalidadeAlteracao: 0
    },
    chave: @chave_pix,
    solicitacaoPagador: solicitacao_pagador,
    infoAdicionais: info_adicionais
  }
  body[:devedor] = build_devedor(payer_tax_id, payer_name) if payer_tax_id && payer_name

  response = post("/pix/v2/cob", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body)) { |response| validate_response(response, "txid") }
  InterApi::Payment.new(response.json, self)
end

#create_payment_request(**args_body) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inter_api/client_production.rb', line 47

def create_payment_request(**args_body)
  body = {
    calendario: {
      expiracao: args_body[:expiration]
    },
    devedor: build_devedor(args_body[:payer_tax_id], args_body[:payer_name]),
    valor: {
      original: format("%.2f", args_body[:amount]),
      modalidadeAlteracao: 0
    },
    chave: @chave_pix,
    solicitacaoPagador: args_body[:solicitacao_pagador],
    infoAdicionais: args_body[:info_adicionais]
  }
  post_request("/pix/v2/cob", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body))
end

#get_payment(payment_id) ⇒ Object



42
43
44
45
# File 'lib/inter_api/client_production.rb', line 42

def get_payment(payment_id)
  response = get("/pix/v2/cob/#{payment_id}", headers: build_headers, sslcert: @crt, sslkey: @key) { |response| validate_response(response, "txid") }
  InterApi::Payment.new(response.json, self)
end

#get_payment_request(payment_id) ⇒ Object



38
39
40
# File 'lib/inter_api/client_production.rb', line 38

def get_payment_request(payment_id)
  get_request("/pix/v2/cob/#{payment_id}", headers: build_headers, sslcert: @crt, sslkey: @key)
end

#get_refund(end_to_end_id, id) ⇒ Object



92
93
94
95
# File 'lib/inter_api/client_production.rb', line 92

def get_refund(end_to_end_id, id)
  response = get("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", headers: build_headers, sslcert: @crt, sslkey: @key) { |response| validate_response(response, "id") }
  response.json
end

#get_webhookObject



115
116
117
118
# File 'lib/inter_api/client_production.rb', line 115

def get_webhook
  response = get("/pix/v2/webhook/#{@chave_pix}", headers: build_headers, sslcert: @crt, sslkey: @key) { |response| validate_response(response, "webhookUrl") }
  response.json
end

#refund_payment(end_to_end_id, amount, refund_nature, description) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/inter_api/client_production.rb', line 97

def refund_payment(end_to_end_id, amount, refund_nature, description)
  id = SecureRandom.hex(10)
  body = {
    valor: format("%.2f", amount),
    natureza: refund_nature,
    descricao: description
  }
  response = put("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body)) { |response| validate_response(response, "id") }
  response.json
end

#update_payment(payment_id, body) ⇒ Object



87
88
89
90
# File 'lib/inter_api/client_production.rb', line 87

def update_payment(payment_id, body)
  response = patch("/pix/v2/cob/#{payment_id}", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body)) { |response| validate_response(response, "txid") }
  InterApi::Payment.new(response.json, self)
end

#update_payment_request(payment_id, body) ⇒ Object



83
84
85
# File 'lib/inter_api/client_production.rb', line 83

def update_payment_request(payment_id, body)
  patch_request("/pix/v2/cob/#{payment_id}", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body))
end

#update_webhook(url) ⇒ Object



108
109
110
111
112
113
# File 'lib/inter_api/client_production.rb', line 108

def update_webhook(url)
  body = {
    webhookUrl: url
  }
  put("/pix/v2/webhook/#{@chave_pix}", headers: build_headers, sslcert: @crt, sslkey: @key, body: JSON.dump(body)) { |response| response.success? }
end