Module: Spree::Payment::Processing

Extended by:
ActiveSupport::Concern
Includes:
InstrumentsGatewayCalls
Included in:
Spree::Payment
Defined in:
app/models/spree/payment/processing.rb

Overview

Gateway mechanics shared by the payments workflows and the payment methods: option serialization, response bookkeeping, error translation and instrumentation. The flows themselves — capture, void, authorize/purchase — live in app/workflows/spree/payments; the deprecated verb methods below delegate there until 7.0.

Those five carry a full major's window rather than the usual one release: they have been the public way to move money since Spree 1, every payment extension and host override calls them, and the replacement is a different shape (a workflow result, not a boolean), so migrating is real work rather than a rename.

Instance Method Summary collapse

Instance Method Details

#authorize! ⇒ Object

Deprecated.

Call Spree.payment_process_workflow — removed in 7.0.



30
31
32
33
# File 'app/models/spree/payment/processing.rb', line 30

def authorize!
  Spree::Deprecation.warn('Spree::Payment#authorize! is deprecated and will be removed in Spree 7.0. Call Spree.payment_process_workflow instead.')
  run_payment_workflow(Spree.payment_process_workflow, payment: self, action: :authorize)
end

#cancel!(refund: true) ⇒ Object

Settles the payment for a canceled order: releases an uncaptured authorization, and refunds a captured one when refund allows it — payment_method.cancel routes refunds through Refunds::Create. An adapter that can void the payment outright does so either way.

A captured charge the gateway did not void keeps its completed status: the money was either refunded, which the refund records, or kept.

A provider that answers requested? settles later by webhook: an uncaptured payment waits on its void, and a completed one stays completed while the refund the gateway requested carries the outcome. So a gateway answers requested? for a completed payment only after recording that refund (through Refunds::Create), or the outcome finds nothing to settle.

Parameters:

  • refund (Boolean) (defaults to: true) —

    whether money already captured may be returned



101
102
103
104
105
106
107
108
109
110
# File 'app/models/spree/payment/processing.rb', line 101

def cancel!(refund: true)
  voids = payment_method.cancel_voids?(self)
  response = instrument_gateway_call(:cancel, payment_method) do
    payment_method.cancel(response_code, self, refund: refund)
  end
  return handle_response(response, voids ? :void : :keep) unless response.success? && response.requested?
  return true if completed?

  request_void!
end

#capture!(amount = nil) ⇒ Object

Deprecated.

Call Spree.payment_capture_workflow — removed in 7.0.



42
43
44
45
# File 'app/models/spree/payment/processing.rb', line 42

def capture!(amount = nil)
  Spree::Deprecation.warn('Spree::Payment#capture! is deprecated and will be removed in Spree 7.0. Call Spree.payment_capture_workflow instead.')
  run_payment_workflow(Spree.payment_capture_workflow, payment: self, amount: amount)
end

#confirm!(captured: nil, capture_reference: nil) ⇒ Object

Confirms a payment already authorized or captured on the gateway side (SDK / Drop-in / payment session flows) — the local status move only, no gateway call.

Deliberately not a workflow, unlike its siblings: its only caller is PaymentSession#settle_payment!, which runs inside owner.with_lock — and a workflow cannot run there (external_step refuses a workflow-opened transaction, halt! raises inside any transaction). The lock is what makes settlement exactly-once when the webhook races the customer's synchronous return, so it stays and this stays pure database work. There is no gateway call to push behind an external_step and no compensation to run, so nothing about it earns the workflow tier.

Parameters:

  • captured (Boolean, nil) (defaults to: nil) —

    whether the gateway reports the funds as captured. Callers who know the gateway state pass it; nil falls back to when the payment method charges.

  • capture_reference (String, nil) (defaults to: nil) —

    the provider's id for the capture, recorded so a late capture failure can find it



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/spree/payment/processing.rb', line 72

def confirm!(captured: nil, capture_reference: nil)
  captured = payment_method&.capture_at_checkout? if captured.nil?

  started_processing! if checkout?

  if captured && can_complete?
    complete!
    capture_events.create!(amount: amount, status: 'completed', transaction_id: capture_reference)
  elsif can_pend?
    pend!
  end
end

#gateway_error(error) ⇒ Object

Raises:



158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/spree/payment/processing.rb', line 158

def gateway_error(error)
  text = if error.is_a? Spree::PaymentResponse
           error.params['message'] || error.params['response_reason_text'] || error.message
         elsif error.is_a? Spree::PaymentConnectionError
           I18n.t('spree.unable_to_connect_to_gateway')
         else
           error.to_s
         end
  Rails.logger.error(I18n.t('spree.gateway_error'))
  Rails.logger.error("  #{error.to_yaml}")
  raise Core::GatewayError, text
end

#gateway_options ⇒ Object



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

def gateway_options
  owner.reload
  gateway_options_class.new(self).to_hash
end

#handle_response(response, success_state, _failure_state = :failure) ⇒ Object

Records a gateway response on the payment: bookkeeping (response code, AVS/CVV results) plus the status write. A failed response writes failed non-raising — gateway_error below is the exception this path reports, and a validation problem must not preempt it.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/spree/payment/processing.rb', line 121

def handle_response(response, success_state, _failure_state = :failure)
  if response.success?
    unless response.authorization.nil?
      self.response_code = response.authorization
      self.avs_response = response.avs_result['code']

      if response.cvv_result
        self.cvv_response_code = response.cvv_result['code']
        self.cvv_response_message = response.cvv_result['message']
      end
    end

    # The bookkeeping assigned above is in memory; void! writes status
    # through a conditional update, so persist the rest first.
    save! if changed?

    case success_state
    when :complete then complete!
    when :pend then pend!
    when :void then void!
    when :keep then nil
    else raise ArgumentError, "Unknown payment success state: #{success_state.inspect}"
    end
  else
    self.status = 'failed'
    save
    gateway_error(response)
  end
end

#process! ⇒ Object

Deprecated.

Call Spree.payment_process_workflow — removed in 7.0.



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

def process!
  Spree::Deprecation.warn('Spree::Payment#process! is deprecated and will be removed in Spree 7.0. Call Spree.payment_process_workflow instead.')
  run_payment_workflow(Spree.payment_process_workflow, payment: self)
end

#protect_from_connection_error ⇒ Object



151
152
153
154
155
156
# File 'app/models/spree/payment/processing.rb', line 151

def protect_from_connection_error
  yield
rescue Spree::PaymentConnectionError => e
  failure!
  gateway_error(e)
end

#purchase! ⇒ Object

Deprecated.

Call Spree.payment_process_workflow — removed in 7.0.



36
37
38
39
# File 'app/models/spree/payment/processing.rb', line 36

def purchase!
  Spree::Deprecation.warn('Spree::Payment#purchase! is deprecated and will be removed in Spree 7.0. Call Spree.payment_process_workflow instead.')
  run_payment_workflow(Spree.payment_process_workflow, payment: self, action: :purchase)
end

#token_based? ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'app/models/spree/payment/processing.rb', line 171

def token_based?
  source.gateway_customer_profile_id.present? || source.gateway_payment_profile_id.present?
end

#void_transaction! ⇒ Object

Deprecated.

Call Spree.payment_void_workflow — removed in 7.0.



48
49
50
51
# File 'app/models/spree/payment/processing.rb', line 48

def void_transaction!
  Spree::Deprecation.warn('Spree::Payment#void_transaction! is deprecated and will be removed in Spree 7.0. Call Spree.payment_void_workflow instead.')
  run_payment_workflow(Spree.payment_void_workflow, payment: self)
end