Sha256: 5fd0805ae250bb003606050172b1a9a2a77fb7ab5c2bc8011f7ba099da855a84

Contents?: true

Size: 1.13 KB

Versions: 43

Compression:

Stored size: 1.13 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 { |p| p.try(:reusable?) }

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

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

  private

  attr_reader :order
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.8.6 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.5 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.4 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.6.6 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.7.4 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.8.5 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.3 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.2 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.10.0.beta1 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.7.3 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.6.5 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.1 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.0 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.9.0.rc.1 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.8.4 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.8.3 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.7.2 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.6.4 app/models/spree/wallet/add_payment_sources_to_wallet.rb
solidus_core-2.8.2 app/models/spree/wallet/add_payment_sources_to_wallet.rb