Class: Akatus::Services::Installments

Inherits:
Akatus::Service show all
Defined in:
lib/akatus/services/installments.rb

Constant Summary collapse

PATH =
'parcelamento/simulacao'
METHOD =
:get

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Akatus::Service

#send_request

Class Method Details

.calculate(payment) ⇒ Object



10
11
12
# File 'lib/akatus/services/installments.rb', line 10

def self.calculate(payment)
  self.new.calculate(payment)
end

Instance Method Details

#calculate(payment) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/akatus/services/installments.rb', line 14

def calculate(payment)

  @payment = payment

  unless payment.payment_method.is_a?(CreditCard)
    return InstallmentOptions.blank(payment)
  end

  data = send_request

  options = InstallmentOptions.new

  options.description        = data['descricao']
  options.taken_installments = data['parcelas_assumidas']

  options.installments = data['parcelas'].map do |parcela|
    Installment.new({
      :quantity       => parcela['quantidade'],
      :unitary_amount => BigDecimal.new(parcela['valor']),
      :total_amount   => BigDecimal.new(parcela['total'])
    })
  end

  options

end

#to_payloadObject



41
42
43
44
45
46
47
48
# File 'lib/akatus/services/installments.rb', line 41

def to_payload
  {
    :email          => @payment.receiver.email,
    :amount         => @payment.total_amount,
    :payment_method => @payment.payment_method.brand,
    :api_key        => @payment.receiver.api_key
  }
end