Sha256: 231972d14e45bfea5ecabb0ba764f8f245f83267fb13010d19c627e77912a951

Contents?: true

Size: 1.52 KB

Versions: 5

Compression:

Stored size: 1.52 KB

Contents

module Pay
  class Charge < Pay::ApplicationRecord
    self.table_name = Pay.chargeable_table

    # Only serialize for non-json columns
    serialize :data unless json_column?("data")

    # Associations
    belongs_to :owner, polymorphic: true

    # Scopes
    scope :sorted, -> { order(created_at: :desc) }
    default_scope -> { sorted }

    # Validations
    validates :amount, presence: true
    validates :processor, presence: true
    validates :processor_id, presence: true
    validates :card_type, presence: true

    store_accessor :data, :paddle_receipt_url

    # Helpers for payment processors
    %w[braintree stripe paddle].each do |processor_name|
      define_method "#{processor_name}?" do
        processor == processor_name
      end

      scope processor_name, -> { where(processor: processor_name) }
    end

    def payment_processor
      @payment_processor ||= payment_processor_for(processor).new(self)
    end

    def payment_processor_for(name)
      "Pay::#{name.to_s.classify}::Charge".constantize
    end

    def processor_charge
      payment_processor.charge
    end

    def refund!(refund_amount = nil)
      refund_amount ||= amount
      payment_processor.refund!(refund_amount)
    end

    def charged_to
      "#{card_type} (**** **** **** #{card_last4})"
    end

    def stripe?
      processor == "stripe"
    end

    def braintree?
      processor == "braintree"
    end

    def paypal?
      braintree? && card_type == "PayPal"
    end

    def paddle?
      processor == "paddle"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pay-2.6.4 app/models/pay/charge.rb
pay-2.6.3 app/models/pay/charge.rb
pay-2.6.2 app/models/pay/charge.rb
pay-2.6.1 app/models/pay/charge.rb
pay-2.6.0 app/models/pay/charge.rb