Class: InterApi::Payment

Inherits:
Object
  • Object
show all
Defined in:
lib/inter_api/payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, client) ⇒ Payment

Returns a new instance of Payment.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/inter_api/payment.rb', line 4

def initialize(response, client)
  @client = client
  @response = response
  @txid = response.dig("txid")
  @end_to_end_id = response.dig("pix", 0, "endToEndId")
  @devolucoes = response.dig("pix", 0, "devolucoes")
  @copia_e_cola = response.dig("pixCopiaECola")
  @status = response.dig("status")
  @solicitacao = response.dig("solicitacaoPagador")
  @info_adicionais = response.dig("infoAdicionais")
  @valor_original = response.dig("valor", "original")
  @valor_pago = response.dig("pix", 0, "valor")
  @criacao = creation_date
  @expiracao = expiration_date
  @pago_em = paid_date
  @devedor_nome = response.dig("devedor", "nome")
  @devedor_tax_id = response.dig("devedor", "cpf") || response.dig("devedor", "cnpj")
  @internal_error = PaymentError.new(response).internal_error
end

Instance Attribute Details

#copia_e_colaObject (readonly)

Returns the value of attribute copia_e_cola.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def copia_e_cola
  @copia_e_cola
end

#criacaoObject (readonly)

Returns the value of attribute criacao.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def criacao
  @criacao
end

#devedor_nomeObject (readonly)

Returns the value of attribute devedor_nome.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def devedor_nome
  @devedor_nome
end

#devedor_tax_idObject (readonly)

Returns the value of attribute devedor_tax_id.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def devedor_tax_id
  @devedor_tax_id
end

#devolucoesObject (readonly)

Returns the value of attribute devolucoes.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def devolucoes
  @devolucoes
end

#end_to_end_idObject (readonly)

Returns the value of attribute end_to_end_id.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def end_to_end_id
  @end_to_end_id
end

#expiracaoObject (readonly)

Returns the value of attribute expiracao.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def expiracao
  @expiracao
end

#info_adicionaisObject (readonly)

Returns the value of attribute info_adicionais.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def info_adicionais
  @info_adicionais
end

#internal_errorObject (readonly)

Returns the value of attribute internal_error.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def internal_error
  @internal_error
end

#pago_emObject (readonly)

Returns the value of attribute pago_em.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def pago_em
  @pago_em
end

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def response
  @response
end

#solicitacaoObject (readonly)

Returns the value of attribute solicitacao.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def solicitacao
  @solicitacao
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def status
  @status
end

#txidObject (readonly)

Returns the value of attribute txid.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def txid
  @txid
end

#valor_originalObject (readonly)

Returns the value of attribute valor_original.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def valor_original
  @valor_original
end

#valor_pagoObject (readonly)

Returns the value of attribute valor_pago.



3
4
5
# File 'lib/inter_api/payment.rb', line 3

def valor_pago
  @valor_pago
end

Instance Method Details

#invalidate!Object



45
46
47
48
# File 'lib/inter_api/payment.rb', line 45

def invalidate!
  update({status: "REMOVIDA_PELO_USUARIO_RECEBEDOR"})
  reload!
end

#paid?(external_value = nil) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/inter_api/payment.rb', line 24

def paid? external_value = nil
  external_value = @valor_original if external_value.nil?
  @status == "CONCLUIDA" && @valor_original.to_d == @valor_pago.to_d && external_value.to_d == @valor_pago.to_d
end

#qr_code(module_size: 4) ⇒ Object



56
57
58
59
60
# File 'lib/inter_api/payment.rb', line 56

def qr_code(module_size: 4)
  return false unless @copia_e_cola
  qr = RQRCode::QRCode.new(@copia_e_cola, size: 10, level: :l)
  qr.as_svg(module_size: module_size)
end

#refund(amount: nil, refund_nature: nil, description: nil) ⇒ Object



50
51
52
53
54
# File 'lib/inter_api/payment.rb', line 50

def refund(amount: nil, refund_nature: nil, description: nil)
  amount ||= @valor_pago
  refund_nature ||= "ORIGINAL"
  @client.refund_payment(@end_to_end_id, amount, refund_nature, description)
end

#reload!Object



33
34
35
36
37
38
39
# File 'lib/inter_api/payment.rb', line 33

def reload!
  new_payment = @client.get_payment(@txid)
  instance_variables.each do |variable|
    instance_variable_set(variable, new_payment.instance_variable_get(variable))
  end
  self
end

#update(body) ⇒ Object



41
42
43
# File 'lib/inter_api/payment.rb', line 41

def update(body)
  @client.update_payment(@txid, body)
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/inter_api/payment.rb', line 29

def valid?
  @status == "ATIVA" && @expiracao > Time.now
end