Class: Spree::Wallet::AddPaymentSourcesToWallet

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/wallet/add_payment_sources_to_wallet.rb

Overview

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`.

Instance Method Summary (collapse)

Constructor Details

- (AddPaymentSourcesToWallet) initialize(order)

Returns a new instance of AddPaymentSourcesToWallet



5
6
7
# File 'app/models/spree/wallet/add_payment_sources_to_wallet.rb', line 5

def initialize(order)
  @order = order
end

Instance Method Details

- (void) add_to_wallet

This method returns an undefined value.

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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/spree/wallet/add_payment_sources_to_wallet.rb', line 13

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