Sha256: ff1db61963ef933d8ddd5f1378d20a7e9f170b24877a9cbb931346d28969bb55

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module SuperGood
  module SolidusTaxJar
    class API
      def self.default_taxjar_client
        ::Taxjar::Client.new(
          api_key: ENV.fetch("TAXJAR_API_KEY"),
          api_url: 'https://api.sandbox.taxjar.com'
        )
      end

      def initialize(taxjar_client: self.class.default_taxjar_client)
        @taxjar_client = taxjar_client
      end

      def tax_for(order)
        taxjar_client.tax_for_order order_params(order)
      end

      private

      attr_reader :taxjar_client

      def order_params(order)
        tax_address = order.tax_address

        {
          to_country: tax_address.country.iso,
          to_zip: tax_address.zipcode,
          to_city: tax_address.city,
          to_state: tax_address&.state&.abbr || tax_address.state_name,
          to_street: tax_address.address1,

          shipping: order.shipment_total,

          line_items: order.line_items.map do |line_item|
            {
              id: line_item.id,
              quantity: line_item.quantity,
              unit_price: line_item.price,
              discount: -line_item.adjustment_total,
              product_tax_code: line_item.tax_category&.tax_code
            }
          end
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
super_good-solidus_taxjar-0.1.0 lib/super_good/solidus_taxjar/api.rb