Skip to content

Commit

Permalink
Add persist invalid, code originally in Spree
Browse files Browse the repository at this point in the history
For some unknown reason this is not always required, we are adding it
now to handle the capture of the payment after a redirect from Stripe
  • Loading branch information
luisramos0 committed Jul 25, 2020
1 parent ec0d06a commit 3fe5bd9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/spree/payment.rb
Expand Up @@ -43,13 +43,25 @@ class Payment < ActiveRecord::Base

after_initialize :build_source


scope :from_credit_card, -> { where(source_type: 'Spree::CreditCard') }
scope :with_state, ->(s) { where(state: s.to_s) }
scope :completed, -> { with_state('completed') }
scope :pending, -> { with_state('pending') }
scope :failed, -> { with_state('failed') }
scope :valid, -> { where('state NOT IN (?)', %w(failed invalid)) }

# When a payment fails, the order state machine rollbacks all transactions
# Here we ensure we always persist failed and invalid payments
after_rollback :persist_invalid

def persist_invalid
return unless ['failed', 'invalid'].include?(state)

state_will_change!
save
end

# order state machine (see http://github.com/pluginaweek/state_machine/tree/master for details)
state_machine initial: :checkout do
# With card payments, happens before purchase or authorization happens
Expand Down

0 comments on commit 3fe5bd9

Please sign in to comment.