Skip to content

Avalara Tax Provider Refactoring#1

Merged
frederik5480 merged 3 commits intomainfrom
ssm/23797-Avalar-Refactoring
May 20, 2025
Merged

Avalara Tax Provider Refactoring#1
frederik5480 merged 3 commits intomainfrom
ssm/23797-Avalar-Refactoring

Conversation

@StanislavSmetaninSSM
Copy link
Contributor

@StanislavSmetaninSSM StanislavSmetaninSSM commented May 13, 2025

Refactored code.

Removed external Avalara package. We don't use external API helper packages in any other our projects.
Refactored code to use our common Client-Server communication layer which we use in other payment/shipping providers.
Added logging of each data we send to server and the data which we get from server.
Some other related changes also were done, but they are mainly related with parameters processing...

Added logo.
Used Net 8.0 Framework.

Removed "Tax Service Url" - we don't need this anymore...
Removed "BoundaryLevel" - it wasn't used in previous code.
Added "Test Mode" checkbox to use sandbox environment for test. Uncheck for production.

The settings of provider now looks like this:

image

Original task: User Story 23797. Avalara Avatax Tax Method

…d we don't use external API packages in other places of our project)

Refactored code
{
public static string SendRequest(string accountId, string licenseKey, string apiUrl, CommandConfiguration configuration)
{
using (var messageHandler = GetMessageHandler())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like these usings are for the whole method, we can avoid a lot of indentation here, making it more readable;
using var messageHandler = GetMessageHandler();
using var client = new HttpClient(messageHandler);
etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

TransactionType.Adjust or TransactionType.Commit => DocumentType.SalesInvoice,
TransactionType.Calculate => DocumentType.SalesOrder,
TransactionType.ProductReturns => DocumentType.ReturnInvoice,
_ => throw new ArgumentOutOfRangeException(nameof(transactionType), $"Unknown or unsupported transaction type: {transactionType}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we throw a NotImplementedException as a default case if we're not just returning null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{
if (Provider.IsTaxableTypeInternal(orderLine) || orderLine.HasType(OrderLineType.PointProduct))
{
if (orderLine.Product is not null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it would be good to avoid indentation in places like this, and just say
if (orderLine.Product is null)
continue;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


private void SetCustomerExemptionData(CreateTransactionRequest request)
{
if (Order.CustomerAccessUserId > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Order.CustomerAccessUserId <= 0)
return;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

nameof(CustomerCodeSource.AccessUserExternalId) => Order.CustomerAccessUserId > 0
? UserManagementServices.Users.GetUserById(Order.CustomerAccessUserId)?.ExternalID ?? ""
: string.Empty,
_ => throw new Exception($"Unsupported option is used: {Provider.GetCustomerCodeFrom}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotImplementedException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ApiCommand.CreateTransaction => GetCommandLink("transactions/create"),
ApiCommand.ResolveAddress => GetCommandLink("addresses/resolve", queryParameters),
ApiCommand.VoidTransaction => GetCommandLink($"companies/{operatorId}/transactions/{operatorSecondId}/void"),
_ => throw new NotSupportedException($"The api command is not supported. Command: {command}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotImplementedException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

//POST
ApiCommand.CreateTransaction or
ApiCommand.VoidTransaction => client.PostAsync(apiCommand, GetContent()),
_ => throw new NotSupportedException($"Unknown operation was used. The operation code: {configuration.CommandType}.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotImplementedException

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@frederik5480 frederik5480 merged commit 5939683 into main May 20, 2025
1 check passed
@frederik5480 frederik5480 deleted the ssm/23797-Avalar-Refactoring branch May 20, 2025 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants