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.

Removed `validate_gift_card_data` action as it's not finalized yet

Damian Legawiec23b6a5140bff
diff --git a/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb b/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
index a3d4200ecc..987cf02bfb 100644
--- a/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
@@ -111,16 +111,6 @@ module Spree
end
end
- def validate_gift_card_data
- if params[:gift_card_code].present? && spree_current_order.gift_card.nil?
- render json: { error: Spree.t('api.v2.gift_card.not_found') }, status: 422
- elsif params[:gift_card_amount].to_d != spree_current_order.gift_card_total.to_d
- render json: { error: Spree.t('api.v2.gift_card.amount_not_valid') }, status: 422
- else
- render json: { success: true }, status: 200
- end
- end
-
private
def resource_serializer
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 27e46051c9..4309960131 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -32,7 +32,6 @@ Spree::Core::Engine.add_routes do
get :shipping_rates
patch :select_shipping_method
post :validate_order_for_payment
- post :validate_gift_card_data
end
resource :account, controller: :account, only: %i[show create update]
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index 4adefba1a6..af9a5c796d 100644
--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
@@ -1118,52 +1118,4 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
end
end
end
-
- describe 'checkout#validate_gift_card_data' do
- let(:order) { create(:order) }
-
- let(:execute) { post '/api/v2/storefront/checkout/validate_gift_card_data', params: params, headers: headers }
-
- context 'when order is not present' do
- let(:params) { { order_token: '' } }
-
- it 'returns a 404 status' do
- execute
- expect(response).to have_http_status(:not_found)
- end
- end
-
- context 'when order is present' do
- context 'and gift card code is not present' do
- let(:params) { { order_token: order.token, gift_card_code: '' } }
-
- it 'returns a 200 status' do
- execute
- expect(response).to have_http_status(:ok)
- end
- end
-
- context 'and gift_card_code param present but order has no gift card' do
- let(:params) { { order_token: order.token, gift_card_code: '123456' } }
-
- it 'returns a 422 status' do
- execute
- expect(response).to have_http_status(:unprocessable_entity)
- expect(json_response['error']).to eq(Spree.t('api.v2.gift_card.not_found'))
- end
- end
-
- context 'and gift_card_amount param present but order gift card total is different' do
- let(:params) { { order_token: order.token, gift_card_amount: 100 } }
- let(:gift_card) { create(:gift_card, amount: 200) }
- let(:order) { create(:order, gift_card: gift_card) }
-
- it 'returns a 422 status' do
- execute
- expect(response).to have_http_status(:unprocessable_entity)
- expect(json_response['error']).to eq(Spree.t('api.v2.gift_card.amount_not_valid'))
- end
- end
- end
- end
end
diff --git a/core/app/models/spree/order/store_credit.rb b/core/app/models/spree/order/store_credit.rb
index c267ed6832..f8edd74fe9 100644
--- a/core/app/models/spree/order/store_credit.rb
+++ b/core/app/models/spree/order/store_credit.rb
@@ -14,12 +14,19 @@ module Spree
end
alias covered_by_store_credit covered_by_store_credit?
+ # Returns the total amount of store credits available to the user associated with the order.
+ # Returns only store credit for this store and same currency as order
+ #
+ # @return [BigDecimal] The total amount of store credits available to the user associated with the order.
def total_available_store_credit
return 0.0 unless user
user.total_available_store_credit(currency, store)
end
+ # Returns the available store credits for the user associated with the order.
+ #
+ # @return [Array<Spree::StoreCredit>] The available store credits for the user associated with the order.
def available_store_credits
return Spree::StoreCredit.none if user.nil?
@@ -32,10 +39,16 @@ module Spree
total_available_store_credit > 0
end
+ # Returns the total amount of the order minus the total amount of store credits applied to the order.
+ #
+ # @return [BigDecimal] The total amount of the order minus the total amount of store credits applied to the order.
def order_total_after_store_credit
total - total_applicable_store_credit
end
+ # Returns the total amount of the order minus the total amount of store credits applied to the order.
+ #
+ # @return [BigDecimal] The total amount of the order minus the total amount of store credits applied to the order.
def total_minus_store_credits
total - total_applied_store_credit
end
@@ -48,10 +61,16 @@ module Spree
end
end
+ # Returns the total amount of store credits applied to the order.
+ #
+ # @return [BigDecimal] The total amount of store credits applied to the order.
def total_applied_store_credit
payments.store_credits.valid.sum(:amount)
end
+ # Returns true if the order is using store credit.
+ #
+ # @return [Boolean] True if the order is using store credit, false otherwise.
def using_store_credit?
total_applied_store_credit > 0
end
diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rb
index 11c26bf647..8eacc5f578 100644
--- a/core/spec/models/spree/order_merger_spec.rb
+++ b/core/spec/models/spree/order_merger_spec.rb
@@ -166,7 +166,12 @@ module Spree
it 'merges the gift card' do
subject.merge!(order_2)
- expect(order_1.reload.gift_card).to eq(gift_card)
+
+ order_1.reload.tap do |merged|
+ expect(merged.gift_card).to eq(gift_card)
+ expect(merged.payments.store_credits.count).to eq(1)
+ expect(merged.total_applied_store_credit).to eq(20)
+ end
end
end
end