Skip to content

Instantly share code, notes, and snippets.

@meesterdude
Created July 12, 2012 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meesterdude/3098318 to your computer and use it in GitHub Desktop.
Save meesterdude/3098318 to your computer and use it in GitHub Desktop.
class StoreController < ApplicationController
before_filter :check_store_status, :except => :store_closed
before_filter :find_cart, :except => :store_closed
before_filter :confirm_cart_not_empty, :only => [:display_cart, :checkout, :confirm_order, :save_order]
layout 'store'
def checkout
unless @cart.all_items_purchasable?
flash[:notice] = "There are unavailable items in your cart. Remove them and checkout again."
redirect_to(display_cart_store_path)
else
@designs = @cart.items.collect {|i| i.design }
unless params[:design].blank?
@design = @cart.items[params[:design].to_i].design
end
@customer = @cart.customer || (Customer.new_based_on(@design) if @design) || Customer.new
@payment = @cart.payment || Payment.new
end
end
def add_design
@shirt = Sku.find(params[:id])
confirm_availability(@shirt)
@design = Design.new()
@design.sponsor_email = 1
end
def prepare_for_cart
sku = Sku.find(params[:id])
if confirm_availability(sku)
if sku.is_shirt?
redirect_to(add_design_store_path(:id => sku))
else
@cart.add_sku(sku)
redirect_to(display_cart_store_path)
end
end
end
def add_shirt_to_cart
@shirt = Sku.find(params[:id])
if confirm_availability(@shirt)
@design = Design.new(params[:design])
@design.brand_id = @shirt.product.brand_id
if @design.valid?
@cart.add_shirt(@shirt, @design)
redirect_to(display_cart_store_path)
else
render(add_design_store_path)
end
end
rescue
logger.error("Add shirt to cart failed for an unknown reason.")
flash[:notice] = "Unknown error."
redirect_to(root_path) and return false
end
def remove_from_cart
@cart.remove_sku(params[:id])
unless @cart.items.empty?
flash[:notice] = "Item removed from your cart."
redirect_to(display_cart_store_path)
else
flash[:notice] = "Your cart is now empty."
redirect_to(root_path)
end
rescue
logger.error("Attempt to remove invalid sku from cart #{params[:id]}")
flash[:notice] = "You have tried to remove an invalid sku."
redirect_to(display_cart_store_path) and return false
end
def display_cart
# everything is handled by the before filters
end
def empty_cart
@cart.empty!
flash[:notice] = "Your cart is now empty."
redirect_to(root_path)
end
def confirm_order
@customer = Customer.new(params[:customer])
amount = params[:payment].delete(:amount)
@payment = Payment.new(params[:payment])
@payment.amount = @payment.pay_type == "cc" ? @cart.total_price : amount
if @customer.valid? && @payment.valid?
@cart.customer = @customer
@cart.payment = @payment
else
render(checkout_store_path)
end
end
def save_order
#check for all the obvious reasons we can't process this order
if !@cart.customer || !@cart.payment
flash[:notice] = "The customer or payment information is missing."
redirect_to(checkout_store_path) and return
elsif !@cart.payment.public_type?
logger.error("Illegal attempt to use payment type #{@cart.payment.pay_type}")
flash[:notice] = "You have attempted an invalid action."
redirect_to(root_path) and return
end
@customer = @cart.customer.clone
@order = Order.new(:created_by => "web store")
for item in @cart.items
new_item = item.clone
@order.line_items << new_item
end
@customer.orders << @order
#get the payment info from the cart
@payment = @cart.payment.clone
@payment.amount = @order.calculate_balance_due if @payment.pay_type == "cc"
if !@customer.valid? || !@payment.valid?
render(checkout_store_path)
else
if @customer.save # should save customer, order, line_items, & designs
logger.warn("Customer #{@customer.id.to_s} saved with order #{@order.invoice_number}")
attempt_order_payment
else #customer save was tried but failed, payment never attempted
flash.now[:notice] = "Your billing information failed to save. Please review it and try again."
render(checkout_store_path)
end
# end
end
end
def card_error
end
def show_receipt
recent_order_id = (session[:recent_order_id] || 0).to_i
@order = Order.find_by_id(recent_order_id)
if @order && (@order.invoice_number == params[:id])
if EMAIL_RECEIPTS == true
@order.try_to_email_receipt
@order.try_to_email_designs
end
else
logger.error("Illegal attempt to access invoice #{params[:id]}")
flash[:notice] = "You do not have access to that invoice."
redirect_to(root_path)
end
end
def store_closed
#just text
end
private
def confirm_availability(sku=nil)
unless sku.is_purchasable?
logger.error("Attempt to access invalid sku: #{sku.id}")
flash[:notice] = "You have tried to select an invalid sku."
redirect_to(root_path) and return false
else
return true
end
end
def confirm_cart_not_empty
if @cart.items.empty?
flash[:notice] = "Your cart is empty."
redirect_to(root_path) and return false
end
end
def attempt_order_payment
if @payment.try_to_pay(@order) #true if payment succeeds, false if it fails
@payment.order_id = @order.id
@payment.save
@cart.subtract_from_inventory
@cart.empty!
session[:recent_order_id] = @order.id.to_s
redirect_to(show_receipt_store_path(@order.invoice_number))
else
#Customer.destroy(@order.customer.id)
#Order.destroy(@order.id)
logger.warn("Destroying customer #{@customer.id.to_s}")
@customer.destroy #this assumes we aren't adding new orders to existing customers!
line_item_ids = @order.line_items.collect {|li| li.id}.join(', ')
design_ids = @order.line_items.collect {|li| li.design.id}.join(', ')
logger.warn("Destroying order #{@order.invoice_number} and line_items #{line_item_ids} and designs #{design_ids}")
@order.destroy
flash[:notice] = 'A payment error occurred: ' + (@payment.transaction_error || "Unknown error")
redirect_to(card_error_store_path)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment