-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcheckout_controller.rb
217 lines (177 loc) · 6.81 KB
/
checkout_controller.rb
1
2
3
4
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
159
160
161
162
163
164
165
166
167
168
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
module Spree
# This is somewhat contrary to standard REST convention since there is not
# actually a Checkout object. There's enough distinct logic specific to
# checkout which has nothing to do with updating an order that this approach
# is warranted.
class CheckoutController < Spree::StoreController
before_action :load_order
around_action :lock_order
before_action :set_state_if_present
before_action :ensure_order_not_completed
before_action :ensure_checkout_allowed
before_action :ensure_sufficient_stock_lines
before_action :ensure_valid_state
before_action :associate_user
before_action :check_authorization
before_action :apply_coupon_code
before_action :setup_for_current_state, only: [:edit, :update]
helper 'spree/orders'
rescue_from Spree::Core::GatewayError, with: :rescue_from_spree_gateway_error
# Updates the order and advances to the next state (when possible.)
def update
if update_order
assign_temp_address
unless transition_forward
redirect_on_failure
return
end
if @order.completed?
finalize_order
else
send_to_next_state
end
else
render :edit
end
end
private
def update_order
OrderUpdateAttributes.new(@order, update_params, request_env: request.headers.env).apply
end
def assign_temp_address
@order.temporary_address = !params[:save_user_address]
end
def redirect_on_failure
flash[:error] = @order.errors.full_messages.join("\n")
redirect_to(checkout_state_path(@order.state))
end
def transition_forward
if @order.can_complete?
@order.complete
else
@order.next
end
end
def finalize_order
@current_order = nil
set_successful_flash_notice
redirect_to completion_route
end
def set_successful_flash_notice
flash.notice = Spree.t(:order_processed_successfully)
flash['order_completed'] = true
end
def send_to_next_state
redirect_to checkout_state_path(@order.state)
end
def update_params
if update_params = massaged_params[:order]
update_params.permit(permitted_checkout_attributes)
else
# We current allow update requests without any parameters in them.
{}
end
end
def massaged_params
massaged_params = params.deep_dup
move_payment_source_into_payments_attributes(massaged_params)
if massaged_params[:order] && massaged_params[:order][:existing_card].present?
Spree::Deprecation.warn("Passing order[:existing_card] is deprecated. Send order[:wallet_payment_source_id] instead.", caller)
move_existing_card_into_payments_attributes(massaged_params) # deprecated
end
move_wallet_payment_source_id_into_payments_attributes(massaged_params)
set_payment_parameters_amount(massaged_params, @order)
massaged_params
end
def ensure_valid_state
unless skip_state_validation?
if (params[:state] && [email protected]_checkout_step?(params[:state])) ||
(!params[:state] && [email protected]_checkout_step?(@order.state))
@order.state = 'cart'
redirect_to checkout_state_path(@order.checkout_steps.first)
end
end
# Fix for https://github.com/spree/spree/issues/4117
# If confirmation of payment fails, redirect back to payment screen
if params[:state] == "confirm" && @order.payment_required? && @order.payments.valid.empty?
flash.keep
redirect_to checkout_state_path("payment")
end
end
# Should be overriden if you have areas of your checkout that don't match
# up to a step within checkout_steps, such as a registration step
def skip_state_validation?
false
end
def load_order
@order = current_order
redirect_to(spree.cart_path) && return unless @order
end
def set_state_if_present
if params[:state]
redirect_to checkout_state_path(@order.state) if @order.can_go_to_state?(params[:state]) && !skip_state_validation?
@order.state = params[:state]
end
end
def ensure_checkout_allowed
unless @order.checkout_allowed?
redirect_to spree.cart_path
end
end
def ensure_order_not_completed
redirect_to spree.cart_path if @order.completed?
end
def ensure_sufficient_stock_lines
if @order.insufficient_stock_lines.present?
out_of_stock_items = @order.insufficient_stock_lines.collect(&:name).to_sentence
flash[:error] = Spree.t(:inventory_error_flash_for_insufficient_quantity, names: out_of_stock_items)
redirect_to spree.cart_path
end
end
# Provides a route to redirect after order completion
def completion_route
spree.order_path(@order)
end
def setup_for_current_state
method_name = :"before_#{@order.state}"
send(method_name) if respond_to?(method_name, true)
end
def before_address
# if the user has a default address, a callback takes care of setting
# that; but if he doesn't, we need to build an empty one here
default = {country_id: Spree::Country.default.id}
@order.build_bill_address(default) unless @order.bill_address
@order.build_ship_address(default) if @order.checkout_steps.include?('delivery') && [email protected]_address
end
def before_delivery
return if params[:order].present?
packages = @order.shipments.map(&:to_package)
@differentiator = Spree::Stock::Differentiator.new(@order, packages)
end
def before_payment
if @order.checkout_steps.include? "delivery"
packages = @order.shipments.map(&:to_package)
@differentiator = Spree::Stock::Differentiator.new(@order, packages)
@differentiator.missing.each do |variant, quantity|
@order.contents.remove(variant, quantity)
end
end
if try_spree_current_user && try_spree_current_user.respond_to?(:wallet)
@wallet_payment_sources = try_spree_current_user.wallet.wallet_payment_sources
@default_wallet_payment_source = @wallet_payment_sources.detect(&:default) ||
@wallet_payment_sources.first
# TODO: How can we deprecate this instance variable? We could try
# wrapping it in a delegating object that produces deprecation warnings.
@payment_sources = try_spree_current_user.wallet.wallet_payment_sources.map(&:payment_source).select { |ps| ps.is_a?(Spree::CreditCard) }
end
end
def rescue_from_spree_gateway_error(exception)
flash.now[:error] = Spree.t(:spree_gateway_error_flash_for_checkout)
@order.errors.add(:base, exception.message)
render :edit
end
def check_authorization
authorize!(:edit, current_order, cookies.signed[:guest_token])
end
end
end