/// <summary>
        /// Handles the <see cref="SalePreparationBase"/> finalizing event.  AvaTax require the "quote" be finalized so that 
        /// it can actually be recorded for reporting purposes.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs<IPaymentResult> args)
        {
            var result = args.Entity;
            var invoice = result.Invoice;

            if (!result.Payment.Success) return;

            var taxation = MerchelloContext.Current.Gateways.Taxation;

            var providerKey = new Guid("DBC48C38-0617-44EA-989A-18AAD8D5DE52");

            IAddress taxAddress = null;
            var shippingItems = invoice.ShippingLineItems().ToArray();
            if (shippingItems.Any())
            {
                var shipment = shippingItems.First().ExtendedData.GetShipment<OrderLineItem>();
                taxAddress = shipment.GetDestinationAddress();
            }

            taxAddress = taxAddress ?? invoice.GetBillingAddress();

            var taxMethod = taxation.GetTaxMethodForTaxAddress(taxAddress);

            // If the taxMethod is not found or 
            if (taxMethod == null || !providerKey.Equals(taxMethod.ProviderKey)) return;

            var provider = taxation.GetProviderByKey(taxMethod.ProviderKey) as AvaTaxTaxationGatewayProvider;

            if (provider == null) return;

            var avaTaxMethod = provider.GetAvaTaxationGatewayMethod(taxMethod);

            avaTaxMethod.CalculateTaxForInvoice(invoice, taxAddress, false);
        }
Esempio n. 2
0
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs <IPaymentResult> args)
        {
            var result = args.Entity;

            if (result.ApproveOrderCreation)
            {
                // order
                var order = result.Invoice.PrepareOrder(MerchelloContext.Current);

                MerchelloContext.Current.Services.OrderService.Save(order);
            }

            var customerKey = result.Invoice.CustomerKey;

            // Clean up the sales prepartation item cache
            if (customerKey == null || Guid.Empty.Equals(customerKey))
            {
                return;
            }
            var customer = MerchelloContext.Current.Services.CustomerService.GetAnyByKey(customerKey.Value);

            if (customer == null)
            {
                return;
            }
            var itemCacheService = MerchelloContext.Current.Services.ItemCacheService;
            var itemCache        = itemCacheService.GetItemCacheByCustomer(customer, ItemCacheType.Checkout);

            itemCacheService.Delete(itemCache);
        }
        /// <summary>
        /// Handles the <see cref="SalePreparationBase"/> finalizing event.  AvaTax require the "quote" be finalized so that
        /// it can actually be recorded for reporting purposes.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs <IPaymentResult> args)
        {
            if (!sender.ApplyTaxesToInvoice)
            {
                return;
            }

            var result  = args.Entity;
            var invoice = result.Invoice;

            if (!result.Payment.Success)
            {
                return;
            }

            var taxation = MerchelloContext.Current.Gateways.Taxation;

            var providerKey = new Guid("DBC48C38-0617-44EA-989A-18AAD8D5DE52");

            IAddress taxAddress    = null;
            var      shippingItems = invoice.ShippingLineItems().ToArray();

            if (shippingItems.Any())
            {
                var shipment = shippingItems.First().ExtendedData.GetShipment <OrderLineItem>();
                taxAddress = shipment.GetDestinationAddress();
            }

            taxAddress = taxAddress ?? invoice.GetBillingAddress();

            var taxMethod = taxation.GetTaxMethodForTaxAddress(taxAddress);

            // If the taxMethod is not found or
            if (taxMethod == null || !providerKey.Equals(taxMethod.ProviderKey))
            {
                return;
            }

            var provider = taxation.GetProviderByKey(taxMethod.ProviderKey) as AvaTaxTaxationGatewayProvider;

            if (provider == null)
            {
                return;
            }

            var avaTaxMethod = provider.GetAvaTaxationGatewayMethod(taxMethod);

            avaTaxMethod.CalculateTaxForInvoice(invoice, taxAddress, false);
        }
Esempio n. 4
0
        /// <summary>
        /// Handles the <see cref="SalePreparationBase"/> finalizing event
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The <see cref="IPaymentResult"/>
        /// </param>
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs<IPaymentResult> args)
        {
            var result = args.Entity;

            var customerKey = result.Invoice.CustomerKey;

            // Clean up the sales prepartation item cache
            if (customerKey == null || Guid.Empty.Equals(customerKey)) return;
            var customer = MerchelloContext.Current.Services.CustomerService.GetAnyByKey(customerKey.Value);

            if (customer == null) return;
            var itemCacheService = MerchelloContext.Current.Services.ItemCacheService;
            var itemCache = itemCacheService.GetItemCacheByCustomer(customer,  ItemCacheType.Checkout);
            itemCacheService.Delete(itemCache);
        }
        /// <summary>
        /// Records the redemption of a coupon offer.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs<IPaymentResult> e)
        {
            var invoice = e.Entity.Invoice;

            // get the collection of redemptions to be record
            var visitor = new CouponRedemptionLineItemVisitor(invoice.CustomerKey);
            invoice.Items.Accept(visitor);

            if (!visitor.Redemptions.Any()) return;

            if (MerchelloContext.Current != null)
            {
                ((ServiceContext)MerchelloContext.Current.Services).OfferRedeemedService.Save(visitor.Redemptions);
            }
            else
            {
                LogHelper.Debug<CouponRedemptionEventHandler>("MerchelloContext was null.  Could not record coupon redemption.");
            }
        }
Esempio n. 6
0
    private void SalePreparationBaseOnInvoicePrepared(SalePreparationBase sender, SalesPreparationEventArgs <IInvoice> e)
    {
        // custom line items
        var ed1 = new ExtendedDataCollection();

        ed1.SetValue(Merchello.Core.Constants.ExtendedDataKeys.Taxable, false.ToString());
        ed1.SetValue(Merchello.Core.Constants.ExtendedDataKeys.Shippable, false.ToString());

        var typeField = EnumTypeFieldConverter.LineItemType.Custom("CcFee");

        //// Act
        var ccFee = new InvoiceLineItem(
            typeField.TypeKey,
            "CC Fee",
            "ccfee",
            1,
            1.0m,
            ed1);

        e.Entity.Items.Add(ccFee);
        e.Entity.Total += 1.0m;

        var ed2 = new ExtendedDataCollection();
        var shippingLineItem = e.Entity.ShippingLineItems().FirstOrDefault();

        if (shippingLineItem != null)
        {
            ////
            if (shippingLineItem.Price >= 30)
            {
                var shipping10Off = new InvoiceLineItem(
                    EnumTypeFieldConverter.LineItemType.Discount.TypeKey,
                    "10 Off Shipping",
                    "BreakOnShipping",
                    1,
                    10M,
                    ed2);
                e.Entity.Items.Add(shipping10Off);
                e.Entity.Total -= 10M;
            }
        }
    }
Esempio n. 7
0
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs <IPaymentResult> salesPreparationEventArgs)
        {
            var result = salesPreparationEventArgs.Entity;

            result.Invoice.AuditCreated();

            if (result.Payment.Success)
            {
                if (result.Invoice.InvoiceStatusKey == Core.Constants.InvoiceStatus.Paid)
                {
                    result.Payment.Result.AuditPaymentCaptured(result.Payment.Result.Amount);
                }
                else
                {
                    result.Payment.Result.AuditPaymentAuthorize(result.Invoice);
                }
            }
            else
            {
                result.Payment.Result.AuditPaymentDeclined();
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Records the redemption of a coupon offer.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs <IPaymentResult> e)
        {
            var invoice = e.Entity.Invoice;

            // get the collection of redemptions to be record
            var visitor = new CouponRedemptionLineItemVisitor(invoice.CustomerKey);

            invoice.Items.Accept(visitor);

            if (!visitor.Redemptions.Any())
            {
                return;
            }

            if (MerchelloContext.Current != null)
            {
                ((ServiceContext)MerchelloContext.Current.Services).OfferRedeemedService.Save(visitor.Redemptions);
            }
            else
            {
                LogHelper.Debug <CouponRedemptionEventHandler>("MerchelloContext was null.  Could not record coupon redemption.");
            }
        }
        /// <summary>
        /// Performs audits on SalePrepartionBase.Finalizing
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="salesPreparationEventArgs">
        /// The sales preparation event args.
        /// </param>
        private void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs<IPaymentResult> salesPreparationEventArgs)
        {
            var result = salesPreparationEventArgs.Entity;

            result.Invoice.AuditCreated();

            if (result.Payment.Success)
            {
                if (result.Invoice.InvoiceStatusKey == Core.Constants.DefaultKeys.InvoiceStatus.Paid)
                {
                    result.Payment.Result.AuditPaymentCaptured(result.Payment.Result.Amount);
                }
                else
                {
                    result.Payment.Result.AuditPaymentAuthorize(result.Invoice);
                }
            }
            else
            {
                result.Payment.Result.AuditPaymentDeclined();
            }
        }
Esempio n. 10
0
        /// <summary>
        /// The sale preparation base on finalizing.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private static void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs <IPaymentResult> e)
        {
            var context = new CheckoutStageResolverContext();

            context.Reset();
        }
 /// <summary>
 /// The sale preparation base on finalizing.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 /// <param name="e">
 /// The e.
 /// </param>
 private static void SalePreparationBaseOnFinalizing(SalePreparationBase sender, SalesPreparationEventArgs<IPaymentResult> e)
 {
     var context = new CheckoutStageResolverContext();
     context.Reset();
 }