Patching up our checkout flow so we don't try to create shipments before we have valid shipping addresses

This commit is contained in:
Will Marshall
2014-02-28 13:41:02 +11:00
parent 86a8b926f3
commit 1d5addb06e
4 changed files with 55 additions and 30 deletions

View File

@@ -11,31 +11,27 @@ class Shop::CheckoutController < Spree::CheckoutController
end
def update
begin
if @order.update_attributes(params[:order])
fire_event('spree.checkout.update')
if @order.update_attributes(params[:order])
fire_event('spree.checkout.update')
while @order.state != "complete"
if @order.next
state_callback(:after)
else
flash[:error] = t(:payment_processing_failed)
respond_with @order, location: main_app.shop_checkout_path
return
end
end
if @order.state == "complete" || @order.completed?
flash.notice = t(:order_processed_successfully)
flash[:commerce_tracking] = "nothing special"
respond_with(@order, :location => order_path(@order))
while @order.state != "complete"
if @order.next
state_callback(:after)
else
flash[:error] = t(:payment_processing_failed)
respond_with @order, location: main_app.shop_checkout_path
return
end
end
if @order.state == "complete" || @order.completed?
flash.notice = t(:order_processed_successfully)
flash[:commerce_tracking] = "nothing special"
respond_with(@order, :location => order_path(@order))
else
respond_with @order, location: main_app.shop_checkout_path
end
rescue ActiveRecord::RecordInvalid
else
respond_with @order, location: main_app.shop_checkout_path
end
end

View File

@@ -14,6 +14,26 @@ Spree::Order.class_eval do
before_validation :shipping_address_from_distributor
checkout_flow do
go_to_state :address
go_to_state :delivery
go_to_state :payment, :if => lambda { |order|
if order.ship_address.andand.valid?
# Fix for #2191
if order.shipping_method
order.create_shipment!
order.update_totals
end
order.payment_required?
else
false
end
}
go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
go_to_state :complete, :if => lambda { |order| (order.payment_required? && order.has_unprocessed_payments?) || !order.payment_required? }
remove_transition :from => :delivery, :to => :confirm
end
# -- Scopes
scope :managed_by, lambda { |user|

View File

@@ -160,7 +160,7 @@ feature "As a consumer I want to check out my cart", js: true do
end
describe "Purchasing" do
pending "re-renders with errors when we submit the incomplete form" do
it "re-renders with errors when we submit the incomplete form" do
choose sm2.name
click_button "Purchase"
current_path.should == "/shop/checkout"
@@ -170,17 +170,20 @@ feature "As a consumer I want to check out my cart", js: true do
it "renders errors on the shipping method where appropriate"
it "takes us to the order confirmation page when we submit a complete form" do
fill_in "Customer E-Mail", with: "test@test.com"
fill_in "Phone", with: "0468363090"
fill_in "First Name", with: "Will"
fill_in "Last Name", with: "Marshall"
fill_in "Billing Address", with: "123 Your Face"
select "Australia", from: "Country"
select "Victoria", from: "State"
fill_in "City", with: "Melbourne"
fill_in "Zip Code", with: "3066"
choose sm1.name
choose sm2.name
choose pm1.name
within "#details" do
fill_in "First Name", with: "Will"
fill_in "Last Name", with: "Marshall"
save_and_open_page
fill_in "Billing Address", with: "123 Your Face"
select "Australia", from: "Country"
select "Victoria", from: "State"
fill_in "Customer E-Mail", with: "test@test.com"
fill_in "Phone", with: "0468363090"
fill_in "City", with: "Melbourne"
fill_in "Zip Code", with: "3066"
end
click_button "Purchase"
page.should have_content "Your order has been processed successfully"
end

View File

@@ -274,14 +274,20 @@ describe Spree::Order do
order.shipping_method = create(:shipping_method, require_ship_address: false)
order.ship_address.update_attribute :firstname, "will"
order.save
order.ship_address.firstname.should == distributor.address.firstname
order.ship_address.firstname.shoul == distributor.address.firstname
end
it "does not populate the shipping address if the shipping method requires a delivery address" do
order.shipping_method = create(:shipping_method, require_ship_address: true)
order.ship_address.update_attribute :firstname, "will"
order.save
order.ship_address.firstname.should == "will"
end
it "doesn't attempt to create a shipment if the order is not yet valid" do
#Shipment.should_not_r
order.create_shipment!
end
end
end