Class: Shoppe::Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
app/models/shoppe/payment.rb

Instance Method Summary collapse

Instance Method Details

#orderShoppe::Order

The associated order

Returns:



11
# File 'app/models/shoppe/payment.rb', line 11

belongs_to :order, class_name: 'Shoppe::Order'

#parentShoppe::Payment

An associated payment (only applies to refunds)

Returns:



16
# File 'app/models/shoppe/payment.rb', line 16

belongs_to :parent, class_name: "Shoppe::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)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/shoppe/payment.rb', line 56

def refund!(amount)
  run_callbacks :refund do
    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 Shoppe::Errors::RefundFailed, message: I18n.t('.refund_failed', refundable_amount: refundable_amount)
    end
  end
end

#refund?Boolean

Is this payment a refund?

Returns:

  • (Boolean)


34
35
36
# File 'app/models/shoppe/payment.rb', line 34

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

#refundable_amountBigDecimal

How much of the payment can be refunded

Returns:

  • (BigDecimal)


48
49
50
# File 'app/models/shoppe/payment.rb', line 48

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)


41
42
43
# File 'app/models/shoppe/payment.rb', line 41

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

#transaction_urlString

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

Returns:

  • (String)


75
76
77
# File 'app/models/shoppe/payment.rb', line 75

def transaction_url
  nil
end