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 when an order uses a default WalletPaymentSource
but it's not the most recent one.

Fixes issue solidusio#4004 [2]

Now, #make_default uses WalletPaymentSources found or created by the
order, which means it will keep the default payment source if used.

[1] solidusio#2913
[2] solidusio#4004
  • Loading branch information
RyanofWoods committed Jan 27, 2023
1 parent 1561e13 commit db25c25
Show file tree
Hide file tree
Showing 2 changed files with 31 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,14 +4,41 @@

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) }

before do
# Ensure the payment is reusable, otherwise false positives occur
allow_any_instance_of(Spree::CreditCard).to receive(:reusable?).and_return(true)
end

it 'saves the payment source' do
expect { subject.add_to_wallet }.to change {
order.user.wallet.wallet_payment_sources.count
}.by(1)
end

context "when the default wallet payment source is used and a more 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 { subject.add_to_wallet }.to_not change {
order.user.wallet.wallet_payment_sources.count
}
end

it 'does not change the default wallet payment source' do
expect { subject.add_to_wallet }.to_not change {
user.wallet.default_wallet_payment_source
}
end
end
end
end

0 comments on commit db25c25

Please sign in to comment.