示例#1
0
        public async Task <PaymentResult> CheckoutAsync(CheckoutViewModel model)
        {
            var items = await GetCartItemsAsync();

            var order = new Order()
            {
                FirstName  = model.FirstName,
                LastName   = model.LastName,
                Address    = model.Address,
                City       = model.City,
                State      = model.State,
                PostalCode = model.PostalCode,
                Country    = model.Country,
                Phone      = model.Phone,
                Email      = model.Email,
                OrderDate  = DateTime.Now
            };

            foreach (var item in items)
            {
                var detail = new OrderDetail()
                {
                    ProductId = item.ProductId,
                    UnitPrice = item.Product.Price,
                    Quantity  = item.Count
                };

                order.Total += (item.Product.Price * item.Count);

                order.OrderDetails.Add(detail);
            }
            model.Total = order.Total;
            //TODO authorize payment
            //TODO assign the transactionid

            var gateway = new PaymentGateway();
            var result  = gateway.ProcessPayment(model);

            if (result.Succeeded)
            {
                order.TransactionId = result.TransactionId;
                _db.Orders.Add(order);
                _db.CartItems.RemoveRange(items);
                await _db.SaveChangesAsync();
            }

            _db.Orders.Add(order);
            //await _db.SaveChangesAsync();
            return(result);
        }
        public async Task<PaymentResult> CheckoutAsync(CheckoutViewModel model)
        {
            var items = await GetCartItemsAsync(); 
            var order = new Order()
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Address = model.Address,
                City = model.City,
                State = model.State,
                PostalCode = model.PostalCode,
                Country = model.Country,
                Phone = model.Phone,
                Email = model.Email,
                OrderDate = DateTime.Now
            };

            foreach (var item in items)
            {
                var detail = new OrderDetail()
                {
                    ProductId = item.ProductId,
                    UnitPrice = item.Product.Price,
                    Quantity = item.Count
                };

                order.Total += (item.Product.Price * item.Count);

                order.OrderDetails.Add(detail);
            }

            var gateway = new PaymentGateway();
            var result = gateway.ProcessPayment(model); 

            if (result.Succeeded)
            {
                order.TransactionId = result.TransactionId;
                _db.Orders.Add(order);
                _db.CartItems.RemoveRange(items);
                await _db.SaveChangesAsync();

            }
            return result;
        }