super_good-solidus_taxjar 0.15.2 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b31e03589ed5699fa26ba2b8f2b5ccb99fe64af208fce5c15eba8ccec10366a
4
- data.tar.gz: 63bb0d2f537723e15feaae9785090f6903c4bf6b7d2763915f22f5668dff6e89
3
+ metadata.gz: 501ae07a0037373ed9f5cca71c67d469e81f10b9ed8be735c6812401fdab9e02
4
+ data.tar.gz: 558cc49c0d712c4a14e74b5f9c98308084791fe98088f6737565a53e6303b8dd
5
5
  SHA512:
6
- metadata.gz: a09644d7afc6976fc173bd000137424ac398057db518491bbfdfd544cf66b046347959388d3c913016ee3c1b0c43211adcb2a83692c96e2a4b66cf20dc5acc45
7
- data.tar.gz: a723866885f456c418004f0642ccd7351e371c288a3f8d81cdb9b7a7179d655b738ac41f2a85b5878cc5da17ebf8a3af8697a3e02270008882d1da0ac0735a64
6
+ metadata.gz: c88ced44e52d7dbd7a90f2e6401d1e9cc01a085e1986ad836c7210e20df99e7ea061ba1129291bee9451107481a8d4f32bd833e4bd9997d960be839d72d1166c
7
+ data.tar.gz: 9aef0cf33ce2181eda0e7fb41ba704af88e3629ea4ed34a3f8063ddd8b61f67123edcf617099619d7c82c668eb07c85407873b327aad91345679f6b535d232eb
@@ -1,4 +1,5 @@
1
1
  ---
2
+ dist: trusty
2
3
  sudo: false
3
4
  language: ruby
4
5
  cache: bundler
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## v0.16.0
6
+
7
+ - Fix `#incomplete_address?` method to be friendly also to completely blank addresses.
8
+
9
+ - Added `SuperGood::SolidusTaxJar::TaxRateCalculator` for retrieving the tax rate for a given `Spree::Address`. The calculator follows `TaxCalculator` conventions by relying on address validators and custom exception handling.
10
+
5
11
  ## v0.15.2
6
12
 
7
13
  - Add order number to param logging.
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
- ## *Warning:* This extension is under active development and not yet at a v1.0 release.
2
-
3
1
  # `SuperGood::SolidusTaxJar` [![Build Status](https://travis-ci.com/SuperGoodSoft/solidus_taxjar.svg?token=rc5QTgHvLLF7cpqkmyfd&branch=master)](https://travis-ci.com/SuperGoodSoft/solidus_taxjar)
4
2
 
5
3
  `SuperGood::SolidusTaxJar` is a [Solidus](https://github.com/solidusio/solidus) extension that allows Solidus stores to use [TaxJar](https://www.taxjar.com/) for tax calculations.
6
4
 
7
- This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spree_taxjar), like [solidus_taxjar](https://github.com/boomerdigital/solidus_taxjar). Instead, `SuperGood::SolidusTaxJar` uses the new configurable tax system [introduced by @adammathys](https://github.com/solidusio/solidus/pull/1892) in Solidus 2.4.
5
+ This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spree_taxjar), like [solidus_taxjar](https://github.com/boomerdigital/solidus_taxjar). Instead of using a custom calculator, `SuperGood::SolidusTaxJar` uses the new configurable tax system [by @adammathys](https://github.com/solidusio/solidus/pull/1892) introduced in Solidus v2.4. This maps better to how the TaxJar API itself works.
8
6
 
9
7
  ## Installation
10
8
 
@@ -33,7 +31,7 @@ This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spr
33
31
  ```ruby
34
32
  # Put this in config/initializers/taxjar.rb
35
33
 
36
- SuperGood::SolidusTaxJar::TaxCalculator.exception_handler = ->(e) {
34
+ SuperGood::SolidusTaxJar.exception_handler = ->(e) {
37
35
  # Report exceptions in here. For example, if you were using the Sentry's
38
36
  # raven-ruby gem to report errors, you might do this:
39
37
  Raven.capture_exception(e)
@@ -42,6 +40,29 @@ This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spr
42
40
 
43
41
  4. Finally, make sure that the `TAXJAR_API_KEY` environment variable is set to a your TaxJar API key and make sure that you have a `Spree::TaxRate` with the name "Sales Tax". This will be used as the source for the tax adjustments that Solidus creates.
44
42
 
43
+ ## Project Status
44
+
45
+ This extension is under active development and not yet at a v1.0 release, but it's currently being used in production by multiple Solidus stores.
46
+
47
+ Requirements for TaxJar integrations vary as some stores also need reporting, which isn't provided out of the box by this extension. This is because individual stores will be using different background job frameworks or runners (Sidekiq, delayed_job, ActiveJob, etc.) and a reliable integration will rely on one of these. Because this part of the integration is small, we've chosen to provide the transaction reporting functionality, but have skipped directly integrating it.
48
+
49
+ If you're having trouble integrating this extension with your store and would like some assistance, please reach out to Jared via e-mail at [jared@super.gd](mailto:jared@super.gd) or on the official Solidus as `@jared`.
50
+
51
+ ## Features
52
+
53
+ The extension provides currently two high level `calculator` classes that wrap the low-level Ruby taxjar gem API calls:
54
+
55
+ * tax calculator
56
+ * tax rate calculator
57
+
58
+ ### TaxCalculator
59
+
60
+ `SuperGood::SolidusTaxJar::TaxCalculator` allows calculating the full tax breakdown for a given `Spree::Order`. The breakdown includes separate line items taxes and shipment taxes.
61
+
62
+ ### TaxRateCalculator
63
+
64
+ `SuperGood::SolidusTaxJar::TaxRateCalculator` allows calculating the tax rate for a given `Spree::Address`. It relies on the same low-level Ruby TaxJar API endpoint of the tax calculator in order to provide the most coherent and reliable results. TaxJar support recommends using this endpoint for live calculations.
65
+
45
66
  ## Development
46
67
 
47
68
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -5,7 +5,9 @@ require 'taxjar'
5
5
  require "super_good/solidus_taxjar/version"
6
6
  require "super_good/solidus_taxjar/api_params"
7
7
  require "super_good/solidus_taxjar/api"
8
+ require "super_good/solidus_taxjar/calculator_helper"
8
9
  require "super_good/solidus_taxjar/tax_calculator"
10
+ require "super_good/solidus_taxjar/tax_rate_calculator"
9
11
  require "super_good/solidus_taxjar/discount_calculator"
10
12
 
11
13
  module SuperGood
@@ -25,7 +27,10 @@ module SuperGood
25
27
  end
26
28
 
27
29
  self.cache_duration = 3.hours
28
- self.cache_key = ->(order) { APIParams.order_params(order).to_json }
30
+ self.cache_key = ->(record) {
31
+ record_type = record.class.name.demodulize.underscore
32
+ APIParams.send("#{record_type}_params", record).to_json
33
+ }
29
34
  self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator
30
35
  self.exception_handler = ->(e) {
31
36
  Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
@@ -22,6 +22,10 @@ module SuperGood
22
22
  end
23
23
  end
24
24
 
25
+ def tax_rate_for(address)
26
+ taxjar_client.tax_for_order(APIParams.tax_rate_address_params(address)).rate
27
+ end
28
+
25
29
  def tax_rates_for(address)
26
30
  taxjar_client.rates_for_location(*APIParams.address_params(address))
27
31
  end
@@ -29,6 +29,13 @@ module SuperGood
29
29
  ]
30
30
  end
31
31
 
32
+ def tax_rate_address_params(address)
33
+ {
34
+ amount: 100,
35
+ shipping: 0,
36
+ }.merge(order_address_params(address))
37
+ end
38
+
32
39
  def transaction_params(order)
33
40
  {}
34
41
  .merge(customer_params(order))
@@ -0,0 +1,44 @@
1
+ module SuperGood
2
+ module SolidusTaxJar
3
+ module CalculatorHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ def default_api
8
+ ::SuperGood::SolidusTaxJar::API.new
9
+ end
10
+ end
11
+
12
+ def incomplete_address?(address)
13
+ return true if address.is_a?(Spree::Tax::TaxLocation)
14
+
15
+ [
16
+ address.address1,
17
+ address.city,
18
+ address.state&.abbr || address.state_name,
19
+ address.zipcode,
20
+ address.country&.iso
21
+ ].any?(&:blank?)
22
+ end
23
+
24
+ def taxable_address?(address)
25
+ SuperGood::SolidusTaxJar.taxable_address_check.(address)
26
+ end
27
+
28
+ def cache
29
+ if !Rails.env.test?
30
+ Rails.cache.fetch(
31
+ cache_key,
32
+ expires_in: SuperGood::SolidusTaxJar.cache_duration
33
+ ) { yield }
34
+ else
35
+ yield
36
+ end
37
+ end
38
+
39
+ def exception_handler
40
+ SuperGood::SolidusTaxJar.exception_handler
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,9 +1,7 @@
1
1
  module SuperGood
2
2
  module SolidusTaxJar
3
3
  class TaxCalculator
4
- def self.default_api
5
- ::SuperGood::SolidusTaxJar::API.new
6
- end
4
+ include CalculatorHelper
7
5
 
8
6
  def initialize(order, api: self.class.default_api)
9
7
  @order = order
@@ -115,33 +113,14 @@ module SuperGood
115
113
  Spree::TaxRate.find_by(name: "Sales Tax")
116
114
  end
117
115
 
118
- def cache
119
- if !Rails.env.test?
120
- Rails.cache.fetch(
121
- cache_key,
122
- expires_in: SuperGood::SolidusTaxJar.cache_duration
123
- ) { yield }
124
- else
125
- yield
126
- end
127
- end
128
-
129
116
  def cache_key
130
117
  SuperGood::SolidusTaxJar.cache_key.(order)
131
118
  end
132
119
 
133
- def exception_handler
134
- SuperGood::SolidusTaxJar.exception_handler
135
- end
136
-
137
120
  def taxable_order?(order)
138
121
  SuperGood::SolidusTaxJar.taxable_order_check.(order)
139
122
  end
140
123
 
141
- def taxable_address?(address)
142
- SuperGood::SolidusTaxJar.taxable_address_check.(address)
143
- end
144
-
145
124
  def shipping_tax_label(shipment, shipping_tax)
146
125
  SuperGood::SolidusTaxJar.shipping_tax_label_maker.(
147
126
  shipment,
@@ -152,18 +131,6 @@ module SuperGood
152
131
  def line_item_tax_label(taxjar_line_item, spree_line_item)
153
132
  SuperGood::SolidusTaxJar.line_item_tax_label_maker.(taxjar_line_item, spree_line_item)
154
133
  end
155
-
156
- def incomplete_address?(tax_address)
157
- return true if tax_address.is_a?(Spree::Tax::TaxLocation)
158
-
159
- [
160
- tax_address.address1,
161
- tax_address.city,
162
- tax_address&.state&.abbr || tax_address.state_name,
163
- tax_address.zipcode,
164
- tax_address.country.iso
165
- ].any?(&:blank?)
166
- end
167
134
  end
168
135
  end
169
136
  end
@@ -0,0 +1,36 @@
1
+ module SuperGood
2
+ module SolidusTaxJar
3
+ class TaxRateCalculator
4
+ include CalculatorHelper
5
+ def initialize(address, api: self.class.default_api)
6
+ @address = address
7
+ @api = api
8
+ end
9
+
10
+ def calculate
11
+ return no_rate if SuperGood::SolidusTaxJar.test_mode
12
+ return no_rate if incomplete_address?(address)
13
+ return no_rate unless taxable_address?(address)
14
+ cache do
15
+ api.tax_rate_for(address).to_d
16
+ end
17
+
18
+ rescue StandardError => e
19
+ exception_handler.(e)
20
+ no_rate
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :address, :api
26
+
27
+ def no_rate
28
+ BigDecimal(0)
29
+ end
30
+
31
+ def cache_key
32
+ SuperGood::SolidusTaxJar.cache_key.(address)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,5 +1,5 @@
1
1
  module SuperGood
2
2
  module SolidusTaxJar
3
- VERSION = "0.15.2"
3
+ VERSION = "0.16.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super_good-solidus_taxjar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.2
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared Norman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-26 00:00:00.000000000 Z
11
+ date: 2020-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -171,8 +171,10 @@ files:
171
171
  - lib/super_good/solidus_taxjar.rb
172
172
  - lib/super_good/solidus_taxjar/api.rb
173
173
  - lib/super_good/solidus_taxjar/api_params.rb
174
+ - lib/super_good/solidus_taxjar/calculator_helper.rb
174
175
  - lib/super_good/solidus_taxjar/discount_calculator.rb
175
176
  - lib/super_good/solidus_taxjar/tax_calculator.rb
177
+ - lib/super_good/solidus_taxjar/tax_rate_calculator.rb
176
178
  - lib/super_good/solidus_taxjar/version.rb
177
179
  - super_good-solidus_taxjar.gemspec
178
180
  homepage: https://github.com/SuperGoodSoft/solidus_taxjar