wip: order checkout

This commit is contained in:
kbe
2025-09-02 02:56:23 +02:00
parent afe074c8a1
commit ca81d2360c
18 changed files with 893 additions and 292 deletions

View File

@@ -1,32 +1,25 @@
class Ticket < ApplicationRecord
# === Constants ===
DRAFT_EXPIRY_TIME = 30.minutes
MAX_PAYMENT_ATTEMPTS = 3
# === Associations ===
belongs_to :user
belongs_to :order
belongs_to :ticket_type
has_one :event, through: :ticket_type
has_one :user, through: :order
# === Validations ===
validates :qr_code, presence: true, uniqueness: true
validates :user_id, presence: true
validates :order_id, presence: true
validates :ticket_type_id, presence: true
validates :price_cents, presence: true, numericality: { greater_than: 0 }
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
@@ -38,36 +31,22 @@ class Ticket < ApplicationRecord
price_cents / 100.0
end
# Check if ticket can be retried for payment
# Delegate payment methods to order
def can_retry_payment?
draft? && payment_attempts < MAX_PAYMENT_ATTEMPTS && !expired?
order.can_retry_payment?
end
# Check if ticket is expired
def expired?
expires_at.present? && expires_at < Time.current
order.expired?
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?
order.expiring_soon?
end
expires_at <= 5.minutes.from_now
# Mark ticket as expired if it's past expiry time
def expire_if_overdue!
order.expire_if_overdue!
end
private
@@ -86,11 +65,6 @@ class Ticket < ApplicationRecord
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"