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
Introduce methods to represent Cart amounts in cents (#12173)
Damian Legawiec1288e971c22e
diff --git a/api/app/serializers/spree/v2/storefront/cart_serializer.rb b/api/app/serializers/spree/v2/storefront/cart_serializer.rbindex 5354904013..115a32cb6e 100644--- a/api/app/serializers/spree/v2/storefront/cart_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/cart_serializer.rb@@ -10,7 +10,31 @@ module Spree:display_item_total, :display_ship_total, :display_adjustment_total, :display_tax_total,:promo_total, :display_promo_total, :item_count, :special_instructions, :display_total,:pre_tax_item_amount, :display_pre_tax_item_amount, :pre_tax_total, :display_pre_tax_total,- :shipment_state, :payment_state, :public_metadata+ :shipment_state, :payment_state, :public_metadata, :total_minus_store_credits, :display_total_minus_store_credits++ attribute :subtotal_cents do |cart|+ cart.display_item_total.amount_in_cents+ end++ attribute :ship_total_cents do |cart|+ cart.display_ship_total.amount_in_cents+ end++ attribute :store_credit_total_cents do |cart|+ cart.display_total_applied_store_credit.abs.amount_in_cents+ end++ attribute :tax_total_cents do |cart|+ cart.display_tax_total.amount_in_cents+ end++ attribute :total_cents do |cart|+ cart.display_total.amount_in_cents+ end++ attribute :total_minus_store_credits_cents do |cart|+ cart.display_total_minus_store_credits.amount_in_cents+ endhas_many :line_itemshas_many :variantsdiff --git a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb b/api/spec/requests/spree/api/v2/storefront/cart_spec.rbindex af555f1218..84c8875cf6 100644--- a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb@@ -114,6 +114,8 @@ describe 'API V2 Storefront Cart Spec', type: :request doexpect(json_response['data']).to have_attribute(:shipment_state).with_value(new_order.shipment_state)expect(json_response['data']).to have_attribute(:token).with_value(new_order.token)expect(json_response['data']).to have_attribute(:total).with_value(new_order.total.to_s)+ expect(json_response['data']).to have_attribute(:total_minus_store_credits).with_value(new_order.total_minus_store_credits.to_s)+ expect(json_response['data']).to have_attribute(:display_total_minus_store_credits).with_value(new_order.display_total_minus_store_credits.to_s)expect(json_response['data']).to have_attribute(:item_total).with_value(new_order.item_total.to_s)expect(json_response['data']).to have_attribute(:ship_total).with_value(new_order.ship_total.to_s)expect(json_response['data']).to have_attribute(:adjustment_total).with_value(new_order.adjustment_total.to_s)@@ -390,7 +392,7 @@ describe 'API V2 Storefront Cart Spec', type: :request doshared_examples 'destroying order' doit 'destroys the order' do- expect{ execute }.to change { Spree::Order.count }.by(-1)+ expect { execute }.to change { Spree::Order.count }.by(-1)endend@@ -601,10 +603,10 @@ describe 'API V2 Storefront Cart Spec', type: :request doinclude_context 'order with a physical line item'it_behaves_like 'showing the cart'- it 'will return included bill_address' do+ it 'returns included bill_address' doget '/api/v2/storefront/cart', params: bill_addr_params, headers: headersexpect(json_response[:included][0]).to have_id(order.bill_address_id.to_s)- expect(json_response[:included][0]).to have_type("address")+ expect(json_response[:included][0]).to have_type('address')expect(json_response[:included][0]).to have_attribute(:firstname).with_value(order.bill_address.firstname)expect(json_response[:included][0]).to have_attribute(:lastname).with_value(order.bill_address.lastname)expect(json_response[:included][0]).to have_attribute(:address1).with_value(order.bill_address.address1)@@ -620,14 +622,14 @@ describe 'API V2 Storefront Cart Spec', type: :request doexpect(json_response[:included][0]).to have_attribute(:public_metadata).with_value(order.bill_address.public_metadata)end- it 'will return included ship_address' do+ it 'returns included ship_address' doaddr = create(:address)order.update(ship_address: addr)get '/api/v2/storefront/cart', params: ship_addr_params, headers: headersexpect(json_response[:included][0]).to have_id(order.ship_address_id.to_s)- expect(json_response[:included][0]).to have_type("address")+ expect(json_response[:included][0]).to have_type('address')expect(json_response[:included][0]).to have_attribute(:firstname).with_value(order.ship_address.firstname)expect(json_response[:included][0]).to have_attribute(:lastname).with_value(order.ship_address.lastname)expect(json_response[:included][0]).to have_attribute(:address1).with_value(order.ship_address.address1)@@ -811,6 +813,7 @@ describe 'API V2 Storefront Cart Spec', type: :request docontext 'removes coupon code correctly' dolet!(:coupon_code) { promotion_with_order_adjustment.code }+before { execute }it_behaves_like 'returns 200 HTTP status'@@ -827,6 +830,7 @@ describe 'API V2 Storefront Cart Spec', type: :request docontext 'tries to remove an empty string' dolet!(:coupon_code) { '' }+before { execute }it 'changes the adjustment total to 0.0' do@@ -840,6 +844,7 @@ describe 'API V2 Storefront Cart Spec', type: :request docontext 'tries to remove nil' dolet(:coupon_code) { nil }+before { execute }it 'changes the adjustment total to 0.0' do@@ -869,6 +874,7 @@ describe 'API V2 Storefront Cart Spec', type: :request docontext 'tries to remove nil' dolet(:coupon_code) { nil }+before { execute }it_behaves_like 'returns 422 HTTP status'@@ -876,6 +882,7 @@ describe 'API V2 Storefront Cart Spec', type: :request docontext 'tries to remove an empty string' dolet(:coupon_code) { '' }+before { execute }it_behaves_like 'returns 422 HTTP status'@@ -926,17 +933,17 @@ describe 'API V2 Storefront Cart Spec', type: :request doit_behaves_like 'returns 200 HTTP status'it 'returns valid shipments JSON' do- [{shipping_method: shipping_method, shipping_rate: shipping_rate}, {shipping_method: shipping_method_2, shipping_rate: shipping_rate_2}].each do |shipping|- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:name).with_value(shipping[:shipping_method].name))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:shipping_method_id).with_value(shipping[:shipping_method].id))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:cost).with_value(shipping[:shipping_rate].cost.to_s))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:tax_amount).with_value(shipping[:shipping_rate].tax_amount.to_s))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:selected).with_value(shipping[:shipping_rate].selected))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:final_price).with_value(shipping[:shipping_rate].final_price.to_s))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:free).with_value(shipping[:shipping_rate].free?))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:display_final_price).with_value(shipping[:shipping_rate].display_final_price.to_s))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:display_cost).with_value(shipping[:shipping_rate].display_cost.to_s))- expect(json_response['data']).to include(have_type('shipping_rate').and have_attribute(:display_tax_amount).with_value(shipping[:shipping_rate].display_tax_amount.to_s))+ [{ shipping_method: shipping_method, shipping_rate: shipping_rate }, { shipping_method: shipping_method_2, shipping_rate: shipping_rate_2 }].each do |shipping|+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:name).with_value(shipping[:shipping_method].name)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:shipping_method_id).with_value(shipping[:shipping_method].id)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:cost).with_value(shipping[:shipping_rate].cost.to_s)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:tax_amount).with_value(shipping[:shipping_rate].tax_amount.to_s)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:selected).with_value(shipping[:shipping_rate].selected)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:final_price).with_value(shipping[:shipping_rate].final_price.to_s)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:free).with_value(shipping[:shipping_rate].free?)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:display_final_price).with_value(shipping[:shipping_rate].display_final_price.to_s)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:display_cost).with_value(shipping[:shipping_rate].display_cost.to_s)))+ expect(json_response['data']).to include(have_type('shipping_rate').and(have_attribute(:display_tax_amount).with_value(shipping[:shipping_rate].display_tax_amount.to_s)))endendend@@ -958,7 +965,7 @@ describe 'API V2 Storefront Cart Spec', type: :request dolet(:order) { create(:order, user: assigned_user, store: store, currency: currency) }before do- patch '/api/v2/storefront/cart/associate', params: { guest_order_token: guest_order_token}, headers: headers+ patch '/api/v2/storefront/cart/associate', params: { guest_order_token: guest_order_token }, headers: headersendcontext 'as a signed in user' do@@ -998,7 +1005,7 @@ describe 'API V2 Storefront Cart Spec', type: :request dodescribe 'cart#change_currency' dolet(:order) { create(:order, store: store, currency: currency) }let!(:line_item) { create(:line_item, order: order) }- let!(:price) { create(:price, currency: 'EUR', variant: order.line_items.first.variant)}+ let!(:price) { create(:price, currency: 'EUR', variant: order.line_items.first.variant) }before dopatch '/api/v2/storefront/cart/change_currency', params: { currency: currency, new_currency: new_currency, order_token: order.token }, headers: headersdiff --git a/core/app/models/spree/order/store_credit.rb b/core/app/models/spree/order/store_credit.rbindex 25cc503d89..c857c4a46c 100644--- a/core/app/models/spree/order/store_credit.rb+++ b/core/app/models/spree/order/store_credit.rb@@ -32,11 +32,15 @@ module Spreetotal - total_applicable_store_creditend+ def total_minus_store_credits+ total - total_applied_store_credit+ end+def total_applicable_store_creditif payment? || confirm? || complete?total_applied_store_creditelse- [total, (user.try(:total_available_store_credit) || 0.0)].min+ [total, user.try(:total_available_store_credit) || 0.0].minendend@@ -67,6 +71,10 @@ module Spreedef display_store_credit_remaining_after_captureSpree::Money.new(total_available_store_credit - total_applicable_store_credit, currency: currency)end++ def display_total_minus_store_credits+ Spree::Money.new(total_minus_store_credits, currency: currency)+ endendendend