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
SAVE_RESPONSES =
true

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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/inter_api/client_production.rb', line 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", 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.



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

def chave_pix
  @chave_pix
end

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#conta_correnteObject (readonly)

Returns the value of attribute conta_corrente.



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

def conta_corrente
  @conta_corrente
end

#crtObject (readonly)

Returns the value of attribute crt.



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

def crt
  @crt
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#scopesObject (readonly)

Returns the value of attribute scopes.



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

def scopes
  @scopes
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#token_expires_atObject

Returns the value of attribute token_expires_at.



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

def token_expires_at
  @token_expires_at
end

Instance Method Details

#authenticate!Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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