Class: Dune::Balanced::Creditcard::Payment

Inherits:
Object
  • Object
show all
Defined in:
app/models/dune/balanced/creditcard/payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine_name, customer, resource, attrs = {}) ⇒ Payment

Returns a new instance of Payment.



5
6
7
8
9
10
# File 'app/models/dune/balanced/creditcard/payment.rb', line 5

def initialize(engine_name, customer, resource, attrs = {})
  @engine_name  = engine_name
  @customer     = customer
  @resource     = resource
  @attrs        = attrs
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



3
4
5
# File 'app/models/dune/balanced/creditcard/payment.rb', line 3

def attrs
  @attrs
end

#customerObject (readonly)

Returns the value of attribute customer.



3
4
5
# File 'app/models/dune/balanced/creditcard/payment.rb', line 3

def customer
  @customer
end

#engine_nameObject (readonly)

Returns the value of attribute engine_name.



3
4
5
# File 'app/models/dune/balanced/creditcard/payment.rb', line 3

def engine_name
  @engine_name
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'app/models/dune/balanced/creditcard/payment.rb', line 3

def resource
  @resource
end

Instance Method Details

#amount_in_centsObject



27
28
29
# File 'app/models/dune/balanced/creditcard/payment.rb', line 27

def amount_in_cents
  (fee_calculator.gross_amount * 100).round
end

#checkout!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/dune/balanced/creditcard/payment.rb', line 12

def checkout!
  perform_debit!
  resource.confirm!
rescue Balanced::PaymentRequired
  resource.cancel!
ensure
  resource.update_attributes(
    payment_id:                       @debit.try(:id),
    payment_method:                   engine_name,
    payment_service_fee:              fee_calculator.fees,
    payment_service_fee_paid_by_user: attrs[:pay_fee]
  )
  update_meta(@debit) if @debit
end

#debitObject



43
44
45
# File 'app/models/dune/balanced/creditcard/payment.rb', line 43

def debit
  @debit.try(:sanitize)
end

#fee_calculatorObject



31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/dune/balanced/creditcard/payment.rb', line 31

def fee_calculator
  @fee_calculator and return @fee_calculator

  calculator_class = if ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include? attrs[:pay_fee]
                       TransactionAdditionalFeeCalculator
                     else
                       TransactionInclusiveFeeCalculator
                     end

  @fee_calculator = calculator_class.new(resource.value)
end

#successful?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/dune/balanced/creditcard/payment.rb', line 47

def successful?
  %w(pending succeeded).include? @debit.try(:status)
end