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
}
})

View File

@@ -6,4 +6,7 @@ module ApplicationHelper
# Include flash message helpers
include FlashMessagesHelper
# Include Stripe helper
include StripeHelper
end

View File

@@ -0,0 +1,32 @@
module StripeHelper
# Check if Stripe is properly configured
def stripe_configured?
Rails.application.config.stripe[:secret_key].present?
end
# Initialize Stripe with the configured API key
def initialize_stripe
return false unless stripe_configured?
Stripe.api_key = Rails.application.config.stripe[:secret_key]
true
rescue => e
Rails.logger.error "Failed to initialize Stripe: #{e.message}"
false
end
# Safely call Stripe methods with error handling
def safe_stripe_call(&block)
return nil unless stripe_configured?
# Initialize Stripe if not already done
initialize_stripe unless Stripe.api_key.present?
begin
yield if block_given?
rescue Stripe::StripeError => e
Rails.logger.error "Stripe Error: #{e.message}"
nil
end
end
end

View File

@@ -1,7 +1,7 @@
<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">
<div class="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 py-8">
<div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
<!-- Breadcrumb -->
<nav class="mb-6" aria-label="Breadcrumb">
<nav class="mb-8" 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">
@@ -33,33 +33,54 @@
<div class="bg-white rounded-2xl shadow-xl overflow-hidden">
<div class="p-6 md:p-8">
<div class="text-center mb-8">
<div class="mx-auto bg-purple-100 rounded-full p-3 w-16 h-16 flex items-center justify-center mb-4">
<svg class="w-8 h-8 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
</div>
<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>
<p class="text-gray-600 max-w-md mx-auto">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| %>
<%= form_with url: event_process_names_path(@event.slug, @event), method: :post, local: true, class: "space-y-8" 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>
<div class="flex items-center justify-center mb-2">
<div class="bg-purple-600 rounded-full p-2 mr-3">
<svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
</svg>
</div>
<h2 class="text-xl font-semibold text-gray-900">Billets nécessitant une identification</h2>
</div>
<p class="text-gray-600 mb-6 text-center">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="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-xl p-6 border border-purple-100">
<div class="flex items-center mb-4">
<div class="bg-purple-500 rounded-lg p-2 mr-3">
<svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
</div>
<h3 class="text-lg font-semibold text-gray-900"><%= ticket[:ticket_type_name] %> #<%= index + 1 %></h3>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-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" %>
class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500 transition-all duration-200 shadow-sm",
placeholder: "Entrez le prénom" %>
</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" %>
class: "w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-purple-500 transition-all duration-200 shadow-sm",
placeholder: "Entrez le nom" %>
</div>
</div>
</div>
@@ -68,8 +89,8 @@
<% 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" %>
<%= 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 duration-200" %>
<%= 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 transform hover:-translate-y-0.5" %>
</div>
<% end %>
</div>