Class: Spree::Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Processing
Defined in:
app/models/spree/payment.rb,
app/models/spree/payment/processing.rb

Defined Under Namespace

Modules: Processing

Constant Summary collapse

IDENTIFIER_CHARS =
(('A'..'Z').to_a + ('0'..'9').to_a - %w(0 1 I O)).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Processing

#authorize!, #cancel!, #capture!, #credit!, #gateway_options, #partial_credit, #process!, #purchase!, #void_transaction!

Instance Attribute Details

#source_attributesObject

Returns the value of attribute source_attributes.



24
25
26
# File 'app/models/spree/payment.rb', line 24

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



112
113
114
115
# File 'app/models/spree/payment.rb', line 112

def actions
  return [] unless payment_source and payment_source.respond_to? :actions
  payment_source.actions.select { |action| !payment_source.respond_to?("can_#{action}?") or payment_source.send("can_#{action}?", self) }
end

#amount=(amount) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'app/models/spree/payment.rb', line 82

def amount=(amount)
  self[:amount] =
    case amount
    when String
      separator = I18n.t('number.currency.format.separator')
      number    = amount.delete("^0-9-#{separator}").tr(separator, '.')
      number.to_d if number.present?
    end || amount
end

#build_sourceObject



105
106
107
108
109
110
# File 'app/models/spree/payment.rb', line 105

def build_source
  return if source_attributes.nil?
  if payment_method and payment_method.payment_source_class
    self.source = payment_method.payment_source_class.new(source_attributes)
  end
end

#can_credit?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/spree/payment.rb', line 100

def can_credit?
  credit_allowed > 0
end

#credit_allowedObject



96
97
98
# File 'app/models/spree/payment.rb', line 96

def credit_allowed
  amount - offsets_total.abs
end

#currencyObject



73
74
75
# File 'app/models/spree/payment.rb', line 73

def currency
  order.currency
end

#moneyObject Also known as: display_amount



77
78
79
# File 'app/models/spree/payment.rb', line 77

def money
  Spree::Money.new(amount, { currency: currency })
end

#offsets_totalObject



92
93
94
# File 'app/models/spree/payment.rb', line 92

def offsets_total
  offsets.pluck(:amount).sum
end

#payment_sourceObject



117
118
119
120
# File 'app/models/spree/payment.rb', line 117

def payment_source
  res = source.is_a?(Payment) ? source.source : source
  res || payment_method
end

#persist_invalidObject



40
41
42
43
44
# File 'app/models/spree/payment.rb', line 40

def persist_invalid
  return unless ['failed', 'invalid'].include?(state)
  state_will_change!
  save
end