feat: PDF ticket generation
- Each ticket has a unique URL for viewing and downloading - Only the ticket owner can access their ticket - The customer's name is clearly displayed on the ticket - The PDF can be downloaded directly from the ticket view page - All existing functionality continues to work as expected
This commit is contained in:
@@ -54,6 +54,32 @@ class TicketsController < ApplicationController
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
redirect_to dashboard_path, alert: "Billet non trouvé"
|
||||
end
|
||||
|
||||
# Download PDF ticket - only accessible by ticket owner
|
||||
def download_ticket
|
||||
# Find ticket and ensure it belongs to current user
|
||||
@ticket = current_user.orders.joins(:tickets).find_by(tickets: { id: params[:ticket_id] })
|
||||
|
||||
if @ticket.nil?
|
||||
redirect_to dashboard_path, alert: "Billet non trouvé ou vous n'avez pas l'autorisation d'accéder à ce billet"
|
||||
return
|
||||
end
|
||||
|
||||
# Generate PDF
|
||||
pdf_content = @ticket.to_pdf
|
||||
|
||||
# Send PDF as download
|
||||
send_data pdf_content,
|
||||
filename: "ticket_#{@ticket.id}_#{@ticket.event.name.parameterize}.pdf",
|
||||
type: "application/pdf",
|
||||
disposition: "attachment"
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
redirect_to dashboard_path, alert: "Billet non trouvé"
|
||||
rescue => e
|
||||
Rails.logger.error "Error generating ticket PDF: #{e.message}"
|
||||
redirect_to dashboard_path, alert: "Erreur lors de la génération du billet"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_event
|
||||
|
||||
@@ -118,7 +118,7 @@ export default class extends Controller {
|
||||
await this.storeCartInSession(cartData);
|
||||
|
||||
// Redirect to event-scoped orders/new page
|
||||
const OrderNewUrl = `/events/${this.eventSlugValue}.${this.eventIdValue}/orders/new`;
|
||||
const OrderNewUrl = `/orders/new/events/${this.eventSlugValue}.${this.eventIdValue}`;
|
||||
window.location.href = OrderNewUrl;
|
||||
} catch (error) {
|
||||
console.error("Error storing cart:", error);
|
||||
|
||||
@@ -32,13 +32,18 @@ class TicketPdfGenerator
|
||||
# Ticket info box
|
||||
pdf.stroke_color "E5E7EB"
|
||||
pdf.fill_color "F9FAFB"
|
||||
pdf.rounded_rectangle [ 0, pdf.cursor ], 310, 120, 10
|
||||
pdf.rounded_rectangle [ 0, pdf.cursor ], 310, 150, 10
|
||||
pdf.fill_and_stroke
|
||||
|
||||
pdf.move_down 10
|
||||
pdf.fill_color "000000"
|
||||
pdf.font "Helvetica", size: 12
|
||||
|
||||
# Customer name
|
||||
pdf.text "Ticket Holder:", style: :bold
|
||||
pdf.text "#{ticket.first_name} #{ticket.last_name}"
|
||||
pdf.move_down 8
|
||||
|
||||
# Ticket details
|
||||
pdf.text "Ticket Type:", style: :bold
|
||||
pdf.text ticket.ticket_type.name
|
||||
|
||||
@@ -157,7 +157,7 @@
|
||||
<% end %>
|
||||
|
||||
<% if @ticket.status == 'active' %>
|
||||
<%= link_to "#",
|
||||
<%= link_to download_ticket_path(@ticket.id),
|
||||
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 text-center" do %>
|
||||
<svg class="w-4 h-4 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
|
||||
|
||||
@@ -53,14 +53,15 @@ Rails.application.routes.draw do
|
||||
get "orders/payments/success", to: "orders#payment_success", as: "order_payment_success"
|
||||
get "orders/payments/cancel", to: "orders#payment_cancel", as: "order_payment_cancel"
|
||||
|
||||
# Legacy ticket routes - redirect to order system
|
||||
get "events/:slug.:id/tickets/checkout", to: "tickets#checkout", as: "ticket_checkout"
|
||||
post "events/:slug.:id/tickets/retry", to: "tickets#retry_payment", as: "ticket_retry_payment"
|
||||
# legacy routes
|
||||
get "payments/success", to: "tickets#payment_success", as: "payment_success"
|
||||
get "payments/cancel", to: "tickets#payment_cancel", as: "payment_cancel"
|
||||
|
||||
# === Tickets ===
|
||||
get "tickets/:ticket_id/download", to: "events#download_ticket", as: "download_ticket"
|
||||
get "tickets/checkout/events/:slug.:id", to: "tickets#checkout", as: "ticket_checkout"
|
||||
post "tickets/retry/events/:slug.:id", to: "tickets#retry_payment", as: "ticket_retry_payment"
|
||||
get "tickets/:ticket_id", to: "tickets#show", as: "ticket"
|
||||
get "tickets/:ticket_id/download", to: "tickets#download_ticket", as: "download_ticket"
|
||||
|
||||
# === Promoter Routes ===
|
||||
namespace :promoter do
|
||||
|
||||
92
test/services/ticket_pdf_generator_customer_name_test.rb
Normal file
92
test/services/ticket_pdf_generator_customer_name_test.rb
Normal file
@@ -0,0 +1,92 @@
|
||||
require "test_helper"
|
||||
|
||||
class TicketPdfGeneratorCustomerNameTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
# Stub QR code generation to avoid dependency issues
|
||||
mock_qrcode = mock("qrcode")
|
||||
mock_qrcode.stubs(:modules).returns([])
|
||||
RQRCode::QRCode.stubs(:new).returns(mock_qrcode)
|
||||
|
||||
@user = User.create!(
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
password_confirmation: "password123"
|
||||
)
|
||||
|
||||
@event = Event.create!(
|
||||
name: "Test Event",
|
||||
slug: "test-event",
|
||||
description: "A valid description for the test event that is long enough",
|
||||
latitude: 48.8566,
|
||||
longitude: 2.3522,
|
||||
venue_name: "Test Venue",
|
||||
venue_address: "123 Test Street",
|
||||
user: @user,
|
||||
start_time: 1.week.from_now,
|
||||
end_time: 1.week.from_now + 3.hours,
|
||||
state: :published
|
||||
)
|
||||
|
||||
@ticket_type = TicketType.create!(
|
||||
name: "General Admission",
|
||||
description: "General admission tickets with full access to the event",
|
||||
price_cents: 2500,
|
||||
quantity: 100,
|
||||
sale_start_at: Time.current,
|
||||
sale_end_at: @event.start_time - 1.hour,
|
||||
requires_id: false,
|
||||
event: @event
|
||||
)
|
||||
|
||||
@order = Order.create!(
|
||||
user: @user,
|
||||
event: @event,
|
||||
status: "paid",
|
||||
total_amount_cents: 2500
|
||||
)
|
||||
|
||||
@ticket = Ticket.create!(
|
||||
order: @order,
|
||||
ticket_type: @ticket_type,
|
||||
status: "active",
|
||||
first_name: "John",
|
||||
last_name: "Doe",
|
||||
qr_code: "test-qr-code-123"
|
||||
)
|
||||
end
|
||||
|
||||
test "should include customer name in PDF" do
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert_kind_of String, pdf_string
|
||||
assert pdf_string.length > 0
|
||||
|
||||
# Check if it starts with PDF header
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
|
||||
# Check that the PDF is larger than expected (indicating content was added)
|
||||
# The customer name should make the PDF larger
|
||||
assert pdf_string.length > 1000, "PDF should be substantial in size"
|
||||
end
|
||||
|
||||
test "should generate valid PDF with customer name" do
|
||||
# Update ticket with name containing special characters
|
||||
@ticket.update!(first_name: "José", last_name: "Martínez")
|
||||
|
||||
generator = TicketPdfGenerator.new(@ticket)
|
||||
pdf_string = generator.generate
|
||||
|
||||
assert_not_nil pdf_string
|
||||
assert_kind_of String, pdf_string
|
||||
assert pdf_string.length > 0
|
||||
|
||||
# Check if it starts with PDF header
|
||||
assert pdf_string.start_with?("%PDF")
|
||||
|
||||
# Check that the PDF is valid
|
||||
assert pdf_string.length > 1000, "PDF should be substantial in size"
|
||||
assert pdf_string.end_with?("%%EOF\n"), "PDF should end with EOF marker"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user