summaryrefslogtreecommitdiff
path: root/lib/super_good/solidus_taxjar/api.rb
blob: d52a4a4c037910281255eeb7ba7c79f17d0d627b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module SuperGood
  module SolidusTaxJar
    class API
      def self.default_taxjar_client
        ::Taxjar::Client.new(
          api_key: ENV.fetch("TAXJAR_API_KEY"),
          api_url: ENV.fetch("TAXJAR_API_URL") { 'https://api.taxjar.com' } # Sandbox 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 APIParams.order_params(order)
      end

      def tax_rates_for(address)
        taxjar_client.rates_for_location(*APIParams.address_params(address))
      end

      private

      attr_reader :taxjar_client
    end
  end
end