Class: Spree::Wallet

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

Overview

Interface for accessing and updating a user's active “wallet”. A Wallet is the active list of reusable payment sources that a user would like to choose from when placing orders.

A Wallet is composed of WalletPaymentSources. A WalletPaymentSource is a join table that links a PaymentSource (e.g. a CreditCard) to a User. One of a user's WalletPaymentSources may be the 'default' WalletPaymentSource.

Defined Under Namespace

Classes: AddPaymentSourcesToWallet, DefaultPaymentBuilder, Unauthorized

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Wallet) initialize(user)

Returns a new instance of Wallet



13
14
15
# File 'app/models/spree/wallet.rb', line 13

def initialize(user)
  @user = user
end

Instance Attribute Details

- (Object) user (readonly)

Returns the value of attribute user



11
12
13
# File 'app/models/spree/wallet.rb', line 11

def user
  @user
end

Instance Method Details

- (WalletPaymentSource) add(payment_source)

Add a PaymentSource to the wallet.

Parameters:

  • payment_source (PaymentSource)

    The payment source to add to the wallet

Returns:



28
29
30
# File 'app/models/spree/wallet.rb', line 28

def add(payment_source)
  user.wallet_payment_sources.find_or_create_by!(payment_source: payment_source)
end

- (WalletPaymentSource) default_wallet_payment_source

Find the default WalletPaymentSource for this wallet, if any.

Returns:



51
52
53
# File 'app/models/spree/wallet.rb', line 51

def default_wallet_payment_source
  user.wallet_payment_sources.find_by(default: true)
end

- (void) default_wallet_payment_source=(wallet_payment_source)

This method returns an undefined value.

Change the default WalletPaymentSource for this wallet.

Parameters:

  • source (WalletPaymentSource)

    The payment source to set as the default. It must be in the wallet already. Pass nil to clear the default.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/spree/wallet.rb', line 59

def default_wallet_payment_source=(wallet_payment_source)
  if wallet_payment_source && !find(wallet_payment_source.id)
    raise Unauthorized, "wallet_payment_source #{wallet_payment_source.id} does not belong to wallet of user #{user.id}"
  end

  wallet_payment_source.transaction do
    # Unset old default
    default_wallet_payment_source.try!(:update!, default: false)
    # Set new default
    wallet_payment_source.try!(:update!, default: true)
  end
end

- (WalletPaymentSource) find(wallet_payment_source_id)

Find a WalletPaymentSource in the wallet by id.

Parameters:

  • wallet_payment_source_id (Integer)

    The id of the WalletPaymentSource.

Returns:



45
46
47
# File 'app/models/spree/wallet.rb', line 45

def find(wallet_payment_source_id)
  user.wallet_payment_sources.find_by(id: wallet_payment_source_id)
end

- (WalletPaymentSource) remove(payment_source)

Remove a PaymentSource from the wallet.

Parameters:

  • payment_source (PaymentSource)

    The payment source to remove from the wallet

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    if the source is not in the wallet.



37
38
39
# File 'app/models/spree/wallet.rb', line 37

def remove(payment_source)
  user.wallet_payment_sources.find_by!(payment_source: payment_source).destroy!
end

- (Array<WalletPaymentSource>) wallet_payment_sources

Returns an array of the WalletPaymentSources in this wallet.

Returns:



20
21
22
# File 'app/models/spree/wallet.rb', line 20

def wallet_payment_sources
  user.wallet_payment_sources.to_a
end