Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

fix[v-3311]: gift cards - add user validation to Apply class (#13505)

Szymon Chorobski034b63b2fd90
diff --git a/core/app/services/spree/gift_cards/apply.rb b/core/app/services/spree/gift_cards/apply.rb
index 393814ee6c..1d048ceba6 100644
--- a/core/app/services/spree/gift_cards/apply.rb
+++ b/core/app/services/spree/gift_cards/apply.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true
+
# Applies a gift card to an order
# under the hood it creates a store credit payment record and updates the gift card amount used
# @param gift_card [Spree::GiftCard] the gift card to apply
@@ -16,6 +17,14 @@ module Spree
# we shouldn't allow a gift card to be applied to an order with a different currency
return failure(:gift_card_mismatched_currency) if gift_card.currency != order.currency
+ # gift card requires logged user
+ if gift_card.user.present?
+ # user must be logged in
+ return failure(:gift_card_customer_not_logged_in) if order.user.blank?
+ # user must be the same as the gift card user
+ return failure(:gift_card_mismatched_customer) if gift_card.user != order.user
+ end
+
amount = [gift_card.amount_remaining, order.total].min
store = order.store
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index cc2a548cad..91121500ae 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1190,7 +1190,10 @@ en:
gift_card_already_redeemed: The Gift Card has already been redeemed.
gift_card_applied: The Gift Card was successfully applied to your order!
gift_card_batch: Gift Card Batch
+ gift_card_customer_not_logged_in: Gift Card cannot be applied with guest user.
gift_card_expired: The Gift Card has expired.
+ gift_card_mismatched_currency: Gift Card cannot be applied with current currency.
+ gift_card_mismatched_customer: This gift card is associated with another user.
gift_card_removed: The Gift Card was successfully removed from your order
gift_card_using_store_credit_error: You can't apply the Gift Card after you applied the store credit.
gift_cards: Gift Cards
diff --git a/core/spec/services/spree/gift_cards/apply_spec.rb b/core/spec/services/spree/gift_cards/apply_spec.rb
index fa9a0d3004..e64a5f43df 100644
--- a/core/spec/services/spree/gift_cards/apply_spec.rb
+++ b/core/spec/services/spree/gift_cards/apply_spec.rb
@@ -4,10 +4,11 @@ RSpec.describe Spree::GiftCards::Apply do
subject { described_class.call(gift_card: gift_card, order: order) }
let(:store) { Spree::Store.default }
- let(:order) { create(:order, store: store) }
-
- let(:gift_card) { create(:gift_card, amount: 50, store: store) }
+ let(:order) { create(:order, store: store, user: order_user) }
+ let(:order_user) { create(:user) }
+ let(:gift_card) { create(:gift_card, amount: 50, store: store, user: gift_card_user) }
+ let(:gift_card_user) { nil }
let(:store_credit_payment) { order.payments.store_credits.last }
let(:store_credit) { store_credit_payment.source }
@@ -17,7 +18,7 @@ RSpec.describe Spree::GiftCards::Apply do
end
it 'applies the gift card to an order' do
- expect { subject }.to change { Spree::StoreCredit.count }.by(1)
+ expect { subject }.to change(Spree::StoreCredit, :count).by(1)
expect(subject).to be_success
expect(order.reload.gift_card).to eq(gift_card)
@@ -49,14 +50,58 @@ RSpec.describe Spree::GiftCards::Apply do
end
it 'responds with an error' do
- expect { subject }.to_not change(Spree::StoreCredit, :count)
+ expect { subject }.not_to change(Spree::StoreCredit, :count)
expect(subject).to be_failure
expect(subject.value).to eq(:gift_card_using_store_credit_error)
- expect(order.reload.gift_card).to eq(nil)
+ expect(order.reload.gift_card).to be_nil
expect(order.total_applied_store_credit).to eq(10)
- expect(order.payments.store_credits.last.source.originator).to eq(nil)
+ expect(order.payments.store_credits.last.source.originator).to be_nil
+ end
+ end
+
+ context 'when the gift card has a different currency' do
+ let(:gift_card) { create(:gift_card, amount: 50, store: store, user: gift_card_user, currency: 'USD') }
+ let(:order) { create(:order, store: store, user: order_user, currency: 'EUR') }
+
+ it 'responds with an error' do
+ expect(subject).to be_failure
+ expect(subject.value).to eq(:gift_card_mismatched_currency)
+ end
+ end
+
+ context 'when the gift card is assigned to a user' do
+ let(:gift_card_user) { create(:user) }
+ let(:order_user) { nil }
+
+ context 'with valid user' do
+ let(:order_user) { gift_card_user }
+
+ it 'applies the gift card to the order' do
+ expect(subject).to be_success
+ end
+
+ it 'calls update_with_updater!' do
+ expect(order).to receive(:update_with_updater!)
+ subject
+ end
+ end
+
+ context 'with guest order' do
+ it 'responds with an error' do
+ expect(subject).to be_failure
+ expect(subject.value).to eq(:gift_card_customer_not_logged_in)
+ end
+ end
+
+ context 'with another user order' do
+ let(:order_user) { create(:user) }
+
+ it 'responds with an error' do
+ expect(subject).to be_failure
+ expect(subject.value).to eq(:gift_card_mismatched_customer)
+ end
end
end
@@ -64,12 +109,12 @@ RSpec.describe Spree::GiftCards::Apply do
before { gift_card.update!(amount_used: gift_card.amount) }
it 'responds with an error' do
- expect { subject }.to_not change(Spree::StoreCredit, :count)
+ expect { subject }.not_to change(Spree::StoreCredit, :count)
expect(subject).to be_failure
expect(subject.value).to eq(:gift_card_no_amount_remaining)
- expect(order.reload.gift_card).to eq(nil)
+ expect(order.reload.gift_card).to be_nil
end
end
end