Class: SpreeStripe::Calculators::StripeTax

Inherits:
Spree::Calculator
  • Object
show all
Defined in:
app/models/spree_stripe/calculators/stripe_tax.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descriptionObject



4
5
6
# File 'app/models/spree_stripe/calculators/stripe_tax.rb', line 4

def self.description
  'Stripe tax calculator'
end

Instance Method Details

#compute_line_item(line_item) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/spree_stripe/calculators/stripe_tax.rb', line 8

def compute_line_item(line_item)
  order = line_item.order
  return 0.to_d unless stripe_tax_enabled?(order)

  tax_calculation_data = get_or_create_tax_calculation(order)
  return 0.to_d unless tax_calculation_data.present?

  # Find the tax line for this specific line item
  line_items = tax_calculation_data.dig('line_items', 'data') || []
  stripe_line_item = line_items.find do |line|
    line['reference'].to_s == line_item.id.to_s
  end

  # Debug logging
  if stripe_line_item.nil?
    Rails.logger.debug "Stripe Tax: No line item found for reference #{line_item.id}. Available references: #{line_items.map { |li| li['reference'] }.join(', ')}"
    return 0.to_d
  end

  # Get the tax amount for this line item from amount_tax field
  amount_in_cents = stripe_line_item['amount_tax'] || 0
  Rails.logger.debug "Stripe Tax: Line item #{line_item.id} tax amount: #{amount_in_cents} cents"
  amount_money = Spree::Money.from_cents(amount_in_cents, { currency: order.currency })
  amount_money.to_d
end

#compute_shipment(shipment) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree_stripe/calculators/stripe_tax.rb', line 34

def compute_shipment(shipment)
  order = shipment.order
  return 0.to_d unless stripe_tax_enabled?(order)

  tax_calculation_data = get_or_create_tax_calculation(order)
  return 0.to_d unless tax_calculation_data.present?

  # Get the shipping cost tax breakdown
  shipping_cost = tax_calculation_data['shipping_cost']
  return 0.to_d unless shipping_cost.present?

  # Get the tax amount for shipping
  amount_in_cents = shipping_cost['amount_tax'] || 0
  amount_money = Spree::Money.from_cents(amount_in_cents, { currency: order.currency })
  amount_money.to_d
end