Skip to content

Instantly share code, notes, and snippets.

@ehoch
Created March 3, 2014 16:35
Show Gist options
  • Save ehoch/9328805 to your computer and use it in GitHub Desktop.
Save ehoch/9328805 to your computer and use it in GitHub Desktop.
class Spree::Calculator::TaxCloudCalculator < Spree::Calculator
NEXUS_STATES = ['FL']
def self.description
"Tax Cloud Calculator"
end
def compute(object)
# given an order, we need to lookup the taxes from TaxCloud by using the hard-coded origin
return 0 unless object.is_a?(Spree::Order) and object.ship_address and object.line_items.size > 0 and NEXUS_STATES.include?(object.ship_address.state.abbr)
Rails.cache.fetch [object, "taxcloud"] do
order = object
transaction = TaxCloud::Transaction.new(
:customer_id => order.user_id,
:cart_id => order.number,
:origin => origin,
:destination => destination(order.ship_address))
order.line_items.each_with_index do |line_item, index|
transaction.cart_items << TaxCloud::CartItem.new(
:index => index,
:item_id => line_item.variant.sku,
:tic => ENV['TAXCLOUD_TIC'],
:price => line_item.price,
:quantity => line_item.quantity)
end
lookup = transaction.lookup
lookup.tax_amount
end
end
private
def origin
TaxCloud::Address.new(
:address1 => '2510 NE 47TH ST',
:address2 => '',
:city => 'LIGHTHOUSE PT',
:state => 'FL',
:zip5 => '33064')
end
def destination(ship_address)
TaxCloud::Address.new(
:address1 => ship_address.address1,
:address2 => ship_address.address2,
:city => ship_address.city,
:state => ship_address.state.abbr,
:zip5 => ship_address.zipcode.strip.slice(0, 5))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment