Class: Spree::Payment

Inherits:
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
NON_RISKY_AVS_CODES =
['B', 'D', 'H', 'J', 'M', 'Q', 'T', 'V', 'X', 'Y'].freeze
RISKY_AVS_CODES =
['A', 'C', 'E', 'F', 'G', 'I', 'K', 'L', 'N', 'O', 'P', 'R', 'S', 'U', 'W', 'Z'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Processing

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

Methods inherited from Base

#initialize_preference_defaults, page, preference

Methods included from Spree::Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#request_envObject

Returns the value of attribute request_env.



38
39
40
# File 'app/models/spree/payment.rb', line 38

def request_env
  @request_env
end

#source_attributesObject

Returns the value of attribute source_attributes.



35
36
37
# File 'app/models/spree/payment.rb', line 35

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsArray<String>

Returns the actions available on this payment.

Returns:

  • (Array<String>)

    the actions available on this payment



168
169
170
171
172
# File 'app/models/spree/payment.rb', line 168

def actions
  sa = source_actions
  sa |= ["failure"] if processing?
  sa
end

#amount=(amount) ⇒ Object

Sets the amount, parsing it based on i18n settings if it is a string.

Parameters:

  • amount (BigDecimal, String)

    the desired new amount



118
119
120
121
122
123
124
125
126
# File 'app/models/spree/payment.rb', line 118

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

#apply_source_attributesObject

When this is a new record without a source, builds a new source based on this payment’s payment method and associates it correctly.

TODO: Move this into upcoming CartUpdate class



154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/spree/payment.rb', line 154

def apply_source_attributes
  return unless new_record?
  return if source_attributes.blank?

  ActiveSupport::Deprecation.warn(<<WARN.squish)
Building payment sources by assigning source_attributes on payments is
deprecated. Instead use either the PaymentCreate class or the
OrderUpdateAttributes class.
WARN

  PaymentCreate.new(order, {source_attributes: source_attributes}, payment: self, request_env: request_env).build
end

#can_credit?Boolean

Returns true when this payment can be credited.

Returns:

  • (Boolean)

    true when this payment can be credited



144
145
146
# File 'app/models/spree/payment.rb', line 144

def can_credit?
  credit_allowed > 0
end

#captured_amountBigDecimal

Returns the total amount captured on this payment.

Returns:

  • (BigDecimal)

    the total amount captured on this payment



195
196
197
# File 'app/models/spree/payment.rb', line 195

def captured_amount
  capture_events.sum(:amount)
end

#credit_allowedBigDecimal

The total amount this payment can be credited.

Returns:

  • (BigDecimal)

    the amount of this payment minus the offsets (old-style refunds) and refunds



139
140
141
# File 'app/models/spree/payment.rb', line 139

def credit_allowed
  amount - (offsets_total.abs + refunds.sum(:amount))
end

#currencyString

Returns this payment’s currency.

Returns:

  • (String)

    this payment’s currency



107
# File 'app/models/spree/payment.rb', line 107

delegate :currency, to: :order

#is_avs_risky?Boolean

Returns true when this payment is risky based on address.

Returns:

  • (Boolean)

    true when this payment is risky based on address



181
182
183
184
# File 'app/models/spree/payment.rb', line 181

def is_avs_risky?
  return false if avs_response.blank? || NON_RISKY_AVS_CODES.include?(avs_response)
  return true
end

#is_cvv_risky?Boolean

Returns true when this payment is risky based on cvv.

Returns:

  • (Boolean)

    true when this payment is risky based on cvv



187
188
189
190
191
192
# File 'app/models/spree/payment.rb', line 187

def is_cvv_risky?
  return false if cvv_response_code == "M"
  return false if cvv_response_code.nil?
  return false if cvv_response_message.present?
  return true
end

#moneySpree::Money Also known as: display_amount

Returns this amount of this payment as money object.

Returns:

  • (Spree::Money)

    this amount of this payment as money object



110
111
112
# File 'app/models/spree/payment.rb', line 110

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

#offsets_totalBigDecimal

The total amount of the offsets (for old-style refunds) for this payment.

Returns:

  • (BigDecimal)

    the total amount of this payment’s offsets



131
132
133
# File 'app/models/spree/payment.rb', line 131

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

#payment_sourceObject

Returns the source of ths payment.

Returns:

  • (Object)

    the source of ths payment



175
176
177
178
# File 'app/models/spree/payment.rb', line 175

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

#store_credit?Boolean

Returns true when the payment method exists and is a store credit payment method.

Returns:

  • (Boolean)

    true when the payment method exists and is a store credit payment method



205
206
207
# File 'app/models/spree/payment.rb', line 205

def store_credit?
  payment_method.try!(:store_credit?)
end

#transaction_idString

Returns this payment’s response code.

Returns:

  • (String)

    this payment’s response code



102
103
104
# File 'app/models/spree/payment.rb', line 102

def transaction_id
  response_code
end

#uncaptured_amountBigDecimal

Returns the total amount left uncaptured on this payment.

Returns:

  • (BigDecimal)

    the total amount left uncaptured on this payment



200
201
202
# File 'app/models/spree/payment.rb', line 200

def uncaptured_amount
  amount - captured_amount
end