develop #3

Merged
kbe merged 227 commits from develop into main 2025-09-16 14:35:23 +00:00
Showing only changes of commit bd6c0d5ed8 - Show all commits

View File

@@ -3,10 +3,9 @@
# This controller manages all events. It load events for homepage # This controller manages all events. It load events for homepage
# and display for pagination. # and display for pagination.
class EventsController < ApplicationController class EventsController < ApplicationController
include StripeConcern
before_action :authenticate_user!, only: [ :checkout, :process_names, :download_ticket ] before_action :authenticate_user!, only: [ ]
before_action :set_event, only: [ :show, :checkout, :process_names ] before_action :set_event, only: [ :show ]
# Display all events # Display all events
def index def index
@@ -20,199 +19,13 @@ class EventsController < ApplicationController
# Event is set by set_event callback # Event is set by set_event callback
end 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
# 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 private
# Set the current event in the controller
#
# Expose the current @event property to method
def set_event def set_event
@event = Event.includes(:ticket_types).find(params[:id]) @event = Event.includes(:ticket_types).find(params[:id])
end 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(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
error_message = e.message.present? ? e.message : "Erreur Stripe inconnue"
Rails.logger.error "Stripe error: #{error_message}"
redirect_to event_path(@event.slug, @event), alert: "Erreur de traitement du paiement : #{error_message}"
end
end
end end