Class: Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/payment.rb

Overview

Schema Information

Table name: payments

id              :integer(4)      not null, primary key
invoice_id      :integer(4)
confirmation_id :string(255)
amount          :integer(4)
error           :string(255)
error_code      :string(255)
message         :string(255)
action          :string(255)
params          :text
success         :boolean(1)
test            :boolean(1)
created_at      :datetime
updated_at      :datetime

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) authorize(amount, credit_card, options = {})



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

def authorize(amount, credit_card, options = {})
  process('authorization', amount) do |gw|
    gw.authorize(amount, credit_card, options)
  end
end

+ (Object) capture(amount, authorization, options = {})



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

def capture(amount, authorization, options = {})
  process('capture', amount) do |gw|
    gw.capture(amount, authorization, options)
  end
end

+ (Object) charge(amount, profile_key, options = {})



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/payment.rb', line 102

def charge( amount, profile_key, options ={})
  options[:order_id] ||= unique_order_number
  if GATEWAY.respond_to?(:purchase)
    process( 'charge', amount ) do |gw|
      gw.purchase( amount, profile_key, options )
    end
  else
    # do it in 2 transactions
    process( 'charge', amount ) do |gw|
      result = gw.authorize( amount, profile_key, options )
      if result.success?
        gw.capture( amount, result.reference, options )
      else
        result
      end
    end
  end
end

+ (Object) store(credit_card, options = {})



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

def store( credit_card, options = {})
  options[:order_id] ||= unique_order_number
  process( 'store' ) do |gw|
    gw.store( credit_card, options )
  end
end

+ (Object) unstore(profile_key, options = {})



83
84
85
86
87
88
# File 'app/models/payment.rb', line 83

def unstore( profile_key, options = {})
  options[:order_id] ||= unique_order_number
  process( 'unstore' ) do |gw|
    gw.unstore( profile_key, options )
  end
end

+ (Object) validate_card(credit_card, options = {})

validate card via transaction



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/payment.rb', line 122

def validate_card( credit_card, options ={})
  options[:order_id] ||= unique_order_number
  # authorize $1
  amount = 100
  result = process( 'validate', amount ) do |gw|
    gw.authorize( amount, credit_card, options )
  end
  if result.success?
    # void it
    result = process( 'validate' ) do |gw|
      gw.void( result.reference, options )
    end
  end
  result
end

Instance Method Details

- (Object) capture_cim



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/payment.rb', line 53

def capture_cim
  @gateway = GATEWAY

  response = @gateway.create_customer_profile_transaction({:transaction => {
                         :type                        => :auth_capture,
                         :amount                      => self.invoice.amount.to_s,
                         :customer_profile_id         => self.invoice.order.user.customer_cim_id,
                         :customer_payment_profile_id => self.invoice.order.user.payment_profile.payment_cim_id}})

  if response.success? and response.authorization
    update_attributes({:confirmation_id => response.authorization})
    return true
  else
    update_attributes({:error => !response.success?,
                       :error_code => response.params['messages']['message']['code'],
                       :error_message => response.params['messages']['message']['text']})
    return false
  end
end