Class: Spree::PaymentMethod

Inherits:
Base
  • Object
show all
Includes:
Spree::Preferences::StaticallyConfigurable
Defined in:
app/models/spree/payment_method.rb

Overview

A base class which is used for implementing payment methods.

See github.com/solidusio/solidus_gateway/ for offically supported payment method implementations.

Uses STI (single table inheritance) to store all implemented payment methods in one table (spree_payment_methods).

This class is not meant to be instantiated. Please create instances of concrete payment methods.

Direct Known Subclasses

BillingIntegration, Check, CreditCard, StoreCredit

Defined Under Namespace

Classes: BogusCreditCard, Check, CreditCard, SimpleBogusCreditCard, StoreCredit

Constant Summary

DISPLAY =
[:both, :front_end, :back_end]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spree::Preferences::StaticallyConfigurable

#preference_source=, #preferences, #preferences=

Methods inherited from Base

display_includes, #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

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/spree/payment_method.rb', line 69

def active?
  where(type: to_s, active: true).count > 0
end

.available(display_on = nil, store: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/spree/payment_method.rb', line 47

def available(display_on = nil, store: nil)
  Spree::Deprecation.warn "Spree::PaymentMethod.available is deprecated."\
    "Please use .active, .available_to_users, and .available_to_admin scopes instead."\
    "For payment methods associated with a specific store, use Spree::PaymentMethod.available_to_store(your_store)"\
    " as the base applying any further filtering"

  display_on = display_on.to_s

  available_payment_methods =
    case display_on
    when 'front_end'
      active.available_to_users
    when 'back_end'
      active.available_to_admin
    else
      active.available_to_users.available_to_admin
    end
  available_payment_methods.select do |p|
    store.nil? || store.payment_methods.empty? || store.payment_methods.include?(p)
  end
end

.find_with_destroyed(*args) ⇒ Object



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

def find_with_destroyed(*args)
  unscoped { find(*args) }
end

.providersObject



41
42
43
44
45
# File 'app/models/spree/payment_method.rb', line 41

def providers
  Spree::Deprecation.warn 'Spree::PaymentMethod.providers is deprecated and will be deleted in Solidus 3.0. ' \
    'Please use Rails.application.config.spree.payment_methods instead'
  Rails.application.config.spree.payment_methods
end

Instance Method Details

#auto_capture?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'app/models/spree/payment_method.rb', line 190

def auto_capture?
  auto_capture.nil? ? Spree::Config[:auto_capture] : auto_capture
end

#cancel(_response) ⇒ Object

Raises:

  • (::NotImplementedError)


203
204
205
# File 'app/models/spree/payment_method.rb', line 203

def cancel(_response)
  raise ::NotImplementedError, 'You must implement cancel method for this payment method.'
end

#deprecated_method_type_overrideObject

:nodoc: If method_type has been overridden, call it and return the value, otherwise return nil



164
165
166
167
168
169
# File 'app/models/spree/payment_method.rb', line 164

def deprecated_method_type_override
  if method(:method_type).owner != Spree::PaymentMethod
    Spree::Deprecation.warn "overriding PaymentMethod#method_type is deprecated and will be removed from Solidus 3.0 (override partial_name instead)", caller
    method_type
  end
end

#display_onObject

Deprecated.

Use #available_to_users and #available_to_admin instead



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/spree/payment_method.rb', line 128

def display_on
  Spree::Deprecation.warn "Spree::PaymentMethod#display_on is deprecated."\
    "Please use #available_to_users and #available_to_admin instead."
  if available_to_users? && available_to_admin?
    ''
  elsif available_to_users?
    'front_end'
  elsif available_to_admin?
    'back_end'
  else
    'none'
  end
end

#display_on=(value) ⇒ Object

Deprecated.

Use #available_to_users= and #available_to_admin= instead



120
121
122
123
124
125
# File 'app/models/spree/payment_method.rb', line 120

def display_on=(value)
  Spree::Deprecation.warn "Spree::PaymentMethod#display_on= is deprecated."\
    "Please use #available_to_users= and #available_to_admin= instead."
  self.available_to_users = value.blank? || value == 'front_end'
  self.available_to_admin = value.blank? || value == 'back_end'
end

#gatewayObject Also known as: provider

Represents the gateway of this payment method

The gateway is responsible for communicating with the providers API.

It implements methods for:

- authorize
- purchase
- capture
- void
- credit


90
91
92
93
94
95
96
97
# File 'app/models/spree/payment_method.rb', line 90

def gateway
  gateway_options = options
  gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?
  if gateway_options[:server]
    ActiveMerchant::Billing::Base.mode = gateway_options[:server].to_sym
  end
  @gateway ||= gateway_class.new(gateway_options)
end

#method_typeObject



171
172
173
174
# File 'app/models/spree/payment_method.rb', line 171

def method_type
  Spree::Deprecation.warn "method_type is deprecated and will be removed from Solidus 3.0 (use partial_name instead)", caller
  partial_name
end

#optionsObject

Represents all preferences as a Hash

Each preference is a key holding the value(s) and gets passed to the gateway via gateway_options

Returns:

  • Hash



106
107
108
# File 'app/models/spree/payment_method.rb', line 106

def options
  preferences.to_hash
end

#partial_nameObject

Used as partial name for your payment method

Currently your payment method needs to provide these partials:

1. app/views/spree/checkout/payment/_{partial_name}.html.erb
The form your customer enters the payment information in during checkout

2. app/views/spree/checkout/existing_payment/_{partial_name}.html.erb
The payment information of your customers reusable sources during checkout

3. app/views/spree/admin/payments/source_forms/_{partial_name}.html.erb
The form an admin enters payment information in when creating orders in the backend

4. app/views/spree/admin/payments/source_views/_{partial_name}.html.erb
The view that represents your payment method on orders in the backend


158
159
160
# File 'app/models/spree/payment_method.rb', line 158

def partial_name
  deprecated_method_type_override || type.demodulize.downcase
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)


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

def payment_profiles_supported?
  false
end

#payment_source_classObject

The class that will store payment sources (re)usable with this payment method

Used by Spree::Payment as source (e.g. Spree::CreditCard in the case of a credit card payment method).

Returning nil means the payment method doesn't support storing sources (e.g. Spree::PaymentMethod::Check)

Raises:

  • (::NotImplementedError)


115
116
117
# File 'app/models/spree/payment_method.rb', line 115

def payment_source_class
  raise ::NotImplementedError, "You must implement payment_source_class method for #{self.class}."
end

#reusable_sources(_order) ⇒ Object

Custom gateways should redefine this method. See Gateway implementation as an example



186
187
188
# File 'app/models/spree/payment_method.rb', line 186

def reusable_sources(_order)
  []
end

#source_required?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'app/models/spree/payment_method.rb', line 180

def source_required?
  true
end

#store_credit?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'app/models/spree/payment_method.rb', line 207

def store_credit?
  is_a? Spree::PaymentMethod::StoreCredit
end

#supports?(_source) ⇒ Boolean

Check if given source is supported by this payment method

Please implement validation logic in your payment method implementation

Returns:

  • (Boolean)

See Also:



199
200
201
# File 'app/models/spree/payment_method.rb', line 199

def supports?(_source)
  true
end