chore: prepare checkout handling with stripe

This commit is contained in:
kbe
2025-08-28 19:11:40 +02:00
parent 0b58768a24
commit be3d80e541
3 changed files with 170 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
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, :payment_success, :download_ticket ]
before_action :set_event, only: [ :show, :checkout ]
# Display all events
def index
@events = Event.includes(:user).upcoming.page(params[:page]).per(12)
@@ -43,14 +43,14 @@ class EventsController < ApplicationController
# Create Stripe line item
line_items << {
price_data: {
currency: 'eur',
currency: "eur",
product_data: {
name: "#{@event.name} - #{ticket_type.name}",
description: ticket_type.description,
description: ticket_type.description
},
unit_amount: ticket_type.price_cents,
unit_amount: ticket_type.price_cents
},
quantity: quantity,
quantity: quantity
}
# Store for ticket creation
@@ -72,10 +72,10 @@ class EventsController < ApplicationController
begin
# Create Stripe Checkout Session
session = Stripe::Checkout::Session.create({
payment_method_types: ['card'],
payment_method_types: [ "card" ],
line_items: line_items,
mode: 'payment',
success_url: payment_success_url(event_id: @event.id, session_id: '{CHECKOUT_SESSION_ID}'),
mode: "payment",
success_url: payment_success_url(event_id: @event.id, session_id: "{CHECKOUT_SESSION_ID}"),
cancel_url: event_url(@event.slug, @event),
customer_email: current_user.email,
metadata: {
@@ -98,29 +98,29 @@ class EventsController < ApplicationController
begin
session = Stripe::Checkout::Session.retrieve(session_id)
if session.payment_status == 'paid'
if session.payment_status == "paid"
# Create tickets
@event = Event.find(event_id)
order_items = JSON.parse(session.metadata['order_items'])
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_type = TicketType.find(item["ticket_type_id"])
item["quantity"].times do
ticket = Ticket.create!(
user: current_user,
ticket_type: ticket_type,
status: 'active'
status: "active"
)
@tickets << ticket
# Send confirmation email for each ticket
TicketMailer.purchase_confirmation(ticket).deliver_now
end
end
render 'payment_success'
render "payment_success"
else
redirect_to event_path(@event.slug, @event), alert: "Le paiement n'a pas été complété avec succès"
end
@@ -134,14 +134,14 @@ class EventsController < ApplicationController
# 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,
send_data pdf,
filename: "ticket-#{@ticket.event.name.parameterize}-#{@ticket.qr_code[0..7]}.pdf",
type: 'application/pdf',
disposition: 'attachment'
type: "application/pdf",
disposition: "attachment"
end
end
end