Skip to content

Commit

Permalink
Marketplace: Collect Taxes for Products with a TaxRate on `Chec…
Browse files Browse the repository at this point in the history
…kout`

- #1137

Whoooo! `TaxRate` gets calculated! And collected!

Co-authored-by: Ana Ulin <anaulin@users.noreply.github.com>
Co-authored-by: Dalton Pruitt <daltonrpruitt@users.noreply.github.com>
Co-authored-by: Kelly Hong <KellyAH@users.noreply.github.com>
  • Loading branch information
4 people committed Mar 9, 2023
1 parent 074217c commit 3113dfc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
10 changes: 8 additions & 2 deletions app/furniture/marketplace/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Cart < Record

def product_total
cart_products.sum(0) do |cart_product|
cart_product.product.price * cart_product.quantity
cart_product.price_total
end
end

Expand All @@ -34,8 +34,14 @@ def delivery_fee
0
end

def tax_total
cart_products.sum(0) do |cart_product|
cart_product.tax_amount
end
end

def price_total
product_total + delivery_fee
product_total + delivery_fee + tax_total
end
end
end
12 changes: 11 additions & 1 deletion app/furniture/marketplace/cart_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ class CartProduct < Record
delegate :shopper, to: :cart

belongs_to :product, inverse_of: :cart_products
delegate :name, :description, :marketplace, :price, :price_cents, to: :product
delegate :name, :description, :marketplace, :price, :price_cents, :tax_rates, to: :product

validates_uniqueness_of :product, scope: :cart_id
validate :editable_cart

attribute :quantity, :integer, default: 0

def tax_amount
price_total * (tax_rates.sum(0) do |tax_rate|
tax_rate.tax_rate
end / 100)
end

def price_total
product.price * quantity
end

private

def editable_cart
Expand Down
3 changes: 3 additions & 0 deletions app/furniture/marketplace/carts/_total.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<span>
Products:
<%= humanized_money_with_symbol(cart.product_total) %></span>
<span>
Taxes:
<%= humanized_money_with_symbol(cart.tax_total) %></span>
<%- if cart.delivery_address.present? %>
<span>Delivery Fee:
<%= humanized_money_with_symbol(cart.delivery_fee) %></span>
Expand Down
11 changes: 10 additions & 1 deletion app/furniture/marketplace/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create_stripe_session(success_url:, cancel_url:)
def stripe_line_items
return [] if cart.blank?

stripe_cart_products_line_items + stripe_delivery_fee_line_items
stripe_cart_products_line_items + stripe_delivery_fee_line_items + stripe_taxes_line_items
end

private def stripe_cart_products_line_items
Expand All @@ -56,5 +56,14 @@ def stripe_line_items
}}
]
end

private def stripe_taxes_line_items
[{quantity: 1,
price_data: {
currency: "USD",
unit_amount: cart.tax_total,
product_data: {name: "Taxes"}
}}]
end
end
end
24 changes: 21 additions & 3 deletions spec/furniture/marketplace/cart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
let(:marketplace) { create(:marketplace, delivery_fee_cents: 1200) }
let(:cart) { create(:marketplace_cart, marketplace: marketplace) }
let(:product_a) { create(:marketplace_product, marketplace: cart.marketplace) }
let(:product_b) { create(:marketplace_product, marketplace: cart.marketplace) }
let(:product_b) { create(:marketplace_product, marketplace: cart.marketplace, tax_rate_ids: [sales_tax.id]) }
let(:sales_tax) { create(:marketplace_tax_rate, marketplace: cart.marketplace, tax_rate: 5.0) }

before do
cart.cart_products.create!(product: product_a, quantity: 1)
cart.cart_products.create!(product: product_b, quantity: 2)
end

it { is_expected.to eql(product_a.price + product_b.price * 2) }
it { is_expected.to eql(product_a.price + product_b.price * 2 + (product_b.price * 2 * 0.05)) }

context "when the #delivery_address is present" do
let(:cart) { create(:marketplace_cart, delivery_address: "123", marketplace: marketplace) }

it { is_expected.to eql(product_a.price + product_b.price * 2 + marketplace.delivery_fee) }
it { is_expected.to eql(product_a.price + product_b.price * 2 + marketplace.delivery_fee + (product_b.price * 2 * 0.05)) }
end
end

describe "#product_total" do
subject(:product_total) { cart.product_total }

let(:cart) { create(:marketplace_cart) }

let(:product_a) { create(:marketplace_product, marketplace: cart.marketplace) }
let(:product_b) { create(:marketplace_product, marketplace: cart.marketplace) }

Expand All @@ -45,4 +47,20 @@

it { is_expected.to eql(product_a.price * 3 + product_b.price * 2) }
end

describe "#taxes_total" do
subject(:tax_total) { cart.tax_total }

let(:cart) { create(:marketplace_cart) }
let(:product_a) { create(:marketplace_product, marketplace: cart.marketplace) }
let(:product_b) { create(:marketplace_product, marketplace: cart.marketplace, tax_rate_ids: [sales_tax.id]) }
let(:sales_tax) { create(:marketplace_tax_rate, marketplace: cart.marketplace, tax_rate: 5.0) }

before do
cart.cart_products.create!(product: product_a, quantity: 3)
cart.cart_products.create!(product: product_b, quantity: 1)
end

it { is_expected.to eql(product_b.price * 0.05) }
end
end

0 comments on commit 3113dfc

Please sign in to comment.