Class: Spree::PaymentMethod
- Inherits:
-
Object
- Object
- Spree::PaymentMethod
- Includes:
- DisplayOn, LegacyMultiStoreSupport, Metadata, Metafields, Security::PaymentMethods, SingleStoreResource
- Defined in:
- app/models/spree/payment_method.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Check, StoreCredit, WebhookSignatureError
Constant Summary
Constants included from DisplayOn
Class Method Summary collapse
Instance Method Summary collapse
- #auto_capture? ⇒ Boolean
-
#available_for_order?(order) ⇒ Boolean
Custom PaymentMethod/Gateway can redefine this method to check method availability for concrete order.
- #available_for_store?(store) ⇒ Boolean
- #cancel(_response) ⇒ Object
-
#complete_payment_session(payment_session:, params: {}) ⇒ Object
Completes a payment session via the provider.
-
#complete_payment_setup_session(setup_session:, params: {}) ⇒ Object
Completes a payment setup session via the provider.
- #confirmation_required? ⇒ Boolean
-
#create_payment_session(order:, amount: nil, external_data: {}) ⇒ Object
Creates a payment session via the provider.
-
#create_payment_setup_session(customer:, external_data: {}) ⇒ Object
Creates a payment setup session via the provider for saving a payment method.
- #default_name ⇒ Object
- #method_type ⇒ Object
-
#parse_webhook_event(raw_body, headers) ⇒ Hash?
Parses an incoming webhook payload from the payment provider.
- #payment_icon_name ⇒ Object
- #payment_profiles_supported? ⇒ Boolean
-
#payment_session_class ⇒ Object
The class used for payment sessions with this payment method.
-
#payment_setup_session_class ⇒ Object
The class used for payment setup sessions with this payment method.
-
#payment_source_class ⇒ Object
The class that will process payments for this payment type, used for @payment.source e.g.
- #provider_class ⇒ Object
- #public_preferences ⇒ Object
-
#reusable_sources(_order) ⇒ Object
Custom gateways should redefine this method.
- #session_required? ⇒ Boolean
-
#setup_session_supported? ⇒ Boolean
Whether this payment method supports setup sessions (saving payment methods for future use).
- #show_in_admin? ⇒ Boolean
- #source_required? ⇒ Boolean
- #store_credit? ⇒ Boolean
- #supports?(_source) ⇒ Boolean
-
#update_payment_session(payment_session:, amount: nil, external_data: {}) ⇒ Object
Updates an existing payment session via the provider.
-
#webhook_url ⇒ String?
Returns the webhook URL for this payment method.
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 |
.providers ⇒ Object
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
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.
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
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
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).
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.
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
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.
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.
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_name ⇒ Object
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_type ⇒ Object
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.
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_name ⇒ Object
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
157 158 159 |
# File 'app/models/spree/payment_method.rb', line 157 def payment_profiles_supported? false end |
#payment_session_class ⇒ Object
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_class ⇒ Object
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_class ⇒ Object
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
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_class ⇒ Object
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_preferences ⇒ Object
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
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.
115 116 117 |
# File 'app/models/spree/payment_method.rb', line 115 def setup_session_supported? false end |
#show_in_admin? ⇒ Boolean
169 170 171 |
# File 'app/models/spree/payment_method.rb', line 169 def show_in_admin? true end |
#source_required? ⇒ Boolean
161 162 163 |
# File 'app/models/spree/payment_method.rb', line 161 def source_required? true end |
#store_credit? ⇒ Boolean
191 192 193 |
# File 'app/models/spree/payment_method.rb', line 191 def store_credit? self.class == Spree::PaymentMethod::StoreCredit end |
#supports?(_source) ⇒ Boolean
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.
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_url ⇒ String?
Returns the webhook URL for this payment method.
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 |