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 ofTaxProviderprimarily used for estimating taxes, but can be used for providing full taxes for implementors without complex tax needs. Otherwise, aTaxProviderintegrated 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. ThisTaxProvidercan 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 StringSIMPLE_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 voidaddTaxDetail(SimpleTaxRecord chosenConfig, TaxItem item, TaxInfo taxInfo, BigDecimal rate, boolean taxExempt)TaxCalculationResponseadjustTaxes(AdjustTaxTransactionRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)ForTax Accessorsthat support "commitTaxes", this allows for adjustments after taxes have been committed.protected StringbuildCacheKey(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)TaxCalculationResponsecalculateTaxes(TaxCalculationRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Takes theTaxCalculationRequestand computes sales tax.List<T2>calculateTaxes(List<T1> taxRequests, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Deprecated.T2calculateTaxes(T1 taxRequest)Deprecated.<R extends TaxCalculationRequest>
booleancanHandle(R request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Indicates if thisTaxProvidercan handle requests for the givenTaxCalculationRequestandContextInfo, which can be null.CommitTaxResponsecommitTaxes(CommitTaxRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)SomeTaxAccessorinstances 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 aLRUMapto containSimpleTaxConfiginstances for different applications and / or tenants.protected SimpleTaxConfigcreateSimpleTaxConfigFromJson(String json)protected SimpleTaxConfigcreateSimpleTaxConfigFromResourcePath(String resourcePath)protected SimpleTaxRecorddefaultIfNoneFound(SimpleTaxConfig simpleTaxConfig)protected BigDecimaldetermineRate(TaxItem item, SimpleTaxRecord chosenConfig)Deprecated.protected BigDecimaldetermineRate(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 SimpleTaxConfiggetConfigFromCache(String key)Fetches theSimpleTaxConfigfrom cache so that it does not have to be deserialized every time.protected Map<String,SimpleTaxConfig>getDefaultConfigCache()protected SimpleTaxConfiggetDefaultSimpleTaxConfig()protected SimpleTaxConfiggetExampleSimpleTaxConfig()protected SimpleTaxRecordgetNoDefaultRateFound()protected com.fasterxml.jackson.databind.ObjectMappergetObjectMapper()intgetOrder()Order this component with a low precedence.StringgetProviderId()This returns a string that is unique to identify eachTaxProvider.protected org.springframework.core.io.ResourceLoadergetResourceLoader()protected SimpleTaxPropertiesgetSimpleTaxProps()protected SimpleTaxRecordgetTaxConfig(T1 taxRequest, TaxItem item)Deprecated.useresolveSimpleTaxConfigprotected TaxInfogetTaxInfo(SimpleTaxRecord chosenConfig, TaxItem item, javax.money.CurrencyUnit currencyUnit)Deprecated.protected TaxInfogetTaxInfo(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 SimpleTaxRecordgetTaxRecord(TaxCalculationGroup group, TaxItem item, SimpleTaxConfig config)protected SimpleTaxRecordgetTaxRecord(T1 taxRequest, TaxItem item, SimpleTaxConfig config)Deprecated.protected T2getTaxResponse(T1 taxRequest)Deprecated.protected T2getTaxResponse(T1 taxRequest, SimpleTaxConfig config)Deprecated.protected com.broadleafcommerce.common.extension.TypeFactorygetTypeFactory()booleanisEstimator()Indicates (or hints) if this provider estimates taxes.protected booleanisSubdivisionMatch(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 voidpopulateTaxResponse(@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 voidputConfigInCache(@NonNull String key, @NonNull SimpleTaxConfig config)Puts theSimpleTaxConfigin a cache so that it does not have to be deserialized every time.protected SimpleTaxConfigresolveSimpleTaxConfig(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)ReverseTaxTransactionResponsereverseTaxTransaction(ReverseTaxTransactionRequest request, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)ForTax Accessorsthat support "commitTaxes", this allows for reversal after taxes have been committed.voidvalidate(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:TaxAccessorCalculate taxes for a request that includes a list of items and address(es).- Specified by:
calculateTaxesin 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:TaxProviderIndicates (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:
isEstimatorin 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 correspondingSimpleTaxRecordby 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:
calculateTaxesin interfaceTaxAccessor<T1 extends TaxRequest,T2 extends TaxResponse>- Parameters:
taxRequests- The request for which to calculate taxes.contextInfo- Context information. May be null.- Returns:
- The
TaxResponseresponse containing calculated taxes.
-
getTaxConfig
@Deprecated protected SimpleTaxRecord getTaxConfig(T1 taxRequest, TaxItem item)
Deprecated.useresolveSimpleTaxConfigDetermines 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
SimpleTaxRecordwith a tax rate.
-
calculateTaxes
public TaxCalculationResponse calculateTaxes(TaxCalculationRequest request, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:TaxAccessorTakes theTaxCalculationRequestand computes sales tax. The details of the tax determination are returned via theTaxCalculationResponse.- Specified by:
calculateTaxesin 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:TaxAccessorForTax Accessorsthat support "commitTaxes", this allows for adjustments after taxes have been committed.- Specified by:
adjustTaxesin 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:TaxAccessorSomeTaxAccessorinstances 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:
commitTaxesin 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:TaxAccessorForTax Accessorsthat support "commitTaxes", this allows for reversal after taxes have been committed.- Specified by:
reverseTaxTransactionin 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
SimpleTaxRecordwith 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:TaxProviderThis 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:
getProviderIdin 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:TaxProviderIndicates if thisTaxProvidercan handle requests for the givenTaxCalculationRequestandContextInfo, which can be null.- Specified by:
canHandlein 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 theSimpleTaxConfigin 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 theSimpleTaxConfigfrom 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 aLRUMapto containSimpleTaxConfiginstances 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_PRECEDENCEminus 1000, which allows for 1000 spots to be lower precedence, if needed.- Specified by:
getOrderin interfaceorg.springframework.core.Ordered- Specified by:
getOrderin 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:TaxProviderThis 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 aRuntimeExceptionshould 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:
validatein 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()
-
-