Class: Payment

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/payments/payment.rb

Overview

Payment subclasses should not involve Artful.ly classes in the internals of ActiveMerchant.

Errors should be commincated to callers with the errors Array

Direct Known Subclasses

CashPayment, CompPayment, CreditCardPayment

Constant Summary collapse

@@payment_methods =

Subclasses should call payment_method :type to get hooked up to the factory

For example, a subclass called WonkaPayment can call payment_method :wonka Then callers can instantiate a WonkaPayment by calling Payment.create :wonka

This will also define an instance method called “payment_method” on the subclass which will return a stringification of the symbol

HashWithIndifferentAccess.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



8
9
10
# File 'app/models/payments/payment.rb', line 8

def amount
  @amount
end

#customerObject

This is named customer as it analogizes the “customer” record on a remote payment system (Braintree, for instance). It is just a Person object.



12
13
14
# File 'app/models/payments/payment.rb', line 12

def customer
  @customer
end

#transaction_idObject

Returns the value of attribute transaction_id.



8
9
10
# File 'app/models/payments/payment.rb', line 8

def transaction_id
  @transaction_id
end

#user_agreementObject

Returns the value of attribute user_agreement.



8
9
10
# File 'app/models/payments/payment.rb', line 8

def user_agreement
  @user_agreement
end

Class Method Details

.create(type, params = {}) ⇒ Object

Call this method to create sub-classes of Payment. params will be passed through to the child class



47
48
49
50
51
52
53
54
55
# File 'app/models/payments/payment.rb', line 47

def self.create(type, params = {})
  type = type.parameterize.underscore.to_sym if type.is_a? String
  c = @@payment_methods[type]
  if c 
    c.new(params)
  else
    raise "No payment method registered for [#{type}], did you call payment_method in the subclass?"
  end
end

.payment_method(names) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/payments/payment.rb', line 27

def self.payment_method names
  names = Array.wrap(names)
  names.each do |name|
    @@payment_methods[name] = self
  end
  
  self.class_eval(<<-EOS, __FILE__, __LINE__)
    def payment_method
      "#{names[0].to_s.gsub('_',' ').capitalize}"
    end
    
    def self.payment_method
      "#{names[0].to_s.gsub('_',' ').capitalize}"
    end
  EOS
end

Instance Method Details

#payment_phone_numberObject



82
83
84
# File 'app/models/payments/payment.rb', line 82

def payment_phone_number
  nil
end

#per_item_processing_chargeObject



90
91
92
# File 'app/models/payments/payment.rb', line 90

def per_item_processing_charge
  lambda { |item| item.realized_price * 0.035 }
end

#purchase(options = {}) ⇒ Object

Subclasses that need to actually process something should override this method



60
61
# File 'app/models/payments/payment.rb', line 60

def purchase(options = {})
end

#reduce_amount_by(amount_in_cents) ⇒ Object



86
87
88
# File 'app/models/payments/payment.rb', line 86

def reduce_amount_by(amount_in_cents)
  self.amount= self.amount - amount_in_cents
end

#refund(refund_amount, transaction_id, options = {}) ⇒ Object



70
71
72
# File 'app/models/payments/payment.rb', line 70

def refund(refund_amount, transaction_id, options = {})
  true
end

#refundable?Boolean

Likewise with payments that need to refund

Returns:

  • (Boolean)


66
67
68
# File 'app/models/payments/payment.rb', line 66

def refundable?
  true
end

#requires_authorization?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/payments/payment.rb', line 74

def requires_authorization?
  false
end

#requires_settlement?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/payments/payment.rb', line 78

def requires_settlement?
  false
end