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
|
# File 'lib/one_page_checkout.rb', line 9
def self.activate
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.env.production? ? require(c) : load(c)
end
CheckoutController.class_eval do
def before_one_page
@order.bill_address ||= Address.new(:country => default_country)
@order.ship_address ||= Address.new(:country => default_country)
@order.shipping_method ||= (@order.rate_hash.first && @order.rate_hash.first[:shipping_method])
@order.payments.destroy_all if request.put?
end
def object_params
if params[:payment_source].present? && source_params = params.delete(:payment_source)[params[:order][:payments_attributes].first[:payment_method_id].underscore]
params[:order][:payments_attributes].first[:source_attributes] = source_params
end
if (params[:order][:payments_attributes])
params[:order][:payments_attributes].first[:amount] = @order.total
end
params[:order]
end
end
Order.class_eval do
state_machines.clear
state_machines[:state] = StateMachine::Machine.new(Order, :initial => 'cart', :use_transactions => false) do
event :next do
transition :from => 'cart', :to => 'one_page'
transition :from => 'one_page', :to => 'complete'
end
event :cancel do
transition :to => 'canceled', :if => :allow_cancel?
end
event :return do
transition :to => 'returned', :from => 'awaiting_return'
end
event :resume do
transition :to => 'resumed', :from => 'canceled', :if => :allow_resume?
end
event :authorize_return do
transition :to => 'awaiting_return'
end
before_transition :to => 'complete' do |order|
begin
order.process_payments!
rescue Spree::GatewayError
if Spree::Config[:allow_checkout_on_gateway_error]
true
else
false
end
end
end
after_transition :to => 'complete', :do => :finalize!
after_transition :to => 'canceled', :do => :after_cancel
end
end
end
|