Class: CreditCardAccount

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

Overview

Table Definition ###############################

create_table :credit_card_accounts do |t|
  t.timestamps
end

Instance Method Summary collapse

Instance Method Details

#account_numberObject



16
17
18
# File 'app/models/credit_card_account.rb', line 16

def 
  self.credit_card.card_number
end

#authorize(financial_txn, cvv, gateway_wrapper, gateway_options = {}, credit_card_to_use = nil) ⇒ Object

params financial_txn cvv gateway_wrapper

Optional gateway_options credit_card_to_use



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/credit_card_account.rb', line 40

def authorize(financial_txn, cvv, gateway_wrapper, gateway_options={}, credit_card_to_use=nil)
  credit_card_to_use = self.credit_card unless credit_card_to_use

  result = gateway_wrapper.authorize(credit_card_to_use, financial_txn.money.amount, cvv, gateway_options)

  unless result[:payment].nil?
    result[:payment].financial_txn = financial_txn
    result[:payment].save
    financial_txn.payments << result[:payment]
    financial_txn.save
  end

  result
end

#capture(financial_txn, cvv, gateway_wrapper, gateway_options = {}, credit_card_to_use = nil) ⇒ Object

params financial_txn cvv gateway_wrapper

Optional gateway_options credit_card_to_use



109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/credit_card_account.rb', line 109

def capture(financial_txn, cvv, gateway_wrapper, gateway_options={}, credit_card_to_use=nil)
  credit_card_to_use = self.credit_card unless credit_card_to_use

  result = {:success => true}
  payment = Payment.where("current_state = ? and success = ? and financial_txn_id = ?",'authorized', 1, financial_txn.id).order('created_at desc').first
  #only capture this payment if it was authorized
  if !payment.nil? && payment.current_state.to_sym == :authorized
    gateway_options[:debug] = true
    result = gateway_wrapper.capture(credit_card_to_use, payment, cvv, gateway_options)
  end
  result
end

#financial_txnsObject



20
21
22
# File 'app/models/credit_card_account.rb', line 20

def financial_txns
  self.biz_txn_events.where('biz_txn_record_type = ?', 'FinancialTxn').collect(&:biz_txn_record)
end

#purchase(financial_txn, cvv, gateway_wrapper, gateway_options = {}, credit_card_to_use = nil) ⇒ Object

params financial_txn cvv gateway_wrapper

Optional gateway_options credit_card_to_use



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/credit_card_account.rb', line 63

def purchase(financial_txn, cvv, gateway_wrapper, gateway_options={}, credit_card_to_use=nil)
  credit_card_to_use = self.credit_card unless credit_card_to_use

  result = gateway_wrapper.purchase(credit_card_to_use, financial_txn.money.amount, cvv, gateway_options)

  unless result[:payment].nil?
    result[:payment].financial_txn = financial_txn
    result[:payment].save
    financial_txn.payments << result[:payment]
    financial_txn.save
  end

  result
end

#purchase_with_existing_card(financial_txn, gateway_wrapper, gateway_options = {}) ⇒ Object

purchase with credit card associated to this CreditCardAccount



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/credit_card_account.rb', line 79

def purchase_with_existing_card(financial_txn, gateway_wrapper, gateway_options={})
  result = gateway_wrapper.purchase_with_existing_card(self.credit_card, financial_txn.money.amount, gateway_options)

  unless result[:payment].nil?
    result[:payment].financial_txn = financial_txn
    result[:payment].save
    financial_txn.payments << result[:payment]
    financial_txn.save
  end

  result
end

#refund(financial_txn, gateway_wrapper, gateway_options = {}, amount = nil) ⇒ Object

purchase with credit card associated to this CreditCardAccount



93
94
95
96
97
98
99
# File 'app/models/credit_card_account.rb', line 93

def refund(financial_txn, gateway_wrapper, gateway_options={}, amount=nil)
  if amount.nil?
    amount = financial_txn.money.amount
  end

  gateway_wrapper.refund(financial_txn.most_recent_payment, amount, gateway_options)
end

#reverse_authorization(financial_txn, cvv, gateway_wrapper, gateway_options = {}, credit_card_to_use = nil) ⇒ Object

params financial_txn cvv gateway_wrapper

Optional gateway_options credit_card_to_use



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/credit_card_account.rb', line 130

def reverse_authorization(financial_txn, cvv, gateway_wrapper, gateway_options={}, credit_card_to_use=nil)
  credit_card_to_use = self.credit_card unless credit_card_to_use

  result = {:success => true}
  payment = Payment.where("current_state = ? and success = ? and financial_txn_id = ?",'authorized', 1, financial_txn.id).order('created_at desc').first
  #only reverse this payment if it was authorized
  if !payment.nil? && payment.current_state.to_sym == :authorized
    gateway_options[:debug] = true
    gateway_options[:amount] = financial_txn.money.amount
    result = gateway_wrapper.full_reverse_of_authorization(credit_card_to_use, payment, cvv, gateway_options)
  end
  result
end

#successful_paymentsObject



24
25
26
27
28
29
30
# File 'app/models/credit_card_account.rb', line 24

def successful_payments
  payments = []
  self.financial_txns.each do |financial_txn|
    payments << financial_txn.payments.last if financial_txn.has_captured_payment?
  end
  payments
end

#voidObject



144
145
146
# File 'app/models/credit_card_account.rb', line 144

def void
  # implement a void transaction of a transaction
end

#void_or_return(financial_txn, gateway_wrapper, money_amount = nil, gateway_options = {}, credit_card_to_use = nil) ⇒ Object

params financial_txn gateway_wrapper

Optional money_amount: amount to refund may be less than amount originally charged, so money_amount param is optional gateway_options credit_card_to_use



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/credit_card_account.rb', line 156

def void_or_return(financial_txn, gateway_wrapper, money_amount = nil, gateway_options={}, credit_card_to_use=nil)
  credit_card_to_use = self.credit_card unless credit_card_to_use
  result = {:success => true}

  if money_amount == nil
    money_amount = financial_txn.money.amount
  end
  payment = Payment.where("reference_number = ? and financial_txn_id = ?",gateway_options[:ReferenceNumber], financial_txn.id).order('created_at desc').first
  gateway_options[:debug] = true
  result = gateway_wrapper.void_or_return(credit_card_to_use, payment, money_amount, gateway_options)

  result
end