1 class Authorization < ActiveRecord::Base
2 attr_accessor :passphrase
6 has_one :void, :as => :voidee
7 belongs_to :credit_card
11 validates_presence_of :transaction_id
12 validates_presence_of :last_four_digits
14 after_validation :remove_other_error_messages
16 def initialize attributes
18 # Do an authorization on an existing credit card
19 if credit_card_id = attributes[:credit_card_id]
20 self.credit_card = CreditCard.find(credit_card_id).decrypt!(attributes[:passphrase])
25 alias_method :set_credit_card_association, :credit_card=
29 card = CreditCard.new(:number => card['number'], :month => card['month'], :year => card['year'], :name => card['name'])
30 set_credit_card_association card
31 self.last_four_digits = card.last_four_digits
33 set_credit_card_association card
40 response = $gateway.authorize(self.amount.to_cents, self.credit_card)
42 self.transaction_id = response.authorization
43 self.last_four_digits = self.credit_card.last_four_digits
45 logger.info "Bad authorization!"
46 logger.info response.message
47 errors.add_to_base(response.message)
51 # There might be a better way to do this, but I don't want ActiveRecord
52 # adding error messages for when the transaction_id or last_four_digits
53 # is missing as a result of a failed authorization. The only error
54 # message should be from the credit card processor.
55 def remove_other_error_messages
56 base_error_msg = errors.on(:base)
58 errors.add_to_base(base_error_msg) if base_error_msg