Skip to content

Commit

Permalink
Fix AddPaymentSourcesToWallet changing default when reused
Browse files Browse the repository at this point in the history
PR solidusio#2913 [1] changed how the new default WalletPaymentSource was set.
Before:
- using the last one found/created by the Order's PaymentSources

After:
- using the user's last WalletPaymentSource.

This is problematic in the scenario where the user uses their default
WalletPaymentSource for an order, but their default source is not their
most recent WalletPaymentSource.

Fixes issue solidusio#4004 [2]

Now, #make_default uses WalletPaymentSources found or created by the
order, it means it will keep the default payment source if used. This
is because wallet#add finds or creates.

[1] solidusio#2913
[2] solidusio#4004
  • Loading branch information
RyanofWoods committed Nov 1, 2022
1 parent 6dae635 commit 7693f12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions core/app/models/spree/wallet/add_payment_sources_to_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ def add_to_wallet
# add valid sources to wallet and optionally set a default
if sources.any?
# arbitrarily sort by id for picking a default
sources.sort_by(&:id).each do |source|
order_wallet_payment_sources = sources.sort_by(&:id).map do |source|
order.user.wallet.add(source)
end

make_default
make_default(order_wallet_payment_sources)
end
end
end

protected

def make_default
order.user.wallet.default_wallet_payment_source = order.user.wallet_payment_sources.last
def make_default(sources)
order.user.wallet.default_wallet_payment_source = sources.last
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

RSpec.describe Spree::Wallet::AddPaymentSourcesToWallet, type: :model do
let(:order) { create(:order_ready_to_complete) }
let(:user) { order.user }

describe '#add_to_wallet' do
subject { described_class.new(order) }
Expand All @@ -13,5 +14,28 @@
order.user.wallet.wallet_payment_sources.count
}.by(1)
end

context "when the default wallet payment source is used and more a recent wallet payment exists" do
before do
credit_card_one = user.wallet.add(create(:credit_card, user: user))
user.wallet.add(create(:credit_card, user: user))
user.wallet.default_wallet_payment_source = credit_card_one # must be the first created card
order.payments.first.update!(source: credit_card_one.payment_source)
end

it 'does not make a new wallet payment source' do
expect(user.wallet).to receive(:add).and_call_original
expect { subject.add_to_wallet }.to_not change {
order.user.wallet.wallet_payment_sources.count
}
end

it 'does change the default wallet payment source' do
expect(user.wallet).to receive(:add).and_call_original
expect { subject.add_to_wallet }.to_not change {
user.wallet.default_wallet_payment_source
}
end
end
end
end

0 comments on commit 7693f12

Please sign in to comment.