Proceeding to work on controller for checkout
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
class EventsController < ApplicationController
|
||||
before_action :authenticate_user!, only: [ :checkout, :payment_success, :download_ticket ]
|
||||
before_action :set_event, only: [ :show, :checkout ]
|
||||
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 ]
|
||||
|
||||
# Display all events
|
||||
def index
|
||||
@@ -12,7 +12,7 @@ class EventsController < ApplicationController
|
||||
# Event is set by set_event callback
|
||||
end
|
||||
|
||||
# Handle checkout process - Create Stripe session
|
||||
# Handle checkout process - Collect names if needed or create Stripe session
|
||||
def checkout
|
||||
cart_data = JSON.parse(params[:cart] || "{}")
|
||||
|
||||
@@ -21,6 +21,161 @@ class EventsController < ApplicationController
|
||||
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
|
||||
|
||||
# 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
|
||||
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]
|
||||
|
||||
# Proceed to payment
|
||||
process_payment(cart_data)
|
||||
end
|
||||
|
||||
# Handle successful payment
|
||||
def payment_success
|
||||
session_id = params[:session_id]
|
||||
event_id = params[:event_id]
|
||||
|
||||
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[: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.find(params[:id])
|
||||
end
|
||||
|
||||
# Process payment and create Stripe session
|
||||
def process_payment(cart_data)
|
||||
# Create order items from cart
|
||||
line_items = []
|
||||
order_items = []
|
||||
@@ -90,65 +245,4 @@ class EventsController < ApplicationController
|
||||
redirect_to event_path(@event.slug, @event), alert: "Erreur de traitement du paiement : #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
# Handle successful payment
|
||||
def payment_success
|
||||
session_id = params[:session_id]
|
||||
event_id = params[:event_id]
|
||||
|
||||
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 = []
|
||||
|
||||
order_items.each do |item|
|
||||
ticket_type = TicketType.find(item["ticket_type_id"])
|
||||
item["quantity"].times do
|
||||
ticket = Ticket.create!(
|
||||
user: current_user,
|
||||
ticket_type: ticket_type,
|
||||
status: "active"
|
||||
)
|
||||
@tickets << ticket
|
||||
|
||||
# Send confirmation email for each ticket
|
||||
TicketMailer.purchase_confirmation(ticket).deliver_now
|
||||
end
|
||||
end
|
||||
|
||||
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.find(params[:id])
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user