Class: Caboose::CheckoutController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/checkout_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#addressObject

GET /checkout/address



110
111
112
113
114
115
# File 'app/controllers/caboose/checkout_controller.rb', line 110

def address
  render :json => {
    :shipping_address => @order.shipping_address,
    :billing_address => @order.billing_address
  }
end

#addressesObject

Step 2 - Shipping and billing addresses GET /checkout/addresses



28
29
30
31
# File 'app/controllers/caboose/checkout_controller.rb', line 28

def addresses      
  redirect_to '/checkout' if !logged_in?      
  @logged_in_user = logged_in_user      
end

#attach_guestObject

POST /checkout/guest



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/controllers/caboose/checkout_controller.rb', line 169

def attach_guest
  resp = Caboose::StdClass.new      
  email = params[:email]      
  
  if email != params[:confirm_email]
    resp.error = "Emails do not match."
  elsif Caboose::User.where(:email => email, :is_guest => false).exists?
    resp.error = "A user with that email address already exists."
  else
    user = Caboose::User.where(:email => email, :is_guest => true).first
    if user.nil?        
      user = Caboose::User.create(:email => email)
      user.is_guest = true
      user.save
      user = Caboose::User.where(:email => email).first
    end                   
    @order.customer_id = user.id
    (user)
    
    if !@order.valid?        
      resp.errors = @order.errors.full_messages
    else
      @order.save
      resp.redirect = '/checkout/addresses'
    end
  end
  render :json => resp            
end

#attach_userObject

POST /checkout/attach-user



161
162
163
164
165
166
# File 'app/controllers/caboose/checkout_controller.rb', line 161

def attach_user              
  render :json => { :success => false, :errors => ['User is not logged in'] } and return if !logged_in?
  @order.customer_id = logged_in_user.id
  #Caboose.log("Attaching user to order: customer_id = #{@order.customer_id}")
  render :json => { :success => @order.save, :errors => @order.errors.full_messages, :logged_in => logged_in? }
end

#authnet_relayObject

POST /checkout/authnet-relay



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/controllers/caboose/checkout_controller.rb', line 228

def authnet_relay
  Caboose.log("Authorize.net relay, order #{params[:x_invoice_id]}")
  
  order = Caboose::Order.find(params[:x_invoice_num])
  ot = Caboose::OrderTransaction.new(
    :order_id => order.id,
    :date_processed => DateTime.now.utc,
    :transaction_type => Caboose::OrderTransaction::TYPE_AUTHORIZE
  )
  ot.success        = params[:x_response_code] && params[:x_response_code] == '1'
  ot.transaction_id = params[:x_trans_id] if params[:x_trans_id]              
  ot.auth_code      = params[:x_auth_code] if params[:x_auth_code]
  ot.response_code  = params[:x_response_code] if params[:x_response_code]
  ot.amount         = order.total
  ot.save
  
  error = nil
  if ot.success
    order.financial_status = 'authorized'
    order.status = 'pending'
     
    # Take funds from any gift cards that were used on the order
    order.take_gift_card_funds
    
    # Send out emails        
    OrdersMailer.configure_for_site(@site.id).customer_new_order(order).deliver
    OrdersMailer.configure_for_site(@site.id).fulfillment_new_order(order).deliver        
    
    # Emit order event
    Caboose.plugin_hook('order_authorized', order)
  else
    order.financial_status = 'unauthorized'        
    error = "There was a problem processing your payment."
  end
        
  order.save
  
  @url = params[:x_after_relay]
  @url << (ot.success ? "?success=1" : "?error=#{error}")             
              
  render :layout => false
end

#authnet_responseObject

GET /checkout/authnet-response/:order_id POST /checkout/authnet-response/:order_id



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'app/controllers/caboose/checkout_controller.rb', line 273

def authnet_response
  Caboose.log("Authorize.net response, order #{params[:order_id]}")
  
  @resp = Caboose::StdClass.new
  @resp.success = true if params[:success]
  @resp.error = params[:error] if params[:error]
  
  if @resp.success        
    session[:cart_id] = nil
    init_cart
  end
  
  render :layout => false
end

#ensure_line_itemsObject



9
10
11
# File 'app/controllers/caboose/checkout_controller.rb', line 9

def ensure_line_items
  redirect_to '/checkout/empty' if @order.line_items.empty?
end

#gift_cardsObject

Step 4 - Gift cards GET /checkout/gift-cards



54
55
56
57
58
59
# File 'app/controllers/caboose/checkout_controller.rb', line 54

def gift_cards
  redirect_to '/checkout'           and return if !logged_in?
  redirect_to '/checkout/addresses' and return if @order.shipping_address.nil? || @order.billing_address.nil?
  redirect_to '/checkout/shipping'  and return if @order.has_empty_shipping_methods?
  @logged_in_user = logged_in_user
end

#indexObject

Step 1 - Login or register GET /checkout



15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/caboose/checkout_controller.rb', line 15

def index        
  if logged_in?
    if @order.customer_id.nil?
      @order.customer_id = logged_in_user.id
      @order.save
    end
    redirect_to '/checkout/addresses'
    return        
  end
end

#paymentObject

Step 5 - Payment GET /checkout/payment



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/caboose/checkout_controller.rb', line 63

def payment
  redirect_to '/checkout'           and return if !logged_in?
  redirect_to '/checkout/addresses' and return if @order.shipping_address.nil? || @order.billing_address.nil?
  redirect_to '/checkout/shipping'  and return if @order.has_empty_shipping_methods?      
  
  # Make sure all the variants still exist      
  @order.line_items.each do |li|
    v = Variant.where(:id => li.variant_id).first
    if v.nil? || v.status == 'Deleted'
      render :file => 'caboose/checkout/deleted_variant'
      return
    end
  end
  
  store_config = @site.store_config
  case store_config.pp_name
    when 'authorize.net'
      
      sc = @site.store_config
      @sim_transaction = AuthorizeNet::SIM::Transaction.new(
        sc.pp_username, 
        sc.pp_password, 
        @order.total,            
        #:relay_url => "#{request.protocol}#{request.host_with_port}/checkout/authnet-relay/#{@order.id}",
        :relay_response => 'TRUE',
        #:relay_url => "#{request.protocol}#{request.host_with_port}/checkout/authnet-relay",
        :relay_url => "#{sc.pp_relay_domain}/checkout/authnet-relay",
        :transaction_type => 'AUTH_ONLY',                        
        :test => sc.pp_testing
      )
      @request = request
      @show_relay = params[:show_relay] && params[:show_relay].to_i == 1
      
    when 'payscape'
      @form_url = Caboose::PaymentProcessor.form_url(@order)
  end
  @logged_in_user = logged_in_user
end

#shippingObject

Step 3 - Shipping method GET /checkout/shipping



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/caboose/checkout_controller.rb', line 35

def shipping
  redirect_to '/checkout'           and return if !logged_in?
  redirect_to '/checkout/addresses' and return if @order.shipping_address.nil? || @order.billing_address.nil?
              
  # Remove any order packages      
  LineItem.where(:order_id => @order.id).update_all(:order_package_id => nil)
  OrderPackage.where(:order_id => @order.id).destroy_all      
    
  # Calculate what shipping packages we'll need            
  OrderPackage.create_for_order(@order)
  
  # Now get the rates for those packages            
  @rates = ShippingCalculator.rates(@order)      
  #Caboose.log(@rates.inspect)
  @logged_in_user = logged_in_user      
end

#thanksObject

GET /checkout/thanks



103
104
105
# File 'app/controllers/caboose/checkout_controller.rb', line 103

def thanks
  @logged_in_user = logged_in_user
end

#update_addressesObject

PUT /checkout/addresses



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/caboose/checkout_controller.rb', line 118

def update_addresses
  
  # Grab or create addresses
  shipping_address = if @order.shipping_address then @order.shipping_address else Address.new end
  billing_address  = if @order.billing_address  then @order.billing_address  else Address.new end
  
  # Shipping address
  shipping_address.first_name = params[:shipping][:first_name]
  shipping_address.last_name  = params[:shipping][:last_name]
  shipping_address.company    = params[:shipping][:company]
  shipping_address.address1   = params[:shipping][:address1]
  shipping_address.address2   = params[:shipping][:address2]
  shipping_address.city       = params[:shipping][:city]
  shipping_address.state      = params[:shipping][:state]
  shipping_address.zip        = params[:shipping][:zip]
  
  # Billing address
  if params[:use_as_billing]
    billing_address.update_attributes(shipping_address.attributes)
  else
    billing_address.first_name = params[:billing][:first_name]
    billing_address.last_name  = params[:billing][:last_name]
    billing_address.company    = params[:billing][:company]
    billing_address.address1   = params[:billing][:address1]
    billing_address.address2   = params[:billing][:address2]
    billing_address.city       = params[:billing][:city]
    billing_address.state      = params[:billing][:state]
    billing_address.zip        = params[:billing][:zip]
  end
  
  # Save address info; generate ids
  render :json => { :success => false, :errors => shipping_address.errors.full_messages, :address => 'shipping' } and return if !shipping_address.save
  render :json => { :success => false, :errors => billing_address.errors.full_messages, :address => 'billing' } and return if !billing_address.save
  
  # Associate address info with order
  @order.shipping_address_id = shipping_address.id
  @order.billing_address_id  = billing_address.id
  
  #render :json => { :redirect => 'checkout/shipping' }
  render :json => { :success => @order.save, :errors => @order.errors.full_messages }
end

#update_shippingObject

PUT /checkout/shipping



199
200
201
202
203
204
205
206
207
# File 'app/controllers/caboose/checkout_controller.rb', line 199

def update_shipping
  op = OrderPackage.find(params[:order_package_id])
  op.shipping_method_id = params[:shipping_method_id]
  op.total = params[:total]
  op.save
  op.order.calculate
                               
  render :json => { :success => true }               
end