Class: InterApi::ClientProduction

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

Direct Known Subclasses

ClientSandbox

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", access_token: nil, token_expires_at: nil, test_mode: false) ⇒ ClientProduction

Returns a new instance of ClientProduction.



8
9
10
11
12
13
14
15
16
17
18
19
# 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", access_token: nil, token_expires_at: nil, test_mode: false)
  @crt = crt
  @key = key
  @client_id = client_id
  @client_secret = client_secret
  @chave_pix = chave_pix
  @conta_corrente = conta_corrente
  @scopes = scopes
  @access_token= access_token
  @token_expires_at = token_expires_at
  @test_mode = test_mode
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

Instance Method Details

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



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

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

  post("/pix/v2/cob", body:)
end

#default_optionsObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/inter_api/client_production.rb', line 21

def default_options
  {
    sslcert: crt,
    sslkey: key,
    headers: {
      "Content-Type" => "application/json",
      "x-conta-corrente" => @conta_corrente
    }
  }
end

#get_payment(payment_id) ⇒ Object



65
66
67
# File 'lib/inter_api/client_production.rb', line 65

def get_payment(payment_id)
  get("/pix/v2/cob/#{payment_id}")
end

#get_refund(end_to_end_id:, id:) ⇒ Object



76
77
78
# File 'lib/inter_api/client_production.rb', line 76

def get_refund(end_to_end_id:, id:)
  get("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}")
end

#get_webhookObject



97
98
99
# File 'lib/inter_api/client_production.rb', line 97

def get_webhook
  get("/pix/v2/webhook/#{@chave_pix}")
end

#refresh_tokenObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/inter_api/client_production.rb', line 32

def refresh_token
  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:, body:, skip_authentication: true)
  @access_token = response.access_token
  @token_expires_at = Time.now + response.expires_in
end

#refund_payment(end_to_end_id:, amount:, refund_nature: "ORIGINAL", description: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/inter_api/client_production.rb', line 80

def refund_payment(end_to_end_id:, amount:, refund_nature: "ORIGINAL", description: nil)
  id = SecureRandom.hex(10)
  body = {
    valor: format("%.2f", amount),
    natureza: refund_nature,
    descricao: description
  }.compact
  put("/pix/v2/pix/#{end_to_end_id}/devolucao/#{id}", body:)
end

#update_payment(payment_id:, status:) ⇒ Object



69
70
71
72
73
74
# File 'lib/inter_api/client_production.rb', line 69

def update_payment(payment_id:, status:)
  body = {
    status: status
  }
  patch("/pix/v2/cob/#{payment_id}", body:)
end

#update_webhook(url) ⇒ Object



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

def update_webhook(url)
  body = {
    webhookUrl: url
  }
  put("/pix/v2/webhook/#{@chave_pix}", body:)
end