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

@@ -1,4 +1,8 @@
class Ticket < ApplicationRecord
# === Constants ===
DRAFT_EXPIRY_TIME = 30.minutes
MAX_PAYMENT_ATTEMPTS = 3
# === Associations ===
belongs_to :user
belongs_to :ticket_type
@@ -12,9 +16,17 @@ class Ticket < ApplicationRecord
validates :status, presence: true, inclusion: { in: %w[draft active used expired refunded] }
validates :first_name, presence: true
validates :last_name, presence: true
validates :payment_attempts, presence: true, numericality: { greater_than_or_equal_to: 0 }
# === Scopes ===
scope :draft, -> { where(status: "draft") }
scope :active, -> { where(status: "active") }
scope :expired_drafts, -> { draft.where("expires_at < ?", Time.current) }
scope :can_retry_payment, -> { draft.where("payment_attempts < ? AND expires_at > ?", MAX_PAYMENT_ATTEMPTS, Time.current) }
before_validation :set_price_from_ticket_type, on: :create
before_validation :generate_qr_code, on: :create
before_validation :set_draft_expiry, on: :create
# Generate PDF ticket
def to_pdf
@@ -26,6 +38,38 @@ class Ticket < ApplicationRecord
price_cents / 100.0
end
# Check if ticket can be retried for payment
def can_retry_payment?
draft? && payment_attempts < MAX_PAYMENT_ATTEMPTS && !expired?
end
# Check if ticket is expired
def expired?
expires_at.present? && expires_at < Time.current
end
# Mark ticket as expired if it"s past expiry time
def expire_if_overdue!
return unless draft? && expired?
update!(status: "expired")
end
# Increment payment attempt counter
def increment_payment_attempt!
update!(
payment_attempts: payment_attempts + 1,
last_payment_attempt_at: Time.current
)
end
# Check if draft is about to expire (within 5 minutes)
def expiring_soon?
return false unless draft? && expires_at.present?
expires_at <= 5.minutes.from_now
end
private
def set_price_from_ticket_type
@@ -41,4 +85,14 @@ class Ticket < ApplicationRecord
break unless Ticket.exists?(qr_code: qr_code)
end
end
def set_draft_expiry
return unless status == "draft"
self.expires_at = DRAFT_EXPIRY_TIME.from_now if expires_at.blank?
end
def draft?
status == "draft"
end
end