taxjar / taxjar-ruby

Sales Tax API Client for Ruby
https://developers.taxjar.com/api/reference/?ruby
MIT License
54 stars 40 forks source link

Can't access line item breakdown, can't modify frozen error #16

Closed wassimk closed 8 years ago

wassimk commented 8 years ago

I've submitted individual line items to the tax_for_order method. I get the results I'm expecting but I can't access the breakdown line items.

result = Taxjar::Client.new(api_key: my_api_key).tax_for_order(
      to_country: order.shipping_address.country.code,
      to_zip: order.shipping_address.zip_code,
      to_city: order.shipping_address.city,
      to_state: order.shipping_address.state.code,
      from_country: order.location.country.code,
      from_zip: order.location.zip_code,
      from_city: order.location.city,
      shipping: order.shipping_cost,
      amount: order.subtotal,
      line_items: order_line_items
    )

This is what order_line_items looks like:

  def order_line_items
    order_line_items = []

    order.order_line_items.each do |oli|
      next if oli.line_type == 'Shipping'

      order_line_item = {
        id: oli.id,
        quantity: oli.quantity,
        unit_price: oli.price,
        discount: oli.discount,
        product_identifier: (oli.variant.sku unless oli.variant.blank?),
        description: (oli.variant.name unless oli.variant.blank?),
        product_tax_code: ('99999' if oli.tax_exempt?)
      }

      order_line_items << order_line_item
    end

    order_line_items
  end

result.breakdown

{
                :state_taxable_amount => 44.99,
               :state_tax_collectable => 2.02,
               :county_taxable_amount => 44.99,
              :county_tax_collectable => 0.41,
                 :city_taxable_amount => 44.99,
                :city_tax_collectable => 1.8,
     :special_district_taxable_amount => 0,
    :special_district_tax_collectable => 0,
                          :line_items => [
        [0] {
                                         :id => "169",
                            :tax_collectable => 0.0,
                       :state_taxable_amount => 0.0,
                       :state_sales_tax_rate => 0.045,
                               :state_amount => 0.0,
                      :county_taxable_amount => 0.0,
                            :county_tax_rate => 0.00917,
                              :county_amount => 0.0,
                        :city_taxable_amount => 0.0,
                              :city_tax_rate => 0.04,
                                :city_amount => 0.0,
            :special_district_taxable_amount => 0.0,
                           :special_tax_rate => 0.0,
                    :special_district_amount => 0.0
        },
        [1] {
                                         :id => "168",
                            :tax_collectable => 0.0,
                       :state_taxable_amount => 0.0,
                       :state_sales_tax_rate => 0.045,
                               :state_amount => 0.0,
                      :county_taxable_amount => 0.0,
                            :county_tax_rate => 0.00917,
                              :county_amount => 0.0,
                        :city_taxable_amount => 0.0,
                              :city_tax_rate => 0.04,
                                :city_amount => 0.0,
            :special_district_taxable_amount => 0.0,
                           :special_tax_rate => 0.0,
                    :special_district_amount => 0.0
        },
...
2.2.4 :009 > result.breakdown.line_items
RuntimeError: can't modify frozen Taxjar::Breakdown
    from /usr/local/rvm/gems/ruby-2.2.4@uber-app/gems/taxjar-ruby-1.1.0/lib/taxjar/breakdown.rb:13:in `line_items'

I'm updating tax on individual line items so I need this data but I can't seem to access it programatically. What am I missing?

wassimk commented 8 years ago

I just saw I can get to it through attrs is this what I should do?

result.attrs[:breakdown][:line_items]
fastdivision commented 8 years ago

Hi @wassimk! Would you mind upgrading to v1.2.2? That should resolve the error.

wassimk commented 8 years ago

gem 'taxjar-ruby', '>= 1.2.2', require: 'taxjar'

That got it working as expected. Thanks @fastdivision.

P.S. Heads up if not expected behavior but without explicitly setting the version was getting 1.1.0.