Skip to content

Commit

Permalink
Merge pull request #4198 from RyanofWoods/fix/add-wallet-payments-sou…
Browse files Browse the repository at this point in the history
…rces-changing-reused-default-payment-source

Fix AddPaymentSourcesToWallet changing default when reused
  • Loading branch information
kennyadsl authored Feb 9, 2023
2 parents d7ef0f2 + 88aed7e commit 4718a33
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 4718a33

Please sign in to comment.