diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 636a391..dc890fe 100755 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -14,7 +14,15 @@ class EventsController < ApplicationController # Handle checkout process - Collect names if needed or create Stripe session def checkout - cart_data = JSON.parse(params[:cart] || "{}") + # Convert cart parameter to proper hash + cart_param = params[:cart] + cart_data = if cart_param.is_a?(String) + JSON.parse(cart_param) + elsif cart_param.is_a?(ActionController::Parameters) + cart_param.to_unsafe_h + else + {} + end if cart_data.empty? redirect_to event_path(@event.slug, @event), alert: "Veuillez sélectionner au moins un billet" @@ -50,7 +58,7 @@ class EventsController < ApplicationController # Display form to collect names for tickets def collect_names @cart_data = session[:pending_cart] || {} - + if @cart_data.empty? redirect_to event_path(@event.slug, @event), alert: "Veuillez sélectionner au moins un billet" return @@ -80,14 +88,21 @@ class EventsController < ApplicationController # Process submitted names and create Stripe session def process_names cart_data = session[:pending_cart] || {} - + if cart_data.empty? redirect_to event_path(@event.slug, @event), alert: "Veuillez sélectionner au moins un billet" return end # Store names in session for later use - session[:ticket_names] = params[:ticket_names] if params[:ticket_names] + if params[:ticket_names].present? + # Convert ActionController::Parameters to hash + if params[:ticket_names].is_a?(ActionController::Parameters) + session[:ticket_names] = params[:ticket_names].to_unsafe_h + else + session[:ticket_names] = params[:ticket_names] + end + end # Proceed to payment process_payment(cart_data) @@ -98,6 +113,18 @@ class EventsController < ApplicationController session_id = params[:session_id] event_id = params[:event_id] + # Check if Stripe is properly configured + unless stripe_configured? + redirect_to event_path(@event.slug, @event), alert: "Le système de paiement n'est pas correctement configuré. Veuillez contacter l'administrateur." + return + end + + # Initialize Stripe during checkout + unless initialize_stripe + redirect_to event_path(@event.slug, @event), alert: "Impossible d'initialiser le système de paiement. Veuillez réessayer plus tard." + return + end + begin session = Stripe::Checkout::Session.retrieve(session_id) @@ -106,9 +133,9 @@ class EventsController < ApplicationController @event = Event.find(event_id) order_items = JSON.parse(session.metadata["order_items"]) @tickets = [] - + # Get names from session if they exist - ticket_names = session[:ticket_names] || {} + ticket_names = session.metadata["ticket_names"] ? JSON.parse(session.metadata["ticket_names"]) : {} order_items.each do |item| ticket_type = TicketType.find(item["ticket_type_id"]) @@ -116,7 +143,7 @@ class EventsController < ApplicationController # Get names if this ticket type requires them first_name = nil last_name = nil - + if ticket_type.requires_id name_key = "#{ticket_type.id}_#{i}" names = ticket_names[name_key] || {} @@ -171,7 +198,7 @@ class EventsController < ApplicationController private def set_event - @event = Event.find(params[:id]) + @event = Event.includes(:ticket_types).find(params[:id]) end # Process payment and create Stripe session @@ -224,6 +251,21 @@ class EventsController < ApplicationController return end + # Get ticket names from session if they exist + ticket_names = session[:ticket_names] || {} + + # Check if Stripe is properly configured + unless stripe_configured? + redirect_to event_path(@event.slug, @event), alert: "Le système de paiement n'est pas correctement configuré. Veuillez contacter l'administrateur." + return + end + + # Initialize Stripe during checkout + unless initialize_stripe + redirect_to event_path(@event.slug, @event), alert: "Impossible d'initialiser le système de paiement. Veuillez réessayer plus tard." + return + end + begin # Create Stripe Checkout Session session = Stripe::Checkout::Session.create({ @@ -236,7 +278,8 @@ class EventsController < ApplicationController metadata: { event_id: @event.id, user_id: current_user.id, - order_items: order_items.to_json + order_items: order_items.to_json, + ticket_names: ticket_names.to_json } }) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index debee41..d946b5c 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -6,4 +6,7 @@ module ApplicationHelper # Include flash message helpers include FlashMessagesHelper + + # Include Stripe helper + include StripeHelper end diff --git a/app/helpers/stripe_helper.rb b/app/helpers/stripe_helper.rb new file mode 100644 index 0000000..d6bf2e2 --- /dev/null +++ b/app/helpers/stripe_helper.rb @@ -0,0 +1,32 @@ +module StripeHelper + # Check if Stripe is properly configured + def stripe_configured? + Rails.application.config.stripe[:secret_key].present? + end + + # Initialize Stripe with the configured API key + def initialize_stripe + return false unless stripe_configured? + + Stripe.api_key = Rails.application.config.stripe[:secret_key] + true + rescue => e + Rails.logger.error "Failed to initialize Stripe: #{e.message}" + false + end + + # Safely call Stripe methods with error handling + def safe_stripe_call(&block) + return nil unless stripe_configured? + + # Initialize Stripe if not already done + initialize_stripe unless Stripe.api_key.present? + + begin + yield if block_given? + rescue Stripe::StripeError => e + Rails.logger.error "Stripe Error: #{e.message}" + nil + end + end +end \ No newline at end of file diff --git a/app/views/events/collect_names.html.erb b/app/views/events/collect_names.html.erb index f360e5c..f191542 100755 --- a/app/views/events/collect_names.html.erb +++ b/app/views/events/collect_names.html.erb @@ -1,7 +1,7 @@ -
-
+
+
-