rename cvv to verification value
[monkeycharger.git] / trunk / app / models / authorization.rb
blob0157e079756e568f8367206b2ff85513b9f80b68
1 class Authorization < ActiveRecord::Base
2   attr_accessor :passphrase
4   has_many    :refunds
5   has_one     :capture
6   has_one     :void, :as => :voidee
7   belongs_to  :credit_card
9   validate :authorize!
11   validates_presence_of :transaction_id
12   validates_presence_of :last_four_digits
13   
14   def initialize attributes
15     super(attributes)
16     # Do an authorization on an existing credit card
17     if credit_card_id = attributes[:credit_card_id]
18       self.credit_card = CreditCard.find(credit_card_id).decrypt!(attributes[:passphrase])
19     end
20   end
23   alias_method :set_credit_card_association, :credit_card=
25   def credit_card= card
26     if card.is_a?(Hash)
27       card =  CreditCard.new(:number => card['number'], :verification_value => card['verification_value'], :month => card['month'], :year => card['year'], :name => card['name'])
28       set_credit_card_association card
29       self.last_four_digits = card.last_four_digits
30     else
31       set_credit_card_association card
32     end
33   end
35   private
37   def authorize! 
38     response = $gateway.authorize(self.amount, self.credit_card)
39     if response.success?
40       self.transaction_id = response.authorization
41       self.last_four_digits = self.credit_card.last_four_digits
42     else
43       logger.info "Bad authorization!"
44       logger.info response.message
45       errors.add_to_base(response.message)
46     end
47   end
48 end