Esempio n. 1
0
        private CartLineComponent CreateLine(Cart cart, CommerceContext commerceContext, string targetItemId, decimal quantity)
        {
            SellableItem gift = AsyncHelper.RunSync(() => _getCommand.Process(commerceContext, targetItemId, false));

            if (gift == null)
            {
                return(null);
            }

            // To make sure all pipeline blocks are executed and do not influence the current cart, we add
            // the gift line to a temporary cart and then copy it to the current cart.
            var temporaryCart = cart.Clone <Cart>();

            temporaryCart.AddComponents(new TemporaryCartComponent(cart.Id));

            var freeGift = new CartLineComponent
            {
                ItemId   = targetItemId,
                Quantity = quantity
            };

            temporaryCart = AsyncHelper.RunSync(() => _addCommand.Process(commerceContext, temporaryCart, freeGift));

            CartLineComponent line = temporaryCart.Lines.Single(x => x.ItemId == targetItemId);

            if (gift.ListPrice.Amount > 0)
            {
                decimal discount = new MoneyEx(commerceContext, gift.ListPrice).Round().Value.Amount;

                line.Adjustments.Add(AwardedAdjustmentFactory.CreateLineLevelAwardedAdjustment(discount * -1,
                                                                                               nameof(CartFreeGiftAction), line.Id, commerceContext));
            }

            return(line);
        }
        public override async Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context)
        {
            if (entityView == null ||
                !entityView.Action.Equals(context.GetPolicy <CartUiPolicy>().AddLineItemActionName, StringComparison.OrdinalIgnoreCase))
            {
                return(entityView);
            }

            try
            {
                var cart = await _getCartCommand.Process(context.CommerceContext, entityView.EntityId, false);

                if (cart == null || cart.DateCreated == cart.DateUpdated)
                {
                    return(entityView);
                }

                var catalog   = entityView.Properties[1];
                var productId = entityView.Properties[2];
                var variantId = entityView.Properties[3];
                var quantity  = entityView.Properties[4];

                cart = await _addCartLineCommand.Process(context.CommerceContext, entityView.EntityId, new CartLineComponent()
                {
                    ItemId   = string.Join("|", catalog.Value, productId.Value, variantId.Value),
                    Quantity = int.Parse(quantity.Value)
                });
            }

            catch (Exception ex)
            {
                context.Logger.LogError($"{this.Name}.PathNotFound: Message={ex.Message}");
            }

            return(entityView);
        }
Esempio n. 3
0
        public async Task <IActionResult> Put([FromBody] ODataActionParameters value)
        {
            if (!this.ModelState.IsValid || value == null)
            {
                return((IActionResult) new BadRequestObjectResult((object)value));
            }

            string id = value["customerId"].ToString();

            if (!this.ModelState.IsValid || string.IsNullOrEmpty(id))
            {
                return((IActionResult)this.NotFound());
            }


            GetCustomerCommand command0 = this.Command <GetCustomerCommand>();

            Customer customer = await command0.Process(this.CurrentContext, id);

            var y = customer.GetComponent <AddressComponent>();


            //refactor
            string randoms = Guid.NewGuid().ToString().Replace("-", string.Empty).Replace("+", string.Empty);

            string  cartId = string.Format("{0}{1}", "CUSTOM", randoms);
            string  str    = value["itemId"].ToString();
            Decimal result;
            string  q = "1";



            if (!Decimal.TryParse(q, out result))
            {
                return((IActionResult) new BadRequestObjectResult((object)q));
            }


            AddCartLineCommand command = this.Command <AddCartLineCommand>();
            CartLineComponent  line    = new CartLineComponent()
            {
                ItemId   = str,
                Quantity = result
            };
            Cart cart = await command.Process(this.CurrentContext, cartId, line);

            //FulfillmentComponent

            FulfillmentComponent fulfillment = (PhysicalFulfillmentComponent) new PhysicalFulfillmentComponent()
            {
                ShippingParty = new Party()
                {
                    Address1      = y.Party.Address1,
                    City          = y.Party.City,
                    ZipPostalCode = y.Party.ZipPostalCode,
                    State         = y.Party.State,
                    StateCode     = y.Party.StateCode,
                    CountryCode   = y.Party.CountryCode,
                    AddressName   = y.Party.AddressName,
                    Name          = y.Party.Name
                },
                FulfillmentMethod = new EntityReference()
                {
                    Name         = "Ground",
                    EntityTarget = "B146622D-DC86-48A3-B72A-05EE8FFD187A"
                }
            };

            SetCartFulfillmentCommand _CartFulfillmentCommand = this.Command <SetCartFulfillmentCommand>();
            Cart cart1 = await _CartFulfillmentCommand.Process(CurrentContext, cartId, fulfillment);


            //FederatedPaymentComponent


            decimal gt;

            Decimal.TryParse(cart1.Totals.GrandTotal.Amount.ToString(), out gt);



            FederatedPaymentComponent paymentComponent = new FederatedPaymentComponent(new Money()
            {
                Amount = gt
            });

            paymentComponent.PaymentMethod = new EntityReference()
            {
                EntityTarget = "0CFFAB11-2674-4A18-AB04-228B1F8A1DEC",
                Name         = "Federated"
            };

            paymentComponent.PaymentMethodNonce = "fake-valid-nonce";

            paymentComponent.BillingParty = new Party()
            {
                Address1      = y.Party.Address1,
                City          = y.Party.City,
                ZipPostalCode = y.Party.ZipPostalCode,
                State         = y.Party.State,
                StateCode     = y.Party.StateCode,
                CountryCode   = y.Party.CountryCode,
                AddressName   = y.Party.AddressName,
            };

            AddPaymentsCommand _PaymentsCommand = this.Command <AddPaymentsCommand>();
            Cart cart2 = await _PaymentsCommand.Process(this.CurrentContext, cartId, (IEnumerable <PaymentComponent>) new List <PaymentComponent>()
            {
                (PaymentComponent)paymentComponent
            });

            //CreateOrderCommand


            string email = customer.Email;


            CreateOrderCommand _CreateOrderCommand = this.Command <CreateOrderCommand>();


            Order order = await _CreateOrderCommand.Process(this.CurrentContext, cartId, email);



            return((IActionResult) new ObjectResult((object)_CreateOrderCommand));
        }