Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for TaxJar Address Validation API #22

Merged
merged 6 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bin/rails-engine
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/solidus_taxjar/engine', __dir__)
ENGINE_ROOT = File.expand_path("..", __dir__)
ENGINE_PATH = File.expand_path("../lib/solidus_taxjar/engine", __dir__)

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])

require 'rails/all'
require 'rails/engine/commands'
require "rails/all"
require "rails/engine/commands"
8 changes: 4 additions & 4 deletions bin/rails-sandbox
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env ruby

app_root = 'sandbox'
app_root = "sandbox"

unless File.exist? "#{app_root}/bin/rails"
warn 'Creating the sandbox app...'
warn "Creating the sandbox app..."
Dir.chdir "#{__dir__}/.." do
system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
warn 'Automatic creation of the sandbox app failed'
warn "Automatic creation of the sandbox app failed"
exit 1
end
end
end

Dir.chdir app_root
exec 'bin/rails', *ARGV
exec "bin/rails", *ARGV
2 changes: 1 addition & 1 deletion lib/super_good-solidus_taxjar.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require 'super_good/solidus_taxjar'
require "super_good/solidus_taxjar"
11 changes: 8 additions & 3 deletions lib/super_good/solidus_taxjar.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'solidus_core'
require 'solidus_support'
require 'taxjar'
require "solidus_core"
require "solidus_support"
require "taxjar"

require "super_good/solidus_taxjar/version"
require "super_good/solidus_taxjar/api_params"
Expand All @@ -9,6 +9,7 @@
require "super_good/solidus_taxjar/tax_calculator"
require "super_good/solidus_taxjar/tax_rate_calculator"
require "super_good/solidus_taxjar/discount_calculator"
require "super_good/solidus_taxjar/addresses"

module SuperGood
module SolidusTaxJar
Expand All @@ -25,6 +26,10 @@ class << self
attr_accessor :taxable_address_check
attr_accessor :taxable_order_check
attr_accessor :test_mode

def api
::SuperGood::SolidusTaxJar::API.new
end
end

self.cache_duration = 3.hours
Expand Down
61 changes: 61 additions & 0 deletions lib/super_good/solidus_taxjar/addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module SuperGood
module SolidusTaxJar
class Addresses
class << self
def normalize(spree_address)
new.normalize(spree_address)
end

def possibilities(spree_address)
new.possibilities(spree_address)
end
end

def initialize(api: ::SuperGood::SolidusTaxJar.api)
@api = api
end

def normalize(spree_address)
taxjar_address = taxjar_addresses(spree_address).first

return if taxjar_address.nil?

Spree::Address.immutable_merge(spree_address, {
country: us, # TaxJar only supports the US currently.
state: state(taxjar_address.state),
zipcode: taxjar_address.zip,
city: taxjar_address.city,
address1: taxjar_address.street
})
end

def possibilities(spree_address)
taxjar_addresses(spree_address).map { |taxjar_address|
Spree::Address.immutable_merge(spree_address, {
country: us, # TaxJar only supports the US currently.
state: state(taxjar_address.state),
zipcode: taxjar_address.zip,
city: taxjar_address.city,
address1: taxjar_address.street
})
}
end

private

attr_reader :api

def taxjar_addresses(spree_address)
api.validate_spree_address(spree_address)
end

def us
Spree::Country.find_by iso: "US"
end

def state(abbr)
us.states.find_by_abbr abbr
end
end
end
end
6 changes: 5 additions & 1 deletion lib/super_good/solidus_taxjar/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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
api_url: ENV.fetch("TAXJAR_API_URL") { "https://api.taxjar.com" } # Sandbox URL: https://api.sandbox.taxjar.com
)
end

Expand Down Expand Up @@ -46,6 +46,10 @@ def create_refund_for(reimbursement)
taxjar_client.create_refund APIParams.refund_params(reimbursement)
end

def validate_spree_address(spree_address)
taxjar_client.validate_address APIParams.validate_address_params(spree_address)
end

private

attr_reader :taxjar_client
Expand Down
20 changes: 15 additions & 5 deletions lib/super_good/solidus_taxjar/api_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def order_params(order)
.merge(order_address_params(order.tax_address))
.merge(line_items_params(order.line_items))
.merge(shipping: shipping(order))
.merge(SuperGood::SolidusTaxJar.custom_order_params.(order))
.merge(SuperGood::SolidusTaxJar.custom_order_params.call(order))
.tap do |params|
next unless SuperGood::SolidusTaxJar.logging_enabled

Expand All @@ -33,7 +33,7 @@ def address_params(address)
def tax_rate_address_params(address)
{
amount: 100,
shipping: 0,
shipping: 0
}.merge(order_address_params(address))
end

Expand Down Expand Up @@ -66,12 +66,22 @@ def refund_params(reimbursement)
)
end

def validate_address_params(spree_address)
{
country: spree_address.country&.iso,
state: spree_address.state&.abbr || adddress.state_name,
zip: spree_address.zipcode,
city: spree_address.city,
street: spree_address.address1
}
end

private

def customer_params(order)
return {} unless order.user_id

{ customer_id: order.user_id.to_s }
{customer_id: order.user_id.to_s}
end

def order_address_params(address)
Expand All @@ -80,7 +90,7 @@ def order_address_params(address)
to_zip: address.zipcode,
to_city: address.city,
to_state: address&.state&.abbr || address.state_name,
to_street: address.address1,
to_street: address.address1
}
end

Expand Down Expand Up @@ -127,7 +137,7 @@ def discount(line_item)
end

def shipping(order)
SuperGood::SolidusTaxJar.shipping_calculator.(order)
SuperGood::SolidusTaxJar.shipping_calculator.call(order)
end

def sales_tax(order)
Expand Down
8 changes: 1 addition & 7 deletions lib/super_good/solidus_taxjar/calculator_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ module SolidusTaxJar
module CalculatorHelper
extend ActiveSupport::Concern

class_methods do
def default_api
::SuperGood::SolidusTaxJar::API.new
end
end

def incomplete_address?(address)
return true if address.is_a?(Spree::Tax::TaxLocation)

Expand All @@ -22,7 +16,7 @@ def incomplete_address?(address)
end

def taxable_address?(address)
SuperGood::SolidusTaxJar.taxable_address_check.(address)
SuperGood::SolidusTaxJar.taxable_address_check.call(address)
end

def cache
Expand Down
20 changes: 10 additions & 10 deletions lib/super_good/solidus_taxjar/tax_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module SolidusTaxJar
class TaxCalculator
include CalculatorHelper

def initialize(order, api: self.class.default_api)
def initialize(order, api: SuperGood::SolidusTaxJar.api)
@order = order
@api = api
end
Expand All @@ -23,8 +23,8 @@ def calculate
shipment_taxes: shipment_taxes
)
end
rescue StandardError => e
exception_handler.(e)
rescue => e
exception_handler.call(e)
no_tax
end

Expand All @@ -34,7 +34,7 @@ def calculate

def line_item_taxes
@line_item_taxes ||=
taxjar_breakdown.line_items.map do |taxjar_line_item|
taxjar_breakdown.line_items.map { |taxjar_line_item|
spree_line_item_id = taxjar_line_item.id.to_i

# Searching in memory because this association is loaded and most
Expand All @@ -48,13 +48,13 @@ def line_item_taxes
amount: taxjar_line_item.tax_collectable,
included_in_price: false
)
end
}
end

def shipment_taxes
@shipment_taxes ||=
if taxjar_breakdown.shipping? &&
(total_shipping_tax = taxjar_breakdown.shipping.tax_collectable) != 0
(total_shipping_tax = taxjar_breakdown.shipping.tax_collectable) != 0

# Distribute shipping tax across shipments:
# TaxJar does not provide a breakdown of shipping taxes, so we have
Expand Down Expand Up @@ -114,22 +114,22 @@ def tax_rate
end

def cache_key
SuperGood::SolidusTaxJar.cache_key.(order)
SuperGood::SolidusTaxJar.cache_key.call(order)
end

def taxable_order?(order)
SuperGood::SolidusTaxJar.taxable_order_check.(order)
SuperGood::SolidusTaxJar.taxable_order_check.call(order)
end

def shipping_tax_label(shipment, shipping_tax)
SuperGood::SolidusTaxJar.shipping_tax_label_maker.(
SuperGood::SolidusTaxJar.shipping_tax_label_maker.call(
shipment,
shipping_tax
)
end

def line_item_tax_label(taxjar_line_item, spree_line_item)
SuperGood::SolidusTaxJar.line_item_tax_label_maker.(taxjar_line_item, spree_line_item)
SuperGood::SolidusTaxJar.line_item_tax_label_maker.call(taxjar_line_item, spree_line_item)
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions lib/super_good/solidus_taxjar/tax_rate_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SuperGood
module SolidusTaxJar
class TaxRateCalculator
include CalculatorHelper
def initialize(address, api: self.class.default_api)
def initialize(address, api: SuperGood::SolidusTaxJar.api)
@address = address
@api = api
end
Expand All @@ -14,9 +14,8 @@ def calculate
cache do
api.tax_rate_for(address).to_d
end

rescue StandardError => e
exception_handler.(e)
rescue => e
exception_handler.call(e)
no_rate
end

Expand All @@ -29,7 +28,7 @@ def no_rate
end

def cache_key
SuperGood::SolidusTaxJar.cache_key.(address)
SuperGood::SolidusTaxJar.cache_key.call(address)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# frozen_string_literal: true

# Configure Rails Environment
ENV['RAILS_ENV'] = 'test'
ENV["RAILS_ENV"] = "test"

require File.expand_path('dummy/config/environment.rb', __dir__).tap { |file|
require File.expand_path("dummy/config/environment.rb", __dir__).tap { |file|
# Create the dummy app if it's still missing.
system 'bin/rake extension:test_app' unless File.exist? file
system "bin/rake extension:test_app" unless File.exist? file
}

require 'solidus_dev_support/rspec/rails_helper'
require "solidus_dev_support/rspec/rails_helper"

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
config.infer_spec_type_from_file_location!
Expand Down
Loading