Example #1
0
 public void Can_load_taxProvider_by_systemKeyword()
 {
     RunWithTestServiceProvider(() =>
     {
         var provider = _taxPluginManager.LoadPluginBySystemName("FixedTaxRateTest");
         provider.Should().NotBeNull();
     });
 }
Example #2
0
        /// <summary>
        /// Handle order deleted event
        /// </summary>
        /// <param name="eventMessage">Event message</param>
        public void HandleEvent(EntityDeletedEvent <Order> eventMessage)
        {
            if (eventMessage.Entity == null)
            {
                return;
            }

            //ensure that Avalara tax provider is active
            if (!(_taxPluginManager.LoadPluginBySystemName(AvalaraTaxDefaults.SystemName) is AvalaraTaxProvider
                  taxProvider) || !_taxPluginManager.IsPluginActive(taxProvider))
            {
                return;
            }

            //delete tax transaction
            taxProvider.DeleteTaxTransaction(eventMessage.Entity);
        }
        public IActionResult TestTaxRequest(ConfigurationModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return(AccessDeniedView());
            }

            if (!ModelState.IsValid)
            {
                return(Configure());
            }

            var taxProvider = _taxPluginManager.LoadPluginBySystemName(AvalaraTaxDefaults.SystemName) as AvalaraTaxProvider;

            //get result
            var transaction = taxProvider.CreateEstimatedTaxTransaction(new Address
            {
                City          = model.TestAddress?.City,
                Country       = _countryService.GetCountryById(model.TestAddress?.CountryId ?? 0),
                Address1      = model.TestAddress?.Address1,
                ZipPostalCode = model.TestAddress?.ZipPostalCode,
                StateProvince = _stateProvinceService.GetStateProvinceById(model.TestAddress?.StateProvinceId ?? 0)
            }, _workContext.CurrentCustomer.Id.ToString());

            if (transaction?.totalTax != null)
            {
                //display tax rates by jurisdictions
                model.TestTaxResult = $"Total tax rate: {transaction.totalTax:0.00}% {Environment.NewLine}";
                if (transaction.summary?.Any() ?? false)
                {
                    model.TestTaxResult = transaction.summary.Aggregate(model.TestTaxResult, (resultString, rate) =>
                                                                        $"{resultString}Jurisdiction: {rate?.jurisName}, Tax rate: {((rate?.rate ?? 0) * 100):0.00}% {Environment.NewLine}");
                }
                _notificationService.SuccessNotification(_localizationService.GetResource("Plugins.Tax.Avalara.TestTax.Success"));
            }
            else
            {
                _notificationService.ErrorNotification(_localizationService.GetResource("Plugins.Tax.Avalara.TestTax.Error"));
            }

            //prepare model
            model.IsConfigured = IsConfigured();
            PrepareAddress(model.TestAddress);
            PrepareLogModel(model.TaxTransactionLogSearchModel);
            model.HideGeneralBlock = _genericAttributeService.GetAttribute <bool>(_workContext.CurrentCustomer, AvalaraTaxDefaults.HideGeneralBlock);
            model.HideLogBlock     = _genericAttributeService.GetAttribute <bool>(_workContext.CurrentCustomer, AvalaraTaxDefaults.HideLogBlock);

            return(View("~/Plugins/Tax.Avalara/Views/Configuration/Configure.cshtml", model));
        }
        public virtual IActionResult MarkAsPrimaryProvider(string systemName)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageTaxSettings))
            {
                return(AccessDeniedView());
            }

            if (string.IsNullOrEmpty(systemName))
            {
                return(RedirectToAction("Providers"));
            }

            var taxProvider = _taxPluginManager.LoadPluginBySystemName(systemName);

            if (taxProvider == null)
            {
                return(RedirectToAction("Providers"));
            }

            _taxSettings.ActiveTaxProviderSystemName = systemName;
            _settingService.SaveSetting(_taxSettings);

            return(RedirectToAction("Providers"));
        }
        /// <summary>
        /// Prepare tax details by Avalara tax service
        /// </summary>
        /// <param name="cart">Shopping cart</param>
        private void PrepareTaxDetails(IList <ShoppingCartItem> cart)
        {
            //ensure that Avalara tax provider is active
            var taxProvider = _taxPluginManager
                              .LoadPluginBySystemName(AvalaraTaxDefaults.SystemName, _workContext.CurrentCustomer, _storeContext.CurrentStore.Id)
                              as AvalaraTaxProvider;

            if (!_taxPluginManager.IsPluginActive(taxProvider))
            {
                return;
            }

            //create dummy order for the tax request
            var order = new Order {
                Customer = _workContext.CurrentCustomer
            };

            //addresses
            order.BillingAddress  = _workContext.CurrentCustomer.BillingAddress;
            order.ShippingAddress = _workContext.CurrentCustomer.ShippingAddress;
            if (_shippingSettings.AllowPickupInStore)
            {
                var pickupPoint = _genericAttributeService.GetAttribute <PickupPoint>(_workContext.CurrentCustomer,
                                                                                      NopCustomerDefaults.SelectedPickupPointAttribute, _storeContext.CurrentStore.Id);
                if (pickupPoint != null)
                {
                    var country = _countryService.GetCountryByTwoLetterIsoCode(pickupPoint.CountryCode);
                    order.PickupAddress = new Address
                    {
                        Address1      = pickupPoint.Address,
                        City          = pickupPoint.City,
                        Country       = country,
                        StateProvince = _stateProvinceService.GetStateProvinceByAbbreviation(pickupPoint.StateAbbreviation, country?.Id),
                        ZipPostalCode = pickupPoint.ZipPostalCode,
                        CreatedOnUtc  = DateTime.UtcNow,
                    };
                }
            }

            //checkout attributes
            order.CheckoutAttributesXml = _genericAttributeService.GetAttribute <string>(_workContext.CurrentCustomer,
                                                                                         NopCustomerDefaults.CheckoutAttributes, _storeContext.CurrentStore.Id);

            //shipping method
            var shippingRateComputationMethods = _shippingPluginManager.LoadActivePlugins(_workContext.CurrentCustomer, _storeContext.CurrentStore.Id);

            order.OrderShippingExclTax = _orderTotalCalculationService.GetShoppingCartShippingTotal(cart, false, shippingRateComputationMethods) ?? 0;
            order.ShippingMethod       = _genericAttributeService.GetAttribute <ShippingOption>(_workContext.CurrentCustomer,
                                                                                                NopCustomerDefaults.SelectedShippingOptionAttribute, _storeContext.CurrentStore.Id)?.Name;

            //payment method
            var paymentMethod = _genericAttributeService.GetAttribute <string>(_workContext.CurrentCustomer,
                                                                               NopCustomerDefaults.SelectedPaymentMethodAttribute, _storeContext.CurrentStore.Id);
            var paymentFee = _paymentService.GetAdditionalHandlingFee(cart, paymentMethod);

            order.PaymentMethodAdditionalFeeExclTax = _taxService.GetPaymentMethodAdditionalFee(paymentFee, false, _workContext.CurrentCustomer);
            order.PaymentMethodSystemName           = paymentMethod;

            //add discount amount
            _orderTotalCalculationService.GetShoppingCartSubTotal(cart, false, out var orderSubTotalDiscountExclTax, out _, out _, out _);
            order.OrderSubTotalDiscountExclTax = orderSubTotalDiscountExclTax;

            //create dummy order items
            foreach (var cartItem in cart)
            {
                var orderItem = new OrderItem
                {
                    AttributesXml = cartItem.AttributesXml,
                    Product       = cartItem.Product,
                    Quantity      = cartItem.Quantity
                };

                var itemSubtotal = _priceCalculationService.GetSubTotal(cartItem, true, out _, out _, out _);
                orderItem.PriceExclTax = _taxService.GetProductPrice(cartItem.Product, itemSubtotal, false, _workContext.CurrentCustomer, out _);

                order.OrderItems.Add(orderItem);
            }

            //get tax details
            var taxTransaction = taxProvider.CreateOrderTaxTransaction(order, false);

            if (taxTransaction == null)
            {
                return;
            }

            //and save it for the further usage
            var taxDetails = new TaxDetails {
                TaxTotal = taxTransaction.totalTax
            };

            foreach (var item in taxTransaction.summary)
            {
                if (!item.rate.HasValue || !item.tax.HasValue)
                {
                    continue;
                }

                var taxRate  = item.rate.Value * 100;
                var taxValue = item.tax.Value;

                if (!taxDetails.TaxRates.ContainsKey(taxRate))
                {
                    taxDetails.TaxRates.Add(taxRate, taxValue);
                }
                else
                {
                    taxDetails.TaxRates[taxRate] = taxDetails.TaxRates[taxRate] + taxValue;
                }
            }
            _httpContextAccessor.HttpContext.Session.Set(AvalaraTaxDefaults.TaxDetailsSessionValue, taxDetails);
        }
        public void Can_load_taxProvider_by_systemKeyword()
        {
            var provider = _taxPluginManager.LoadPluginBySystemName("FixedTaxRateTest");

            provider.ShouldNotBeNull();
        }