コード例 #1
0
        public async Task <CheckoutViewModel> Post([FromBody] CheckoutViewModel viewModel)
        {
            var managerId = Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
            var user      = await UserManager.FindByIdAsync(managerId.ToString());

            var teamIds = viewModel.Teams.Select(t => t.TeamId).ToList();
            // get all the teams that the manager is paying for (and only their teams)
            var actualTeams = await TeamRepository.All.Where(t => teamIds.Contains(t.Id) && t.ManagerId == managerId).Include(t => t.PromotionalCode).Include(t => t.Division).ToListAsync();

            var transactionAmount = 0M;

            viewModel.Success = true;
            viewModel.Errors  = new List <string>();

            if (!viewModel.AgreesToTerms)
            {
                viewModel.Success = false;
                viewModel.Errors.Add("You must accept the license agreement in order to continue.");
            }
            else
            {
                if (actualTeams.Count > 0)
                {
                    transactionAmount = CalculateTransactionAmount(actualTeams);

                    if (transactionAmount <= 0)
                    {
                        await UpdateFreeTransaction(actualTeams);
                    }
                    else
                    {
                        var cardType = CardValidator.DetermineCardType(viewModel.CcInfo.CardNumber);
                        if (cardType != CardType.AMEX && cardType != CardType.Visa &&
                            cardType != CardType.MasterCard && cardType != CardType.Discover)
                        {
                            viewModel.Success = false;
                            viewModel.Errors.Add("You need to use either a Visa, Mastercard, Discover Card, or American Express for payment.");
                        }

                        if (viewModel.Success)
                        {
                            viewModel.Success = await ProcessPayment(viewModel, user, transactionAmount, actualTeams);

                            if (!viewModel.Success)
                            {
                                viewModel.Errors.Add("An error occurred while trying to process your payment. Please validate all information is correct and then try again. If this continues, please contact us for support.");
                            }
                            else
                            {
                                // Send confirmation email
                                await SendConfirmationEmail(teamIds);
                            }
                        }
                    }
                }
                else
                {
                    viewModel.Errors.Add("There are no teams that you are a manager of in your shopping cart.");
                    viewModel.Success = false;
                }
            }

            return(viewModel);
        }