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
Fixed: properly marks the gift card `partially_redeemed` when there i… (#12900)
Damian Legawiec00c51cc40530
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 0a3ff511a2..9584300348 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -953,7 +953,7 @@ module Spreeif gift_card.present?recalculate_gift_cardelsif using_store_credit?- Spree::Checkout::AddStoreCredit.call(order: self)+ Spree::Dependencies.checkout_add_store_credit_service.constantize.call(order: self)endendenddiff --git a/core/app/models/spree/order/gift_card.rb b/core/app/models/spree/order/gift_card.rbindex 21497ffff4..379612cefd 100644--- a/core/app/models/spree/order/gift_card.rb+++ b/core/app/models/spree/order/gift_card.rb@@ -42,7 +42,9 @@ module Spreeenddef redeem_gift_card- gift_card.redeem! if gift_card.present?+ return unless gift_card.present?++ Spree::Dependencies.gift_card_redeem_service.constantize.call(gift_card: gift_card)endendenddiff --git a/core/app/services/spree/gift_cards/redeem.rb b/core/app/services/spree/gift_cards/redeem.rbnew file mode 100644index 0000000000..27c795383b--- /dev/null+++ b/core/app/services/spree/gift_cards/redeem.rb@@ -0,0 +1,17 @@+module Spree+ module GiftCards+ class Redeem+ prepend Spree::ServiceModule::Base++ def call(gift_card:)+ if gift_card.amount_remaining.zero?+ gift_card.redeem!+ else+ gift_card.partial_redeem!+ end++ success(gift_card)+ end+ end+ end+enddiff --git a/core/lib/spree/core/dependencies.rb b/core/lib/spree/core/dependencies.rbindex 37cbd2b326..2b029466d8 100644--- a/core/lib/spree/core/dependencies.rb+++ b/core/lib/spree/core/dependencies.rb@@ -36,6 +36,7 @@ module Spree# gift cardsgift_card_apply_service: 'Spree::GiftCards::Apply',gift_card_remove_service: 'Spree::GiftCards::Remove',+ gift_card_redeem_service: 'Spree::GiftCards::Redeem',# orderorder_approve_service: 'Spree::Orders::Approve',diff --git a/core/spec/models/spree/order/checkout_spec.rb b/core/spec/models/spree/order/checkout_spec.rbindex 709a228750..bd1182b0b4 100644--- a/core/spec/models/spree/order/checkout_spec.rb+++ b/core/spec/models/spree/order/checkout_spec.rb@@ -495,6 +495,34 @@ describe Spree::Order, type: :model doexpect(order.user.reload.default_credit_card).to be_nilend+ context 'when gift card is present' do+ let(:gift_card) { create(:gift_card, amount: order.total, store: store) }+ let(:order) { create(:order_with_line_items, store: store, state: 'payment') }++ before do+ order.apply_gift_card(gift_card)+ end++ it 'redeems the gift card' do+ expect(gift_card.redeemed_at).to be_nil+ expect { order.next! }.to change { gift_card.reload.state }.from('active').to('redeemed')+ expect(gift_card.amount_used).to eq(order.total)+ expect(gift_card.amount_remaining).to eq(0)+ expect(gift_card.redeemed_at).to be_present+ end++ context 'when gift card has amount bigger than order total' do+ let(:gift_card) { create(:gift_card, amount: order.total + 1, store: store) }++ it 'partially redeems the gift card' do+ expect { order.next! }.to change { gift_card.reload.state }.from('active').to('partially_redeemed')+ expect(gift_card.amount_used).to eq(order.total)+ expect(gift_card.amount_remaining).to eq(1)+ expect(gift_card.redeemed_at).to be_nil+ end+ end+ end+context 'when user is not present' dobefore doorder.user = nildiff --git a/core/spec/services/spree/gift_cards/redeem_spec.rb b/core/spec/services/spree/gift_cards/redeem_spec.rbnew file mode 100644index 0000000000..e3d5fccf43--- /dev/null+++ b/core/spec/services/spree/gift_cards/redeem_spec.rb@@ -0,0 +1,42 @@+require 'spec_helper'++RSpec.describe Spree::GiftCards::Redeem do+ subject { described_class.call(gift_card: gift_card) }++ let(:store) { Spree::Store.default }+ let(:order) { create(:order, store: store) }+ let(:gift_card) { create(:gift_card, amount: 50, store: store) }++ before do+ order.update_column(:total, 30)+ order.update_column(:shipment_total, 10)+ end++ context 'when the gift card has no amount remaining' do+ before { gift_card.update!(amount_used: gift_card.amount) }++ it 'calls redeem! on the gift card' do+ expect(gift_card).to receive(:redeem!)+ subject+ end++ it 'returns success with the gift card' do+ expect(subject).to be_success+ expect(subject.value).to eq(gift_card)+ end+ end++ context 'when the gift card has amount remaining' do+ before { gift_card.update!(amount_used: 20) }++ it 'calls partial_redeem! on the gift card' do+ expect(gift_card).to receive(:partial_redeem!)+ subject+ end++ it 'returns success with the gift card' do+ expect(subject).to be_success+ expect(subject.value).to eq(gift_card)+ end+ end+end