Sha256: 7d423e08e7fc569d6ca78eb29caf502690c94b6783978c5bed85cb929252d65b

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

class Checkout < ActiveRecord::Base  
  before_save :authorize_creditcard, :unless => "Spree::Config[:auto_capture]"
  before_save :capture_creditcard, :if => "Spree::Config[:auto_capture]"
  after_save :process_coupon_code
  
  belongs_to :order
  belongs_to :bill_address, :foreign_key => "bill_address_id", :class_name => "Address"
  has_one :shipment, :through => :order, :source => :shipments, :order => "shipments.created_at ASC"                       
  
  accepts_nested_attributes_for :bill_address
  accepts_nested_attributes_for :shipment

  # for memory-only storage of creditcard details
  attr_accessor :creditcard    
  attr_accessor :coupon_code

  validates_presence_of :order_id

  private
  def authorize_creditcard
    return unless process_creditcard?
    cc = Creditcard.new(creditcard.merge(:address => self.bill_address, :checkout => self))
    return false unless cc.valid? 
    return false unless cc.authorize(order.total)
    return false unless order.complete
  end

  def capture_creditcard
    return unless process_creditcard? 
    cc = Creditcard.new(creditcard.merge(:address => self.bill_address, :checkout => self))
    return false unless cc.valid?
    return false unless cc.purchase(order.total)
    return false unless order.complete
    order.pay
  end

  def process_creditcard?
    order and creditcard and not creditcard[:number].blank?
  end

  def process_coupon_code
    return unless @coupon_code and coupon = Coupon.find_by_code(@coupon_code.upcase)
    coupon.create_discount(order)       
    # recalculate order totals
    order.save
  end

end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree-enriquez-0.9.4 app/models/checkout.rb
spree-0.9.4 app/models/checkout.rb
spree-0.9.3 app/models/checkout.rb
spree-0.9.2 app/models/checkout.rb
spree-0.9.1 app/models/checkout.rb
spree-0.9.0 app/models/checkout.rb