feat: Implement payment retry system and draft ticket expiry management

- Add 30-minute expiry window for draft tickets with automatic cleanup
- Implement 3-attempt payment retry mechanism with tracking
- Create background job for cleaning expired draft tickets every 10 minutes
- Add comprehensive UI warnings for expiring tickets and retry attempts
- Enhance dashboard to display pending draft tickets with retry options
- Add payment cancellation handling with smart retry redirections
- Include rake tasks for manual cleanup and statistics
- Add database fields: expires_at, payment_attempts, last_payment_attempt_at, stripe_session_id
- Fix payment attempt counter display to show correct attempt number (1/3, 2/3, 3/3)
This commit is contained in:
kbe
2025-08-31 10:22:38 +02:00
parent 48c648e2ca
commit 1acc3e09d4
12 changed files with 338 additions and 24 deletions

View File

@@ -29,6 +29,11 @@ class PagesController < ApplicationController
.distinct
.limit(5)
# Draft tickets that can be retried
@draft_tickets = current_user.tickets.includes(:ticket_type, :event)
.can_retry_payment
.order(:expires_at)
# Events sections
@today_events = Event.published.where("DATE(start_time) = ?", Date.current).order(start_time: :asc)
@tomorrow_events = Event.published.where("DATE(start_time) = ?", Date.current + 1).order(start_time: :asc)

View File

@@ -106,6 +106,36 @@ class TicketsController < ApplicationController
@tickets = current_user.tickets.includes(:ticket_type)
.where(id: draft_ticket_ids, status: "draft")
# Check for expired tickets and clean them up
expired_tickets = @tickets.select(&:expired?)
if expired_tickets.any?
expired_tickets.each(&:expire_if_overdue!)
@tickets = @tickets.reject(&:expired?)
if @tickets.empty?
session.delete(:draft_ticket_ids)
redirect_to event_path(@event.slug, @event), alert: "Vos billets ont expiré. Veuillez recommencer votre commande."
return
end
flash[:notice] = "Certains billets ont expiré et ont été supprimés de votre commande."
end
# Check if tickets can still be retried
non_retryable_tickets = @tickets.reject(&:can_retry_payment?)
if non_retryable_tickets.any?
non_retryable_tickets.each(&:expire_if_overdue!)
@tickets = @tickets.select(&:can_retry_payment?)
if @tickets.empty?
session.delete(:draft_ticket_ids)
redirect_to event_path(@event.slug, @event), alert: "Nombre maximum de tentatives de paiement atteint. Veuillez recommencer votre commande."
return
end
flash[:notice] = "Certains billets ont atteint le nombre maximum de tentatives de paiement."
end
if @tickets.empty?
redirect_to event_path(@event.slug, @event), alert: "Billets non trouvés ou déjà traités"
return
@@ -113,10 +143,16 @@ class TicketsController < ApplicationController
@total_amount = @tickets.sum(&:price_cents)
# Check for expiring soon tickets
@expiring_soon = @tickets.any?(&:expiring_soon?)
# Create Stripe checkout session if Stripe is configured
if Rails.application.config.stripe[:secret_key].present?
begin
@checkout_session = create_stripe_session
# Only increment payment attempts after successfully creating the session
@tickets.each(&:increment_payment_attempt!)
rescue => e
error_message = e.message.present? ? e.message : "Erreur Stripe inconnue"
Rails.logger.error "Stripe checkout session creation failed: #{error_message}"
@@ -138,9 +174,6 @@ class TicketsController < ApplicationController
return
end
# Stripe is now initialized at application startup, no need to initialize here
Rails.logger.debug "Payment success - Using globally initialized Stripe"
begin
stripe_session = Stripe::Checkout::Session.retrieve(session_id)
@@ -148,7 +181,7 @@ class TicketsController < ApplicationController
# Get event_id and ticket_ids from session metadata
event_id = stripe_session.metadata["event_id"]
ticket_ids_data = stripe_session.metadata["ticket_ids"]
unless event_id.present? && ticket_ids_data.present?
redirect_to dashboard_path, alert: "Informations de commande manquantes"
return
@@ -158,14 +191,14 @@ class TicketsController < ApplicationController
@event = Event.find(event_id)
ticket_ids = ticket_ids_data.split(",")
@tickets = current_user.tickets.where(id: ticket_ids, status: "draft")
if @tickets.empty?
redirect_to dashboard_path, alert: "Billets non trouvés"
return
end
@tickets.update_all(status: "active")
# Send confirmation emails
@tickets.each do |ticket|
TicketMailer.purchase_confirmation(ticket).deliver_now
@@ -192,16 +225,51 @@ class TicketsController < ApplicationController
# Handle payment failure/cancellation
def payment_cancel
redirect_to dashboard_path, alert: "Le paiement a été annulé"
# Keep draft tickets for potential retry, just redirect back to checkout
draft_ticket_ids = session[:draft_ticket_ids] || []
if draft_ticket_ids.any?
tickets = current_user.tickets.where(id: draft_ticket_ids, status: "draft")
retryable_tickets = tickets.select(&:can_retry_payment?)
if retryable_tickets.any?
event = retryable_tickets.first.event
redirect_to ticket_checkout_path(event.slug, event.id),
alert: "Le paiement a été annulé. Vous pouvez réessayer."
else
session.delete(:draft_ticket_ids)
redirect_to dashboard_path, alert: "Le paiement a été annulé et vos billets ont expiré."
end
else
redirect_to dashboard_path, alert: "Le paiement a été annulé"
end
end
def show
@ticket = current_user.tickets.includes(:ticket_type, :event).find(params[:ticket_id])
@event = @ticket.event
rescue ActiveRecord::RecordNotFound
redirect_to dashboard_path, alert: "Billet non trouvé"
# Allow users to retry payment for failed/cancelled payments
def retry_payment
@event = Event.includes(:ticket_types).find(params[:id])
ticket_ids = params[:ticket_ids]&.split(',') || []
@tickets = current_user.tickets.where(id: ticket_ids)
.select(&:can_retry_payment?)
if @tickets.empty?
redirect_to event_path(@event.slug, @event),
alert: "Aucun billet disponible pour un nouveau paiement"
return
end
# Set session for checkout
session[:draft_ticket_ids] = @tickets.map(&:id)
redirect_to ticket_checkout_path(@event.slug, @event.id)
end
def show
@ticket = current_user.tickets.includes(:ticket_type, :event).find(params[:ticket_id])
@event = @ticket.event
rescue ActiveRecord::RecordNotFound
redirect_to dashboard_path, alert: "Billet non trouvé"
end
private
def set_event