Skip to main content

MocAdapter for Tax

MocAdapter Document link

 

Shipping Country and State

Tax Logic Implemented

πŸ”Ή India β†’ 30% Tax

πŸ”Ή USA β†’ 40% Base Country Tax + State Tax

Example State Tax Configuration:

  • Utah β†’ 2%
  • South Dakota β†’ 2.5%
  • Minnesota β†’ 3%
  • North Dakota β†’ 3.5%
  • Nebraska β†’ 4%
  • New Hampshire β†’ 4.5%
  • Idaho β†’ 5%
  • Washington β†’ 5.5%
  • Vermont β†’ 6%

πŸ”Ή Other Countries β†’ Default 20% Tax 

 

global virtual class MockAdapter implements commercetax.TaxEngineAdapter {

global commercetax.TaxEngineResponse processRequest(

commercetax.TaxEngineContext taxEngineContext

) {

commercetax.RequestType requestType =

taxEngineContext.getRequestType();

commercetax.CalculateTaxRequest request =

(commercetax.CalculateTaxRequest)

taxEngineContext.getRequest();

if (request.documentCode == null) {

return new commercetax.ErrorResponse(

commercetax.resultcode.TaxEngineError,

'404',

'documentCode is mandatory'

);

}

if (requestType ==

commercetax.RequestType.CalculateTax) {

commercetax.calculatetaxtype type =

request.taxtype;

String docCode = '';

if (request.DocumentCode != null) {

docCode = request.DocumentCode;

} else if (request.ReferenceEntityId != null) {

docCode = request.ReferenceEntityId;

} else {

docCode = String.valueOf(

getRandomInteger(0, 2147483647)

);

}

commercetax.CalculateTaxResponse response =

new commercetax.CalculateTaxResponse();

if (request.isCommit == true) {

response.setStatus(

commercetax.TaxTransactionStatus.Committed

);

} else {

response.setStatus(

commercetax.TaxTransactionStatus.Uncommitted

);

}

response.setDocumentCode(docCode);

response.setReferenceDocumentCode(

request.referenceDocumentCode

);

response.setTaxType(type);

response.setStatusDescription(

'statusDescription'

);

response.setDescription('description');

response.setEffectiveDate(System.now());

if (request.transactionDate == null) {

response.setTransactionDate(System.now());

} else {

response.setTransactionDate(

request.transactionDate

);

}

if (request.taxTransactionType == null) {

response.setTaxTransactionType(

commercetax.TaxTransactionType.Debit

);

} else {

response.setTaxTransactionType(

request.taxTransactionType

);

}

if (String.isBlank(request.currencyIsoCode)) {

response.setCurrencyIsoCode('USD');

} else {

response.setCurrencyIsoCode(

request.currencyIsoCode

);

}

response.setReferenceEntityId(

request.ReferenceEntityId

);

String country = '';

String shippingState = '';

try {

if (request.ReferenceEntityId != null) {

Id refId = (Id) request.ReferenceEntityId;

String objectName = refId.getSObjectType()

.getDescribe()

.getName();

if (objectName == 'Quote') {

Quote q = [

SELECT ShippingCountry,

ShippingState

FROM Quote

WHERE Id = :refId

LIMIT 1

];

country = q.ShippingCountry;

shippingState = q.ShippingState;

}

else if (objectName == 'Order') {

Order o = [

SELECT ShippingCountry,

ShippingState

FROM Order

WHERE Id = :refId

LIMIT 1

];

country = o.ShippingCountry;

shippingState = o.ShippingState;

}

else if (objectName == 'Invoice') {

try {

List<InvoiceLine> lines = [

SELECT Id,

ShippingAddressId,

ShippingAddress.Country,

ShippingAddress.State

FROM InvoiceLine

WHERE InvoiceId = :refId

AND ShippingAddressId != null

ORDER BY Name ASC

LIMIT 1

];

if (!lines.isEmpty()

&& lines[0].ShippingAddress != null) {

country =

lines[0].ShippingAddress.Country;

shippingState =

lines[0].ShippingAddress.State;

} else {

}

} catch (Exception invoiceHeaderEx) {

}

}

else if (objectName == 'InvoiceLine') {

try {

InvoiceLine il = [

SELECT Id,

ShippingAddressId,

ShippingAddress.Country,

ShippingAddress.State

FROM InvoiceLine

WHERE Id = :refId

LIMIT 1

];

if (il.ShippingAddress != null) {

country =

il.ShippingAddress.Country;

shippingState =

il.ShippingAddress.State;

}

} catch (Exception invoiceEx) {

}

}

else {

}

}

} catch (Exception e) {

}

if (!String.isBlank(country)) {

country = country.trim().toLowerCase();

}

if (!String.isBlank(shippingState)) {

shippingState = shippingState.trim().toLowerCase();

}

Double totalTax = 0.0;

Double totalAmount = 0.0;

List<commercetax.LineItemResponse>

lineItemResponses =

new List<commercetax.LineItemResponse>();

Integer lineCounter = 0;

for (commercetax.TaxLineItemRequest lineItem :

request.lineItems) {

lineCounter++;

commercetax.AddressesResponse addressesRes =

new commercetax.AddressesResponse();

commercetax.AddressResponse addRes =

new commercetax.AddressResponse();

addRes.setLocationCode('locationCode');

addressesRes.setShipFrom(addRes);

addressesRes.setShipTO(addRes);

addressesRes.setSoldTo(addRes);

commercetax.LineItemResponse lineItemResponse =

new commercetax.LineItemResponse();

Double totalLineTax = 0;

List<commercetax.TaxDetailsResponse>

taxDetailsResponses =

new List<commercetax.TaxDetailsResponse>();

for (Integer i = 0; i < 1; i++) {

Double rate = 0.20;

if (country == 'india'

|| country == 'in') {

rate = 0.30;

}

else if (

country == 'usa'

|| country == 'us'

|| country == 'united states'

|| country ==

'united states of america'

) {

Double usaCountryRate = 0.40;

Double stateTaxRate = 0.00;

if (

shippingState == 'utah'

|| shippingState == 'ut'

) {

stateTaxRate = 0.02;

}

else if (

shippingState == 'south dakota'

|| shippingState == 'sd'

) {

stateTaxRate = 0.025;

}

else if (

shippingState == 'minnesota'

|| shippingState == 'mn'

) {

stateTaxRate = 0.03;

}

else if (

shippingState == 'north dakota'

|| shippingState == 'nd'

) {

stateTaxRate = 0.035;

}

else if (

shippingState == 'nebraska'

|| shippingState == 'ne'

) {

stateTaxRate = 0.04;

}

else if (

shippingState == 'new hampshire'

|| shippingState == 'nh'

) {

stateTaxRate = 0.045;

}

else if (

shippingState == 'idaho'

|| shippingState == 'id'

) {

stateTaxRate = 0.05;

}

else if (

shippingState == 'washington'

|| shippingState == 'wa'

) {

stateTaxRate = 0.055;

}

else if (

shippingState == 'vermont'

|| shippingState == 'vt'

) {

stateTaxRate = 0.06;

}

rate =

usaCountryRate +

stateTaxRate;

}

else {

}

Double taxableAmount = lineItem.amount;

commercetax.TaxDetailsResponse

taxDetailsResponse =

new commercetax.TaxDetailsResponse();

taxDetailsResponse.setRate(rate);

taxDetailsResponse.setTaxableAmount(

taxableAmount

);

Double tax =

taxableAmount * rate;

totalLineTax += tax;

taxDetailsResponse.setTax(tax);

taxDetailsResponse.setExemptAmount(0);

taxDetailsResponse.setExemptReason(

'exemptReason'

);

taxDetailsResponse.setTaxRegionId(

'taxRegionId'

);

taxDetailsResponse.setTaxId(

String.valueOf(

getRandomInteger(

0,

2323233

)

)

);

taxDetailsResponse.setSerCode(

'serCode'

);

taxDetailsResponse.setTaxAuthorityTypeId(

'taxAuthorityTypeId'

);

commercetax.ImpositionResponse imposition =

new commercetax.ImpositionResponse();

imposition.setSubType('subtype');

imposition.setType('type');

taxDetailsResponse.setImposition(

imposition

);

commercetax.JurisdictionResponse jurisdiction =

new commercetax.JurisdictionResponse();

jurisdiction.setCountry(country);

jurisdiction.setRegion('region');

jurisdiction.setName('name');

jurisdiction.setStateAssignedNumber(

'stateAssignedNo'

);

jurisdiction.setId('id');

jurisdiction.setLevel('level');

taxDetailsResponse.setJurisdiction(

jurisdiction

);

taxDetailsResponses.add(

taxDetailsResponse

);

}

lineItemResponse.setTaxes(

taxDetailsResponses

);

totalTax += totalLineTax;

totalAmount += lineItem.amount;

commercetax.AmountDetailsResponse amountResponse =

new commercetax.AmountDetailsResponse();

amountResponse.setTotalAmountWithTax(

totalTax + totalAmount

);

amountResponse.setExemptAmount(0);

amountResponse.setTotalAmount(

totalAmount

);

amountResponse.setTaxAmount(

totalTax

);

lineItemResponse.setAmountDetails(

amountResponse

);

lineItemResponse.setEffectiveDate(

System.now()

);

lineItemResponse.setTaxCode(

lineItem.taxCode

);

lineItemResponse.setProductCode(

lineItem.ProductCode

);

lineItemResponse.setLineNumber(

lineItem.linenumber

);

lineItemResponse.setIsTaxable(true);

lineItemResponse.setQuantity(

lineItem.quantity

);

lineItemResponse.setAddresses(

addressesRes

);

lineItemResponses.add(

lineItemResponse

);

}

response.setLineItems(

lineItemResponses

);

commercetax.AmountDetailsResponse

headerAmountResponse =

new commercetax.AmountDetailsResponse();

headerAmountResponse.setTotalAmountWithTax(

totalTax + totalAmount

);

headerAmountResponse.setExemptAmount(0);

headerAmountResponse.setTotalAmount(

totalAmount

);

headerAmountResponse.setTaxAmount(

totalTax

);

response.setAmountDetails(

headerAmountResponse

);

commercetax.AddressesResponse headerAddresses =

new commercetax.AddressesResponse();

commercetax.AddressResponse headerAddRes =

new commercetax.AddressResponse();

headerAddRes.setLocationCode(

'locationCode'

);

headerAddresses.setShipFrom(

headerAddRes

);

headerAddresses.setShipTO(

headerAddRes

);

headerAddresses.setSoldTo(

headerAddRes

);

response.setAddresses(

headerAddresses

);

return response;

}

return null;

}

public static Integer getRandomInteger(

Integer min,

Integer max

) {

return min +

(Integer.valueOf(Math.random()) *

(max - min));

}

}

0/9000