Sha256: 7c4f074e95598390639678196b9c55e91c041595aa01fee5229e7f23e8f0d9c7

Contents?: true

Size: 777 Bytes

Versions: 3

Compression:

Stored size: 777 Bytes

Contents

module Transactionable
  class Debit < Transaction
    has_many :refunds

    def refund!(refund_amount = nil)
      ensure_valid_refund(refund_amount)
      remote_refund = refund_amount ? remote.refund(amount: amount_in_cents(refund_amount)) : remote.refund
      transaction = Refund.create_from_remote(remote_refund)
      refunds << transaction
      transactionable.transactions << transaction
    end

    def max_refund_amount
      amount - refunds.sum(:amount)
    end

    private

    def ensure_valid_refund(refund_amount)
      refund_amount = refund_amount ? refund_amount : amount
      raise Exceptions::InvalidRefundAmount if refund_amount > max_refund_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/debit.rb
transactionable-0.2.0 app/models/transactionable/debit.rb
transactionable-0.1.0 app/models/transactionable/debit.rb