diff --git a/app/controllers/concerns/order_completion.rb b/app/controllers/concerns/order_completion.rb index 215f747045..5e3d2c198c 100644 --- a/app/controllers/concerns/order_completion.rb +++ b/app/controllers/concerns/order_completion.rb @@ -41,8 +41,8 @@ module OrderCompletion main_app.order_path(@order, order_token: @order.token) end - def order_failed_route - main_app.checkout_path + def order_failed_route(step: 'details') + main_app.checkout_step_path(step:) end def order_invalid_for_checkout? @@ -60,8 +60,8 @@ module OrderCompletion def process_payment_completion! unless @order.process_payments! - processing_failed - return redirect_to order_failed_route + payment_failed + return redirect_to order_failed_route(step: 'payment') end if OrderWorkflow.new(@order).next && @order.complete? @@ -82,12 +82,20 @@ module OrderCompletion order_completion_reset(@order) end - def processing_failed(error = RuntimeError.new(order_processing_error)) + def payment_failed + notify_failure + end + + def processing_failed + notify_failure + Checkout::PostCheckoutActions.new(@order).failure + end + + def notify_failure(error = RuntimeError.new(order_processing_error)) Bugsnag.notify(error) do |payload| payload.add_metadata :order, @order end flash[:error] = order_processing_error if flash.blank? - Checkout::PostCheckoutActions.new(@order).failure end def order_processing_error diff --git a/spec/controllers/payment_gateways/paypal_controller_spec.rb b/spec/controllers/payment_gateways/paypal_controller_spec.rb index 25152eb2b9..d348054fde 100644 --- a/spec/controllers/payment_gateways/paypal_controller_spec.rb +++ b/spec/controllers/payment_gateways/paypal_controller_spec.rb @@ -53,7 +53,7 @@ module PaymentGateways it "redirects to checkout state path" do expect(post(:confirm, params: { payment_method_id: payment_method.id })). - to redirect_to checkout_path + to redirect_to checkout_step_path(step: :payment) expect(flash[:error]).to eq( 'Payment could not be processed, please check the details you entered' diff --git a/spec/controllers/payment_gateways/stripe_controller_spec.rb b/spec/controllers/payment_gateways/stripe_controller_spec.rb index 3c611df908..9e9a993297 100644 --- a/spec/controllers/payment_gateways/stripe_controller_spec.rb +++ b/spec/controllers/payment_gateways/stripe_controller_spec.rb @@ -44,6 +44,7 @@ module PaymentGateways order.update_attribute :state, "payment" order.payments << payment + order.update_attribute :state, "confirmation" end it "creates a customer record" do @@ -72,20 +73,28 @@ Please try again!" end end - context "using split checkout" do - before do - order.update_attribute :state, "confirmation" - end + it "completes the order and redirects to the order confirmation page" do + expect(controller).to receive(:processing_succeeded).and_call_original + expect(controller).to receive(:order_completion_reset).and_call_original - it "completes the order and redirects to the order confirmation page" do - expect(controller).to receive(:processing_succeeded).and_call_original - expect(controller).to receive(:order_completion_reset).and_call_original + get :confirm, params: { payment_intent: "pi_123" } - get :confirm, params: { payment_intent: "pi_123" } + expect(order.completed?).to be true + expect(response).to redirect_to order_path(order, order_token: order.token) + expect(flash[:notice]).to eq 'Your order has been processed successfully' + end - expect(order.completed?).to be true - expect(response).to redirect_to order_path(order, order_token: order.token) - expect(flash[:notice]).to eq 'Your order has been processed successfully' + context 'when order completion fails' do + it "redirects to checkout state path" do + expect(controller).to receive(:process_payment_completion!).and_call_original + allow(order).to receive(:process_payments!).and_return(false) + expect( + get(:confirm, params: { payment_intent: "pi_123" }) + ).to redirect_to checkout_step_path(step: :payment) + + expect(flash[:error]).to eq( + 'Payment could not be processed, please check the details you entered' + ) end end end @@ -99,7 +108,7 @@ Please try again!" get :confirm, params: { payment_intent: "pi_123" } expect(order.completed?).to be false - expect(response).to redirect_to checkout_path + expect(response).to redirect_to checkout_step_path(step: :details) expect(flash[:error]).to eq "Payment could not be processed, \ please check the details you entered" end @@ -112,7 +121,7 @@ please check the details you entered" get :confirm, params: { payment_intent: "pi_666" } expect(order.completed?).to be false - expect(response).to redirect_to checkout_path + expect(response).to redirect_to checkout_step_path(step: :details) expect(flash[:error]).to eq "Payment could not be processed, \ please check the details you entered" end @@ -130,7 +139,7 @@ please check the details you entered" get :confirm, params: { payment_intent: "pi_123" } expect(order.completed?).to be false - expect(response).to redirect_to checkout_path + expect(response).to redirect_to checkout_step_path(step: :details) expect(flash[:error]).to eq "Payment could not be processed, \ please check the details you entered" end