5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/spree/checkout.rb', line 5
def checkout
build_object
load_object
load_data
load_checkout_steps
if params[:order]
params[:order][:bill_address_attributes][:id] = @order.bill_address.id if @order.bill_address
params[:order][:ship_address_attributes][:id] = @order.ship_address.id if @order.ship_address
end
@order.update_attributes(params[:order])
@order.bill_address ||= Address.new(:country => @default_country)
@order.ship_address ||= Address.new(:country => @default_country)
if @order.creditcards.empty?
@order.creditcards.build(:month => Date.today.month, :year => Date.today.year)
end
@shipping_method = ShippingMethod.find_by_id(params[:method_id]) if params[:method_id]
@shipping_method ||= @order.shipping_methods.first
@order.shipments.build(:address => @order.ship_address, :shipping_method => @shipping_method) if @order.shipments.empty?
if request.post?
@order.creditcards[0].address = @order.bill_address if @order.creditcards.present?
@order.user = current_user
@order.ip_address = request.env['REMOTE_ADDR']
@order.update_totals
@order.email = current_user.email if @order.email.blank? && current_user
begin
if @order.valid?
if params[:final_answer].blank?
@order.save
else
@order.creditcards[0].authorize(@order.total)
@order.complete
session[:order_id] = nil
end
else
flash.now[:error] = t("unable_to_save_order")
render :action => "checkout" and return unless request.xhr?
end
rescue Spree::GatewayError => ge
flash.now[:error] = t("unable_to_authorize_credit_card") + ": #{ge.message}"
render :action => "checkout" and return
end
respond_to do |format|
format.html do
flash[:notice] = t('order_processed_successfully')
order_params = {:checkout_complete => true}
order_params[:order_token] = @order.token unless @order.user
redirect_to order_url(@order, order_params)
end
format.js {render :json => { :order_total => number_to_currency(@order.total),
:ship_amount => number_to_currency(@order.ship_amount),
:tax_amount => number_to_currency(@order.tax_amount),
:available_methods => rate_hash}.to_json,
:layout => false}
end
end
end
|