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

Rest v2 avatax ping #72

Merged
merged 2 commits into from
May 31, 2017
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
2 changes: 2 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
AVATAX_USERNAME =
AVATAX_PASSWORD =
AVATAX_COMPANY_CODE =
AVATAX_ENDPOINT = https://development.avalara.net
AVATAX_ACCOUNT =
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
branch = ENV.fetch('SOLIDUS_BRANCH', 'v2.0')
gem "solidus", github: "solidusio/solidus", branch: branch
gem "solidus_auth_devise", github: "solidusio/solidus_auth_devise"
gem 'avatax-ruby'

group :test, :development do
gem "pry"
Expand Down
8 changes: 5 additions & 3 deletions app/controllers/spree/admin/avatax_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ def erase_data

def ping_my_service
mytax = TaxSvc.new
pingResult = mytax.ping
if pingResult['ResultCode'] == 'Success'
response = mytax.ping

if response.success?
flash[:success] = 'Ping Successful'

else
flash[:error] = 'Ping Error'
end

respond_to do |format|
format.js
format.html { render :layout => !request.xhr? }
format.js { render :layout => false }
end
end

Expand Down
12 changes: 11 additions & 1 deletion app/models/spree/avatax_configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Spree::AvataxConfiguration < Spree::Preferences::Configuration
preference :username, :string
preference :password, :string
preference :company_code, :string
preference :endpoint, :string
preference :account, :string
Expand All @@ -20,6 +22,14 @@ def self.boolean_preferences
end

def self.storable_env_preferences
%w(company_code endpoint account license_key)
%w(username password company_code endpoint account license_key)
end

def self.environment
if Rails.env.production?
:production
else
:sandbox
end
end
end
43 changes: 19 additions & 24 deletions app/models/tax_svc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,11 @@ def cancel_tax(request_hash)
handle_response(response)
end

def estimate_tax(coordinates, sale_amount)
if tax_calculation_enabled?
log(__method__)

return nil if coordinates.nil?
sale_amount = 0 if sale_amount.nil?
coor = coordinates[:latitude].to_s + ',' + coordinates[:longitude].to_s

uri = URI(service_url + coor + '/get?saleamount=' + sale_amount.to_s)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.open_timeout = 1
http.read_timeout = 1

res = http.get(uri.request_uri, 'Authorization' => credential, 'Content-Type' => 'application/json')
JSON.parse(res.body)
end
rescue => e
logger.error e, 'Estimate Tax Error'
'Estimate Tax Error'
end

def ping
logger.info 'Ping Call'
estimate_tax({ latitude: '40.714623', longitude: '-74.006605' }, 0)

# Testing if configuration is set up properly, ping will fail if it is not
client.tax_rates.get(:by_postal_code, { country: 'US', postalCode: '07801' })
end

def validate_address(address)
Expand Down Expand Up @@ -124,6 +103,22 @@ def account_number
Spree::Avatax::Config.account
end

def username
Spree::Avatax::Config.username
end

def password
Spree::Avatax::Config.password
end

def client
@client ||= Avatax::Client.new(
username: username,
password: password,
env: Spree::AvataxConfiguration.environment
)
end

def request(uri, request_hash)
begin
res = RestClient::Request.execute(method: :post,
Expand Down
12 changes: 12 additions & 0 deletions app/views/spree/admin/avatax_settings/ping_my_service.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% success = flash.discard(:success)
if success %>
<script>
show_flash('success', "<%= success %>")
</script>
<% end %>
<% error = flash.discard(:error)
if error %>
<script>
show_flash('error', "<%= error %>")
</script>
<% end %>
1 change: 1 addition & 0 deletions lib/solidus_avatax_certified.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require 'solidus_avatax_certified/engine'
require 'solidus_avatax_certified/exceptions'
require 'solidus_avatax_certified/version'
require 'avatax'
1 change: 1 addition & 0 deletions solidus_avatax_certified.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|

s.add_dependency "solidus_core", [">= 1.0.0", "< 2.1.0"]
s.add_dependency "json", "~> 1.8"
s.add_dependency "avatax-ruby"
s.add_dependency "addressable", "~> 2.4"
s.add_dependency "rest-client", "~> 1.7"
s.add_dependency "logging", "~> 2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@
end

describe '/avatax_settings/ping_my_service' do
subject do
VCR.use_cassette('ping', allow_playback_repeats: true) do
get :ping_my_service
end
end

it 'flashes message' do
subject { get :ping_my_service }
subject
response.should be_success
flash.should_not be_nil
end
Expand Down
11 changes: 8 additions & 3 deletions spec/models/tax_svc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@
end

describe '#ping' do
it 'should return estimate' do
result = taxsvc.ping
expect(result['ResultCode']).to eq('Success')
subject do
VCR.use_cassette('ping', allow_playback_repeats: true) do
taxsvc.ping
end
end

it 'should return successful' do
expect(subject.success?).to be_truthy
end
end
end
Expand Down
42 changes: 42 additions & 0 deletions spec/vcr/ping.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.