feat: Implement complete ticket purchasing flow with new TicketsController
- Create new TicketsController with actions for name collection, creation, and checkout - Add dedicated ticket views (new.html.erb, checkout.html.erb, show.html.erb) - Update ticket_selection_controller.js to handle form submission via AJAX - Add store_cart endpoint in EventsController for session-based cart management - Update routes to support new ticket flow: /tickets/new, /create, /checkout - Fix attribute name consistency across views (title→name, starts_at→start_time) - Add Stripe checkout integration with proper error handling - Remove deprecated collect_names flow in favor of streamlined approach The flow is now: Event selection → AJAX cart storage → Name collection → Checkout → Payment
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
# Events controller
|
||||
#
|
||||
# This controller manages all events. It load events for homepage
|
||||
# and display for pagination.
|
||||
class EventsController < ApplicationController
|
||||
before_action :authenticate_user!, only: [ :checkout, :collect_names, :process_names, :payment_success, :download_ticket ]
|
||||
before_action :set_event, only: [ :show, :checkout, :collect_names, :process_names ]
|
||||
include StripeConcern
|
||||
|
||||
before_action :authenticate_user!, only: [ :checkout, :process_names, :payment_success, :download_ticket ]
|
||||
before_action :set_event, only: [ :show, :checkout, :process_names, :store_cart ]
|
||||
|
||||
# Display all events
|
||||
def index
|
||||
@@ -8,6 +14,8 @@ class EventsController < ApplicationController
|
||||
end
|
||||
|
||||
# Display desired event
|
||||
#
|
||||
# Find requested event and display it to the user
|
||||
def show
|
||||
# Event is set by set_event callback
|
||||
end
|
||||
@@ -17,12 +25,12 @@ class EventsController < ApplicationController
|
||||
# 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
|
||||
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"
|
||||
@@ -55,41 +63,15 @@ class EventsController < ApplicationController
|
||||
process_payment(cart_data)
|
||||
end
|
||||
|
||||
# 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
|
||||
end
|
||||
|
||||
# Build list of tickets requiring names
|
||||
@tickets_needing_names = []
|
||||
@cart_data.each do |ticket_type_id, item|
|
||||
ticket_type = @event.ticket_types.find_by(id: ticket_type_id)
|
||||
next unless ticket_type
|
||||
|
||||
quantity = item["quantity"].to_i
|
||||
next if quantity <= 0
|
||||
|
||||
if ticket_type.requires_id
|
||||
quantity.times do |i|
|
||||
@tickets_needing_names << {
|
||||
ticket_type_id: ticket_type.id,
|
||||
ticket_type_name: ticket_type.name,
|
||||
index: i
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Process submitted names and create Stripe session
|
||||
def process_names
|
||||
Rails.logger.debug "Processing names for event: #{@event.id}"
|
||||
|
||||
cart_data = session[:pending_cart] || {}
|
||||
|
||||
if cart_data.empty?
|
||||
Rails.logger.debug "Cart data is empty"
|
||||
redirect_to event_path(@event.slug, @event), alert: "Veuillez sélectionner au moins un billet"
|
||||
return
|
||||
end
|
||||
@@ -104,26 +86,38 @@ class EventsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
Rails.logger.debug "Proceeding to payment with cart data: #{cart_data}"
|
||||
# Proceed to payment
|
||||
process_payment(cart_data)
|
||||
end
|
||||
|
||||
# Store cart data in session (AJAX endpoint)
|
||||
def store_cart
|
||||
cart_data = params[:cart] || {}
|
||||
session[:pending_cart] = cart_data
|
||||
|
||||
render json: { status: "success", message: "Cart stored successfully" }
|
||||
rescue => e
|
||||
Rails.logger.error "Error storing cart: #{e.message}"
|
||||
render json: { status: "error", message: "Failed to store cart" }, status: 500
|
||||
end
|
||||
|
||||
# Handle successful payment
|
||||
def payment_success
|
||||
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."
|
||||
stripe_configured = Rails.application.config.stripe[:secret_key].present?
|
||||
Rails.logger.debug "Payment success - Stripe configured: #{stripe_configured}"
|
||||
|
||||
unless stripe_configured
|
||||
redirect_to dashboard_path, 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
|
||||
# Stripe is now initialized at application startup, no need to initialize here
|
||||
Rails.logger.debug "Payment success - Using globally initialized Stripe"
|
||||
|
||||
begin
|
||||
session = Stripe::Checkout::Session.retrieve(session_id)
|
||||
@@ -203,6 +197,9 @@ class EventsController < ApplicationController
|
||||
|
||||
# Process payment and create Stripe session
|
||||
def process_payment(cart_data)
|
||||
Rails.logger.debug "Starting process_payment method"
|
||||
Rails.logger.debug "Cart data: #{cart_data}"
|
||||
|
||||
# Create order items from cart
|
||||
line_items = []
|
||||
order_items = []
|
||||
@@ -254,19 +251,27 @@ class EventsController < ApplicationController
|
||||
# Get ticket names from session if they exist
|
||||
ticket_names = session[:ticket_names] || {}
|
||||
|
||||
# Debug: Log Stripe configuration status
|
||||
Rails.logger.debug "Stripe configuration check:"
|
||||
Rails.logger.debug " Config: #{Rails.application.config.stripe}"
|
||||
Rails.logger.debug " Secret key present: #{Rails.application.config.stripe[:secret_key].present?}"
|
||||
Rails.logger.debug " stripe_configured? method exists: #{respond_to?(:stripe_configured?)}"
|
||||
|
||||
# Check if Stripe is properly configured
|
||||
unless stripe_configured?
|
||||
stripe_configured = Rails.application.config.stripe[:secret_key].present?
|
||||
Rails.logger.debug " Direct stripe_configured check: #{stripe_configured}"
|
||||
|
||||
unless stripe_configured
|
||||
Rails.logger.error "Stripe not configured properly - redirecting to event page"
|
||||
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
|
||||
# Stripe is now initialized at application startup, no need to initialize here
|
||||
Rails.logger.debug " Using globally initialized Stripe"
|
||||
|
||||
begin
|
||||
Rails.logger.debug "Creating Stripe Checkout Session"
|
||||
# Create Stripe Checkout Session
|
||||
session = Stripe::Checkout::Session.create({
|
||||
payment_method_types: [ "card" ],
|
||||
@@ -283,8 +288,10 @@ class EventsController < ApplicationController
|
||||
}
|
||||
})
|
||||
|
||||
Rails.logger.debug "Redirecting to Stripe session URL: #{session.url}"
|
||||
redirect_to session.url, allow_other_host: true
|
||||
rescue Stripe::StripeError => e
|
||||
Rails.logger.error "Stripe error: #{e.message}"
|
||||
redirect_to event_path(@event.slug, @event), alert: "Erreur de traitement du paiement : #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user