Class: Tienda::Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tienda/payment.rb

Instance Method Summary collapse

Instance Method Details

#orderTienda::Order

The associated order

Returns:



7
# File 'app/models/tienda/payment.rb', line 7

belongs_to :order, :class_name => 'Tienda::Order'

#parentTienda::Payment

An associated payment (only applies to refunds)

Returns:



12
# File 'app/models/tienda/payment.rb', line 12

belongs_to :parent, :class_name => "Tienda::Payment", :foreign_key => "parent_payment_id"

#refund!(amount) ⇒ Boolean

Process a refund from this payment.

Parameters:

  • amount (String)

    the amount which should be refunded

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/tienda/payment.rb', line 52

def refund!(amount)
  amount = BigDecimal(amount)
  if refundable_amount >= amount
    transaction do
      self.class.create(:parent => self, :order_id => self.order_id, :amount => 0-amount, :method => self.method, :reference => reference)
      self.update_attribute(:amount_refunded, self.amount_refunded + amount)
      true
    end
  else
    raise Tienda::Errors::RefundFailed, :message => I18n.t('.refund_failed', refundable_amount: refundable_amount)
  end
end

#refund?Boolean

Is this payment a refund?

Returns:

  • (Boolean)


30
31
32
# File 'app/models/tienda/payment.rb', line 30

def refund?
  self.amount < BigDecimal(0)
end

#refundable_amountBigDecimal

How much of the payment can be refunded

Returns:

  • (BigDecimal)


44
45
46
# File 'app/models/tienda/payment.rb', line 44

def refundable_amount
  refundable? ? (self.amount - self.amount_refunded) : BigDecimal(0)
end

#refunded?Boolean

Has this payment had any refunds taken from it?

Returns:

  • (Boolean)


37
38
39
# File 'app/models/tienda/payment.rb', line 37

def refunded?
  self.amount_refunded > BigDecimal(0)
end

#transaction_urlString

Return a transaction URL for viewing further information about this payment.

Returns:

  • (String)


69
70
71
# File 'app/models/tienda/payment.rb', line 69

def transaction_url
  nil
end