summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md18
-rw-r--r--lib/super_good/solidus_taxjar.rb2
-rw-r--r--lib/super_good/solidus_taxjar/api_params.rb1
-rw-r--r--spec/super_good/solidus_taxjar/api_params_spec.rb47
4 files changed, 68 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9284bff..a3036b4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,24 @@
## master
+- 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:
+ ```ruby
+ SuperGood::SolidusTaxJar.custom_order_params = ->(order) {
+ {
+ nexus_addresses: [
+ {
+ id: 'Main Location',
+ country: 'AU',
+ zip: 'NSW 2000',
+ city: 'Sydney',
+ street: '483 George St',
+ }
+ ]
+ }
+ }
+ ```
+ The callback receives the `Spree::Order` that the params are for and the return value can override existing values like the order's ID.
+
## v0.16.0
- Fix `#incomplete_address?` method to be friendly also to completely blank addresses.
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb
index 1b1a4ac..2c47dda 100644
--- a/lib/super_good/solidus_taxjar.rb
+++ b/lib/super_good/solidus_taxjar.rb
@@ -15,6 +15,7 @@ module SuperGood
class << self
attr_accessor :cache_duration
attr_accessor :cache_key
+ attr_accessor :custom_order_params
attr_accessor :discount_calculator
attr_accessor :exception_handler
attr_accessor :line_item_tax_label_maker
@@ -31,6 +32,7 @@ module SuperGood
record_type = record.class.name.demodulize.underscore
APIParams.send("#{record_type}_params", record).to_json
}
+ self.custom_order_params = ->(order) { {} }
self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator
self.exception_handler = ->(e) {
Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
diff --git a/lib/super_good/solidus_taxjar/api_params.rb b/lib/super_good/solidus_taxjar/api_params.rb
index 3e5bcb4..6458018 100644
--- a/lib/super_good/solidus_taxjar/api_params.rb
+++ b/lib/super_good/solidus_taxjar/api_params.rb
@@ -8,6 +8,7 @@ module SuperGood
.merge(order_address_params(order.tax_address))
.merge(line_items_params(order.line_items))
.merge(shipping: order.shipment_total)
+ .merge(SuperGood::SolidusTaxJar.custom_order_params.(order))
.tap do |params|
next unless SuperGood::SolidusTaxJar.logging_enabled
diff --git a/spec/super_good/solidus_taxjar/api_params_spec.rb b/spec/super_good/solidus_taxjar/api_params_spec.rb
index e0a8766..8e9536f 100644
--- a/spec/super_good/solidus_taxjar/api_params_spec.rb
+++ b/spec/super_good/solidus_taxjar/api_params_spec.rb
@@ -141,6 +141,53 @@ RSpec.describe SuperGood::SolidusTaxJar::APIParams do
)
end
+ context "when custom params are used" do
+ around do |example|
+ default = SuperGood::SolidusTaxJar.custom_order_params
+ SuperGood::SolidusTaxJar.custom_order_params = -> (order) {
+ {
+ nexus_addresses: [
+ {
+ id: 'Main Location',
+ country: 'AU',
+ zip: 'NSW 2000',
+ city: 'Sydney',
+ street: '483 George St',
+ }
+ ]
+ }
+ }
+ example.run
+ SuperGood::SolidusTaxJar.custom_order_params = default
+ end
+
+ it "returns params for fetching the tax for the order" do
+ expect(subject).to eq(
+ customer_id: "12345",
+ line_items: [{
+ discount: 2.00,
+ id: order.line_items.first.id,
+ product_tax_code: "A_GEN_TAX",
+ quantity: 3,
+ unit_price: 10.00
+ }],
+ nexus_addresses: [{
+ id: 'Main Location',
+ country: 'AU',
+ zip: 'NSW 2000',
+ city: 'Sydney',
+ street: '483 George St',
+ }],
+ shipping: 3.01,
+ to_city: "Los Angeles",
+ to_country: "US",
+ to_state: "CA",
+ to_street: "475 N Beverly Dr",
+ to_zip: "90210"
+ )
+ end
+ end
+
context "when the line item has zero quantity" do
let(:line_item) do
Spree::LineItem.new(