Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Order < ActiveRecord::Base
- include ActiveMerchant::Billing
- def self.validates_presence_of(fields)
- if $already_included
- raise "I was already included, this is the second time, the first was #{$already_included.inspect}"
- end
- begin
- raise "This is the first time"
- rescue Exception => e
- $already_included = e.backtrace
- end
- end
- has_many :line_items
- has_many :line_item_options
- CARD_TYPES = [
- # Displayed stored in db
- [ "Visa", "visa" ],
- [ "Mastercard", "mastercard" ],
- ]
- CARD_EXPIRATION_MONTHS = [
- # Displayed stored in db
- [ "January", "1" ],
- [ "February", "2" ],
- [ "March", "3" ],
- [ "April", "4" ],
- [ "May", "5" ],
- [ "June", "6" ],
- [ "July", "7" ],
- [ "August", "8" ],
- [ "September", "9" ],
- [ "October", "10" ],
- [ "November", "11" ],
- [ "December", "12" ],
- ]
- CARD_EXPIRATION_YEARS = [
- # Displayed stored in db
- [ Time.now.year, Time.now.year ],
- [ 1.year.from_now.year, "#{1.year.from_now.year}" ],
- [ 2.years.from_now.year, "#{2.years.from_now.year}" ],
- [ 3.years.from_now.year, "#{3.years.from_now.year}" ],
- [ 4.years.from_now.year, "#{4.years.from_now.year}" ],
- [ 5.years.from_now.year, "#{5.years.from_now.year}" ],
- [ 6.years.from_now.year, "#{6.years.from_now.year}" ],
- ]
- validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :if=> Proc.new{|record| record.step == "checkout"}
- validates_presence_of :billing_addr1, #:billing_address,
- :first_name,
- :last_name,
- :email,
- :billing_city,
- :billing_state,
- :billing_zip,
- :customer_ip,
- :card_type,
- :card_verification_value,
- :card_number,
- :card_expiration_month,
- :card_expiration_year,
- :if=> Proc.new{|record| record.step == "checkout"}
- # cc fields as accessors so we don't store anything bad.
- attr_accessor :card_type, :card_expiration_month, :card_expiration_year, :card_number, :card_verification_value, :step
- def add_line_items_from_cart(cart)
- cart.image_items.each do |item|
- li = LineItem.from_cart_item(item)
- line_items << li
- end
- end
- # Process the payment
- def process
- puts "doing it"
- self.processed_at = Time.now
- begin
- process_payment
- rescue => e
- logger.error("reservation #{self.id} failed with error message #{e} ")
- self.error_message = "#{e}"
- self.status = 7 #failed
- return false
- end
- save!
- end
- def subtotal_price
- self.line_items.sum(:total_price)
- end
- def tax
- self.line_items.first.subject.job.tax_rate * self.subtotal_price
- end
- def shipping
- self.line_items.first.subject.job.shipping_handling
- end
- def total_price
- self.subtotal_price + self.tax + self.shipping
- end
- protected #--------------------------------------------------------------------------------------------------------------------------------
- def process_payment
- # this forces the system to use the testing server, which is what all dev accounts use.
- ActiveMerchant::Billing::Base.mode = :test if RAILS_ENV != "production"
- # read api key and transaction # from config file
- c = YAML::load(File.open("#{RAILS_ROOT}/config/config.yml"))
- user = c[RAILS_ENV]['auth_net_user']
- pass = c[RAILS_ENV]['auth_net_pass']
- creditcard = ActiveMerchant::Billing::CreditCard.new(
- :first_name => self.first_name,
- :last_name => self.last_name,
- :number=> self.card_number,
- :verification_value =>card_verification_value,
- :type => self.card_type,
- :month => self.card_expiration_month,
- :year => self.card_expiration_year
- )
- if creditcard.valid?
- options = {:name => self.first_name,
- :email => self.email,
- :phone => self.phone_number,
- :ip => self.customer_ip,
- :card_code => self.card_verification_value,
- :order_id => self.id,
- :description => "Portrait Package",
- :address1 => self.billing_addr1,
- :city => self.billing_city,
- :state => self.billing_state,
- :zip => self.billing_zip,
- :country => "US"# }
- }
- gateway = AuthorizeNetGateway.new({:login => user, :password=>pass})
- response = gateway.purchase(self.total_cost, creditcard, options)
- if response.success?
- self.status = 1
- puts "success"
- self.error_message = nil
- self.trans_number = response.authorization
- else
- self.status = 7
- self.error_message = response.message
- end
- else
- self.status = 7
- self.error_message = response.message
- end
- end
- end
Add Comment
Please, Sign In to add comment