Class: Spree::Wallet
- Inherits:
-
Object
- Object
- Spree::Wallet
- 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
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#add(payment_source) ⇒ WalletPaymentSource
Add a PaymentSource to the wallet.
-
#default_wallet_payment_source ⇒ WalletPaymentSource
Find the default WalletPaymentSource for this wallet, if any.
-
#default_wallet_payment_source=(wallet_payment_source) ⇒ void
Change the default WalletPaymentSource for this wallet.
-
#find(wallet_payment_source_id) ⇒ WalletPaymentSource
Find a WalletPaymentSource in the wallet by id.
-
#initialize(user) ⇒ Wallet
constructor
A new instance of Wallet.
-
#remove(payment_source) ⇒ WalletPaymentSource
Remove a PaymentSource from the wallet.
-
#wallet_payment_sources ⇒ Array<WalletPaymentSource>
Returns an array of the WalletPaymentSources in this wallet.
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
#user ⇒ Object (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.
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:) end |
#default_wallet_payment_source ⇒ WalletPaymentSource
Find the default WalletPaymentSource for this wallet, if any.
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.
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.
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.
39 40 41 |
# File 'app/models/spree/wallet.rb', line 39 def remove(payment_source) user.wallet_payment_sources.find_by!(payment_source:).destroy! end |
#wallet_payment_sources ⇒ Array<WalletPaymentSource>
Returns an array of the WalletPaymentSources in this wallet.
22 23 24 |
# File 'app/models/spree/wallet.rb', line 22 def wallet_payment_sources user.wallet_payment_sources.to_a end |