Class: Akatus::Payment

Inherits:
Object
  • Object
show all
Includes:
Transferrable
Defined in:
lib/akatus/payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transferrable

included

Constructor Details

#initialize(opts = {}) ⇒ Payment

Returns a new instance of Payment.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/akatus/payment.rb', line 11

def initialize(opts = {})
  options = {
    :items         => [],
    :discount      => 0,
    :shipping_cost => 0,
    :weight        => 0
  }.merge(opts)
  super(options)

  @receiver = Receiver.new({ :email => Akatus.config.email, :api_key => Akatus.config.api_key })
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def currency
  @currency
end

#discountObject

Returns the value of attribute discount.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def discount
  @discount
end

#errorObject

Returns the value of attribute error.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def error
  @error
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def id
  @id
end

#itemsObject

Returns the value of attribute items.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def items
  @items
end

#payerObject

Returns the value of attribute payer.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def payer
  @payer
end

#payment_methodObject

Returns the value of attribute payment_method.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def payment_method
  @payment_method
end

#receiverObject (readonly)

Returns the value of attribute receiver.



5
6
7
# File 'lib/akatus/payment.rb', line 5

def receiver
  @receiver
end

#referenceObject

Returns the value of attribute reference.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def reference
  @reference
end

#shipping_costObject

Returns the value of attribute shipping_cost.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def shipping_cost
  @shipping_cost
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def status
  @status
end

#total_amountObject

Returns the value of attribute total_amount.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def total_amount
  @total_amount
end

#transaction_idObject

Returns the value of attribute transaction_id.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def transaction_id
  @transaction_id
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def url
  @url
end

#weightObject

Returns the value of attribute weight.



7
8
9
# File 'lib/akatus/payment.rb', line 7

def weight
  @weight
end

Instance Method Details

#to_payloadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/akatus/payment.rb', line 23

def to_payload
  hsh = {
    'carrinho' => {
      'produtos'  => { 'produto' => items.map { |i| i.to_payload(false) } },
      'transacao' => {
        'peso'       => Akatus.format_number(total_weight),
        'frete'      => Akatus.format_number(total_shipping_cost),
        'desconto'   => Akatus.format_number(total_discount),
        'referencia' => reference,
        'moeda'      => 'BRL'
      }.merge(payment_method.to_payload),
      'recebedor'  => receiver.to_payload(false),
      'pagador'    => payer.to_payload(false)
    }
  }
end

#total_discountObject



48
49
50
# File 'lib/akatus/payment.rb', line 48

def total_discount
  discount + items.inject(0) { |sum, item| sum + item.discount }
end

#total_items_amountObject



52
53
54
# File 'lib/akatus/payment.rb', line 52

def total_items_amount
  items.inject(0) { |sum, item| sum + item.total_amount }
end

#total_shipping_costObject



44
45
46
# File 'lib/akatus/payment.rb', line 44

def total_shipping_cost
  shipping_cost + items.inject(0) { |sum, item| sum + item.shipping_cost }
end

#total_weightObject



40
41
42
# File 'lib/akatus/payment.rb', line 40

def total_weight
  weight + items.inject(0) { |sum, item| sum + item.weight }
end