Fix stripe controller

This commit is contained in:
Gaetan Craig-Riou
2025-02-04 13:38:26 +11:00
parent 851cccb823
commit 90eecc783d
2 changed files with 19 additions and 6 deletions

View File

@@ -8,9 +8,12 @@ module PaymentGateways
before_action :load_checkout_order, only: :confirm
before_action :validate_payment_intent, only: :confirm
before_action :check_order_cycle_expiry, only: :confirm
before_action :validate_stock, only: :confirm
def confirm
validate_stock
return redirect_to order_failed_route if @any_out_of_stock == true
process_payment_completion!
end

View File

@@ -151,13 +151,17 @@ module PaymentGateways
end
end
context "items running out of stock during order completion" do
context "when items run out of stock during order completion" do
before do
mock_order_check_stock_service(order)
end
it "redirects to cart when some items are out of stock" do
allow(controller).to receive(:valid_payment_intent?).and_return true
allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return false
get :confirm, params: { payment_intent: "pi_123" }
expect(response).to redirect_to cart_path
expect(response).to redirect_to checkout_step_path(step: :details)
end
context "handling pending payments" do
@@ -167,7 +171,6 @@ module PaymentGateways
}
before do
allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?) { false }
order.save
allow(order).to receive_message_chain(:payments, :completed) { [] }
allow(order).to receive_message_chain(:payments, :incomplete) { [payment] }
@@ -181,7 +184,7 @@ module PaymentGateways
get :confirm, params: { payment_intent: "pi_123" }
expect(response).to redirect_to cart_path
expect(response).to redirect_to checkout_step_path(step: :details)
expect(flash[:notice]).to eq(
"Payment cancelled: the checkout could not be completed due to stock issues."
)
@@ -334,5 +337,12 @@ module PaymentGateways
end
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).twice
expect(check_stock_service_mock).to receive(:update_line_items).and_return(order.variants)
end
end
end