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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 currencyreturn 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].minstore = order.storediff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 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 ordergift_card_using_store_credit_error: You can't apply the Gift Card after you applied the store credit.gift_cards: Gift Cardsdiff --git a/core/spec/services/spree/gift_cards/apply_spec.rb b/core/spec/services/spree/gift_cards/apply_spec.rbindex 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 dosubject { 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 doendit '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_successexpect(order.reload.gift_card).to eq(gift_card)@@ -49,14 +50,58 @@ RSpec.describe Spree::GiftCards::Apply doendit '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_failureexpect(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_nilexpect(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)+ endendend@@ -64,12 +109,12 @@ RSpec.describe Spree::GiftCards::Apply dobefore { 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_failureexpect(subject.value).to eq(:gift_card_no_amount_remaining)- expect(order.reload.gift_card).to eq(nil)+ expect(order.reload.gift_card).to be_nilendendend