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

#initialize(user) ⇒ Wallet

Returns a new instance of Wallet.



15
16
17
# File 'app/models/spree/wallet.rb', line 15

def initialize(user)
  @user = user
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#add(payment_source) ⇒ WalletPaymentSource

Add a PaymentSource to the wallet.

Parameters:

  • payment_source (PaymentSource)

    The payment source to add to the wallet

Returns:



30
31
32
# File 'app/models/spree/wallet.rb', line 30

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

#default_wallet_payment_sourceWalletPaymentSource

Find the default WalletPaymentSource for this wallet, if any.

Returns:



53
54
55
# File 'app/models/spree/wallet.rb', line 53

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

#default_wallet_payment_source=(wallet_payment_source) ⇒ void

This method returns an undefined value.

Change the default WalletPaymentSource for this wallet.

Parameters:

  • wallet_payment_source (WalletPaymentSource)

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/spree/wallet.rb', line 61

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

  # Do not update the payment source if the passed source is already default
  if default_wallet_payment_source == wallet_payment_source
    return
  end

  Spree::WalletPaymentSource.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

#find(wallet_payment_source_id) ⇒ WalletPaymentSource

Find a WalletPaymentSource in the wallet by id.

Parameters:

  • wallet_payment_source_id (Integer)

    The id of the WalletPaymentSource.

Returns:



47
48
49
# File 'app/models/spree/wallet.rb', line 47

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

#remove(payment_source) ⇒ WalletPaymentSource

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.



39
40
41
# File 'app/models/spree/wallet.rb', line 39

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

#wallet_payment_sourcesArray<WalletPaymentSource>

Returns an array of the WalletPaymentSources in this wallet.

Returns:



22
23
24
# File 'app/models/spree/wallet.rb', line 22

def wallet_payment_sources
  user.wallet_payment_sources.to_a
end