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
|
||||
|
||||
@@ -8,12 +8,16 @@ import LogoutController from "./logout_controller"
|
||||
import FlashMessageController from "./flash_message_controller"
|
||||
import CounterController from "./counter_controller"
|
||||
import FeaturedEventController from "./featured_event_controller"
|
||||
import TicketCartController from "./ticket_cart_controller"
|
||||
|
||||
import ShadcnTestController from "./shadcn_test_controller"
|
||||
|
||||
application.register("logout", LogoutController) // Allow logout using js
|
||||
application.register("flash-message", FlashMessageController) // Dismiss notification after 5 secondes
|
||||
application.register("counter", CounterController) // Simple counter for homepage
|
||||
application.register("featured-event", FeaturedEventController) // Featured event controller for homepage
|
||||
application.register("ticket-cart-controller", TicketCartController) // Handle ticket checkout
|
||||
|
||||
application.register("shadcn-test", ShadcnTestController) // Test controller for Shadcn
|
||||
|
||||
|
||||
// import ShadcnTestController from "./shadcn_test_controller"
|
||||
// application.register("shadcn-test", ShadcnTestController) // Test controller for Shadcn
|
||||
|
||||
@@ -10,6 +10,8 @@ class Ticket < ApplicationRecord
|
||||
validates :ticket_type_id, presence: true
|
||||
validates :price_cents, presence: true, numericality: { greater_than: 0 }
|
||||
validates :status, presence: true, inclusion: { in: %w[active used expired refunded] }
|
||||
validates :first_name, presence: true, if: :requires_names?
|
||||
validates :last_name, presence: true, if: :requires_names?
|
||||
|
||||
before_validation :set_price_from_ticket_type, on: :create
|
||||
before_validation :generate_qr_code, on: :create
|
||||
@@ -24,6 +26,11 @@ class Ticket < ApplicationRecord
|
||||
price_cents / 100.0
|
||||
end
|
||||
|
||||
# Check if names are required for this ticket type
|
||||
def requires_names?
|
||||
ticket_type&.requires_id
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_price_from_ticket_type
|
||||
|
||||
78
app/views/events/collect_names.html.erb
Executable file
78
app/views/events/collect_names.html.erb
Executable file
@@ -0,0 +1,78 @@
|
||||
<div class="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100">
|
||||
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
||||
<!-- Breadcrumb -->
|
||||
<nav class="mb-6" aria-label="Breadcrumb">
|
||||
<ol class="flex items-center space-x-2 text-sm">
|
||||
<%= link_to root_path, class: "text-gray-500 hover:text-purple-600 transition-colors" do %>
|
||||
<svg class="w-4 h-4 inline-block mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" />
|
||||
</svg>
|
||||
Accueil
|
||||
<% end %>
|
||||
<svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
|
||||
</svg>
|
||||
<%= link_to events_path, class: "text-gray-500 hover:text-purple-600 transition-colors" do %>
|
||||
Événements
|
||||
<% end %>
|
||||
<svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
|
||||
</svg>
|
||||
<%= link_to event_path(@event.slug, @event), class: "text-gray-500 hover:text-purple-600 transition-colors" do %>
|
||||
<%= @event.name %>
|
||||
<% end %>
|
||||
<svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
|
||||
</svg>
|
||||
<li class="font-medium text-gray-900" aria-current="page">
|
||||
Informations des participants
|
||||
</li>
|
||||
</ol>
|
||||
</nav>
|
||||
|
||||
<div class="bg-white rounded-2xl shadow-xl overflow-hidden">
|
||||
<div class="p-6 md:p-8">
|
||||
<div class="text-center mb-8">
|
||||
<h1 class="text-3xl font-bold text-gray-900 mb-2">Informations des participants</h1>
|
||||
<p class="text-gray-600">Veuillez fournir les prénoms et noms des personnes qui utiliseront les billets.</p>
|
||||
</div>
|
||||
|
||||
<%= form_with url: event_process_names_path(@event.slug, @event), method: :post, local: true, class: "space-y-6" do |form| %>
|
||||
<% if @tickets_needing_names.any? %>
|
||||
<div class="space-y-6">
|
||||
<h2 class="text-xl font-semibold text-gray-900">Billets nécessitant une identification</h2>
|
||||
<p class="text-gray-600 mb-4">Les billets suivants nécessitent que vous indiquiez le prénom et le nom de chaque participant.</p>
|
||||
|
||||
<% @tickets_needing_names.each_with_index do |ticket, index| %>
|
||||
<div class="bg-gray-50 rounded-xl p-6">
|
||||
<h3 class="text-lg font-medium text-gray-900 mb-4"><%= ticket[:ticket_type_name] %> #<%= index + 1 %></h3>
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<%= form.label "ticket_names[#{ticket[:ticket_type_id]}_#{ticket[:index]}][first_name]", "Prénom", class: "block text-sm font-medium text-gray-700 mb-1" %>
|
||||
<%= form.text_field "ticket_names[#{ticket[:ticket_type_id]}_#{ticket[:index]}][first_name]",
|
||||
required: true,
|
||||
class: "w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500" %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.label "ticket_names[#{ticket[:ticket_type_id]}_#{ticket[:index]}][last_name]", "Nom", class: "block text-sm font-medium text-gray-700 mb-1" %>
|
||||
<%= form.text_field "ticket_names[#{ticket[:ticket_type_id]}_#{ticket[:index]}][last_name]",
|
||||
required: true,
|
||||
class: "w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500" %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="flex flex-col sm:flex-row gap-4 pt-6">
|
||||
<%= link_to "Retour", event_path(@event.slug, @event), class: "px-6 py-3 border border-gray-300 text-gray-700 rounded-xl hover:bg-gray-50 text-center font-medium transition-colors" %>
|
||||
<%= form.submit "Procéder au paiement", class: "flex-1 bg-gradient-to-r from-purple-600 to-indigo-600 hover:from-purple-700 hover:to-indigo-700 text-white font-medium py-3 px-6 rounded-xl shadow-sm transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-purple-500 focus:ring-offset-2" %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user