Class: Bs2Api::Entities::Payment

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Payment

Returns a new instance of Payment.



8
9
10
11
12
13
14
15
16
# File 'lib/bs2_api/entities/payment.rb', line 8

def initialize(args = {})
  @payment_id    = args.fetch(:payment_id, nil)
  @end_to_end_id = args.fetch(:end_to_end_id, nil)
  @receiver      = args.fetch(:receiver, nil)
  @payer         = args.fetch(:payer, nil)
  @status        = args.fetch(:status, nil)
  @error_code    = args.fetch(:error_code, nil)
  @error_message = args.fetch(:error_message, nil)
end

Instance Attribute Details

#end_to_end_idObject

Returns the value of attribute end_to_end_id.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def end_to_end_id
  @end_to_end_id
end

#error_codeObject

Returns the value of attribute error_code.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def error_code
  @error_code
end

#error_messageObject

Returns the value of attribute error_message.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def error_message
  @error_message
end

#payerObject

Returns the value of attribute payer.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def payer
  @payer
end

#payment_idObject

Returns the value of attribute payment_id.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def payment_id
  @payment_id
end

#receiverObject

Returns the value of attribute receiver.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def receiver
  @receiver
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/bs2_api/entities/payment.rb', line 6

def status
  @status
end

Class Method Details

.from_response(hash_payload) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bs2_api/entities/payment.rb', line 32

def self.from_response(hash_payload)
  hash = ActiveSupport::HashWithIndifferentAccess.new(hash_payload)

  Bs2Api::Entities::Payment.new(
    payment_id:    hash["pagamentoId"] || hash["cobranca"]["id"],
    end_to_end_id: hash["endToEndId"],
    receiver:      Bs2Api::Entities::Bank.from_response(hash["recebedor"]),
    payer:         Bs2Api::Entities::Bank.from_response(hash["pagador"]),
    status:        hash["status"],
    error_code:    hash.dig('erro', 'erroCodigo'),
    error_message: hash.dig('erro', 'erroDescricao')
  )
end

Instance Method Details

#to_hashObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bs2_api/entities/payment.rb', line 18

def to_hash
  hash_data = {
    "pagamentoId":   @payment_id,
    "endToEndId":    @end_to_end_id,
    "status":        @status,
    "error_code":    @error_code,
    "error_message": @error_message
  }

  hash_data.merge!({ "recebedor": @receiver.to_hash } ) if @receiver.present?
  hash_data.merge!({ "pagador": @payer.to_hash } ) if @payer.present?
  ActiveSupport::HashWithIndifferentAccess.new(hash_data)
end