Sha256: 07f436103e0c38c9399402fd8b6b85ea2291f3a717ac66e6dcdc2d37af104442

Contents?: true

Size: 821 Bytes

Versions: 3

Compression:

Stored size: 821 Bytes

Contents

module Transactionable
  class Credit < Transaction
    has_many :reversals

    def reverse!(reversal_amount = nil)
      ensure_valid_reversal(reversal_amount)
      remote_reversal = reversal_amount ? remote.reverse(amount: amount_in_cents(reversal_amount)) : remote.reverse
      transaction = Reversal.create_from_remote(remote_reversal)
      reversals << transaction
      transactionable.transactions << transaction
    end

    def max_reversal_amount
      amount - reversals.sum(:amount)
    end

    private

    def ensure_valid_reversal(reversal_amount)
      reversal_amount = reversal_amount ? reversal_amount : amount
      raise Exceptions::InvalidReversalAmount if reversal_amount > max_reversal_amount
    end

    def amount_in_cents(dollar_amount)
      (dollar_amount * 100).to_i
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
transactionable-0.3.0 app/models/transactionable/credit.rb
transactionable-0.2.0 app/models/transactionable/credit.rb
transactionable-0.1.0 app/models/transactionable/credit.rb