Package com.broadleafcommerce.tax.simple
Class SimpleTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse>
- java.lang.Object
-
- com.broadleafcommerce.tax.simple.SimpleTaxProvider<T1,T2>
-
- All Implemented Interfaces:
TaxAccessor<T1,T2>
,TaxProvider<T1,T2>
,org.springframework.core.Ordered
public class SimpleTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse> extends Object implements TaxProvider<T1,T2>
Simple implementation ofTaxProvider
primarily used for estimating taxes, but can be used for providing full taxes for implementors without complex tax needs. Otherwise, aTaxProvider
integrated with a 3rd party, such as Avalara, is preferred. If this simple provider is intended to be used in a live production environment, the implementor must be aware that they own the consequences of tax law and must maintain the tax configuration details. ThisTaxProvider
can also act as a fallback tax provider when used with Circuit Breaker functionality as is the case withDefaultTaxDelegate
.- Author:
- Chad Harchar (charchar), Kelly Tisdell (ktisdell)
-
-
Field Summary
Fields Modifier and Type Field Description static String
SIMPLE_TAX_PROVIDER_ID
-
Constructor Summary
Constructors Constructor Description SimpleTaxProvider(@NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper, @NonNull com.broadleafcommerce.common.extension.TypeFactory typeFactory, @NonNull SimpleTaxProperties properties)
SimpleTaxProvider(@NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper, @NonNull com.broadleafcommerce.common.extension.TypeFactory typeFactory, @NonNull SimpleTaxProperties properties, org.springframework.core.io.ResourceLoader resourceLoader)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
addTaxDetail(SimpleTaxRecord chosenConfig, TaxItem item, TaxInfo taxInfo, BigDecimal rate, boolean taxExempt)
TaxCalculationResponse
adjustTaxes(AdjustTaxTransactionRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
ForTax Accessors
that support "commitTaxes", this allows for adjustments after taxes have been committed.protected String
buildCacheKey(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
TaxCalculationResponse
calculateTaxes(TaxCalculationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Takes theTaxCalculationRequest
and computes sales tax.List<T2>
calculateTaxes(List<T1> taxRequests, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Deprecated.T2
calculateTaxes(T1 taxRequest)
Deprecated.<R extends TaxCalculationRequest>
booleancanHandle(R request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Indicates if thisTaxProvider
can handle requests for the givenTaxCalculationRequest
andContextInfo
, which can be null.CommitTaxResponse
commitTaxes(CommitTaxRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
SomeTaxAccessor
instances allow clients to "commit" taxes, which essentially marks them as being collected and owed to a jurisdiction or tax authority.protected Map<String,SimpleTaxConfig>
createDefaultConfigCache()
Creates aLRUMap
to containSimpleTaxConfig
instances for different applications and / or tenants.protected SimpleTaxConfig
createSimpleTaxConfigFromJson(String json)
protected SimpleTaxConfig
createSimpleTaxConfigFromResourcePath(String resourcePath)
protected SimpleTaxRecord
defaultIfNoneFound(SimpleTaxConfig simpleTaxConfig)
protected BigDecimal
determineRate(TaxItem item, SimpleTaxRecord chosenConfig)
Deprecated.protected BigDecimal
determineRate(TaxItem item, SimpleTaxRecord chosenConfig, boolean taxExempt)
protected Optional<SimpleTaxRecord>
findByCountry(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
protected Optional<SimpleTaxRecord>
findByPostalCode(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
protected Optional<SimpleTaxRecord>
findBySubdivision(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
protected Optional<SimpleTaxRecord>
findBySubdivisionAndCity(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
protected SimpleTaxConfig
getConfigFromCache(String key)
Fetches theSimpleTaxConfig
from cache so that it does not have to be deserialized every time.protected Map<String,SimpleTaxConfig>
getDefaultConfigCache()
protected SimpleTaxConfig
getDefaultSimpleTaxConfig()
protected SimpleTaxConfig
getExampleSimpleTaxConfig()
protected SimpleTaxRecord
getNoDefaultRateFound()
protected com.fasterxml.jackson.databind.ObjectMapper
getObjectMapper()
int
getOrder()
Order this component with a low precedence.String
getProviderId()
This returns a string that is unique to identify eachTaxProvider
.protected org.springframework.core.io.ResourceLoader
getResourceLoader()
protected SimpleTaxProperties
getSimpleTaxProps()
protected SimpleTaxRecord
getTaxConfig(T1 taxRequest, TaxItem item)
Deprecated.useresolveSimpleTaxConfig
protected TaxInfo
getTaxInfo(SimpleTaxRecord chosenConfig, TaxItem item, javax.money.CurrencyUnit currencyUnit)
Deprecated.protected TaxInfo
getTaxInfo(BigDecimal rate, TaxItem item, javax.money.CurrencyUnit currencyUnit)
Deprecated.since 1.7.protected <R extends TaxCalculationRequest>
TaxInfogetTaxInfo(R request, SimpleTaxRecord chosenConfig, TaxItem item, javax.money.CurrencyUnit currencyUnit, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
protected SimpleTaxRecord
getTaxRecord(TaxCalculationGroup group, TaxItem item, SimpleTaxConfig config)
protected SimpleTaxRecord
getTaxRecord(T1 taxRequest, TaxItem item, SimpleTaxConfig config)
Deprecated.protected T2
getTaxResponse(T1 taxRequest)
Deprecated.protected T2
getTaxResponse(T1 taxRequest, SimpleTaxConfig config)
Deprecated.protected com.broadleafcommerce.common.extension.TypeFactory
getTypeFactory()
boolean
isEstimator()
Indicates (or hints) if this provider estimates taxes.protected boolean
isSubdivisionMatch(TaxAddress address, SimpleTaxRecord taxRecord)
protected <R extends TaxCalculationRequest>
booleanisTaxExempt(R request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
protected Optional<SimpleTaxRecord>
lookupTaxConfig(SimpleTaxConfig simpleTaxConfig, TaxAddress toAddress)
protected Optional<SimpleTaxRecord>
lookupTaxConfig(SimpleTaxConfig simpleTaxConfig, TaxAddress toAddress, String lookupKey)
protected void
populateTaxResponse(@NonNull TaxCalculationRequest request, @NonNull TaxCalculationResponse response, @NonNull SimpleTaxConfig config, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
protected <R extends TaxCalculationRequest>
voidpopulateTaxResponseGroup(@NonNull TaxResponseGroup taxResponseGroup, R request, @NonNull TaxCalculationGroup calculationGroup, @NonNull SimpleTaxConfig config, @NonNull javax.money.CurrencyUnit currency, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
protected void
putConfigInCache(@NonNull String key, @NonNull SimpleTaxConfig config)
Puts theSimpleTaxConfig
in a cache so that it does not have to be deserialized every time.protected SimpleTaxConfig
resolveSimpleTaxConfig(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
ReverseTaxTransactionResponse
reverseTaxTransaction(ReverseTaxTransactionRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
ForTax Accessors
that support "commitTaxes", this allows for reversal after taxes have been committed.void
validate(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
This can be used to determine if configurations, credential, and connections are correct and working properly together.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.broadleafcommerce.tax.TaxAccessor
commitTaxes, voidTaxTransaction
-
-
-
-
Field Detail
-
SIMPLE_TAX_PROVIDER_ID
public static final String SIMPLE_TAX_PROVIDER_ID
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SimpleTaxProvider
public SimpleTaxProvider(@NonNull @NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper, @NonNull @NonNull com.broadleafcommerce.common.extension.TypeFactory typeFactory, @NonNull @NonNull SimpleTaxProperties properties)
-
SimpleTaxProvider
public SimpleTaxProvider(@NonNull @NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper, @NonNull @NonNull com.broadleafcommerce.common.extension.TypeFactory typeFactory, @NonNull @NonNull SimpleTaxProperties properties, @Nullable org.springframework.core.io.ResourceLoader resourceLoader)
-
-
Method Detail
-
calculateTaxes
@Deprecated public T2 calculateTaxes(T1 taxRequest)
Deprecated.Description copied from interface:TaxAccessor
Calculate taxes for a request that includes a list of items and address(es).- Specified by:
calculateTaxes
in interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>
- Parameters:
taxRequest
- The request for which to calculate taxes.
-
getTaxResponse
@Deprecated protected T2 getTaxResponse(T1 taxRequest)
Deprecated.
-
isEstimator
public boolean isEstimator()
Description copied from interface:TaxProvider
Indicates (or hints) if this provider estimates taxes. This is similar to (or could be the same as) calculating taxes. This is usually a consideration where a provider prefers not to make a remote call, or where the provider can operate on limited info (e.g. just a state or province or country and not a full address). The default is false.- Specified by:
isEstimator
in interfaceTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse>
- Returns:
-
calculateTaxes
@Deprecated public List<T2> calculateTaxes(List<T1> taxRequests, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Deprecated.The simple tax provider determines tax rates by looking atSimpleTaxProperties.getConfigPath(ContextInfo)
orSimpleTaxProperties.getConfigJsonString(ContextInfo)
and mapping it into the data structureSimpleTaxConfig
. This is then used to determine the most accurate correspondingSimpleTaxRecord
by the following priority, moving to the next item if no matching tax record is found:- Postal code
- City and subdivision (i.e. state, province, or region)
- Subdivision (i.e. state, province, or region)
- Country
- Specified by:
calculateTaxes
in interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>
- Parameters:
taxRequests
- The request for which to calculate taxes.contextInfo
- Context information. May be null.- Returns:
- The
TaxResponse
response containing calculated taxes.
-
getTaxConfig
@Deprecated protected SimpleTaxRecord getTaxConfig(T1 taxRequest, TaxItem item)
Deprecated.useresolveSimpleTaxConfig
Determines the key to use for looking up tax records. This will use the tax code from the item if available. Otherwise, it will use the country code from the taxRequest address. If there is no configuration for the tax code, it will try the country code before returning the default configuration.- Parameters:
taxRequest
- Represents a fulfillment group for tax calculations.item
- The item to be taxed.- Returns:
- A
SimpleTaxRecord
with a tax rate.
-
calculateTaxes
public TaxCalculationResponse calculateTaxes(TaxCalculationRequest request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxAccessor
Takes theTaxCalculationRequest
and computes sales tax. The details of the tax determination are returned via theTaxCalculationResponse
.- Specified by:
calculateTaxes
in interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>
- Parameters:
request
- TaxCalculationRequest, or details about what needs to be taxescontextInfo
- The ContextInfo that determines which tenant and application are making the request- Returns:
-
adjustTaxes
public TaxCalculationResponse adjustTaxes(AdjustTaxTransactionRequest request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxAccessor
ForTax Accessors
that support "commitTaxes", this allows for adjustments after taxes have been committed.- Specified by:
adjustTaxes
in interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>
- Returns:
-
commitTaxes
public CommitTaxResponse commitTaxes(CommitTaxRequest request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxAccessor
SomeTaxAccessor
instances allow clients to "commit" taxes, which essentially marks them as being collected and owed to a jurisdiction or tax authority. This is basically a way of saying, "We collected this much taxes for these products... and therefore we are saving these details for reporting and remittance."- Specified by:
commitTaxes
in interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>
- Returns:
-
reverseTaxTransaction
public ReverseTaxTransactionResponse reverseTaxTransaction(ReverseTaxTransactionRequest request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxAccessor
ForTax Accessors
that support "commitTaxes", this allows for reversal after taxes have been committed.- Specified by:
reverseTaxTransaction
in interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>
- Returns:
-
defaultIfNoneFound
protected SimpleTaxRecord defaultIfNoneFound(SimpleTaxConfig simpleTaxConfig)
-
getNoDefaultRateFound
protected SimpleTaxRecord getNoDefaultRateFound()
-
populateTaxResponse
protected void populateTaxResponse(@NonNull @NonNull TaxCalculationRequest request, @NonNull @NonNull TaxCalculationResponse response, @NonNull @NonNull SimpleTaxConfig config, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
populateTaxResponseGroup
protected <R extends TaxCalculationRequest> void populateTaxResponseGroup(@NonNull @NonNull TaxResponseGroup taxResponseGroup, @NonNull R request, @NonNull @NonNull TaxCalculationGroup calculationGroup, @NonNull @NonNull SimpleTaxConfig config, @NonNull @NonNull javax.money.CurrencyUnit currency, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
getTaxRecord
protected SimpleTaxRecord getTaxRecord(TaxCalculationGroup group, TaxItem item, SimpleTaxConfig config)
-
getTaxResponse
@Deprecated protected T2 getTaxResponse(T1 taxRequest, SimpleTaxConfig config)
Deprecated.
-
getTaxRecord
@Deprecated protected SimpleTaxRecord getTaxRecord(T1 taxRequest, TaxItem item, SimpleTaxConfig config)
Deprecated.Determines the key to use for looking up tax records. This will use the tax code from the item if available. Otherwise, it will use the country code from the taxRequest address. If there is no configuration for the tax code, it will try the country code before returning the default configuration.- Parameters:
taxRequest
- Represents a fulfillment group for tax calculations.item
- The item to be taxed.- Returns:
- A
SimpleTaxRecord
with a tax rate.
-
isTaxExempt
protected <R extends TaxCalculationRequest> boolean isTaxExempt(R request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
getTaxInfo
protected <R extends TaxCalculationRequest> TaxInfo getTaxInfo(R request, SimpleTaxRecord chosenConfig, TaxItem item, javax.money.CurrencyUnit currencyUnit, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
getTaxInfo
@Deprecated protected TaxInfo getTaxInfo(SimpleTaxRecord chosenConfig, TaxItem item, javax.money.CurrencyUnit currencyUnit)
Deprecated.
-
addTaxDetail
protected void addTaxDetail(SimpleTaxRecord chosenConfig, TaxItem item, TaxInfo taxInfo, BigDecimal rate, boolean taxExempt)
-
determineRate
protected BigDecimal determineRate(TaxItem item, SimpleTaxRecord chosenConfig, boolean taxExempt)
-
determineRate
@Deprecated protected BigDecimal determineRate(TaxItem item, SimpleTaxRecord chosenConfig)
Deprecated.
-
getTaxInfo
@Deprecated protected TaxInfo getTaxInfo(BigDecimal rate, TaxItem item, javax.money.CurrencyUnit currencyUnit)
Deprecated.since 1.7. UsegetTaxInfo(SimpleTaxRecord, TaxItem, CurrencyUnit)
.- Parameters:
rate
- The tax rate to use during calculations.item
- The item to be taxed.currencyUnit
- The currency of the monetary amounts.
-
lookupTaxConfig
protected Optional<SimpleTaxRecord> lookupTaxConfig(SimpleTaxConfig simpleTaxConfig, TaxAddress toAddress)
-
lookupTaxConfig
protected Optional<SimpleTaxRecord> lookupTaxConfig(SimpleTaxConfig simpleTaxConfig, TaxAddress toAddress, String lookupKey)
-
findByCountry
protected Optional<SimpleTaxRecord> findByCountry(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
-
findBySubdivision
protected Optional<SimpleTaxRecord> findBySubdivision(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
-
findByPostalCode
protected Optional<SimpleTaxRecord> findByPostalCode(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
-
findBySubdivisionAndCity
protected Optional<SimpleTaxRecord> findBySubdivisionAndCity(List<SimpleTaxRecord> taxTable, TaxAddress toAddress)
-
isSubdivisionMatch
protected boolean isSubdivisionMatch(TaxAddress address, SimpleTaxRecord taxRecord)
-
getProviderId
public final String getProviderId()
Description copied from interface:TaxProvider
This returns a string that is unique to identify eachTaxProvider
. Broadleaf implementations will use "BLC_" as a prefix. Custom implementations should NOT use this prefix.- Specified by:
getProviderId
in interfaceTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse>
- Returns:
-
canHandle
public <R extends TaxCalculationRequest> boolean canHandle(R request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxProvider
Indicates if thisTaxProvider
can handle requests for the givenTaxCalculationRequest
andContextInfo
, which can be null.- Specified by:
canHandle
in interfaceTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse>
- Parameters:
request
- the tax request being- Returns:
-
resolveSimpleTaxConfig
protected SimpleTaxConfig resolveSimpleTaxConfig(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
createSimpleTaxConfigFromJson
protected SimpleTaxConfig createSimpleTaxConfigFromJson(String json)
-
createSimpleTaxConfigFromResourcePath
protected SimpleTaxConfig createSimpleTaxConfigFromResourcePath(String resourcePath)
-
putConfigInCache
protected void putConfigInCache(@NonNull @NonNull String key, @NonNull @NonNull SimpleTaxConfig config)
Puts theSimpleTaxConfig
in a cache so that it does not have to be deserialized every time. This can be overridden to use a different cache implementation.- Parameters:
key
- usually a combination of tenantId and applicationId or placeholdersconfig
-- See Also:
getConfigFromCache(String)
-
getConfigFromCache
@Nullable protected SimpleTaxConfig getConfigFromCache(String key)
Fetches theSimpleTaxConfig
from cache so that it does not have to be deserialized every time. This can be overridden to use a different cache implementation.- Parameters:
key
-- Returns:
- See Also:
putConfigInCache(String, SimpleTaxConfig)
-
buildCacheKey
protected String buildCacheKey(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
createDefaultConfigCache
protected Map<String,SimpleTaxConfig> createDefaultConfigCache()
Creates aLRUMap
to containSimpleTaxConfig
instances for different applications and / or tenants. Default max size of the cache is 5000. Default initial size is 10. Note that if you plan to use a different cache mechanism, you can optionally override this method to return null as it will no longer be used.- Returns:
- See Also:
getConfigFromCache(String)
,putConfigInCache(String, SimpleTaxConfig)
-
getOrder
public int getOrder()
Order this component with a low precedence. However, since we want to give others room to inject or order another component with lower precedence, this is defined asOrdered.LOWEST_PRECEDENCE
minus 1000, which allows for 1000 spots to be lower precedence, if needed.- Specified by:
getOrder
in interfaceorg.springframework.core.Ordered
- Specified by:
getOrder
in interfaceTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse>
- Returns:
-
validate
public void validate(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxProvider
This can be used to determine if configurations, credential, and connections are correct and working properly together. This should be called by the Admin UI or other methods to simply validate whether connection and credential details are correct. If everything is working, this is a pass-through method. If something is not working, then aRuntimeException
should be thrown, typically anIllegalStateException
,IllegalArgumentException
, orUnsupportedOperationException
. This method may be invoked by a call from the Admin or other systems to ensure that the configuration is correct.- Specified by:
validate
in interfaceTaxProvider<T1 extends TaxRequest,T2 extends TaxResponse>
-
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
-
getTypeFactory
protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
-
getSimpleTaxProps
protected SimpleTaxProperties getSimpleTaxProps()
-
getResourceLoader
protected org.springframework.core.io.ResourceLoader getResourceLoader()
-
getExampleSimpleTaxConfig
protected SimpleTaxConfig getExampleSimpleTaxConfig()
-
getDefaultSimpleTaxConfig
protected SimpleTaxConfig getDefaultSimpleTaxConfig()
-
getDefaultConfigCache
protected Map<String,SimpleTaxConfig> getDefaultConfigCache()
-
-