feat: Prepare to use Stripe a checkout component

This commit is contained in:
kbe
2025-08-30 14:57:33 +02:00
parent 055640b73e
commit 476438c5c4
9 changed files with 294 additions and 42 deletions

View File

@@ -14,7 +14,15 @@ class EventsController < ApplicationController
# Handle checkout process - Collect names if needed or create Stripe session
def checkout
cart_data = JSON.parse(params[:cart] || "{}")
# 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"
@@ -50,7 +58,7 @@ class EventsController < ApplicationController
# 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
@@ -80,14 +88,21 @@ class EventsController < ApplicationController
# 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]
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
# Proceed to payment
process_payment(cart_data)
@@ -98,6 +113,18 @@ class EventsController < ApplicationController
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."
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
begin
session = Stripe::Checkout::Session.retrieve(session_id)
@@ -106,9 +133,9 @@ class EventsController < ApplicationController
@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] || {}
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"])
@@ -116,7 +143,7 @@ class EventsController < ApplicationController
# 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] || {}
@@ -171,7 +198,7 @@ class EventsController < ApplicationController
private
def set_event
@event = Event.find(params[:id])
@event = Event.includes(:ticket_types).find(params[:id])
end
# Process payment and create Stripe session
@@ -224,6 +251,21 @@ class EventsController < ApplicationController
return
end
# Get ticket names from session if they exist
ticket_names = session[:ticket_names] || {}
# 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."
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
begin
# Create Stripe Checkout Session
session = Stripe::Checkout::Session.create({
@@ -236,7 +278,8 @@ class EventsController < ApplicationController
metadata: {
event_id: @event.id,
user_id: current_user.id,
order_items: order_items.to_json
order_items: order_items.to_json,
ticket_names: ticket_names.to_json
}
})