Class: Spree::PaymentMethod

Inherits:
Object
  • Object
show all
Includes:
DisplayOn, LegacyMultiStoreSupport, Metadata, Metafields, Security::PaymentMethods, SingleStoreResource
Defined in:
app/models/spree/payment_method.rb

Direct Known Subclasses

Gateway, Check, StoreCredit

Defined Under Namespace

Classes: Check, StoreCredit, WebhookSignatureError

Constant Summary

Constants included from DisplayOn

DisplayOn::DISPLAY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Class Method Details

.find_with_destroyed(*args) ⇒ Object



149
150
151
# File 'app/models/spree/payment_method.rb', line 149

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

.providersObject



37
38
39
# File 'app/models/spree/payment_method.rb', line 37

def self.providers
  Spree.payment_methods
end

Instance Method Details

#auto_capture?Boolean

Returns:



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

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

#available_for_order?(order) ⇒ Boolean

Custom PaymentMethod/Gateway can redefine this method to check method availability for concrete order.

Returns:



197
198
199
# File 'app/models/spree/payment_method.rb', line 197

def available_for_order?(order)
  !order.covered_by_store_credit?
end

#available_for_store?(store) ⇒ Boolean

Returns:



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

def available_for_store?(store)
  return true if store.blank?

  store_id == store.id
end

#cancel(_response) ⇒ Object

Raises:

  • (::NotImplementedError)


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

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

#complete_payment_session(payment_session:, params: {}) ⇒ Object

Completes a payment session via the provider. Override in gateway subclasses to implement provider-specific session completion.

Responsibilities:

  • Verify payment status with the provider
  • Create/update the Spree::Payment record
  • Patch order data from provider (e.g. wallet billing address)
  • Transition payment session to completed/failed

Must NOT complete the order — that is handled by Carts::Complete (called by the frontend or by the webhook handler).

Raises:

  • (::NotImplementedError)


85
86
87
# File 'app/models/spree/payment_method.rb', line 85

def complete_payment_session(payment_session:, params: {})
  raise ::NotImplementedError, 'You must implement complete_payment_session method for this gateway.'
end

#complete_payment_setup_session(setup_session:, params: {}) ⇒ Object

Completes a payment setup session via the provider. Override in gateway subclasses to implement provider-specific setup session completion.

Raises:

  • (::NotImplementedError)


133
134
135
# File 'app/models/spree/payment_method.rb', line 133

def complete_payment_setup_session(setup_session:, params: {})
  raise ::NotImplementedError, "#{self.class.name} does not implement #complete_payment_setup_session"
end

#confirmation_required?Boolean

Returns:



153
154
155
# File 'app/models/spree/payment_method.rb', line 153

def confirmation_required?
  false
end

#create_payment_session(order:, amount: nil, external_data: {}) ⇒ Object

Creates a payment session via the provider. Override in gateway subclasses to implement provider-specific session creation.

Raises:

  • (::NotImplementedError)


64
65
66
# File 'app/models/spree/payment_method.rb', line 64

def create_payment_session(order:, amount: nil, external_data: {})
  raise ::NotImplementedError, 'You must implement create_payment_session method for this gateway.'
end

#create_payment_setup_session(customer:, external_data: {}) ⇒ Object

Creates a payment setup session via the provider for saving a payment method. Override in gateway subclasses to implement provider-specific setup session creation.

Raises:

  • (::NotImplementedError)


127
128
129
# File 'app/models/spree/payment_method.rb', line 127

def create_payment_setup_session(customer:, external_data: {})
  raise ::NotImplementedError, "#{self.class.name} does not implement #create_payment_setup_session"
end

#default_nameObject



141
142
143
# File 'app/models/spree/payment_method.rb', line 141

def default_name
  self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip
end

#method_typeObject



137
138
139
# File 'app/models/spree/payment_method.rb', line 137

def method_type
  type.demodulize.downcase
end

#parse_webhook_event(raw_body, headers) ⇒ Hash?

Parses an incoming webhook payload from the payment provider. Override in gateway subclasses to implement provider-specific webhook parsing.

Parameters:

  • raw_body (String)

    the raw request body

  • headers (Hash)

    the request headers

Returns:

  • (Hash, nil)

    normalized result or nil for unsupported events { action: :captured/:authorized/:failed/:canceled, payment_session: Spree::PaymentSession, metadata: {} }

Raises:



99
100
101
# File 'app/models/spree/payment_method.rb', line 99

def parse_webhook_event(raw_body, headers)
  raise ::NotImplementedError, 'You must implement parse_webhook_event method for this gateway.'
end

#payment_icon_nameObject



145
146
147
# File 'app/models/spree/payment_method.rb', line 145

def payment_icon_name
  type.demodulize.gsub(/(^Spree::Gateway::|Gateway$)/, '').downcase.gsub(/\s+/, '').strip
end

#payment_profiles_supported?Boolean

Returns:



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

def payment_profiles_supported?
  false
end

#payment_session_classObject

The class used for payment sessions with this payment method. Override in gateway subclasses to provide a provider-specific session class that inherits from Spree::PaymentSession (STI). nil means the payment method doesn't support payment sessions.



58
59
60
# File 'app/models/spree/payment_method.rb', line 58

def payment_session_class
  nil
end

#payment_setup_session_classObject

The class used for payment setup sessions with this payment method. Override in gateway subclasses to provide a provider-specific session class.



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

def payment_setup_session_class
  nil
end

#payment_source_classObject

The class that will process payments for this payment type, used for @payment.source e.g. CreditCard in the case of a the Gateway payment type nil means the payment method doesn't require a source e.g. check

Raises:

  • (::NotImplementedError)


48
49
50
51
52
# File 'app/models/spree/payment_method.rb', line 48

def payment_source_class
  return unless source_required?

  raise ::NotImplementedError, 'You must implement payment_source_class method for this gateway.'
end

#provider_classObject

Raises:

  • (::NotImplementedError)


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

def provider_class
  raise ::NotImplementedError, 'You must implement provider_class method for this gateway.'
end

#public_preferencesObject



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

def public_preferences
  public_preference_keys.each_with_object({}) do |key, hash|
    hash[key] = preferences[key]
  end
end

#reusable_sources(_order) ⇒ Object

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



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

def reusable_sources(_order)
  []
end

#session_required?Boolean

Returns:



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

def session_required?
  false
end

#setup_session_supported?Boolean

Whether this payment method supports setup sessions (saving payment methods for future use). Override in gateway subclasses that support tokenization without a payment.

Returns:



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

def setup_session_supported?
  false
end

#show_in_admin?Boolean

Returns:



169
170
171
# File 'app/models/spree/payment_method.rb', line 169

def show_in_admin?
  true
end

#source_required?Boolean

Returns:



161
162
163
# File 'app/models/spree/payment_method.rb', line 161

def source_required?
  true
end

#store_credit?Boolean

Returns:



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

def store_credit?
  self.class == Spree::PaymentMethod::StoreCredit
end

#supports?(_source) ⇒ Boolean

Returns:



183
184
185
# File 'app/models/spree/payment_method.rb', line 183

def supports?(_source)
  true
end

#update_payment_session(payment_session:, amount: nil, external_data: {}) ⇒ Object

Updates an existing payment session via the provider. Override in gateway subclasses to implement provider-specific session updates.

Raises:

  • (::NotImplementedError)


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

def update_payment_session(payment_session:, amount: nil, external_data: {})
  raise ::NotImplementedError, 'You must implement update_payment_session method for this gateway.'
end

#webhook_urlString?

Returns the webhook URL for this payment method.

Returns:

  • (String, nil)


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

def webhook_url
  return nil unless store

  "#{store.url_or_custom_domain}/api/v3/webhooks/payments/#{prefixed_id}"
end