Sha256: 95aec1802a89c3e16e2cf8251c3aff55788ecfe71115fdd4f9f401fe318e6802

Contents?: true

Size: 1.18 KB

Versions: 54

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# This class is responsible for saving payment sources in the user's "wallet"
# for future use.  You can substitute your own class via
# `Spree::Config.add_payment_sources_to_wallet_class`.
class Spree::Wallet::AddPaymentSourcesToWallet
  def initialize(order)
    @order = order
  end

  # This is called after an order transistions to complete and should save the
  # order's payment source in the user's "wallet" for future use.
  #
  # @return [void]
  def add_to_wallet
    if !order.temporary_payment_source && order.user
      # select valid sources
      payments = order.payments.valid
      sources = payments.map(&:source).
        uniq.
        compact.
        select { |payment| payment.try(:reusable?) }

      # add valid sources to wallet and optionally set a default
      if sources.any?
        # arbitrarily sort by id for picking a default
        sources.sort_by(&:id).each do |source|
          order.user.wallet.add(source)
        end

        make_default
      end
    end
  end

  protected

  def make_default
    order.user.wallet.default_wallet_payment_source = order.user.wallet_payment_sources.last
  end

  private

  attr_reader :order
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
solidus_core-3.3.4 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.9 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.8 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.3.3 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.7 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.3.2 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.3.1 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.6 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.1.9 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.3.0 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.5 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.4 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.3 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.1.8 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.0.8 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.2 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.1 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.0 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.2.0.alpha app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-3.0.7 app/models/spree/wallet/add_payment_sources_to_wallet.rb