spree_avalara 6.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +188 -0
- data/Rakefile +15 -0
- data/app/models/spree_avalara/entity_use_codes.rb +74 -0
- data/app/models/spree_avalara/integration.rb +160 -0
- data/app/models/spree_avalara/tax_provider.rb +420 -0
- data/app/models/spree_avalara/transaction_line.rb +306 -0
- data/app/presenters/spree_avalara/address_presenter.rb +28 -0
- data/app/presenters/spree_avalara/item_presenter.rb +191 -0
- data/app/presenters/spree_avalara/refund_presenter.rb +167 -0
- data/app/presenters/spree_avalara/transaction_presenter.rb +118 -0
- data/app/services/spree_avalara/address/validate.rb +91 -0
- data/app/services/spree_avalara/client.rb +253 -0
- data/app/services/spree_avalara/estimate_cache_key.rb +83 -0
- data/app/services/spree_avalara/validate_checkout_address.rb +32 -0
- data/config/locales/en.yml +11 -0
- data/lib/spree_avalara/engine.rb +19 -0
- data/lib/spree_avalara/factories.rb +1 -0
- data/lib/spree_avalara/testing_support/factories/integration_factory.rb +29 -0
- data/lib/spree_avalara.rb +123 -0
- data/lib/tasks/upgrade.rake +257 -0
- metadata +109 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f40ba6e106789f4f8a0db29c4822b70ca5fac200ab799677182d1ebe61d822cb
|
|
4
|
+
data.tar.gz: ca465984810f4ae48321d984d2016d4b13af91a0f397bbb188a905de138b5e8c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 97e3644e2bbbee497662bd010d3cb8d6e4534ccd8c551a21fdd19d5cdc0e594a39be31c09c620d2bce2fdc80f269bf6cfcbcff6b8eaed15eaa077cd5b7fe5af8
|
|
7
|
+
data.tar.gz: b217f44f8a9eb9ab8dfa11c921ae4ae657b2e1927694360b44fa87d84549b46a9d0b26bcec8c93ce2cb3a2557919e90229daa580968222850095118cc6f1497b
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-present, Vendo Sp. z o.o., Vendo Connect Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# Spree Avalara
|
|
2
|
+
|
|
3
|
+
The officially supported Avalara AvaTax extension for [Spree Commerce](https://spreecommerce.org/)
|
|
4
|
+
using [Avalara REST API v2](https://developer.avalara.com/api-reference/avatax/rest/v2/).
|
|
5
|
+
|
|
6
|
+
`spree_avalara` is the Spree 6 successor of the `spree_avatax_official` extension. It implements
|
|
7
|
+
Spree's `Spree::TaxProvider` interface on Avalara's official
|
|
8
|
+
[`avatax`](https://github.com/avadev/AvaTax-REST-V2-Ruby-SDK) SDK. For Spree 5 use
|
|
9
|
+
[spree_avatax_official](https://github.com/spree/spree_avatax_official).
|
|
10
|
+
|
|
11
|
+
- [Introduction](#introduction)
|
|
12
|
+
- [Maintenance & support](#maintenance--support)
|
|
13
|
+
- [Features](#features)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Setup](#setup)
|
|
16
|
+
- [Upgrading from `spree_avatax_official`](#upgrading-from-spree_avatax_official)
|
|
17
|
+
- [Development](#development)
|
|
18
|
+
- [Contributing](#contributing)
|
|
19
|
+
|
|
20
|
+
## Introduction
|
|
21
|
+
|
|
22
|
+
Avalara AvaTax is a cloud-based solution automating transaction tax calculations and the tax filing
|
|
23
|
+
process. Avalara provides real-time tax calculation using tax content from more than 12,000 US taxing
|
|
24
|
+
jurisdictions and over 200 countries, ensuring your transaction tax is calculated based on the most
|
|
25
|
+
current tax rules.
|
|
26
|
+
|
|
27
|
+
Avalara prices the whole sale on every call and files a document when the order is placed, so tax is
|
|
28
|
+
computed by Avalara rather than from rate tables you maintain.
|
|
29
|
+
|
|
30
|
+
## Maintenance & support
|
|
31
|
+
|
|
32
|
+
This extension is maintained by the Spree Commerce core team and Avalara, and will be kept up to date
|
|
33
|
+
with future Avalara platform changes.
|
|
34
|
+
|
|
35
|
+
For more information, support and guidance on how to implement the Spree Avalara extension feel free to:
|
|
36
|
+
|
|
37
|
+
- Read the [Spree Avalara AvaTax documentation](https://spreecommerce.org/docs/integrations/tax/avalara)
|
|
38
|
+
- Reach out through the [Spree contact form](https://spreecommerce.org/contact/)
|
|
39
|
+
- Join the Spree Discord at [discord.spreecommerce.org](https://discord.spreecommerce.org/)
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
1. Tax calculation (additional and included tax) — US state tax, Canadian HST and VAT supported
|
|
44
|
+
2. Tax calculation on line items, shipping charges and fees
|
|
45
|
+
3. Tax codes for products and shipping, assignable per tax category and searchable from Avalara's catalogue
|
|
46
|
+
4. Multi-stock location support — origin-based tax calculation per shipment across your fulfilment network
|
|
47
|
+
5. Tax exemptions — entity use codes on company exemption certificates, and buyer tax registrations (VAT numbers)
|
|
48
|
+
6. Address validation for US and Canada at checkout
|
|
49
|
+
7. Committing complete orders as sales invoice transactions
|
|
50
|
+
8. Cancelling (voiding) orders
|
|
51
|
+
9. Refunding orders (full and partial) — returns, claims and exchanges file a credit with the tax the sale charged
|
|
52
|
+
10. Per-market setup — move one region at a time while the others keep Spree's built-in tax engine
|
|
53
|
+
11. Client-side logging
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
spree add avatax
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Without the Spree CLI, add `gem 'spree_avalara'` to the `Gemfile` with the same version
|
|
62
|
+
requirement as your `spree` gem and run `bundle install`. The provider registers itself — there is
|
|
63
|
+
nothing to generate, and the gem ships no migrations and adds no columns.
|
|
64
|
+
|
|
65
|
+
## Setup
|
|
66
|
+
|
|
67
|
+
### Connect Spree to AvaTax
|
|
68
|
+
|
|
69
|
+
In the admin dashboard open **Settings → Integrations**, find the **Avalara AvaTax** tile and connect
|
|
70
|
+
it. You need an account number, a license key and the company code the documents should be filed
|
|
71
|
+
against. New connections default to Avalara's sandbox
|
|
72
|
+
host; switch `endpoint` to `https://rest.avatax.com` when you go live.
|
|
73
|
+
|
|
74
|
+
| Preference | Meaning |
|
|
75
|
+
|---|---|
|
|
76
|
+
| `account_number`, `license_key` | Your AvaTax credentials |
|
|
77
|
+
| `endpoint` | Sandbox (default) or production host |
|
|
78
|
+
| `company_code` | The company documents are filed against |
|
|
79
|
+
| `document_recording` | What happens to a placed sale at Avalara. **Record and commit** (default) files it as Committed. **Record without committing** files it as Saved, so it stays out of Avalara's filings until something commits it — the legacy extension's behaviour with commit switched off. **Don't record** files nothing: only quotes are sent, and no sale, void or credit reaches Avalara, for a store whose ERP records the sales instead. The tax charged is the same in all three. Switching to Don't record also stops voids and credits for documents recorded before the switch |
|
|
80
|
+
| `address_validation_enabled` | Refuse a checkout whose US or Canadian address Avalara cannot resolve (default off) |
|
|
81
|
+
| `show_rate_in_label` | Reserved; carried over so upgraded settings survive |
|
|
82
|
+
| `logging_enabled` | Write every AvaTax call to the Rails log (default off) — see below |
|
|
83
|
+
|
|
84
|
+
Activating the integration verifies the credentials with a ping. A rejected key blocks activation and
|
|
85
|
+
shows Avalara's own message.
|
|
86
|
+
|
|
87
|
+
**Logging.** With `logging_enabled` on, every call to Avalara writes one line to your Rails log, tagged
|
|
88
|
+
`[AVATAX]`: the operation, the HTTP status (or the network error), how long it took, and the request and
|
|
89
|
+
response bodies. Credentials are never logged. The bodies carry customer addresses, and a guest's email
|
|
90
|
+
is their customer code, so switch it on while you need it and keep the log under your usual retention.
|
|
91
|
+
|
|
92
|
+
**Your stock locations need real addresses.** Avalara computes tax from an origin as well as a
|
|
93
|
+
destination, so a warehouse carrying only a country makes every estimate fail. Fill in street, city,
|
|
94
|
+
region and postal code for the locations you ship from.
|
|
95
|
+
|
|
96
|
+
**A market pointing at a disconnected integration computes no tax.** Deactivating the integration
|
|
97
|
+
does not break your storefront — carts and checkout keep working, Avalara simply has no opinion, and
|
|
98
|
+
the misconfiguration is reported to your error tracker. Watch for orders with no tax after a
|
|
99
|
+
credential change. A *connected* Avalara that cannot be reached does fail closed, because that is
|
|
100
|
+
the case where quietly charging nothing is a liability rather than a setting.
|
|
101
|
+
|
|
102
|
+
### Calculate through Avalara
|
|
103
|
+
|
|
104
|
+
Tax engines are chosen per market: set a market's tax provider to **Avalara AvaTax** and every sale in
|
|
105
|
+
it calculates through your account. Markets you leave alone keep using Spree's built-in engine, so you
|
|
106
|
+
can move one region at a time.
|
|
107
|
+
|
|
108
|
+
### How exemptions reach Avalara
|
|
109
|
+
|
|
110
|
+
Company tax exemption certificates recorded in Spree are sent with the sale, each with the code of its
|
|
111
|
+
exemption reason. Exemption reasons are your store's own list, managed under **Settings → Taxes → Exemption reasons**:
|
|
112
|
+
give each one an Avalara entity use code (`G` for resale, `A` for federal government, …) and name it
|
|
113
|
+
however you like. A new store's six default reasons carry neutral codes, so change the ones you use to
|
|
114
|
+
Avalara's; once a market uses Avalara the dashboard offers Avalara's codes, and every certificate follows the change.
|
|
115
|
+
|
|
116
|
+
A reason whose code Avalara does not define is sent as OTHER/CUSTOM, and the reason you recorded is kept
|
|
117
|
+
on the tax row.
|
|
118
|
+
|
|
119
|
+
### Returns, claims and exchanges
|
|
120
|
+
|
|
121
|
+
The tax given back on a return, a claim or an exchange is exactly what the sale charged on the units
|
|
122
|
+
coming back, so a customer is never refunded more or less tax than they paid. Once the money has gone
|
|
123
|
+
back, Spree files a credit with Avalara carrying that same tax. The credit is dated the day of the
|
|
124
|
+
refund but taxed as of the original sale, and it is keyed to the return, claim or exchange number, so
|
|
125
|
+
retrying a refund adjusts one document.
|
|
126
|
+
|
|
127
|
+
An exchange's replacement is taxed by Avalara as a new sale while the exchange is open, and filed as
|
|
128
|
+
its own document once it ships.
|
|
129
|
+
|
|
130
|
+
## Upgrading from `spree_avatax_official`
|
|
131
|
+
|
|
132
|
+
Replace `spree_avatax_official` with `spree_avalara` in your `Gemfile`, using the same version
|
|
133
|
+
requirement as your `spree` gem. The old extension's install generator and migrations are gone. Then run:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
bundle install
|
|
137
|
+
bin/rails spree_avalara:upgrade
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Idempotent, and not needed on a fresh install. It retypes your existing integration (credentials carry
|
|
141
|
+
over untouched, and a legacy "commit transactions" switch that was off becomes **Record without committing**), turns legacy `spree_users.vat_id` values into buyer tax registrations, deletes the
|
|
142
|
+
synthesized "AvaTax Official Tax Rate" rows — left behind, Spree's own engine would read them as real
|
|
143
|
+
configuration — points markets that name no engine at Avalara, and gives each connected store an
|
|
144
|
+
exemption reason per Avalara entity use code, as the legacy extension's code table did. A market where
|
|
145
|
+
you chose an engine explicitly, including Spree's own, is never rewritten.
|
|
146
|
+
|
|
147
|
+
A legacy `vat_id` that no longer validates is reported rather than migrated, so you can reconcile the
|
|
148
|
+
list; until then that buyer is charged as a consumer, which is the safe direction.
|
|
149
|
+
|
|
150
|
+
Legacy schema is left in place. Dropping it removes `spree_users.vat_id`, the only copy of a reported
|
|
151
|
+
value, so reconcile or export those first. When you are satisfied with the result:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
bin/rails spree_avalara:upgrade:drop_legacy_schema
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
That keeps `spree_users.exemption_number` and `avatax_entity_use_code_id`, which are the source for
|
|
158
|
+
whichever answer individual exemptions get.
|
|
159
|
+
|
|
160
|
+
**Orders filed by the legacy extension carry no Avalara document id.** The legacy table recorded the
|
|
161
|
+
document *code*, which is the order number — and that is what voids and credits key on anyway, so
|
|
162
|
+
nothing is lost.
|
|
163
|
+
|
|
164
|
+
## Development
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
cd spree/providers/avalara
|
|
168
|
+
bundle install
|
|
169
|
+
bundle exec rake test_app # once, to generate spec/dummy
|
|
170
|
+
bundle exec rspec
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
The suite makes no network calls. Specs that assert what Avalara actually returns are backed by VCR
|
|
174
|
+
cassettes recorded by the maintainer against a sandbox account; a missing cassette fails its spec.
|
|
175
|
+
To record:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
AVATAX_ACCOUNT_NUMBER=… AVATAX_LICENSE_KEY=… AVATAX_COMPANY_CODE=… VCR_RECORD_MODE=new_episodes bundle exec rspec spec/integration
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Credentials and the account's own user name are filtered out of every cassette.
|
|
182
|
+
|
|
183
|
+
## Contributing
|
|
184
|
+
|
|
185
|
+
This gem lives in the [Spree monorepo](https://github.com/spree/spree) under `spree/providers/avalara`.
|
|
186
|
+
Please open issues and pull requests there.
|
|
187
|
+
|
|
188
|
+
Copyright (c) 2026-present [Vendo Sp. z o.o.](https://getvendo.com), [Vendo Connect Inc.](https://getvendo.com), released under the [MIT license](LICENSE).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
require 'rake/testtask'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
require 'spree/testing_support/common_rake'
|
|
6
|
+
|
|
7
|
+
RSpec::Core::RakeTask.new
|
|
8
|
+
|
|
9
|
+
task default: :spec
|
|
10
|
+
|
|
11
|
+
desc "Generates a dummy app for testing"
|
|
12
|
+
task :test_app do
|
|
13
|
+
ENV['LIB_NAME'] = 'spree_avalara'
|
|
14
|
+
Rake::Task['common:test_app'].invoke
|
|
15
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module SpreeAvalara
|
|
2
|
+
# Avalara's entity use codes — why a buyer is exempt, in Avalara's own
|
|
3
|
+
# vocabulary. Frozen here rather than seeded into a table: the list is
|
|
4
|
+
# Avalara's, not the merchant's, and the legacy extension's editable table
|
|
5
|
+
# only ever held these same seventeen rows.
|
|
6
|
+
#
|
|
7
|
+
# Two directions, because two callers need opposite things. The dashboard
|
|
8
|
+
# offers the merchant this list (code to label, registered in the engine) and
|
|
9
|
+
# sends back a code. A reason recorded some other way — a hand-written API
|
|
10
|
+
# call, an importer — may arrive as a name instead, and {.for} resolves it.
|
|
11
|
+
module EntityUseCodes
|
|
12
|
+
ALL = {
|
|
13
|
+
'A' => 'FEDERAL GOV',
|
|
14
|
+
'B' => 'STATE GOV',
|
|
15
|
+
'C' => 'TRIBAL GOVERNMENT',
|
|
16
|
+
'D' => 'FOREIGN DIPLOMAT',
|
|
17
|
+
'E' => 'CHARITABLE/EXEMPT ORG',
|
|
18
|
+
'F' => 'RELIGIOUS ORG',
|
|
19
|
+
'G' => 'RESALE',
|
|
20
|
+
'H' => 'AGRICULTURE',
|
|
21
|
+
'I' => 'INDUSTRIAL PROD/MANUFACTURERS',
|
|
22
|
+
'J' => 'DIRECT PAY',
|
|
23
|
+
'K' => 'DIRECT MAIL',
|
|
24
|
+
'L' => 'OTHER/CUSTOM',
|
|
25
|
+
'N' => 'EDUCATIONAL ORG',
|
|
26
|
+
'P' => 'COMMERCIAL AQUACULTURE',
|
|
27
|
+
'Q' => 'COMMERCIAL FISHERY',
|
|
28
|
+
'R' => 'NON-RESIDENT',
|
|
29
|
+
'TAXABLE' => 'NON-EXEMPT TAXABLE CUSTOMER'
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
# A reason Avalara cannot classify is still a claim worth sending, so it
|
|
33
|
+
# goes as OTHER/CUSTOM rather than being dropped — the raw reason stays on
|
|
34
|
+
# the row for anyone reading the filing later.
|
|
35
|
+
FALLBACK_CODE = 'L'.freeze
|
|
36
|
+
|
|
37
|
+
# Name to code, derived so a new code needs no second entry. Nothing is
|
|
38
|
+
# hand-aliased onto it: a name this does not hold is a name nobody can
|
|
39
|
+
# show Avalara meant, and guessing files the wrong exemption. Unknown
|
|
40
|
+
# reasons take the fallback, which files a claim Avalara will look at.
|
|
41
|
+
BY_NAME = ALL.each_with_object({}) { |(code, label), index| index[label] = code }.freeze
|
|
42
|
+
|
|
43
|
+
# @param reason_code [String, nil] whatever the certificate recorded
|
|
44
|
+
# @return [String, nil] an Avalara entity use code, nil when nothing was claimed
|
|
45
|
+
def self.for(reason_code)
|
|
46
|
+
return if reason_code.blank?
|
|
47
|
+
|
|
48
|
+
value = normalize(reason_code)
|
|
49
|
+
return value if ALL.key?(value)
|
|
50
|
+
|
|
51
|
+
BY_NAME[value] || FALLBACK_CODE
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Whether the code was understood, so a caller can keep the raw reason when
|
|
55
|
+
# it was not.
|
|
56
|
+
#
|
|
57
|
+
# @param reason_code [String, nil]
|
|
58
|
+
# @return [Boolean]
|
|
59
|
+
def self.recognized?(reason_code)
|
|
60
|
+
return false if reason_code.blank?
|
|
61
|
+
|
|
62
|
+
value = normalize(reason_code)
|
|
63
|
+
|
|
64
|
+
ALL.key?(value) || BY_NAME.key?(value)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# One form for both indexes, so 'federal_gov', 'Federal Gov' and
|
|
68
|
+
# 'FEDERAL GOV' are read as the same claim.
|
|
69
|
+
def self.normalize(value)
|
|
70
|
+
value.to_s.strip.upcase.tr('_', ' ').squeeze(' ')
|
|
71
|
+
end
|
|
72
|
+
private_class_method :normalize
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
module SpreeAvalara
|
|
2
|
+
# Per-store AvaTax credentials. Preference names are deliberately identical to
|
|
3
|
+
# the legacy `spree_avatax_official` integration's, so upgrading a store is a
|
|
4
|
+
# matter of retyping the row — its serialized preferences carry over untouched.
|
|
5
|
+
class Integration < Spree::Integration
|
|
6
|
+
SANDBOX_ENDPOINT = 'https://sandbox-rest.avatax.com'.freeze
|
|
7
|
+
PRODUCTION_ENDPOINT = 'https://rest.avatax.com'.freeze
|
|
8
|
+
|
|
9
|
+
preference :account_number, :string
|
|
10
|
+
preference :license_key, :password
|
|
11
|
+
# Sandbox by default: a merchant connecting for the first time should not be
|
|
12
|
+
# filing live documents before they have looked at one. Avalara publishes
|
|
13
|
+
# these two hosts and no others, and the legacy extension offered the same
|
|
14
|
+
# pair under the same labels, so a merchant sees the choice they already
|
|
15
|
+
# know rather than a URL to type.
|
|
16
|
+
preference :endpoint, :string, default: SANDBOX_ENDPOINT,
|
|
17
|
+
choices: { SANDBOX_ENDPOINT => 'Sandbox',
|
|
18
|
+
PRODUCTION_ENDPOINT => 'Production' }
|
|
19
|
+
preference :company_code, :string
|
|
20
|
+
|
|
21
|
+
# What happens to a placed sale at Avalara. "Don't record" leaves recording to
|
|
22
|
+
# another system, such as an ERP, so only quotes are sent and nothing is
|
|
23
|
+
# filed, voided or credited.
|
|
24
|
+
DOCUMENT_RECORDING_CHOICES = {
|
|
25
|
+
'commit' => 'Record and commit',
|
|
26
|
+
'save' => 'Record without committing',
|
|
27
|
+
'none' => "Don't record"
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
preference :address_validation_enabled, :boolean, default: false
|
|
31
|
+
preference :document_recording, :string, default: 'commit', choices: DOCUMENT_RECORDING_CHOICES
|
|
32
|
+
preference :show_rate_in_label, :boolean, default: false
|
|
33
|
+
# Off by default: requests carry customer addresses, and a guest's customer
|
|
34
|
+
# code is their email.
|
|
35
|
+
preference :logging_enabled, :boolean, default: false
|
|
36
|
+
|
|
37
|
+
# Without these a half-filled credential set saves and then fails at
|
|
38
|
+
# activation with a vendor error, which reads as an Avalara outage rather
|
|
39
|
+
# than a missing field.
|
|
40
|
+
validates :preferred_account_number, :preferred_license_key, :preferred_company_code, presence: true
|
|
41
|
+
|
|
42
|
+
# `choices:` above builds the picker and guards API writes; the allowlist is
|
|
43
|
+
# enforced on the model as well, so no other write path gets past it: the value is handed to the HTTP client along with the credentials,
|
|
44
|
+
# and an arbitrary host turns this into a way to make the server issue
|
|
45
|
+
# requests wherever an admin points it — internal services and cloud
|
|
46
|
+
# metadata included — with the reply readable through the connection test.
|
|
47
|
+
validates :preferred_endpoint, inclusion: { in: [SANDBOX_ENDPOINT, PRODUCTION_ENDPOINT] }
|
|
48
|
+
validates :preferred_document_recording, inclusion: { in: DOCUMENT_RECORDING_CHOICES.keys }
|
|
49
|
+
|
|
50
|
+
def self.integration_group
|
|
51
|
+
'tax'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.integration_name
|
|
55
|
+
'Avalara AvaTax'
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Avalara's own mark, hotlinked. The path is year-versioned, so it may
|
|
59
|
+
# eventually stop resolving; the gallery falls back to a letter avatar for an
|
|
60
|
+
# unreachable logo, so that degrades rather than breaks.
|
|
61
|
+
def self.logo_url
|
|
62
|
+
'https://www.avalara.com/etc.clientlibs/avalara/clientlibs/clientlib-ava-26-site/resources/favicons/favicon.svg'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Fallback for hosts without the gem's translations; the localized
|
|
66
|
+
# description in config/locales wins.
|
|
67
|
+
def self.description
|
|
68
|
+
'Calculate sales tax, VAT and GST at checkout through your Avalara AvaTax account.'
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The connected integration a store calculates tax through, or nil.
|
|
72
|
+
#
|
|
73
|
+
# @param store [Spree::Store, nil]
|
|
74
|
+
# @return [SpreeAvalara::Integration, nil]
|
|
75
|
+
def self.active_for(store)
|
|
76
|
+
store&.integrations&.active&.find_by(type: name)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# @param store [Spree::Store, nil]
|
|
80
|
+
# @return [SpreeAvalara::Integration]
|
|
81
|
+
# @raise [SpreeAvalara::NotConfiguredError] when the store has none
|
|
82
|
+
def self.active_for!(store)
|
|
83
|
+
active_for(store) ||
|
|
84
|
+
raise(SpreeAvalara::NotConfiguredError,
|
|
85
|
+
"Store #{store&.id.inspect} has no active Avalara integration")
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @return [SpreeAvalara::Client]
|
|
89
|
+
def client
|
|
90
|
+
@client ||= SpreeAvalara::Client.new(
|
|
91
|
+
account_number: preferred_account_number,
|
|
92
|
+
license_key: preferred_license_key,
|
|
93
|
+
endpoint: preferred_endpoint,
|
|
94
|
+
company_code: preferred_company_code,
|
|
95
|
+
logging: preferred_logging_enabled
|
|
96
|
+
)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Whether placed sales, cancellations and credits are filed with Avalara.
|
|
100
|
+
#
|
|
101
|
+
# @return [Boolean]
|
|
102
|
+
def records_documents?
|
|
103
|
+
preferred_document_recording != 'none'
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Whether a filed document is committed rather than left Saved.
|
|
107
|
+
#
|
|
108
|
+
# @return [Boolean]
|
|
109
|
+
def commits_documents?
|
|
110
|
+
preferred_document_recording == 'commit'
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Verifies the credentials with a ping, the one AvaTax call that costs
|
|
114
|
+
# nothing and files nothing, then that the account holds the company code.
|
|
115
|
+
# The ping reports authentication rather than raising on a bad key, so a
|
|
116
|
+
# successful response still has to be read.
|
|
117
|
+
#
|
|
118
|
+
# @return [Boolean]
|
|
119
|
+
def can_connect?
|
|
120
|
+
body = client.ping
|
|
121
|
+
|
|
122
|
+
if body.is_a?(Hash) && body['authenticated']
|
|
123
|
+
return true if client.company_exists?(preferred_company_code)
|
|
124
|
+
|
|
125
|
+
self.connection_error_message =
|
|
126
|
+
I18n.t('spree.integrations.avalara.errors.company_not_found', company_code: preferred_company_code)
|
|
127
|
+
return false
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
self.connection_error_message =
|
|
131
|
+
if body.is_a?(Hash) && body.key?('authenticated')
|
|
132
|
+
I18n.t('spree.integrations.avalara.errors.invalid_credentials')
|
|
133
|
+
else
|
|
134
|
+
I18n.t('spree.integrations.avalara.errors.connection_failed')
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
false
|
|
138
|
+
rescue SpreeAvalara::RequestError => error
|
|
139
|
+
self.connection_error_message = request_failure_message(error)
|
|
140
|
+
|
|
141
|
+
false
|
|
142
|
+
rescue StandardError => error
|
|
143
|
+
self.connection_error_message = error.message
|
|
144
|
+
|
|
145
|
+
false
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
# A request with no status was never answered, so the network error is the
|
|
151
|
+
# only thing worth showing. Anything Avalara did answer speaks for itself
|
|
152
|
+
# when it explained why.
|
|
153
|
+
def request_failure_message(error)
|
|
154
|
+
return error.message if error.status.nil?
|
|
155
|
+
|
|
156
|
+
(error.details.is_a?(Hash) && error.details['message'].presence) ||
|
|
157
|
+
I18n.t('spree.integrations.avalara.errors.connection_failed')
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|