Class: Payment

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/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

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/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/payment.rb', line 12

def customer
  @customer
end

#transaction_idObject

Returns the value of attribute transaction_id.



8
9
10
# File 'app/models/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/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/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/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

#authorize(options = {}) ⇒ Object



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

def authorize(options = {})
  true
end

#build_address_from(params) ⇒ Object



65
66
67
68
69
70
71
72
# File 'app/models/payment.rb', line 65

def build_address_from(params)
  unless params.fetch(:customer, {}).fetch(:address, {}).blank?
    self.customer.address = Address.new(:address1 => params[:customer][:address][:address1],
                                        :city     => params[:customer][:address][:city],
                                        :state    => params[:customer][:address][:state],
                                        :zip      => params[:customer][:address][:zip])
  end
end

#build_customer_from(params) ⇒ Object



57
58
59
60
61
62
63
# File 'app/models/payment.rb', line 57

def build_customer_from(params)
  self.customer.first_name  = params.fetch(:customer, {}).fetch(:first_name, "")
  self.customer.last_name   = params.fetch(:customer, {}).fetch(:last_name, "")
  self.customer.email       = params.fetch(:customer, {}).fetch(:email, "")
  
  self.customer.phones      << Phone.new(:number => params.fetch(:customer, {}).fetch(:phone, "")) unless params[:customer].blank? || params[:customer][:phone].blank?
end

#capture(transaction_id, options = {}) ⇒ Object



85
86
# File 'app/models/payment.rb', line 85

def capture(transaction_id, options = {})
end

#payment_phone_numberObject



110
111
112
# File 'app/models/payment.rb', line 110

def payment_phone_number
  nil
end

#per_item_processing_chargeObject



118
119
120
# File 'app/models/payment.rb', line 118

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 these methods



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

def purchase(options = {})
  true
end

#reduce_amount_by(amount_in_cents) ⇒ Object



114
115
116
# File 'app/models/payment.rb', line 114

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

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



98
99
100
# File 'app/models/payment.rb', line 98

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

#refundable?Boolean

Likewise with payments that need to refund

Returns:

  • (Boolean)


94
95
96
# File 'app/models/payment.rb', line 94

def refundable?
  true
end

#requires_authorization?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/payment.rb', line 102

def requires_authorization?
  false
end

#requires_settlement?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/payment.rb', line 106

def requires_settlement?
  false
end

#void(transaction_id, options = {}) ⇒ Object



88
89
# File 'app/models/payment.rb', line 88

def void(transaction_id, options = {})
end