super_good-solidus_taxjar 0.15.0 → 0.17.1

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: 045cc30a313283d465e1252ef957dc484d780e4dcf9eae06263dd9dd1e95f5d7
4
- data.tar.gz: fb605130f6c7602f31c4bea0604183df3e77c166bbaad1b7391a7f51ad7e0cff
3
+ metadata.gz: a31766c3361794c3a8180a0a8f8b6af7f2b8e970929e203fa5a9b02e8b917f5a
4
+ data.tar.gz: 7f588d8f3f693426ead6eb43ca801a1e1d7cb9151d951d2ec4af11bb666b49f8
5
5
  SHA512:
6
- metadata.gz: c25108f00d39800dad1bb3d217b71d929e64ce4bf5713f414fa40878b89fc814f5fdac5abdb7e897aebc5fa937edcf84e4e9d25122c2c82dba36427fa968c605
7
- data.tar.gz: 06a05cb7a0fdbbbb15c3472353219f0f7db544be35c2d3f5c00dae8537d67e869789b141d526b991365d3af90c19e903cedfc85966fecffae19661f13eae2778
6
+ metadata.gz: 588484c98a53622a57266891dfc5c504e637c5962071268847bf7bcbab7f6242c32b60f81130cdfe6858acd8d9502272a18ebcb7f7b30ca3d7bf86973e7dc585
7
+ data.tar.gz: ef6153d10eb4cfd65e19d4ed1805f4b7e30a1190b65ba07fda2602b66ebf3450fb58ab57c2c197e758e4e9d1da2f34c5cc65a8b320fd71225f1059a42fe9ad57
@@ -1,4 +1,5 @@
1
1
  ---
2
+ dist: trusty
2
3
  sudo: false
3
4
  language: ruby
4
5
  cache: bundler
@@ -2,6 +2,44 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## v0.17.1
6
+
7
+ - Fixed bug where shipping calculator was not used for order shipping param. (Thanks @spaghetticode!)
8
+
9
+ ## v0.17.0
10
+
11
+ - Added `SuperGood::SolidusTaxJar.custom_order_params` to allow for custom overrides to the parameters sent to TaxJar when calculating order taxes. For example, if you needed to send a custom nexus address you could do:
12
+ ```ruby
13
+ SuperGood::SolidusTaxJar.custom_order_params = ->(order) {
14
+ {
15
+ nexus_addresses: [
16
+ {
17
+ id: 'Main Location',
18
+ country: 'AU',
19
+ zip: 'NSW 2000',
20
+ city: 'Sydney',
21
+ street: '483 George St',
22
+ }
23
+ ]
24
+ }
25
+ }
26
+ ```
27
+ The callback receives the `Spree::Order` that the params are for and the return value can override existing values like the order's ID.
28
+
29
+ ## v0.16.0
30
+
31
+ - Fix `#incomplete_address?` method to be friendly also to completely blank addresses.
32
+
33
+ - 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.
34
+
35
+ ## v0.15.2
36
+
37
+ - Add order number to param logging.
38
+
39
+ ## v0.15.1
40
+
41
+ - Add support for request/response logging.
42
+
5
43
  ## v0.15.0
6
44
 
7
45
  - Made sure cache key is a string, instead of a giant nested hash/array structure that probably won't get interpreted by Rails well. Still not happy with the caching behaviour, but it's configurable.
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
@@ -13,9 +15,11 @@ module SuperGood
13
15
  class << self
14
16
  attr_accessor :cache_duration
15
17
  attr_accessor :cache_key
18
+ attr_accessor :custom_order_params
16
19
  attr_accessor :discount_calculator
17
20
  attr_accessor :exception_handler
18
21
  attr_accessor :line_item_tax_label_maker
22
+ attr_accessor :logging_enabled
19
23
  attr_accessor :shipping_calculator
20
24
  attr_accessor :shipping_tax_label_maker
21
25
  attr_accessor :taxable_address_check
@@ -24,12 +28,17 @@ module SuperGood
24
28
  end
25
29
 
26
30
  self.cache_duration = 3.hours
27
- self.cache_key = ->(order) { APIParams.order_params(order).to_json }
31
+ self.cache_key = ->(record) {
32
+ record_type = record.class.name.demodulize.underscore
33
+ APIParams.send("#{record_type}_params", record).to_json
34
+ }
35
+ self.custom_order_params = ->(order) { {} }
28
36
  self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator
29
37
  self.exception_handler = ->(e) {
30
38
  Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
31
39
  }
32
40
  self.line_item_tax_label_maker = ->(taxjar_line_item, spree_line_item) { "Sales Tax" }
41
+ self.logging_enabled = false
33
42
  self.shipping_calculator = ->(order) { order.shipment_total }
34
43
  self.shipping_tax_label_maker = ->(shipment, shipping_tax) { "Sales Tax" }
35
44
  self.taxable_address_check = ->(address) { true }
@@ -13,7 +13,17 @@ module SuperGood
13
13
  end
14
14
 
15
15
  def tax_for(order)
16
- taxjar_client.tax_for_order APIParams.order_params(order)
16
+ taxjar_client.tax_for_order(APIParams.order_params(order)).tap do |taxes|
17
+ next unless SuperGood::SolidusTaxJar.logging_enabled
18
+
19
+ Rails.logger.info(
20
+ "TaxJar response for #{order.number}: #{taxes.to_h.inspect}"
21
+ )
22
+ end
23
+ end
24
+
25
+ def tax_rate_for(address)
26
+ taxjar_client.tax_for_order(APIParams.tax_rate_address_params(address)).rate
17
27
  end
18
28
 
19
29
  def tax_rates_for(address)
@@ -7,7 +7,15 @@ module SuperGood
7
7
  .merge(customer_params(order))
8
8
  .merge(order_address_params(order.tax_address))
9
9
  .merge(line_items_params(order.line_items))
10
- .merge(shipping: order.shipment_total)
10
+ .merge(shipping: shipping(order))
11
+ .merge(SuperGood::SolidusTaxJar.custom_order_params.(order))
12
+ .tap do |params|
13
+ next unless SuperGood::SolidusTaxJar.logging_enabled
14
+
15
+ Rails.logger.info(
16
+ "TaxJar params for #{order.number}: #{params.inspect}"
17
+ )
18
+ end
11
19
  end
12
20
 
13
21
  def address_params(address)
@@ -22,6 +30,13 @@ module SuperGood
22
30
  ]
23
31
  end
24
32
 
33
+ def tax_rate_address_params(address)
34
+ {
35
+ amount: 100,
36
+ shipping: 0,
37
+ }.merge(order_address_params(address))
38
+ end
39
+
25
40
  def transaction_params(order)
26
41
  {}
27
42
  .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.0"
3
+ VERSION = "0.17.1"
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.0
4
+ version: 0.17.1
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-25 00:00:00.000000000 Z
11
+ date: 2020-05-28 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