From 851cccb823281caec03d2aa78be7bddc003e27c2 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 4 Feb 2025 13:37:32 +1100 Subject: [PATCH] Fix paypal controller --- .../payment_gateways/paypal_controller.rb | 4 +++ .../paypal_controller_spec.rb | 28 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/controllers/payment_gateways/paypal_controller.rb b/app/controllers/payment_gateways/paypal_controller.rb index b3ba7b9957..778cb6e956 100644 --- a/app/controllers/payment_gateways/paypal_controller.rb +++ b/app/controllers/payment_gateways/paypal_controller.rb @@ -14,6 +14,8 @@ module PaymentGateways after_action :reset_order_when_complete, only: :confirm def express + return redirect_to order_failed_route if @any_out_of_stock == true + pp_request = provider.build_set_express_checkout( express_checkout_request_details(@order) ) @@ -41,6 +43,8 @@ module PaymentGateways end def confirm + return redirect_to order_failed_route if @any_out_of_stock == true + # At this point the user has come back from the Paypal form, and we get one # last chance to interact with the payment process before the money moves... diff --git a/spec/controllers/payment_gateways/paypal_controller_spec.rb b/spec/controllers/payment_gateways/paypal_controller_spec.rb index 4670a69bc1..cf22798fed 100644 --- a/spec/controllers/payment_gateways/paypal_controller_spec.rb +++ b/spec/controllers/payment_gateways/paypal_controller_spec.rb @@ -30,13 +30,12 @@ module PaymentGateways end context "if the stock ran out whilst the payment was being placed" do - before do - allow(controller.current_order).to receive(:insufficient_stock_lines).and_return(true) - end + it "redirects to the details page with out of stock error" do + mock_order_check_stock_service(controller.current_order) - it "redirects to the cart with out of stock error" do - expect(post(:confirm, params: { payment_method_id: payment_method.id })). - to redirect_to cart_path + post(:confirm, params: { payment_method_id: payment_method.id }) + + expect(response).to redirect_to checkout_step_path(step: :details) order = controller.current_order.reload @@ -104,6 +103,16 @@ module PaymentGateways expect(flash[:error]).to eq "Could not connect to PayPal." end end + + context "when the stock ran out whilst the payment was being placed" do + it "redirects to the details page with out of stock error" do + mock_order_check_stock_service(controller.current_order) + + post(:express) + + expect(response).to redirect_to checkout_step_path(step: :details) + end + end end describe '#expire_current_order' do @@ -117,5 +126,12 @@ module PaymentGateways expect(controller.instance_variable_get(:@current_order)).to be_nil end end + + def mock_order_check_stock_service(order) + check_stock_service_mock = instance_double(Orders::CheckStockService) + expect(Orders::CheckStockService).to receive(:new).and_return(check_stock_service_mock) + expect(check_stock_service_mock).to receive(:sufficient_stock?).and_return(false) + expect(check_stock_service_mock).to receive(:update_line_items).and_return(order.variants) + end end end