-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interfaces for persist_user_credit_card and assign_default_credit_card
Extract the logic for persist_user_credit_card and assign_default_credit_card into configurable classes.
- Loading branch information
1 parent
4e0f27d
commit 6238c5c
Showing
4 changed files
with
86 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Spree::Wallet::AddAfterOrderComplete | ||
def initialize(order) | ||
@order = order | ||
end | ||
|
||
# AddAfterOrderComplete is called after an order transitions to complete. It | ||
# is responsible for saving payment sources in the user's "wallet" for future | ||
# use. | ||
def add_to_wallet | ||
if !order.temporary_credit_card && | ||
order.user_id && | ||
order.valid_credit_cards.present? | ||
# arbitrarily pick the first one for the default | ||
default_cc = order.valid_credit_cards.first | ||
# TODO: target for refactoring -- why is order checkout responsible for the user -> credit_card relationship? | ||
default_cc.user_id = order.user_id | ||
default_cc.default = true | ||
default_cc.save | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :order | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Spree::Wallet::AddDefaultPayment | ||
def initialize(order) | ||
@order = order | ||
end | ||
|
||
# Build a payment to be added to an order prior to moving into the "payment" | ||
# state. | ||
# | ||
# @return [Payment] the unsaved payment to be added, or nil if none. | ||
def build_payment | ||
credit_card = order.user.try!(:default_credit_card) | ||
|
||
if credit_card.try!(:valid?) && order.payments.from_credit_card.count == 0 | ||
Spree::Payment.new( | ||
payment_method_id: credit_card.payment_method_id, | ||
source: credit_card, | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :order | ||
end |