Make checkout controller add flash error if order contains any type of error

Here we add translations for a particular case where the credit card expiry date is in the past
This commit is contained in:
Luis Ramos
2020-03-25 17:33:29 +00:00
parent 6a1c541479
commit b898ce1ae1
3 changed files with 12 additions and 7 deletions

View File

@@ -165,7 +165,7 @@ class CheckoutController < Spree::StoreController
checkout_succeeded
redirect_to(order_path(@order)) && return
else
flash[:error] = order_workflow_error
flash[:error] = order_error
checkout_failed
end
end
@@ -179,8 +179,6 @@ class CheckoutController < Spree::StoreController
@order.select_shipping_method(shipping_method_id) if @order.state == "delivery"
next if advance_order_state(@order)
flash[:error] = order_workflow_error
return update_failed
end
@@ -205,7 +203,7 @@ class CheckoutController < Spree::StoreController
false
end
def order_workflow_error
def order_error
if @order.errors.present?
@order.errors.full_messages.to_sentence
else
@@ -245,6 +243,7 @@ class CheckoutController < Spree::StoreController
end
def update_failed
flash[:error] = order_error if flash.empty?
checkout_failed
update_failed_response
end

View File

@@ -40,6 +40,8 @@ en:
shipping_category_id: "Shipping Category"
variant_unit: "Variant Unit"
variant_unit_name: "Variant Unit Name"
spree/credit_card:
base: "Credit Card"
order_cycle:
orders_close_at: Close date
errors:
@@ -50,6 +52,10 @@ en:
taken: "There's already an account for this email. Please login or reset your password."
spree/order:
no_card: There are no authorised credit cards available to charge
spree/credit_card:
attributes:
base:
card_expired: "has expired"
order_cycle:
attributes:
orders_close_at:

View File

@@ -197,13 +197,13 @@ describe CheckoutController, type: :controller do
allow(controller).to receive(:current_order).and_return(order)
end
it "returns errors" do
it "returns errors and flash if order.update_attributes fails" do
spree_post :update, format: :json, order: {}
expect(response.status).to eq(400)
expect(response.body).to eq({ errors: assigns[:order].errors, flash: {} }.to_json)
expect(response.body).to eq({ errors: assigns[:order].errors, flash: { error: order.errors.full_messages.to_sentence } }.to_json)
end
it "returns flash" do
it "returns errors and flash if order.next fails" do
allow(order).to receive(:update_attributes).and_return true
allow(order).to receive(:next).and_return false
spree_post :update, format: :json, order: {}