class Donation < ApplicationRecord include AASM belongs_to :user validates_presence_of :user validates_presence_of :donation_method, inclusion: { in: %w[ custom btcpay lndhub ] } validates_presence_of :payment_status, allow_nil: true, inclusion: { in: %w[ pending processing settled ] } validates_presence_of :paid_at, allow_nil: true validates_presence_of :amount_sats, allow_nil: true validates_presence_of :fiat_amount, allow_nil: true validates_presence_of :fiat_currency, allow_nil: true, inclusion: { in: %w[ EUR USD ] } scope :pending, -> { where(payment_status: "pending") } scope :processing, -> { where(payment_status: "processing") } scope :completed, -> { where(payment_status: "settled") } scope :incomplete, -> { where.not(payment_status: "settled") } aasm column: :payment_status do state :pending, initial: true state :processing state :settled event :start_processing do transitions from: :pending, to: :processing end event :complete do transitions from: :processing, to: :settled, after: [:set_paid_at, :set_sustainer_status] transitions from: :pending, to: :settled, after: [:set_paid_at, :set_sustainer_status] end end def pending? payment_status == "pending" end def processing? payment_status == "processing" end def completed? payment_status == "settled" end private def set_paid_at update paid_at: DateTime.now if paid_at.nil? end def set_sustainer_status user.add_member_status :sustainer rescue => e Sentry.capture_exception(e) if Setting.sentry_enabled? Rails.logger.error("Failed to set memberStatus: #{e.message}") end end