- 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
299 lines
9.5 KiB
Ruby
Executable File
299 lines
9.5 KiB
Ruby
Executable File
# Events controller
|
|
#
|
|
# This controller manages all events. It load events for homepage
|
|
# and display for pagination.
|
|
class EventsController < ApplicationController
|
|
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
|
|
@events = Event.includes(:user).upcoming.page(params[:page]).per(12)
|
|
end
|
|
|
|
# Display desired event
|
|
#
|
|
# Find requested event and display it to the user
|
|
def show
|
|
# Event is set by set_event callback
|
|
end
|
|
|
|
# Handle checkout process - Collect names if needed or create Stripe session
|
|
def checkout
|
|
# 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"
|
|
return
|
|
end
|
|
|
|
# Check if any ticket types require names
|
|
requires_names = false
|
|
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
|
|
requires_names = true
|
|
break
|
|
end
|
|
end
|
|
|
|
# If names are required, redirect to name collection
|
|
if requires_names
|
|
session[:pending_cart] = cart_data
|
|
redirect_to event_collect_names_path(@event.slug, @event)
|
|
return
|
|
end
|
|
|
|
# Otherwise proceed directly to payment
|
|
process_payment(cart_data)
|
|
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
|
|
|
|
# Store names in session for later use
|
|
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
|
|
|
|
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
|
|
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
|
|
|
|
# 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)
|
|
|
|
if session.payment_status == "paid"
|
|
# Create tickets
|
|
@event = Event.find(event_id)
|
|
order_items = JSON.parse(session.metadata["order_items"])
|
|
@tickets = []
|
|
|
|
# Get names from session if they exist
|
|
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"])
|
|
item["quantity"].times do |i|
|
|
# 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] || {}
|
|
first_name = names["first_name"]
|
|
last_name = names["last_name"]
|
|
end
|
|
|
|
ticket = Ticket.create!(
|
|
user: current_user,
|
|
ticket_type: ticket_type,
|
|
status: "active",
|
|
first_name: first_name,
|
|
last_name: last_name
|
|
)
|
|
@tickets << ticket
|
|
|
|
# Send confirmation email for each ticket
|
|
TicketMailer.purchase_confirmation(ticket).deliver_now
|
|
end
|
|
end
|
|
|
|
# Clear session data
|
|
session.delete(:pending_cart)
|
|
session.delete(:ticket_names)
|
|
|
|
render "payment_success"
|
|
else
|
|
redirect_to event_path(@event.slug, @event), alert: "Le paiement n'a pas été complété avec succès"
|
|
end
|
|
rescue Stripe::StripeError => e
|
|
redirect_to dashboard_path, alert: "Erreur lors du traitement de votre confirmation de paiement : #{e.message}"
|
|
rescue => e
|
|
redirect_to dashboard_path, alert: "Une erreur inattendue s'est produite : #{e.message}"
|
|
end
|
|
end
|
|
|
|
# Download ticket PDF
|
|
def download_ticket
|
|
@ticket = current_user.tickets.find(params[:ticket_id])
|
|
|
|
respond_to do |format|
|
|
format.pdf do
|
|
pdf = @ticket.to_pdf
|
|
send_data pdf,
|
|
filename: "ticket-#{@ticket.event.name.parameterize}-#{@ticket.qr_code[0..7]}.pdf",
|
|
type: "application/pdf",
|
|
disposition: "attachment"
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_event
|
|
@event = Event.includes(:ticket_types).find(params[:id])
|
|
end
|
|
|
|
# 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 = []
|
|
total_amount = 0
|
|
|
|
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
|
|
|
|
# Check availability
|
|
available = ticket_type.quantity - ticket_type.tickets.count
|
|
if quantity > available
|
|
redirect_to event_path(@event.slug, @event), alert: "Pas assez de billets disponibles pour #{ticket_type.name}"
|
|
return
|
|
end
|
|
|
|
# Create Stripe line item
|
|
line_items << {
|
|
price_data: {
|
|
currency: "eur",
|
|
product_data: {
|
|
name: "#{@event.name} - #{ticket_type.name}",
|
|
description: ticket_type.description
|
|
},
|
|
unit_amount: ticket_type.price_cents
|
|
},
|
|
quantity: quantity
|
|
}
|
|
|
|
# Store for ticket creation
|
|
order_items << {
|
|
ticket_type_id: ticket_type.id,
|
|
ticket_type_name: ticket_type.name,
|
|
quantity: quantity,
|
|
price_cents: ticket_type.price_cents
|
|
}
|
|
|
|
total_amount += ticket_type.price_cents * quantity
|
|
end
|
|
|
|
if order_items.empty?
|
|
redirect_to event_path(@event.slug, @event), alert: "Commande invalide"
|
|
return
|
|
end
|
|
|
|
# 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
|
|
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
|
|
|
|
# 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" ],
|
|
line_items: line_items,
|
|
mode: "payment",
|
|
success_url: payment_success_url(event_id: @event.id, session_id: "{CHECKOUT_SESSION_ID}"),
|
|
cancel_url: event_url(@event.slug, @event),
|
|
customer_email: current_user.email,
|
|
metadata: {
|
|
event_id: @event.id,
|
|
user_id: current_user.id,
|
|
order_items: order_items.to_json,
|
|
ticket_names: ticket_names.to_json
|
|
}
|
|
})
|
|
|
|
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
|
|
end
|