示例#1
0
        public async Task <Order> CreatePaypalOrder()
        {
            var     orderItems = new List <Item>();
            decimal totalPrice = 0;

            using (var context = new DbContext())
            {
                var customer    = _accountService.ValidateCustomerSession(Request.Cookies, context, true);
                var itemsInCart = context.CartItems
                                  .Where(x => x.SessionId == customer.Account.SessionId)
                                  .Include(x => x.Item)
                                  .Where(x => !x.Item.Hidden)
                                  .Select(cartItem => cartItem.Item).Include(x => x.Seller)
                                  .ToList();

                foreach (var item in itemsInCart)
                {
                    totalPrice += item.Price;

                    // Limit description and title size so that Paypal doesn't refuse it
                    const int maxStrSize      = 100;
                    var       itemDescription = "Sold by " + item.Seller.Name + ". " + item.Description;
                    if (itemDescription.Length > maxStrSize)
                    {
                        itemDescription = itemDescription.Substring(0, maxStrSize);
                    }
                    var itemName = item.Name;
                    if (itemName.Length > maxStrSize)
                    {
                        itemName = itemName.Substring(0, maxStrSize);
                    }

                    orderItems.Add(new Item
                    {
                        Name        = itemName,
                        Sku         = item.Id.ToString(),
                        Quantity    = 1.ToString(),
                        Category    = "PHYSICAL_GOODS",
                        Description = itemDescription,
                        Tax         = new Money
                        {
                            CurrencyCode = "AUD",
                            Value        = "0.00"
                        },
                        UnitAmount = new Money
                        {
                            CurrencyCode = "AUD",
                            Value        = item.Price.ToString(CultureInfo.InvariantCulture)
                        }
                    });
                }
            }

            // Create mostViewed for order
            var order = await _paypalService.CreateOrder(orderItems, totalPrice);

            return(order);
        }